lib/draper/decorator.rb in draper-1.1.0 vs lib/draper/decorator.rb in draper-1.2.0

- old
+ new

@@ -1,10 +1,13 @@ module Draper class Decorator include Draper::ViewHelpers extend Draper::Delegation + include ActiveModel::Serialization + include ActiveModel::Serializers::JSON + include ActiveModel::Serializers::Xml # @return the object being decorated. attr_reader :source alias_method :model, :source alias_method :to_source, :source @@ -158,11 +161,11 @@ # Compares the source with a possibly-decorated object. # # @return [Boolean] def ==(other) - source.extend(Draper::Decoratable::Equality) == other + Draper::Decoratable::Equality.test(source, other) end # Checks if `self.kind_of?(klass)` or `source.kind_of?(klass)` # # @param [Class] klass @@ -185,20 +188,28 @@ # @private def to_model self end + # @return [Hash] the source's attributes, sliced to only include those + # implemented by the decorator. + def attributes + source.attributes.select {|attribute, _| respond_to?(attribute) } + end + # ActiveModel compatibility - delegate :attributes, :to_param, :to_partial_path + delegate :to_param, :to_partial_path # ActiveModel compatibility singleton_class.delegate :model_name, to: :source_class # @return [Class] the class created by {decorate_collection}. def self.collection_decorator_class - collection_decorator_name.constantize - rescue NameError + name = collection_decorator_name + name.constantize + rescue NameError => error + raise if name && !error.missing_name?(name) Draper::CollectionDecorator end private @@ -206,11 +217,13 @@ raise NameError if name.nil? || name.demodulize !~ /.+Decorator$/ name.chomp("Decorator") end def self.inferred_source_class - source_name.constantize - rescue NameError + name = source_name + name.constantize + rescue NameError => error + raise if name && !error.missing_name?(name) raise Draper::UninferrableSourceError.new(self) end def self.collection_decorator_name plural = source_name.pluralize