{"id":13880249,"url":"https://github.com/saturnflyer/characterize","last_synced_at":"2025-03-17T06:31:33.470Z","repository":{"id":17958721,"uuid":"20946656","full_name":"saturnflyer/characterize","owner":"saturnflyer","description":"Decorate objects in Rails without the pain of wrappers","archived":false,"fork":false,"pushed_at":"2025-03-11T02:50:10.000Z","size":229,"stargazers_count":16,"open_issues_count":8,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-11T03:27:27.755Z","etag":null,"topics":["rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/saturnflyer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-06-18T02:27:13.000Z","updated_at":"2025-03-11T02:50:06.000Z","dependencies_parsed_at":"2023-02-10T22:16:06.216Z","dependency_job_id":"9b1a7d33-4f17-44ae-b411-d580f87ef099","html_url":"https://github.com/saturnflyer/characterize","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saturnflyer%2Fcharacterize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saturnflyer%2Fcharacterize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saturnflyer%2Fcharacterize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saturnflyer%2Fcharacterize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/saturnflyer","download_url":"https://codeload.github.com/saturnflyer/characterize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243847062,"owners_count":20357317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","ruby"],"created_at":"2024-08-06T08:02:53.630Z","updated_at":"2025-03-17T06:31:32.920Z","avatar_url":"https://github.com/saturnflyer.png","language":"Ruby","readme":"# Characterize\n\nMake your models behave in special ways without wrapping them.\n\nCharacterize is built on top of [Casting](https://github.com/saturnflyer/casting) and makes it easy to get going in Rails.\n\n[![Code Climate](https://codeclimate.com/github/saturnflyer/characterize.png)](https://codeclimate.com/github/saturnflyer/characterize)\n[![Gem Version](https://badge.fury.io/rb/characterize.png)](http://badge.fury.io/rb/characterize)\n\n## Usage\n\n```ruby\nclass UsersController \u003c ApplicationController\n  characterize :user\n\n  def show\n  end\nend\n# the above sets a helper_method of 'user' and loads UserCharacter\n\nmodule UserCharacter\n  def special_behavior_available_in_the_view\n     # ...\n  end\nend\n\nclass UsersController \u003c ApplicationController\n  def show\n    characterize(user, display_module)\n  end\n\n  def display_module\n    current_user.can_edit?(user) ? AdministratedUser : StandardUser\n  end\nend\n\n# use a standard interface in your views but change the character of the object\n\nmodule AdministratedUser\n  def edit_link\n    view.link_to('Edit', admin_user_path)\n  end\nend\n\nmodule StandardUser\n  def edit_link\n    \"\"\n  end\nend\n```\n\nSet special modules to be used for different actions:\n\n```ruby\nclass UsersController \u003c ApplicationController\n  characterize :user, show: [SpecialStuff, StandardStuff],\n                      edit: [EditingCharacter],\n                      default: [StandardStuff]\n\n  def show\n  end\nend\n```\n\nBy default Characterize will look for modules that match the name of your object. So `characterize :user` would apply a `UserCharacter` module (and will blow up if it can't find it.) Or you can override it with the above configuration.\n\nYou can also use it to characterize collections:\n\n```ruby\nclass WidgetsController \u003c ApplicationController\n  characterize_each :widgets, index: [SuperWidgetCharacter]\n\n  def index\n  end\nend\n```\n\nThis will create a `widgets` helper method that will return a collection object where enumerable methods will cast the object as the provided modules.\n\nBy default these methods will assume a loading method for your records but you can override this:\n\n```ruby\nclass UsersController \u003c ApplicationController\n  characterize :user # creates a `load_user` method\n  characterize :user, load_with: :get_a_user\n\n  def get_a_user\n    UserRepository.get(params[:user_id])\n  end\nend\n```\n\n## Altering the Settings\n\n### Module names\n\nCharacterize will automatically look for modules using the \"Character\" suffix in it's name. But you can change this if you like.\n\nJust create an initializer which will change the setting when your Rails application boots:\n\n```ruby\nCharacterize.module_suffix = 'Details'\n```\n\nWith the above change, using `characterize :user` in your controller, it will attempt to load `UserDetails` instead of `UserCharacter`. This will apply for your *entire* application; if you only want to override the suffix in some places, just specify the module you want in your controller.\n\n### Creating your own standard features\n\nBy default Characterize has some helpful features built in. You can use them like this:\n\n```ruby\nclass UsersController \u003c ApplicationController\n  characterize :user, show: [SpecialStuff].concat(Characterize.standard_features)\n\n  def show\n  end\nend\n```\n\nThat will load the built-in features from Characterize. But you can change what is considered \"standard\" in your application.\n\nSet the `standard_features` option in your initializer to whatever you want:\n\n```ruby\noriginal_features = Characterize.standard_features\nCharacterize.standard_features = [MyAwesomeStuff, ExtraDoodads].concat(original_features)\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'characterize'\n\nAnd then execute:\n\n    $ bundle\n\nAnd finally\n\n    $ rails g characterize:install\n\nOr install it yourself as:\n\n    $ gem install characterize\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaturnflyer%2Fcharacterize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaturnflyer%2Fcharacterize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaturnflyer%2Fcharacterize/lists"}