Class: ViewModels::Helpers::Mapping::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/view_models/helpers/collection.rb

Overview

The Collection view_model helper has the purpose of presenting presentable collections.

  • Render as list

  • Render as table

  • Render as collection

  • Render a pagination

Instance Method Summary (collapse)

Constructor Details

- (Collection) initialize(collection, context)

A new instance of Collection



37
38
39
# File 'lib/view_models/helpers/collection.rb', line 37

def initialize collection, context
  @collection, @context = collection, context
end

Instance Method Details

- (Object) collection(options = {})

Note:

The only difference between a list and a collection is the enclosing list type. While list uses ol, the collection uses ul.

Renders a collection.

Parameters:

  • options (Hash) (defaults to: {})

    The options for the collection

Options Hash (options):

  • :collection (Object)

    the collection to iterate over (default: The collection of the collection view_model to iterate over)

  • :context (Object)

    the context to render in (default: The original context given to the collection view_model to render in)

  • :template_name (Object)

    the template to render for each model element (default: Uses :collection_item as the default element template)

  • :separator (Object)

    > separator between each element (default: nil separator in html)



63
64
65
# File 'lib/view_models/helpers/collection.rb', line 63

def collection options = {}          
  render_partial :collection, template_locals(:collection_item, options)
end

- (Object) list(options = {})

Renders a list (in the broadest sense of the word).

Parameters:

  • options (Hash) (defaults to: {})

    The options for the list

Options Hash (options):

  • :collection (Object)

    the collection to iterate over (default: The collection of the collection view_model to iterate over)

  • :context (Object)

    context to render in (default: The original context given to the collection view_model to render in)

  • :template_name (Object)

    the template to render for each model element (default: list_item as the default element template)

  • :separator (Object)

    the separator between each element (default: nil separator in html)



49
50
51
# File 'lib/view_models/helpers/collection.rb', line 49

def list options = {}          
  render_partial :list, template_locals(:list_item, options)
end

- (Object) pagination(options = {})

Renders a pagination.

Parameters:

  • options (Hash) (defaults to: {})

    The options for the collection

Options Hash (options):

  • :collection (Object)

    the collection to iterate over (default: The collection of the collection view_model to iterate over)

  • :context (Object)

    the context to render in (default: The original context given to the collection view_model to render in)

  • :template_name (Object)

    the template to render for each model element (default: Uses :collection_item as the default element template)

  • :separator (Object)

    > separator between each element (default: | separator in html)



89
90
91
# File 'lib/view_models/helpers/collection.rb', line 89

def pagination options = {}          
  render_partial :pagination, template_locals(:pagination, {:separator => '|'}.merge(options))
end

- (Object) table(options = {})

Renders a table.

Note: Each item represents a table row.

Parameters:

  • options (Hash) (defaults to: {})

    The options for the table

Options Hash (options):

  • :collection (Object)

    the collection to iterate over (default: The collection of the collection view_model to iterate over)

  • :context (Object)

    the context to render in (default: The original context given to the collection view_model to render in)

  • :template_name (Object)

    the template to render for each model element (default: Uses :table_row as the default element template)

  • :separator (Object)

    > separator between each element (default: nil separator in html)



77
78
79
# File 'lib/view_models/helpers/collection.rb', line 77

def table options = {}          
  render_partial :table, template_locals(:table_row, options)
end