https://github.com/blackducksoftware/oh_delegator
Delegator pattern for Rails 4 models
https://github.com/blackducksoftware/oh_delegator
activerecord concern delegator ohloh ruby
Last synced: 2 months ago
JSON representation
Delegator pattern for Rails 4 models
- Host: GitHub
- URL: https://github.com/blackducksoftware/oh_delegator
- Owner: blackducksoftware
- License: mit
- Created: 2014-12-16T15:15:44.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2022-01-12T09:11:08.000Z (over 3 years ago)
- Last Synced: 2025-02-18T15:19:34.115Z (2 months ago)
- Topics: activerecord, concern, delegator, ohloh, ruby
- Language: Ruby
- Size: 18.6 KB
- Stars: 1
- Watchers: 12
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
## Inspiration
From [Wikipedia](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself):
> The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”
## Delegators vs Concerns
ActiveSupport::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:
```ruby
# app/models/concerns/account_profile.rb
module AccountProfile
extend ActiveSupport::Concernincluded do
has_many :profilesvalidates :has_profile, inclusion: { in: [true, false] }
enddef active_profile
profiles.find_by(active: true)
end
end# app/models/account.rb
class Account < ActiveRecord::Base
include AccountProfile
end# app/controllers/accounts_controller.rb
...
def index
@profile = @account.active_profile
end
...
```When 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).
Delegators 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.
## Usage
The delegators can be placed anywhere in your application's load path, the only requirement is that it must be nested under the delegatee object.
```ruby
# app/delegators/account/profile_delegator.rb
class Account::ProfileDelegator < OhDelegator::Base
parent_scope do
has_many :profilesvalidates :has_profile, inclusion: { in: [true, false] }
enddef active
profiles.find_by(active: true)
end
end# app/models/account.rb
class Account < ActiveRecord::Base
oh_delegators :profile_delegator
end# app/controllers/accounts_controller.rb
...
def index
@profile = @account.profile_delegator.active
end
...
```As we see, migrating code from an **ActiveRecord** model to a delegator is as simple as migrating a concern.
## Extras
The delegatee object will be available inside the delegator. The name is inferred from the `parent` scope.
```ruby
# app/delegators/account/profile_delegator.rb
class Account::ProfileDelegator < OhDelegator::Base
...def create_profile
Profile.create(account: account) # Account::ProfileDelegator.parent.name.downcase == 'account'
end
end
```