{"id":20328803,"url":"https://github.com/komposable/dekorator","last_synced_at":"2025-08-22T05:32:35.484Z","repository":{"id":55531024,"uuid":"168049576","full_name":"komposable/dekorator","owner":"komposable","description":"A simple decorator for Rails ","archived":false,"fork":false,"pushed_at":"2024-11-10T14:24:24.000Z","size":161,"stargazers_count":13,"open_issues_count":5,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-25T08:07:29.648Z","etag":null,"topics":["decorators","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/komposable.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2019-01-28T22:38:33.000Z","updated_at":"2024-11-19T16:05:47.000Z","dependencies_parsed_at":"2024-09-10T15:04:58.486Z","dependency_job_id":"6d75c366-198a-4b82-9fd0-cec70e47ad20","html_url":"https://github.com/komposable/dekorator","commit_stats":{"total_commits":65,"total_committers":6,"mean_commits":"10.833333333333334","dds":0.4769230769230769,"last_synced_commit":"ff780b8703964ce1b0f164c8c16529dcafa9ac56"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komposable%2Fdekorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komposable%2Fdekorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komposable%2Fdekorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/komposable%2Fdekorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/komposable","download_url":"https://codeload.github.com/komposable/dekorator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230561014,"owners_count":18245324,"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":["decorators","rails","ruby"],"created_at":"2024-11-14T20:08:01.560Z","updated_at":"2024-12-20T09:07:37.637Z","avatar_url":"https://github.com/komposable.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dekorator\n\n[![Tests](https://github.com/komposable/dekorator/workflows/Tests/badge.svg)](https://github.com/komposable/dekorator/actions)\n[![Gem Version](https://badge.fury.io/rb/dekorator.svg)](https://rubygems.org/gems/dekorator)\n[![Maintainability](https://api.codeclimate.com/v1/badges/f7ab08512ead00da34c0/maintainability)](https://codeclimate.com/github/komposable/dekorator/maintainability)\n[![Inch CI](https://inch-ci.org/github/komposable/dekorator.svg?branch=main)](https://inch-ci.org/github/komposable/dekorator)\n[![Yardoc](https://img.shields.io/badge/doc-yardoc-blue.svg)](https://www.rubydoc.info/github/komposable/dekorator/main)\n\n**Dekorator** is a lightweight library to implement _presenters_ and/or _decorators_ in your Rails app. It has less features than [`draper`](https://github.com/drapergem/draper) and aims at having a lower memory footprint.\n\nThis gem has been inspired by our Rails development practices at [Pantographe](https://pantographe.studio), and the [Ruby memory, ActiveRecord and Draper](https://medium.com/appaloosa-store-engineering/ruby-memory-activerecord-and-draper-64f06abeeb34) talk by [Benoit Tigeot](https://github.com/benoittgt).\n\n## Compatibility\n\n* Ruby 3.1+\n* Rails 7.0+\n\n## Installation\n\nAdd this line to your application `Gemfile`:\n\n```ruby\ngem \"dekorator\"\n```\n\nAnd then execute:\n\n    $ bundle\n\n## Getting started\n\nRun the following command to set up your project:\n\n    $ rails generate dekorator:install\n\nThis command will create an `ApplicationDecorator` file.\n\n## Usage\n\nGenerate a new decorator with the `decorator` generator:\n\n    $ rails generate decorator user\n\nThis command will generate the following file:\n\n```ruby\nclass UserDecorator \u003c ApplicationDecorator\n  include ActionView::Helpers::TextHelper\n\n  decorates_association :posts\n\n  def full_name\n    [first_name, last_name].join(\" \")\n  end\n\n  def biography_summary\n    truncate(biography, length: 170)\n  end\nend\n```\n\n### Decorate from a controller\n\n```ruby\nclass UsersController \u003c ApplicationController\n  def index\n    @users = decorate User.all\n  end\n\n  def show\n    @user = decorate User.find(params[:id])\n  end\nend\n```\n\n### Decorate from a view\n\n```erb\n# app/views/users/index.html.erb\n\n\u003cul\u003e\n  \u003c% decorate(@users).each do |user| %\u003e\n    \u003cli\u003e\u003c%= user.full_name %\u003e\u003c/li\u003e\n  \u003c% end %\u003e\n\u003c/ul\u003e\n```\n\n### Decorate outside a controller/view\n\n```ruby\nUserDecorate.decorate(User.first) # =\u003e UserDecorator\n```\n\n### Associations\n\nIf you want to automatically decorate an association for a decorated object,\nyou have to use `#decorates_association` as following:\n\n```ruby\nclass UserDecorator \u003c ApplicationDecorator\n  decorates_association :posts\n\n  ...\nend\n\nclass PostDecorator \u003c ApplicationDecorator\n  ...\nend\n```\n\nIn this example, `UserDecorator#posts` will be decorated as `#decorated_posts`.\n\n```ruby\ndecorated_user = decorate(User.first)\ndecorated_user # =\u003e UserDecorator\ndecorated_user.decorated_posts.first # =\u003e PostDecorator\n```\n\n### Custom decorator\n\nBy default, Dekorator searches for the decorator class by adding `Decorator` at the end.\nFor `User`, Dekorator looks for the `UserDecorator` class, and for `User::Profile`\nit looks for `User::ProfileDecorator`.\n\nIf you want to create a specific decorator or sub-decorator, you can simply\nspecify the decorator class that should be used.\n\n```ruby\nclass AdminDecorator \u003c ApplicationDecorator\n  ...\nend\n\ndecorated_user = decorate(User.first, with: AdminDecorator)\ndecorated_user # =\u003e AdminDecorator\n```\n\nYou can also specify the decorator for associations:\n\n```ruby\nclass UserDecorator \u003c ApplicationDecorator\n  decorates_association :posts, with: ArticleDecorator\n\n  ...\nend\n\nclass ArticleDecorator \u003c ApplicationDecorator\nend\n\ndecorated_user = decorate(User.first)\ndecorated_user # =\u003e UserDecorator\ndecorated_user.decorated_posts.first # =\u003e ArticleDecorator\n```\n\n## Compatibility\n\n### Devise\n\nIf you use the [`Devise`][devise] gem you may have an issue if you decorate your\n`User` model.\n\nYou must define `#devise_scope` as following. Devise needs to manage with the\n`User` model (https://github.com/plataformatec/devise/blob/369ba267efaa10d01c8dba59b09c3b94dd9e5551/lib/devise/mapping.rb#L35).\n\n```ruby\nclass UserDecorator \u003c ApplicationDecorator\n  ...\n\n  def devise_scope\n    __getobj__\n  end\nend\n```\n\n## Testing\n\n`rails generate decorator user` also generates a testing file based on your\nconfiguration.\n\nYou can test a decorator the same way you do for helpers.\n\n### RSpec\n\n```ruby\ndescribe UserDecorator, type: :decorator do\n  let(:object) { User.new(first_name: \"John\", last_name: \"Doe\") }\n  let(:decorated_user) { described_class.new(object) }\n\n  describe \"#full_name\" do\n    it { expect(decorated_user.full_name).to eq(\"John Doe\") }\n  end\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run\n`rake spec` to run the tests. You can also run `bin/console` for an interactive\nprompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`.\nTo release a new version, update the version number in `version.rb`, then\nrun `bundle exec rake release`, which will create a git tag for the version,\npush git commits and tags, and push the `.gem` file to [rubygems.org].\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at\nhttps://github.com/komposable/dekorator. This project is intended to be a safe,\nwelcoming space for collaboration, and contributors are expected to adhere to\nthe [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License].\n\n## Code of Conduct\n\nEveryone interacting in the Dekorator project codebases, issue trackers,\nchat rooms and mailing lists is expected to follow the [code of conduct].\n\n[activeadmin]: https://activeadmin.info/11-decorators.html\n[devise]: https://github.com/plataformatec/devise/\n[rubygems.org]: https://rubygems.org\n[MIT License]: https://opensource.org/licenses/MIT\n[code of conduct]: https://github.com/komposable/dekorator/blob/main/CODE_OF_CONDUCT.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkomposable%2Fdekorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkomposable%2Fdekorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkomposable%2Fdekorator/lists"}