Class: Draper::Decorator
- Inherits:
-
Object
- Object
- Draper::Decorator
- Extended by:
- Delegation
- Includes:
- ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, Compatibility::GlobalID, ViewHelpers
- Defined in:
- lib/draper/decorator.rb
Instance Attribute Summary collapse
-
#context ⇒ Hash
Extra data to be used in user-defined methods.
-
#object ⇒ Object
(also: #model)
readonly
The object being decorated.
Class Method Summary collapse
-
.collection_decorator_class ⇒ Class
The class created by Decorator.decorate_collection.
-
.decorate_collection(object, options = {}) ⇒ Object
Decorates a collection of objects.
-
.decorates(object_class) ⇒ void
Sets the source class corresponding to the decorator class.
-
.decorates_association(association, options = {}) ⇒ void
Automatically decorate an association.
-
.decorates_associations(*associations, options = {}) ⇒ void
Automatically decorate multiple associations.
-
.decorates_finders ⇒ void
Automatically decorates ActiveRecord finder methods, so that you can use
ProductDecorator.find(id)
instead ofProductDecorator.decorate(Product.find(id))
. -
.delegate_all ⇒ void
Automatically delegates instance methods to the source object.
-
.object_class ⇒ Class
Returns the source class corresponding to the decorator class, as set by Decorator.decorates, or as inferred from the decorator class name (e.g.
ProductDecorator
maps toProduct
). -
.object_class? ⇒ Boolean
Checks whether this decorator class has a corresponding Decorator.object_class.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the source object with a possibly-decorated object.
-
#applied_decorators ⇒ Array<Class>
The list of decorators that have been applied to the object.
-
#attributes ⇒ Hash
implemented by the decorator.
-
#decorated? ⇒ true
Checks if this object is decorated.
-
#decorated_with?(decorator_class) ⇒ Boolean
Checks if a given decorator has been applied to the object.
-
#eql?(other) ⇒ Boolean
Delegates equality to :== as expected.
-
#hash ⇒ Fixnum
Returns a unique hash for a decorated object based on the decorator class and the object being decorated.
-
#initialize(object, options = {}) ⇒ Decorator
constructor
Wraps an object in a new instance of the decorator.
-
#instance_of?(klass) ⇒ Boolean
Checks if
self.instance_of?(klass)
orobject.instance_of?(klass)
. -
#kind_of?(klass) ⇒ Boolean
(also: #is_a?)
Checks if
self.kind_of?(klass)
orobject.kind_of?(klass)
.
Methods included from Delegation
Methods included from ViewHelpers
Constructor Details
#initialize(object, options = {}) ⇒ Decorator
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.
32 33 34 35 36 37 |
# File 'lib/draper/decorator.rb', line 32 def initialize(object, = {}) .assert_valid_keys(:context) @object = object @context = .fetch(:context, {}) handle_multiple_decoration() if object.instance_of?(self.class) end |
Instance Attribute Details
#context ⇒ Hash
Returns extra data to be used in user-defined methods.
19 20 21 |
# File 'lib/draper/decorator.rb', line 19 def context @context end |
#object ⇒ Object (readonly) Also known as: model
Returns the object being decorated.
14 15 16 |
# File 'lib/draper/decorator.rb', line 14 def object @object end |
Class Method Details
.collection_decorator_class ⇒ Class
Returns the class created by decorate_collection.
228 229 230 231 232 233 |
# File 'lib/draper/decorator.rb', line 228 def self.collection_decorator_class name = collection_decorator_name name_constant = name&.safe_constantize name_constant || Draper::CollectionDecorator end |
.decorate_collection(object, options = {}) ⇒ Object
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
CollectionDecorator.
140 141 142 143 |
# File 'lib/draper/decorator.rb', line 140 def self.decorate_collection(object, = {}) .assert_valid_keys(:with, :context) collection_decorator_class.new(object, .reverse_merge(with: self)) end |
.decorates(object_class) ⇒ void
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
).
This method returns an undefined value.
Sets the source class corresponding to the decorator class.
60 61 62 63 |
# File 'lib/draper/decorator.rb', line 60 def self.decorates(object_class) @object_class = object_class.to_s.camelize.constantize alias_object_to_object_class_name end |
.decorates_association(association, options = {}) ⇒ void
This method returns an undefined value.
Automatically decorate an association.
106 107 108 109 110 111 112 |
# File 'lib/draper/decorator.rb', line 106 def self.decorates_association(association, = {}) .assert_valid_keys(:with, :scope, :context) define_method(association) do decorated_associations[association] ||= Draper::DecoratedAssociation.new(self, association, ) decorated_associations[association].call end end |
.decorates_associations(*associations, options = {}) ⇒ void
121 122 123 124 125 126 |
# File 'lib/draper/decorator.rb', line 121 def self.decorates_associations(*associations) = associations. associations.each do |association| decorates_association(association, ) end end |
.decorates_finders ⇒ void
This method returns an undefined value.
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 object_class.
88 89 90 |
# File 'lib/draper/decorator.rb', line 88 def self.decorates_finders extend Draper::Finders end |
.delegate_all ⇒ void
This method returns an undefined value.
Automatically delegates instance methods to the source object. Class methods will be delegated to the object_class, if it is set.
47 48 49 |
# File 'lib/draper/decorator.rb', line 47 def self.delegate_all include Draper::AutomaticDelegation end |
.object_class ⇒ Class
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
).
70 71 72 |
# File 'lib/draper/decorator.rb', line 70 def self.object_class @object_class ||= inferred_object_class end |
.object_class? ⇒ Boolean
Checks whether this decorator class has a corresponding object_class.
75 76 77 78 79 |
# File 'lib/draper/decorator.rb', line 75 def self.object_class? object_class rescue Draper::UninferrableObjectError false end |
Instance Method Details
#==(other) ⇒ Boolean
Compares the source object with a possibly-decorated object.
169 170 171 |
# File 'lib/draper/decorator.rb', line 169 def ==(other) Draper::Decoratable::Equality.test(object, other) end |
#applied_decorators ⇒ Array<Class>
Returns the list of decorators that have been applied to the object.
147 148 149 150 |
# File 'lib/draper/decorator.rb', line 147 def applied_decorators chain = object.respond_to?(:applied_decorators) ? object.applied_decorators : [] chain << self.class end |
#attributes ⇒ Hash
implemented by the decorator.
217 218 219 |
# File 'lib/draper/decorator.rb', line 217 def attributes object.attributes.select {|attribute, _| respond_to?(attribute) } end |
#decorated? ⇒ true
Checks if this object is decorated.
162 163 164 |
# File 'lib/draper/decorator.rb', line 162 def decorated? true end |
#decorated_with?(decorator_class) ⇒ Boolean
Checks if a given decorator has been applied to the object.
155 156 157 |
# File 'lib/draper/decorator.rb', line 155 def decorated_with?(decorator_class) applied_decorators.include?(decorator_class) end |
#eql?(other) ⇒ Boolean
Delegates equality to :== as expected
176 177 178 |
# File 'lib/draper/decorator.rb', line 176 def eql?(other) self == other end |
#hash ⇒ Fixnum
Returns a unique hash for a decorated object based on the decorator class and the object being decorated.
184 185 186 |
# File 'lib/draper/decorator.rb', line 184 def hash self.class.hash ^ object.hash end |
#instance_of?(klass) ⇒ Boolean
Checks if self.instance_of?(klass)
or object.instance_of?(klass)
200 201 202 |
# File 'lib/draper/decorator.rb', line 200 def instance_of?(klass) super || object.instance_of?(klass) end |
#kind_of?(klass) ⇒ Boolean Also known as: is_a?
Checks if self.kind_of?(klass)
or object.kind_of?(klass)
191 192 193 |
# File 'lib/draper/decorator.rb', line 191 def kind_of?(klass) super || object.kind_of?(klass) end |