mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Fix Decorator#inspect on Ruby 1.9
1.9 uses `to_s`, if defined, for `inspect`, so with `to_s` delegated, it's difficult to distinguish the decorator from the object at the console. This hacks `inspect` to return the same thing on 1.9 as it does on 2.0
This commit is contained in:
parent
7c14385ee6
commit
8fa5cd0d94
2 changed files with 39 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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"}))
|
||||
|
|
Loading…
Reference in a new issue