Giter Club home page Giter Club logo

oh_delegator's Introduction

Inspiration

From Wikipedia:

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:

# app/models/concerns/account_profile.rb
module AccountProfile
  extend ActiveSupport::Concern

  included do
    has_many :profiles

    validates :has_profile, inclusion: { in: [true, false] }
  end

  def 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.

# app/delegators/account/profile_delegator.rb
class Account::ProfileDelegator < OhDelegator::Base
  parent_scope do
    has_many :profiles

    validates :has_profile, inclusion: { in: [true, false] }
  end

  def 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.

# 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

oh_delegator's People

Contributors

notalex avatar pdegenportnoy avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oh_delegator's Issues

Open Source this?

Should we change the name to something like ActiveDelegator and release it as OSS?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.