Skip to content

Commit

Permalink
Be sure to pass a Decorator class and not a string to 'with:'
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Jan 28, 2024
1 parent 345806a commit f8496db
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/draper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ def self._decorate(object_or_enumerable, with: nil, namespace: nil)

def self._guess_decorator(object_or_enumerable, with: nil, namespace: nil)
object_or_enumerable = object_or_enumerable.first if object_or_enumerable.is_a? Enumerable
klass = with || object_or_enumerable.class.name
namespace = "#{namespace}::" if namespace.present?
decorator = "#{namespace}#{klass}Decorator"
klass = with || "#{object_or_enumerable.class.name}Decorator"
decorator = [namespace, klass].compact.join('::')
decorator.safe_constantize || raise(Draper::UninferrableDecoratorError.new(klass, decorator))
end

Expand Down
2 changes: 1 addition & 1 deletion lib/draper/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def delegate(*methods)
# end
def decorates_association(relation_name, with: nil, namespace: nil, scope: nil)
relation_sym = ":#{relation_name}"
with = with.nil? ? "nil" : "'#{with}'"
with = with.nil? ? "nil" : "#{with}"
namespace = namespace.nil? ? "nil" : "'#{namespace}'"
scope = scope.nil? ? "nil" : ":#{scope}"

Expand Down
3 changes: 2 additions & 1 deletion spec/dummy/app/decorators/email/comment_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Email
class CommentDecorator < CommentDecorator
decorates_association :author, with: 'Foo', namespace: '::Bar'
# reset namespace to '' to not inherit 'Email' module namespace
decorates_association :author, with: ::Bar::FooDecorator, namespace: ''
end
end
2 changes: 1 addition & 1 deletion spec/dummy/app/decorators/post_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PostDecorator < Draper::Decorator
delegate :id, :created_at, :new_record?

decorates_association :comments
decorates_association :author, with: 'User'
decorates_association :author, with: UserDecorator

def posted_date
if created_at.to_date == DateTime.now.utc.to_date
Expand Down

0 comments on commit f8496db

Please sign in to comment.