diff --git a/lib/draper/decorator.rb b/lib/draper/decorator.rb index 2824172..e71c328 100755 --- a/lib/draper/decorator.rb +++ b/lib/draper/decorator.rb @@ -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 diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 047bb2b..2a0f824 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -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 /#/ + end + + it "includes the object" do + decorator = Decorator.new(double(inspect: "#")) + + expect(decorator.inspect).to include "@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"}))