Class: Listalicious::TableBuilder
- Inherits:
-
GenericBuilder
- Object
- GenericBuilder
- Listalicious::TableBuilder
- Defined in:
- lib/builders/table_builder.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #body(options = {}, &proc) ⇒ Object
- #body_column(contents, options = {}) ⇒ Object
- #column(*args, &proc) ⇒ Object
- #column_group(scope, options = {}, &proc) ⇒ Object
- #column_group_body(options = {}, &proc) ⇒ Object
- #column_group_foot(options = {}, &proc) ⇒ Object
- #column_group_head(options = {}, &proc) ⇒ Object
- #columns(scope = :body, options = {}, &proc) ⇒ Object
- #controls(contents = nil, options = {}, &proc) ⇒ Object
- #extra(contents = nil, options = {}, &proc) ⇒ Object
- #foot(options = {}, &proc) ⇒ Object
- #foot_column(contents, options = {}) ⇒ Object
- #full_column(contents = nil, options = {}, &proc) ⇒ Object
- #head(options = {}, &proc) ⇒ Object
- #head_column(contents, options = {}) ⇒ Object
- #render(options, &proc) ⇒ Object
Methods inherited from GenericBuilder
Constructor Details
This class inherits a constructor from Listalicious::GenericBuilder
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
4 5 6 |
# File 'lib/builders/table_builder.rb', line 4 def collection @collection end |
#template ⇒ Object
Returns the value of attribute template.
4 5 6 |
# File 'lib/builders/table_builder.rb', line 4 def template @template end |
Instance Method Details
#body(options = {}, &proc) ⇒ Object
15 16 17 |
# File 'lib/builders/table_builder.rb', line 15 def body( = {}, &proc) column_group(:body, , &proc) end |
#body_column(contents, options = {}) ⇒ Object
140 141 142 143 144 |
# File 'lib/builders/table_builder.rb', line 140 def body_column(contents, = {}) contents = @object.send(contents) if @object && contents.is_a?(Symbol) template.content_tag(:td, contents, .delete(:html)) end |
#column(*args, &proc) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/builders/table_builder.rb', line 76 def column(*args, &proc) = args. contents = == args.first ? nil : args.first if @current_scope == :body contents = if block_given? template.capture(self, &proc) elsif args.last.is_a?(Proc) args.last.call else contents end else contents = [:title] || contents end @column_count = @column_count + 1 self.send("#{@current_scope}_column", contents, ) end |
#column_group(scope, options = {}, &proc) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/builders/table_builder.rb', line 33 def column_group(scope, = {}, &proc) @current_scope = scope [:html] ||= {} self.send("column_group_#{scope}", , &proc) end |
#column_group_body(options = {}, &proc) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/builders/table_builder.rb', line 49 def column_group_body( = {}, &proc) buffer = '' return unless collection.first.present? collection.each_with_index do |record, index| @column_count = 0 @object = record if @options[:grouped_by] buffer << @head_wrapper if record[@options[:grouped_by]] != @last_row_grouping @last_row_grouping = record[@options[:grouped_by]] end @cycle = template.cycle('even', 'odd'); buffer << template.content_tag(:tr, template.capture(record, index, &proc), [:html].merge({:class => template.add_class([:html][:class], @cycle)})) buffer << template.content_tag(:tr, @extra, [:html].merge({:class => template.add_class([:html][:class], [@cycle, 'extra'])})) if @extra.present? @extra = nil end template.content_tag(:tbody, buffer, .delete(:wrapper_html)) end |
#column_group_foot(options = {}, &proc) ⇒ Object
71 72 73 74 |
# File 'lib/builders/table_builder.rb', line 71 def column_group_foot( = {}, &proc) buffer = template.content_tag(:tr, template.capture(collection.first, 0, &proc), [:html]) template.content_tag(:tfoot, buffer, .delete(:wrapper_html)) end |
#column_group_head(options = {}, &proc) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/builders/table_builder.rb', line 41 def column_group_head( = {}, &proc) @column_count = 0 @head_wrapper = template.content_tag(:tr, template.capture(collection.first || @object, 0, &proc), [:html].merge({:class => template.add_class([:html][:class], 'header')})) template.content_tag(:thead, @head_wrapper, .delete(:wrapper_html)) end |
#columns(scope = :body, options = {}, &proc) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/builders/table_builder.rb', line 23 def columns(scope = :body, = {}, &proc) raise ArgumentError, "Missing block" unless block_given? if scope.kind_of? Array scope.collect { |scope| column_group(scope, , &proc) } else column_group(scope, , &proc) end end |
#controls(contents = nil, options = {}, &proc) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/builders/table_builder.rb', line 106 def controls(contents = nil, = {}, &proc) return unless @current_scope == :body raise ArgumentError, "Must provide a string or a block" if !block_given? && contents.nil? contents ||= template.capture(self, &proc) [:html] ||= {} [:html][:class] = template.add_class([:html][:class], 'controls') self.send("#{@current_scope}_column", contents, ) end |
#extra(contents = nil, options = {}, &proc) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/builders/table_builder.rb', line 117 def extra(contents = nil, = {}, &proc) return unless @current_scope == :body raise ArgumentError, "Must provide a string or a block" if !block_given? && contents.nil? contents ||= template.capture(self, &proc) [:html] ||= {} [:html][:colspan] ||= @column_count @extra = self.send("#{@current_scope}_column", contents, ) '' end |
#foot(options = {}, &proc) ⇒ Object
19 20 21 |
# File 'lib/builders/table_builder.rb', line 19 def foot( = {}, &proc) column_group(:foot, , &proc) end |
#foot_column(contents, options = {}) ⇒ Object
146 147 148 149 150 |
# File 'lib/builders/table_builder.rb', line 146 def foot_column(contents, = {}) contents = orderable_link(contents, [:sort]) if [:sort] template.content_tag(:th, contents, .delete(:html)) end |
#full_column(contents = nil, options = {}, &proc) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/builders/table_builder.rb', line 96 def full_column(contents = nil, = {}, &proc) raise ArgumentError, "Must provide a string or a block" if !block_given? && contents.nil? contents ||= template.capture(self, &proc) [:html] ||= {} [:html][:colspan] ||= @column_count @extra = self.send("#{@current_scope}_column", contents, ) end |
#head(options = {}, &proc) ⇒ Object
11 12 13 |
# File 'lib/builders/table_builder.rb', line 11 def head( = {}, &proc) column_group(:head, , &proc) end |
#head_column(contents, options = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/builders/table_builder.rb', line 129 def head_column(contents, = {}) [:html] ||= {} [:html][:width] ||= [:width] [:sort] ||= contents.is_a?(Symbol) ? contents : nil contents = contents.to_s.humanize.titleize contents = orderable_link(contents, [:sort]) if [:sort] template.content_tag(:th, contents, .delete(:html)) end |
#render(options, &proc) ⇒ Object
6 7 8 9 |
# File 'lib/builders/table_builder.rb', line 6 def render(, &proc) buffer = template.capture(self, &proc) template.concat(template.content_tag(:table, buffer, )) end |