Class: Listalicious::GenericBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/builders/generic_builder.rb

Direct Known Subclasses

TableBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, collection, options = {}, &proc) ⇒ GenericBuilder

Returns a new instance of GenericBuilder.



6
7
8
9
10
11
12
# File 'lib/builders/generic_builder.rb', line 6

def initialize(template, collection, options = {}, &proc)
  @template, @collection, @options = template, collection, options
  @object_name = "#{options[:as] || collection.first ? collection.first.class.name.underscore : ''}"
  @table_name = (options[:table_name] || @object_name.pluralize).to_s

  render(options.delete(:html), &proc)
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



4
5
6
# File 'lib/builders/generic_builder.rb', line 4

def collection
  @collection
end

#templateObject

Returns the value of attribute template.



4
5
6
# File 'lib/builders/generic_builder.rb', line 4

def template
  @template
end

Instance Method Details

Creates the anchor tag for the orderable links. It looks in the params for specificly styled query params:

eg. order[]=login:asc&order[]=first_name:asc



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/builders/generic_builder.rb', line 20

def orderable_link(contents, field)
  return contents if @object_name.empty?

  order_params = (template.params['order'] || {})[@table_name]
  fields = Hash[*order_params.to_a.collect { |field_and_dir| field_and_dir.split(':') }.flatten]

  order_params = (template.params['order'] || {}).clone.
      merge({@table_name => ["#{field}:#{"#{fields[field] == 'asc' ? 'desc' : 'asc'}"}"]})

  query = template.params.reject{ |param, value| ['action', 'controller'].include?(param) }
  query.merge!('order' => order_params)

  template.(:a, contents,
            :href => "?#{query.to_query}",
            :class => "#{fields[field] ? fields[field] == 'asc' ? 'ascending' : 'descending' : ''}")
end

#render(options, &proc) ⇒ Object



14
# File 'lib/builders/generic_builder.rb', line 14

def render(options, &proc); end