Class: Shape::ViewDecorator

Inherits:
Object
  • Object
show all
Includes:
Base::ClassMethods
Defined in:
lib/shape/view_decorator.rb

Overview

View Decorator

Allows for creating different, composable decorator views

Example:

property :href
property :addr_line1
property :addr_line2
property :city
# ...

view :with_lat_long do
  property :latitude
  property :longitude
end

view :with_facilities do
  link :facilities
end

view :full do
  view :with_lat_long    # nests with_lat_long view
  view :with_facilities  # nests with_facilities view
end

To use a decorator view, pass the view in to the decorator context. For example:

@address = AddressDecorator.new(
  Address.find(params[:id]),
  context: {view: :full})

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::ClassMethods

#_properties_from, #associations, #delegate, #properties, #properties_from, #property, #shape, #shape_collection, #shaper_context

Constructor Details

#initialize(decorator_context, name, options = {}, &block) ⇒ ViewDecorator

Returns a new instance of ViewDecorator.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shape/view_decorator.rb', line 43

def initialize(decorator_context, name, options={}, &block)
  self.decorator_context = decorator_context
  self.name = name
  self.options = options

  if block
    instance_eval(&block)
  else
    views[name] = decorator_context.views[name]
  end
end

Instance Attribute Details

#decorator_contextObject

Returns the value of attribute decorator_context.



40
41
42
# File 'lib/shape/view_decorator.rb', line 40

def decorator_context
  @decorator_context
end

#nameObject

Returns the value of attribute name.



39
40
41
# File 'lib/shape/view_decorator.rb', line 39

def name
  @name
end

#optionsObject

Returns the value of attribute options.



41
42
43
# File 'lib/shape/view_decorator.rb', line 41

def options
  @options
end