class PactBroker::Api::Decorators::BaseDecorator

Public Class Methods

camelize_property_names() click to toggle source

Call this method to automatically camelize property names without having to define an :as each time.

# File lib/pact_broker/api/decorators/base_decorator.rb, line 21
def self.camelize_property_names
  @camelize = true
end
eager_load_associations() click to toggle source

Returns the names of the model associations to eager load for use with this decorator. The default implementation attempts to do an “auto detect” of the associations. For single item decorators, it attempts to identify the attributes that are themselves models. For collection decorators, it delegates to the eager_load_associations method of the single item decorator used to decorate the collection.

The “auto detect” logic can only go so far. It cannot identify when a child object needs its own child object(s) to render the attribute. This method should be overridden when the “auto detect” logic cannot identify the correct associations to load. eg VersionDecorator @return [Array<Symbol>]

# File lib/pact_broker/api/decorators/base_decorator.rb, line 48
def self.eager_load_associations
  if is_collection_resource?
    collection_item_decorator_class.eager_load_associations
  else
    embedded_and_collection_attribute_names
  end
end
property(name, options={}, &block) click to toggle source

Overrides the default property method to add a camelised :as option when camelize_property_names has been called for this decorator. @override

Calls superclass method
# File lib/pact_broker/api/decorators/base_decorator.rb, line 28
def self.property(name, options={}, &block)
  if options.delete(:camelize) || @camelize
    camelized_name = name.to_s.camelcase(false).to_sym
    super(name, { as: camelized_name }.merge(options), &block)
  else
    super
  end
end

Private Class Methods

collection_item_decorator_class() click to toggle source

@return [Class] The decorator class used to decorate the items in the collection

# File lib/pact_broker/api/decorators/base_decorator.rb, line 74
def self.collection_item_decorator_class
  representable_attrs.to_h.without("links", "page").values.first[:extend].call
end
embedded_and_collection_attribute_names() click to toggle source

Returns the names of the model attributes that are collections, embedded or nested items @return [Array<Symbol>]

# File lib/pact_broker/api/decorators/base_decorator.rb, line 68
def self.embedded_and_collection_attribute_names
  representable_attrs.values.select{ | attr| attr[:collection] || attr[:embedded] || attr[:nested] }.collect{ |attr| attr[:name].to_sym }
end
is_collection_resource?() click to toggle source

Returns true if this class is a decorator for a collection @return [true, false]

# File lib/pact_broker/api/decorators/base_decorator.rb, line 58
def self.is_collection_resource?
  representable_attrs_without_links = representable_attrs.to_h.without("links", "page")
  representable_attrs_without_links.size == 1 &&
    representable_attrs_without_links.values.first[:collection] &&
    representable_attrs_without_links.values.first[:extend]
end