From d96c67d2a821d4ee68cec9a374bf7c8f33397e69 Mon Sep 17 00:00:00 2001 From: Nicolas Rodriguez Date: Sun, 28 Jan 2024 02:14:57 +0100 Subject: [PATCH] Use class_eval instead of define_method --- lib/draper/decorator.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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