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

- old
+ new

@@ -6,41 +6,42 @@ 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 + attr_reader :object + alias_method :model, :object + alias_method :source, :object # TODO: deprecate this + alias_method :to_source, :object # TODO: deprecate this # @return [Hash] extra data to be used in user-defined methods. attr_accessor :context # Wraps an object in a new instance of the decorator. # # Decorators may be applied to other decorators. However, applying a # decorator to an instance of itself will create a decorator with the same # source as the original, rather than redecorating the other instance. # - # @param [Object] source + # @param [Object] object # object to decorate. # @option options [Hash] :context ({}) # extra data to be stored in the decorator and used in user-defined # methods. - def initialize(source, options = {}) + def initialize(object, options = {}) options.assert_valid_keys(:context) - @source = source + @object = object @context = options.fetch(:context, {}) - handle_multiple_decoration(options) if source.instance_of?(self.class) + handle_multiple_decoration(options) if object.instance_of?(self.class) end class << self alias_method :decorate, :new end # Automatically delegates instance methods to the source object. Class - # methods will be delegated to the {source_class}, if it is set. + # methods will be delegated to the {object_class}, if it is set. # # @return [void] def self.delegate_all include Draper::AutomaticDelegation end @@ -49,38 +50,43 @@ # # @note This is only necessary if you wish to proxy class methods to the # source (including when using {decorates_finders}), and the source class # cannot be inferred from the decorator class (e.g. `ProductDecorator` # maps to `Product`). - # @param [String, Symbol, Class] source_class + # @param [String, Symbol, Class] object_class # source class (or class name) that corresponds to this decorator. # @return [void] - def self.decorates(source_class) - @source_class = source_class.to_s.camelize.constantize + def self.decorates(object_class) + @object_class = object_class.to_s.camelize.constantize 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`). # # @return [Class] the source class that corresponds to this decorator. - def self.source_class - @source_class ||= inferred_source_class + def self.object_class + @object_class ||= inferred_object_class end - # Checks whether this decorator class has a corresponding {source_class}. - def self.source_class? - source_class + # Checks whether this decorator class has a corresponding {object_class}. + def self.object_class? + object_class rescue Draper::UninferrableSourceError false end + class << self # TODO deprecate this + alias_method :source_class, :object_class + alias_method :source_class?, :object_class? + end + # Automatically decorates ActiveRecord finder methods, so that you can use # `ProductDecorator.find(id)` instead of # `ProductDecorator.decorate(Product.find(id))`. # - # Finder methods are applied to the {source_class}. + # Finder methods are applied to the {object_class}. # # @return [void] def self.decorates_finders extend Draper::Finders end @@ -124,26 +130,26 @@ # Decorates a collection of objects. The class of the collection decorator # is inferred from the decorator class if possible (e.g. `ProductDecorator` # maps to `ProductsDecorator`), but otherwise defaults to # {Draper::CollectionDecorator}. # - # @param [Object] source + # @param [Object] object # collection to decorate. # @option options [Class, nil] :with (self) # the decorator class used to decorate each item. When `nil`, it is # inferred from each item. # @option options [Hash] :context # extra data to be stored in the collection decorator. - def self.decorate_collection(source, options = {}) + def self.decorate_collection(object, options = {}) options.assert_valid_keys(:with, :context) - collection_decorator_class.new(source, options.reverse_merge(with: self)) + collection_decorator_class.new(object, options.reverse_merge(with: self)) end # @return [Array<Class>] the list of decorators that have been applied to # the object. def applied_decorators - chain = source.respond_to?(:applied_decorators) ? source.applied_decorators : [] + chain = object.respond_to?(:applied_decorators) ? object.applied_decorators : [] chain << self.class end # Checks if a given decorator has been applied to the object. # @@ -157,52 +163,52 @@ # @return [true] def decorated? true end - # Compares the source with a possibly-decorated object. + # Compares the source object with a possibly-decorated object. # # @return [Boolean] def ==(other) - Draper::Decoratable::Equality.test(source, other) + Draper::Decoratable::Equality.test(object, other) end - # Checks if `self.kind_of?(klass)` or `source.kind_of?(klass)` + # Checks if `self.kind_of?(klass)` or `object.kind_of?(klass)` # # @param [Class] klass def kind_of?(klass) - super || source.kind_of?(klass) + super || object.kind_of?(klass) end alias_method :is_a?, :kind_of? - # Checks if `self.instance_of?(klass)` or `source.instance_of?(klass)` + # Checks if `self.instance_of?(klass)` or `object.instance_of?(klass)` # # @param [Class] klass def instance_of?(klass) - super || source.instance_of?(klass) + super || object.instance_of?(klass) end - # In case source is nil - delegate :present? + # In case object is nil + delegate :present?, :blank? # ActiveModel compatibility # @private def to_model self end - # @return [Hash] the source's attributes, sliced to only include those + # @return [Hash] the object's attributes, sliced to only include those # implemented by the decorator. def attributes - source.attributes.select {|attribute, _| respond_to?(attribute) } + object.attributes.select {|attribute, _| respond_to?(attribute) } end # ActiveModel compatibility delegate :to_param, :to_partial_path # ActiveModel compatibility - singleton_class.delegate :model_name, to: :source_class + singleton_class.delegate :model_name, to: :object_class # @return [Class] the class created by {decorate_collection}. def self.collection_decorator_class name = collection_decorator_name name.constantize @@ -211,32 +217,32 @@ Draper::CollectionDecorator end private - def self.source_name + def self.object_class_name raise NameError if name.nil? || name.demodulize !~ /.+Decorator$/ name.chomp("Decorator") end - def self.inferred_source_class - name = source_name + def self.inferred_object_class + name = object_class_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 - raise NameError if plural == source_name + plural = object_class_name.pluralize + raise NameError if plural == object_class_name "#{plural}Decorator" end def handle_multiple_decoration(options) - if source.applied_decorators.last == self.class - @context = source.context unless options.has_key?(:context) - @source = source.source + if object.applied_decorators.last == self.class + @context = object.context unless options.has_key?(:context) + @object = object.object else warn "Reapplying #{self.class} decorator to target that is already decorated with it. Call stack:\n#{caller(1).join("\n")}" end end