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 13 14 |
# File 'lib/builders/generic_builder.rb', line 6 def initialize(template, collection, = {}, &proc) @template, @collection, @options = template, collection, @object_name = "#{[:as] || (collection.empty? ? '' : collection.first.class.name.underscore)}" @table_name = ([:table_name] || @object_name.pluralize).to_s @object = @object_name.classify.constantize.new @column_count = 0 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
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.content_tag(: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(, &proc); end |