Skip to content

Commit

Permalink
Use class_eval instead of define_method
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Jan 28, 2024
1 parent ce2f983 commit d96c67d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/draper/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d96c67d

Please sign in to comment.