diff --git a/lib/draper/decorator.rb b/lib/draper/decorator.rb index f0bb3db..e3f3465 100644 --- a/lib/draper/decorator.rb +++ b/lib/draper/decorator.rb @@ -30,11 +30,17 @@ def delegate(*methods) # decorates_association :posts, PostDecorator # end def decorates_association(relation_name, with: nil, namespace: nil, scope: nil) - relation_name = relation_name.to_sym - - define_method(relation_name) do - @decorated_associations[relation_name] ||= decorate(_scoped_decorator(relation_name, scope), with: with, namespace: namespace) - end + relation_sym = ":#{relation_name}" + with = with.nil? ? "nil" : "'#{with}'" + namespace = namespace.nil? ? "nil" : "'#{namespace}'" + scope = scope.nil? ? "nil" : ":#{scope}" + + class_eval <<-METHOD, __FILE__, __LINE__ + 1 + # frozen_string_literal: true + def #{relation_name} + @decorated_associations[#{relation_sym}] ||= decorate(_scoped_decorator(#{relation_sym}, #{scope}), with: #{with}, namespace: #{namespace}) + end + METHOD end # Access the helpers proxy to call built-in and user-defined