Class: Listalicious::GenericBuilder
- Inherits:
-
Object
- Object
- Listalicious::GenericBuilder
- Defined in:
- lib/builders/generic_builder.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
-
#initialize(template, collection, options = {}, &proc) ⇒ GenericBuilder
constructor
A new instance of GenericBuilder.
-
#orderable_link(contents, field) ⇒ Object
Creates the anchor tag for the orderable links.
- #render(options, &proc) ⇒ Object
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, = {}, &proc) @template, @collection, @options = template, collection, @object_name = "#{[:as] || collection.first ? collection.first.class.name.underscore : ''}" @table_name = ([:table_name] || @object_name.pluralize).to_s render(.delete(:html), &proc) end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
4 5 6 |
# File 'lib/builders/generic_builder.rb', line 4 def collection @collection end |
#template ⇒ Object
Returns the value of attribute template.
4 5 6 |
# File 'lib/builders/generic_builder.rb', line 4 def template @template end |
Instance Method Details
#orderable_link(contents, field) ⇒ Object
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.content_tag(: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(, &proc); end |