Class: Listalicious::TableBuilder

Inherits:
GenericBuilder show all
Defined in:
lib/builders/table_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from GenericBuilder

#initialize, #orderable_link

Constructor Details

This class inherits a constructor from Listalicious::GenericBuilder

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

#templateObject

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(options = {}, &proc)
  column_group(:body, options, &proc)
end

#body_column(contents, options = {}) ⇒ Object



140
141
142
143
144
# File 'lib/builders/table_builder.rb', line 140

def body_column(contents, options = {})
  contents = @object.send(contents) if @object && contents.is_a?(Symbol)

  template.(:td, contents, options.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)
  options = args.extract_options!
  contents = options == 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 = options[:title] || contents
  end

  @column_count = @column_count + 1
  self.send("#{@current_scope}_column", contents, options)
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, options = {}, &proc)
  @current_scope = scope

  options[:html] ||= {}

  self.send("column_group_#{scope}", options, &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(options = {}, &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.(:tr, template.capture(record, index, &proc),
                 options[:html].merge({:class => template.add_class(options[:html][:class], @cycle)}))
    buffer << template.(:tr, @extra,
                 options[:html].merge({:class => template.add_class(options[:html][:class], [@cycle, 'extra'])})) if @extra.present?
    @extra = nil
  end
  template.(:tbody, buffer, options.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(options = {}, &proc)
  buffer = template.(:tr, template.capture(collection.first, 0, &proc), options[:html])
  template.(:tfoot, buffer, options.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(options = {}, &proc)
  @column_count = 0

  @head_wrapper = template.(:tr, template.capture(collection.first || @object, 0, &proc),
                     options[:html].merge({:class => template.add_class(options[:html][:class], 'header')}))
  template.(:thead, @head_wrapper, options.delete(:wrapper_html))
end

#columns(scope = :body, options = {}, &proc) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
# File 'lib/builders/table_builder.rb', line 23

def columns(scope = :body, options = {}, &proc)
  raise ArgumentError, "Missing block" unless block_given?

  if scope.kind_of? Array
    scope.collect { |scope| column_group(scope, options, &proc) }
  else
    column_group(scope, options, &proc)
  end
end

#controls(contents = nil, options = {}, &proc) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
# File 'lib/builders/table_builder.rb', line 106

def controls(contents = nil, options = {}, &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)

  options[:html] ||= {}
  options[:html][:class] = template.add_class(options[:html][:class], 'controls')

  self.send("#{@current_scope}_column", contents, options)
end

#extra(contents = nil, options = {}, &proc) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
# File 'lib/builders/table_builder.rb', line 117

def extra(contents = nil, options = {}, &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)

  options[:html] ||= {}
  options[:html][:colspan] ||= @column_count

  @extra = self.send("#{@current_scope}_column", contents, options)
  ''
end

#foot(options = {}, &proc) ⇒ Object



19
20
21
# File 'lib/builders/table_builder.rb', line 19

def foot(options = {}, &proc)
  column_group(:foot, options, &proc)
end

#foot_column(contents, options = {}) ⇒ Object



146
147
148
149
150
# File 'lib/builders/table_builder.rb', line 146

def foot_column(contents, options = {})
  contents = orderable_link(contents, options[:sort]) if options[:sort]

  template.(:th, contents, options.delete(:html))
end

#full_column(contents = nil, options = {}, &proc) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
101
102
103
104
# File 'lib/builders/table_builder.rb', line 96

def full_column(contents = nil, options = {}, &proc)
  raise ArgumentError, "Must provide a string or a block" if !block_given? && contents.nil?
  contents ||= template.capture(self, &proc)

  options[:html] ||= {}
  options[:html][:colspan] ||= @column_count

  @extra = self.send("#{@current_scope}_column", contents, options)
end

#head(options = {}, &proc) ⇒ Object



11
12
13
# File 'lib/builders/table_builder.rb', line 11

def head(options = {}, &proc)
  column_group(:head, options, &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, options = {})
  options[:html] ||= {}
  options[:html][:width] ||= options[:width]

  options[:sort] ||= contents.is_a?(Symbol) ? contents : nil
  contents = contents.to_s.humanize.titleize
  contents = orderable_link(contents, options[:sort]) if options[:sort]

  template.(:th, contents, options.delete(:html))
end

#render(options, &proc) ⇒ Object



6
7
8
9
# File 'lib/builders/table_builder.rb', line 6

def render(options, &proc)
  buffer = template.capture(self, &proc)
  template.concat(template.(:table, buffer, options))
end