1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Merge pull request #563 from haines/to_s

Fix Decorator#inspect on Ruby 1.9
This commit is contained in:
Steve Klabnik 2013-07-15 08:01:22 -07:00
commit 68dd3b978a
2 changed files with 39 additions and 0 deletions

View file

@ -188,6 +188,18 @@ module Draper
super || object.instance_of?(klass)
end
if RUBY_VERSION < "2.0"
# nasty hack to stop 1.9.x using the delegated `to_s` in `inspect`
alias_method :_to_s, :to_s
def inspect
ivars = instance_variables.map do |name|
"#{name}=#{instance_variable_get(name).inspect}"
end
_to_s.insert(-2, " #{ivars.join(", ")}")
end
end
delegate :to_s
# In case object is nil

View file

@ -449,6 +449,33 @@ module Draper
end
end
describe "#inspect" do
it "returns a detailed description of the decorator" do
decorator = ProductDecorator.new(double)
expect(decorator.inspect).to match /#<ProductDecorator:0x\h+ .+>/
end
it "includes the object" do
decorator = Decorator.new(double(inspect: "#<the object>"))
expect(decorator.inspect).to include "@object=#<the object>"
end
it "includes the context" do
decorator = Decorator.new(double, context: {foo: "bar"})
expect(decorator.inspect).to include '@context={:foo=>"bar"}'
end
it "includes other instance variables" do
decorator = Decorator.new(double)
decorator.instance_variable_set :@foo, "bar"
expect(decorator.inspect).to include '@foo="bar"'
end
end
describe "#attributes" do
it "returns only the object's attributes that are implemented by the decorator" do
decorator = Decorator.new(double(attributes: {foo: "bar", baz: "qux"}))