Skip to content

Commit

Permalink
Merge pull request #105 from sinsoku/patch/fix_a_wrong_decoration
Browse files Browse the repository at this point in the history
Fix wrong decoration for nested classes
  • Loading branch information
amatsuda committed Aug 15, 2019
2 parents c7e93c4 + dbc85d3 commit 28d9bcc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_decorator/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def decorator_for(model_class)
return @@decorators[model_class] if @@decorators.key? model_class

decorator_name = "#{model_class.name}#{ActiveDecorator.config.decorator_suffix}"
d = Object.const_get decorator_name, false
d = decorator_name.constantize
unless Class === d
d.send :include, ActiveDecorator::Helpers
@@decorators[model_class] = d
Expand Down
7 changes: 7 additions & 0 deletions test/decorator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ class DecoratorTest < Test::Unit::TestCase
assert value.is_a?(BookDecorator)
end
end

test "Don't use the wrong decorator for nested classes" do
comic = Foo::Comic.new

assert_equal comic, ActiveDecorator::Decorator.instance.decorate(comic)
assert !comic.is_a?(ComicDecorator)
end
end
3 changes: 3 additions & 0 deletions test/fake_app/app/decorators/comic_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# decorator to test auto-loading behavior
# this module is intended not to be loaded.
module ComicDecorator; end
4 changes: 4 additions & 0 deletions test/fake_app/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,7 @@ def method_missing(method, *args, &block)
@ar_relation.public_send(method, *args, &block)
end
end

module Foo
class Comic; end
end

0 comments on commit 28d9bcc

Please sign in to comment.