Skip to content

Instantly share code, notes, and snippets.

@maxcal
Last active February 11, 2020 16:31
Show Gist options
  • Save maxcal/de2b922f8cff9dc2e55f62f38b49ecf9 to your computer and use it in GitHub Desktop.
Save maxcal/de2b922f8cff9dc2e55f62f38b49ecf9 to your computer and use it in GitHub Desktop.
Assocation Decoration in Rails
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