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
13
14
# 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.empty? ? '' : collection.first.class.name.underscore)}"
  @table_name = (options[:table_name] || @object_name.pluralize).to_s
  @object = @object_name.classify.constantize.new
  @column_count = 0

  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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/builders/generic_builder.rb', line 22

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

  order_params = (template.params['order'] || {})[@table_name]
  fields = Hash[*order_params.to_a.collect { |field_and_dir| field_and_dir.split(':') }.flatten]
  if @object && @object.default_order[:field].to_s == field
    # TODO: this should technically be fields.empty? && @object.default_order[:options][:stable],
    # but it lends itself to a little bit of an odd behavior unless you're expecting the additive ordering
    fields[field] = fields[field] || @object.default_order[:direction].to_s if fields.empty?
  end

  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



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

def render(options, &proc); end