Last active
February 11, 2020 16:31
-
-
Save maxcal/de2b922f8cff9dc2e55f62f38b49ecf9 to your computer and use it in GitHub Desktop.
Assocation Decoration in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyApp | |
module Assocations | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def decorate_association(**options, &block) | |
yield AssocationDecorator.new(self, options) | |
end | |
end | |
class AssocationDecorator | |
attr_accessor :options, :klass | |
def initialize(klass, **options) | |
@klass = klass | |
@options = options | |
end | |
def has_many(name, scope = nil, **options, &extension) | |
@klass.has_many(name, scope, options.reverse_merge(@options), &extension) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment