lib/draper/decorator.rb in draper-1.2.1 vs lib/draper/decorator.rb in draper-1.3.0

- old
+ new

@@ -55,10 +55,11 @@ # @param [String, Symbol, Class] object_class # source class (or class name) that corresponds to this decorator. # @return [void] def self.decorates(object_class) @object_class = object_class.to_s.camelize.constantize + alias_object_to_object_class_name end # Returns the source class corresponding to the decorator class, as set by # {decorates}, or as inferred from the decorator class name (e.g. # `ProductDecorator` maps to `Product`). @@ -185,10 +186,24 @@ # @param [Class] klass def instance_of?(klass) 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 delegate :present?, :blank? # ActiveModel compatibility # @private @@ -216,9 +231,18 @@ raise if name && !error.missing_name?(name) Draper::CollectionDecorator end private + + def self.inherited(subclass) + subclass.alias_object_to_object_class_name + super + end + + def self.alias_object_to_object_class_name + alias_method object_class.name.underscore, :object if object_class? + end def self.object_class_name raise NameError if name.nil? || name.demodulize !~ /.+Decorator$/ name.chomp("Decorator") end