Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/draper/automatic_delegation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def respond_to_missing?(method, include_private = false)
super || delegatable?(method)
end

# Proxies missing constants to the source class.
def const_missing(name)
return object_class.const_get(name) if object_class?
Comment thread
Alexander-Senko marked this conversation as resolved.
Outdated

super
end

# @private
def delegatable?(method)
object_class? && object_class.respond_to?(method)
Expand Down
24 changes: 24 additions & 0 deletions spec/draper/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,30 @@ def hello_world
end
end

context ".const_missing" do
context "without an object class" do
it "raises a NameError on missing constants" do
expect{Decorator::HELLO_WORLD}.to raise_error NameError, /HELLO_WORLD/
end
end

context "with an object class" do
it "delegates constants that exist on the object class" do
object_class = Class.new
object_class.const_set(:HELLO_WORLD, :delegated)
allow(Decorator).to receive_messages object_class: object_class

expect(Decorator::HELLO_WORLD).to be :delegated
end

it "raises a NameError for constants that do not exist on the object class" do
allow(Decorator).to receive_messages object_class: Class.new

expect{Decorator::HELLO_WORLD}.to raise_error NameError, /HELLO_WORLD/
end
end
end

describe "#respond_to?" do
it "returns true for its own methods" do
Decorator.class_eval{def hello_world; end}
Expand Down