{"id":13879430,"url":"https://github.com/joelmoss/gifted","last_synced_at":"2025-11-11T20:40:53.522Z","repository":{"id":34943770,"uuid":"192982063","full_name":"joelmoss/gifted","owner":"joelmoss","description":"Simple, yet powerful decorators for Ruby on Rails","archived":false,"fork":false,"pushed_at":"2023-01-19T23:18:57.000Z","size":634,"stargazers_count":3,"open_issues_count":7,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-07T00:49:12.751Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joelmoss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-20T20:15:48.000Z","updated_at":"2024-04-07T00:49:12.752Z","dependencies_parsed_at":"2023-02-11T22:15:17.609Z","dependency_job_id":null,"html_url":"https://github.com/joelmoss/gifted","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joelmoss%2Fgifted","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joelmoss%2Fgifted/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joelmoss%2Fgifted/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joelmoss%2Fgifted/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joelmoss","download_url":"https://codeload.github.com/joelmoss/gifted/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228339330,"owners_count":17904513,"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-08-06T08:02:20.904Z","updated_at":"2025-11-11T20:40:48.483Z","avatar_url":"https://github.com/joelmoss.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Gifted\n\nSimple, powerful, and explicit decorators for Ruby on Rails, inspired (and forked) from [ActiveDecorator](https://github.com/amatsuda/active_decorator). It has the following goals:\n\n1. Fast and efficient.\n2. Implicit decorators.\n3. Explicit decoration.\n\n## Features\n\nGifted has an almost identical feature set to ActiveDecorator...\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\nBut with the following differences...\n\n1. Decorator methods are name-spaced to a single `gift` method. This avoids confusion and collision.\n2. Decorators are mixed into SimpleDelegator classes, which themselves delegate to the decorated object.\n2. Decorator Views allow you to create child decorators.\n3. Only works with Rails.\n\n## Installation\n\nGifted requires Rails \u003e= 5.\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'gifted'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install gifted\n\n## Usage\n\nCreate your Decorators into `/app/decorators` for each model you want decorated, and define methods. For example, a model `User`, can have a decorator `UserDecorator`:\n\n* Model\n```ruby\nclass User \u003c ActiveRecord::Base\n  # first_name:string last_name:string\nend\n```\n\n* Controller\n```ruby\nclass UsersController \u003c ApplicationController\n  def show\n    @user = User.find(params[:id])\n  end\nend\n```\n\n* Decorator\n```ruby\n# I can call any method from the decorated object, and because I am mixed into SimpleDelegator, I\n# can access the decorated object directly at __getobj__.\nmodule UserDecorator\n  # This is a Decorator View, and can be access by passing the name of the view (:admin) to `#gift`.\n  module AdminDecorator\n    def full_name\n      \"#{first_name} #{last_initial} (administrator)\"\n    end\n  end\n\n  def full_name\n    \"#{first_name} #{last_initial}\"\n  end\n\n  def first_name\n    first_name.titleize\n  end\n\n  def last_initial\n    last_name[0]\n  end\nend\n```\n\n* View\n```erb\n\u003c!-- @user here is auto-decorated in between the controller and the view --\u003e\n\n\u003c!-- @user.gift.full_name will use the method #full_name from the decorator. --\u003e\n\u003cp\u003e\u003c%= @user.gift.full_name %\u003e\u003c/p\u003e\n\n\u003c!--\n  @user.gift.last_name will use the method #last_name from the model, because @user.gift delegates\n  to the model.\n--\u003e\n\u003cp\u003e\u003c%= @user.gift.last_name %\u003e\u003c/p\u003e\n\n\u003c!--\n  Calling gift with a symbol as an argument, will decorate the object with a Decorator view.\n  So @user.gift(:admin).last_name will use the method #full_name from UserDecorator::AdminDecorator.\n--\u003e\n\u003cp\u003e\u003c%= @user.gift(:admin).full_name %\u003e\u003c/p\u003e\n\n\u003c!-- @user.last_name will use the method #last_name from the model. --\u003e\n\u003cp\u003e\u003c%= @user.last_name %\u003e\u003c/p\u003e\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/joelmoss/gifted.\n\n## Thanks!\n\nThis gem is hugely inspired by and (in parts) copied from [ActiveDecorator](https://github.com/amatsuda/active_decorator).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoelmoss%2Fgifted","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoelmoss%2Fgifted","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoelmoss%2Fgifted/lists"}