{"id":13463322,"url":"https://github.com/amatsuda/active_decorator","last_synced_at":"2025-05-13T17:06:42.350Z","repository":{"id":1967321,"uuid":"2898239","full_name":"amatsuda/active_decorator","owner":"amatsuda","description":"ORM agnostic truly Object-Oriented view helper for Rails 4, 5, 6, 7, and 8","archived":false,"fork":false,"pushed_at":"2025-01-16T15:06:02.000Z","size":356,"stargazers_count":1112,"open_issues_count":10,"forks_count":109,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-04-16T11:38:49.392Z","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/amatsuda.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","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":"2011-12-02T12:09:38.000Z","updated_at":"2025-03-24T09:27:14.000Z","dependencies_parsed_at":"2024-01-26T04:28:28.763Z","dependency_job_id":"9686f3f1-5397-47ba-9cc6-882f9d99ada3","html_url":"https://github.com/amatsuda/active_decorator","commit_stats":{"total_commits":421,"total_committers":51,"mean_commits":8.254901960784315,"dds":"0.20190023752969122","last_synced_commit":"fdda5a90db7720bf038259e87e4b1e85e61f4388"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Factive_decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Factive_decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Factive_decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Factive_decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amatsuda","download_url":"https://codeload.github.com/amatsuda/active_decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249874564,"owners_count":21338346,"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-07-31T13:00:50.889Z","updated_at":"2025-04-23T23:05:16.520Z","avatar_url":"https://github.com/amatsuda.png","language":"Ruby","funding_links":[],"categories":["Rails Plugins","Ruby","Gems","Uncategorized"],"sub_categories":["Rails Presenters","Articles","Uncategorized"],"readme":"# ActiveDecorator [![Build Status](https://github.com/amatsuda/active_decorator/actions/workflows/main.yml/badge.svg)](https://github.com/amatsuda/active_decorator/actions) [![Code Climate](https://codeclimate.com/github/amatsuda/active_decorator/badges/gpa.svg)](https://codeclimate.com/github/amatsuda/active_decorator)\n\nA simple and Rubyish view helper for Rails 4, Rails 5, Rails 6, Rails 7, and Rails 8. Keep your helpers and views Object-Oriented!\n\n\n## Features ##\n\n1. automatically mixes decorator module into corresponding model only when:\n   1. passing a model or collection of models or an instance of ActiveRecord::Relation from controllers to views\n   2. rendering partials with models (using `:collection` or `:object` or `:locals` explicitly or implicitly)\n   3. fetching already decorated Active Record model object's association\n2. the decorator module runs in the model's context. So, you can directly call any attributes or methods in the decorator module\n3. since decorators are considered as sort of helpers, you can also call any ActionView's helper methods such as `content_tag` or `link_to`\n\n\n## Supported versions ##\n\n* Ruby 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, and 3.5 (trunk)\n\n* Rails 4.2.x, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 7.2, 8.0, and 8.1 (edge)\n\n\n## Supported ORMs ##\n\nActiveRecord, ActiveResource, and any kind of ORMs that uses Ruby Objects as model objects\n\n\n## Usage ##\n\n1. bundle 'active_decorator' gem\n2. create a decorator module for each AR model. For example, a decorator for a model `User` should be named `UserDecorator`.\nYou can use the generator for doing this ( `% rails g decorator user` )\n3. Then it's all done. Without altering any single line of the existing code, the decorator modules will be automatically mixed into your models only in the view context.\n\n\n## Examples ##\n\n### Auto-decorating via `render` ###\n\n* Model\n```ruby\nclass Author \u003c ActiveRecord::Base\n  # first_name:string last_name:string\nend\n```\n\n* Controller\n```ruby\nclass AuthorsController \u003c ApplicationController\n  def show(id)  # powered by action_args\n    @author = Author.find id\n  end\nend\n```\n\n* Decorator\n```ruby\nmodule AuthorDecorator\n  def full_name\n    \"#{first_name} #{last_name}\"\n  end\nend\n```\n\n* View\n```erb\n\u003c%# @author here is auto-decorated in between the controller and the view %\u003e\n\u003cp\u003e\u003c%= @author.full_name %\u003e\u003c/p\u003e\n```\n\n### Auto-decorating via AR model's associated objects ###\n\n* Models\n```ruby\nclass Author \u003c ActiveRecord::Base\n  # name:string\n  has_many :books\nend\n\nclass Book \u003c ActiveRecord::Base\n  # title:string url:string\n  belongs_to :author\nend\n```\n\n* Controller\n```ruby\nclass AuthorsController \u003c ApplicationController\n  def show(id)\n    @author = Author.find id\n  end\nend\n```\n\n* Decorator\n```ruby\nmodule BookDecorator\n  def link\n    link_to title, url\n  end\nend\n```\n\n* View\n```erb\n\u003cp\u003e\u003c%= @author.name %\u003e\u003c/p\u003e\n\u003cul\u003e\n\u003c% @author.books.order(:id).each do |book| %\u003e\n  \u003c%# `book` here is auto-decorated because @author is a decorated instance %\u003e\n  \u003cli\u003e\u003c%= book.link %\u003e\u003c/li\u003e\n\u003c% end %\u003e\n\u003c/ul\u003e\n```\n\n### Using ActiveDecorator outside of Action View ###\n\nSometimes you may want to use decorators outside of Action View, for example,\nfor background tasks for ActiveJob.\nFor such use case, ActiveDecorator module provides `run_with` method\nthat takes some kind of Action View and a block.\n\n```ruby\nActiveDecorator::ViewContext.run_with ApplicationController.new.view_context do\n  ## perform some heavy jobs here\nend\n```\n\n\n## Testing\n\nYou can test a decorator using your favorite test framework by decorating the model instance with\n\n```ruby\nActiveDecorator::Decorator.instance.decorate(model_instance)\n```\n\nConsidering an `Organization` model and its simple decorator:\n\n```ruby\nmodule OrganizationDecorator\n  def full_name\n    \"#{first_name} #{last_name}\"\n  end\nend\n```\n\nAn RSpec test would look like:\n\n```ruby\ndescribe '#full_name' do\n  it 'returns the full organization name' do\n    organization = Organization.new(first_name: 'John', last_name: 'Doe')\n    decorated_organization = ActiveDecorator::Decorator.instance.decorate(organization)\n\n    expect(decorated_organization.full_name).to eq('John Doe')\n  end\nend\n```\n\n## Configuring the decorator suffix\n\nBy default, ActiveDecorator searches a decorator module named `target_class.name + \"Decorator\"`\n\nIf you would like a different rule, you can configure in your initializer.\n\n```ruby\nActiveDecorator.configure do |config|\n  config.decorator_suffix = 'Presenter'\nend\n```\n\n## Contributing to ActiveDecorator ##\n\n* Fork, fix, then send me a pull request.\n\n\n## Copyright ##\n\nCopyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famatsuda%2Factive_decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famatsuda%2Factive_decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famatsuda%2Factive_decorator/lists"}