{"id":25506478,"url":"https://github.com/blackducksoftware/oh_delegator","last_synced_at":"2025-11-17T09:30:14.718Z","repository":{"id":24681608,"uuid":"28092541","full_name":"blackducksoftware/oh_delegator","owner":"blackducksoftware","description":"Delegator pattern for Rails 4 models","archived":false,"fork":false,"pushed_at":"2022-01-12T09:11:08.000Z","size":19,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-02-18T15:19:34.115Z","etag":null,"topics":["activerecord","concern","delegator","ohloh","ruby"],"latest_commit_sha":null,"homepage":null,"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/blackducksoftware.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-16T15:15:44.000Z","updated_at":"2022-01-12T09:11:11.000Z","dependencies_parsed_at":"2022-08-02T23:31:05.248Z","dependency_job_id":null,"html_url":"https://github.com/blackducksoftware/oh_delegator","commit_stats":null,"previous_names":["blackducksw/oh_delegator"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blackducksoftware%2Foh_delegator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blackducksoftware%2Foh_delegator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blackducksoftware%2Foh_delegator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blackducksoftware%2Foh_delegator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blackducksoftware","download_url":"https://codeload.github.com/blackducksoftware/oh_delegator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239493676,"owners_count":19647995,"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":["activerecord","concern","delegator","ohloh","ruby"],"created_at":"2025-02-19T06:57:59.286Z","updated_at":"2025-11-17T09:30:14.657Z","avatar_url":"https://github.com/blackducksoftware.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Inspiration\n\nFrom [Wikipedia](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself):\n\n\u003e The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”\n\n## Delegators vs Concerns\n\nActiveSupport::Concern is a convenient solution for splitting ActiveRecord models. It can serve as a single and authoritative feature representation, but it is ambiguous. Consider the following example:\n\n```ruby\n# app/models/concerns/account_profile.rb\nmodule AccountProfile\n  extend ActiveSupport::Concern\n\n  included do\n    has_many :profiles\n\n    validates :has_profile, inclusion: { in: [true, false] }\n  end\n\n  def active_profile\n    profiles.find_by(active: true)\n  end\nend\n\n# app/models/account.rb\nclass Account \u003c ActiveRecord::Base\n  include AccountProfile\nend\n\n# app/controllers/accounts_controller.rb\n...\n  def index\n    @profile = @account.active_profile\n  end\n...\n```\n\nWhen viewing the controller, the location of `@account.active_profile` seems ambiguous. The first thing that most developers would do is look at the `Account` model. Tools like ctags can help, but they have other limitations(e.g. cannot deal with duplicate method names in separate modules).\n\nDelegators are more ideal as they comply with all three requirements of DRY. Also, it makes way for editor plugins to accurately track source location. However it lacks any convenient wrapper like `ActiveSupport::Concern` for working with ActiveRecord. This gem serves to fill that gap.\n\n## Usage\n\nThe delegators can be placed anywhere in your application's load path, the only requirement is that it must be nested under the delegatee object.\n\n```ruby\n# app/delegators/account/profile_delegator.rb\nclass Account::ProfileDelegator \u003c OhDelegator::Base\n  parent_scope do\n    has_many :profiles\n\n    validates :has_profile, inclusion: { in: [true, false] }\n  end\n\n  def active\n    profiles.find_by(active: true)\n  end\nend\n\n# app/models/account.rb\nclass Account \u003c ActiveRecord::Base\n  oh_delegators :profile_delegator\nend\n\n# app/controllers/accounts_controller.rb\n...\n  def index\n    @profile = @account.profile_delegator.active\n  end\n...\n```\n\nAs we see, migrating code from an **ActiveRecord** model to a delegator is as simple as migrating a concern.\n\n## Extras\n\nThe delegatee object will be available inside the delegator. The name is inferred from the `parent` scope.\n\n```ruby\n# app/delegators/account/profile_delegator.rb\nclass Account::ProfileDelegator \u003c OhDelegator::Base\n  ...\n\n  def create_profile\n    Profile.create(account: account)         # Account::ProfileDelegator.parent.name.downcase == 'account'\n  end\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblackducksoftware%2Foh_delegator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblackducksoftware%2Foh_delegator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblackducksoftware%2Foh_delegator/lists"}