{"id":13880059,"url":"https://github.com/fnando/burgundy","last_synced_at":"2025-05-08T21:12:45.918Z","repository":{"id":62554778,"uuid":"13831725","full_name":"fnando/burgundy","owner":"fnando","description":"A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 150 lines.","archived":false,"fork":false,"pushed_at":"2024-05-16T18:13:07.000Z","size":87,"stargazers_count":49,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-08T21:12:27.927Z","etag":null,"topics":["decorator","presenter","ruby","view-object"],"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/fnando.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["fnando"],"custom":["https://paypal.me/nandovieira/🍕"]}},"created_at":"2013-10-24T12:42:02.000Z","updated_at":"2024-05-16T18:13:09.000Z","dependencies_parsed_at":"2024-10-20T12:40:02.441Z","dependency_job_id":null,"html_url":"https://github.com/fnando/burgundy","commit_stats":{"total_commits":52,"total_committers":4,"mean_commits":13.0,"dds":"0.46153846153846156","last_synced_commit":"9660ef8af42908007822924c5b5e2ff1bd8a2165"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fburgundy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fburgundy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fburgundy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fburgundy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/burgundy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253149617,"owners_count":21861739,"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":["decorator","presenter","ruby","view-object"],"created_at":"2024-08-06T08:02:45.548Z","updated_at":"2025-05-08T21:12:45.897Z","avatar_url":"https://github.com/fnando.png","language":"Ruby","funding_links":["https://github.com/sponsors/fnando","https://paypal.me/nandovieira/🍕"],"categories":["Ruby"],"sub_categories":[],"readme":"# Burgundy\n\n[![Tests](https://github.com/fnando/burgundy/workflows/ruby-tests/badge.svg)](https://github.com/fnando/burgundy)\n[![Gem](https://img.shields.io/gem/v/burgundy.svg)](https://rubygems.org/gems/burgundy)\n[![Gem](https://img.shields.io/gem/dt/burgundy.svg)](https://rubygems.org/gems/burgundy)\n[![MIT License](https://img.shields.io/:License-MIT-blue.svg)](https://tldrlegal.com/license/mit-license)\n\nA simple wrapper for objects (think of Burgundy as a decorator/presenter) in\nless than 150 lines.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"burgundy\"\n```\n\nAnd then execute:\n\n```console\n$ bundle\n```\n\nOr install it yourself as:\n\n```console\n$ gem install burgundy\n```\n\n## Usage\n\nFirst, define your wrapping class.\n\n```ruby\nclass UserPresenter \u003c Burgundy::Item\nend\n```\n\nThen you can instantiate it:\n\n```ruby\nuser = UserPresenter.new(User.first)\n```\n\nThe `Burgundy::Item` has access to helper and route methods. Notice that the\nwrapper item is accessible through the `Burgundy::Item#item` method.\n\n```ruby\nclass UserPresenter \u003c Burgundy::Item\n  def profile_url\n    routes.profile_url(item.username)\n  end\nend\n```\n\nYou don't have to expose attributes; everything is delegated to the wrapped\nitem.\n\nTo wrap an entire collection, just use the `Burgundy::Item.wrap` class.\n\n```ruby\nclass WorkshopsController \u003c ApplicationController\n  def index\n    @workshops = WorkshopPresenter.wrap(Workshop.sorted_by_name)\n  end\nend\n```\n\nAlternatively, you can also use the `Burgundy::Collection` class.\n\n```ruby\nclass WorkshopsController \u003c ApplicationController\n  def index\n    @workshops = Burgundy::Collection.new(\n      WorkshopPresenter,\n      Workshop.sorted_by_name\n    )\n  end\nend\n```\n\nYou may need to provide additional arguments to the item class. On your\ncollection, all additional arguments will be delegated to the item classe, like\nthe following example:\n\n```ruby\nWorkshopPresenter.wrap(Workshop.all, current_user)\nBurgundy::Collection.new(Workshop.all, WorkshopPresenter, current_user)\n\nclass WorkshopPresenter \u003c Burgundy::Item\n  def initialize(workshop, current_user)\n    super(workshop)\n    @current_user = current_user\n  end\nend\n```\n\nYou can also use keyword arguments to pass in additional objects.\n\n```ruby\nWorkshopPresenter.wrap(Workshop.all, current_user:)\nBurgundy::Collection.new(Workshop.all, WorkshopPresenter, current_user:)\n\nclass WorkshopPresenter \u003c Burgundy::Item\n  def initialize(workshop, current_user:)\n    super(workshop)\n    @current_user = current_user\n  end\nend\n```\n\n\u003e [!NOTE]\n\u003e\n\u003e The target object will always be a positional argument.\n\nThe query will be performed only when needed, usually on the view (easier to\ncache). The collection is an enumerable object and can be passed directly to the\n`render` method. Each item will be wrapped by the provided class.\n\n```erb\n\u003c%= render @workshops %\u003e\n```\n\nRoute URLs may require the default url options. Burgundy try to get them from\nthe following objects:\n\n- `Rails.configuration.action_mailer.default_url_options`\n- `Rails.application.routes.default_url_options`\n\nSo you can just put this on your environment file\n\n```ruby\nconfig.action_controller.default_url_options = {\n  host: \"example.org\"\n}\n```\n\nYou can map attributes into a hash; I use this strategy for using presenters on\nAPI responses (so you can skip adding yet another dependency to your projects).\n\n```ruby\nclass UserPresenter \u003c Burgundy::Item\n  attributes :username, :name, :email\n\n  def profile_url\n    routes.profile_url(item.username)\n  end\nend\n\nUserPresenter.new(User.first).attributes\n#=\u003e {:username=\u003e'johndoe', :name=\u003e'John Doe', :email=\u003e'john@example.org'}\n\nUserPresenter.new(User.first).to_hash\n#=\u003e {:username=\u003e'johndoe', :name=\u003e'John Doe', :email=\u003e'john@example.org'}\n\nUserPresenter.new(User.first).to_h\n#=\u003e {:username=\u003e'johndoe', :name=\u003e'John Doe', :email=\u003e'john@example.org'}\n\nUserPresenter.new(User.first).as_json\n#=\u003e {:username=\u003e'johndoe', :name=\u003e'John Doe', :email=\u003e'john@example.org'}\n```\n\nNotice that `as_json` will ignore any options provided.\n\nIf you want to remap an attribute, provide a hash.\n\n```ruby\nclass UserPresenter \u003c Burgundy::Item\n  attributes :name, :email, username: :login\n\n  def profile_url\n    routes.profile_url(item.username)\n  end\nend\n\nUserPresenter.new(User.first).attributes\n#=\u003e {:login=\u003e'johndoe', :name=\u003e'John Doe', :email=\u003e'john@example.org'}\n```\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\n## License\n\nCopyright (c) 2013 Nando Vieira\n\nMIT License\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fburgundy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Fburgundy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fburgundy/lists"}