Class: Releaf::Builders::TableBuilder
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
          - Object
- Releaf::Builders::TableBuilder
 show all
      - Includes:
- Base, Toolbox
    - Defined in:
- app/builders/releaf/builders/table_builder.rb
 
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
      
        - 
  
    
      #association_column?(column)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #association_name(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #body  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #cell(resource, column, options)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #cell_content(resource, column, options)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #cell_content_method(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #cell_format_method(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #cell_method(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #column_klass(klass, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #column_names  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #column_type(klass, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #column_type_format_method(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #column_value(resource_or_value, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #columns  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #columns_schema  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #empty_body  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_association_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_boolean_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_date_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_datetime_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_richtext_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_string_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_text_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_textarea_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #format_time_content(resource, column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #head  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #head_cell(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #head_cell_content(column)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #initialize(collection, resource_class, template, options)  ⇒ TableBuilder 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of TableBuilder. 
 
- 
  
    
      #output  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #row(resource)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #row_attributes(resource)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #row_url(resource)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #row_url_action(_resource)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #table_attributes  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #toolbox_cell(resource, options)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #type_format_method(type)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
Methods included from Toolbox
  #toolbox, #toolbox_button, #toolbox_menu
  
  
  
  
  
  
  
  
  Methods included from Base
  #default_translation_scope, #html_escape, #icon, #layout_settings, #locale_options, #resource_title, #safe_join, #t, #tag, #template_variable, #translate_locale, #wrapper
  
  Constructor Details
  
    
  
  
    #initialize(collection, resource_class, template, options)  ⇒ TableBuilder 
  
  
  
  
    
Returns a new instance of TableBuilder.
   
 
  
  
    | 
6
7
8
9
10
11 | # File 'app/builders/releaf/builders/table_builder.rb', line 6
def initialize(collection, resource_class, template, options)
  self.collection = collection
  self.options = options
  self.template = template
  self.resource_class = resource_class
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #collection  ⇒ Object 
  
  
  
  
    
Returns the value of attribute collection.
   
 
  
  
    | 
4
5
6 | # File 'app/builders/releaf/builders/table_builder.rb', line 4
def collection
  @collection
end | 
 
    
      
      
      
  
  
    #options  ⇒ Object 
  
  
  
  
    
Returns the value of attribute options.
   
 
  
  
    | 
4
5
6 | # File 'app/builders/releaf/builders/table_builder.rb', line 4
def options
  @options
end | 
 
    
      
      
      
  
  
    #resource_class  ⇒ Object 
  
  
  
  
    
Returns the value of attribute resource_class.
   
 
  
  
    | 
4
5
6 | # File 'app/builders/releaf/builders/table_builder.rb', line 4
def resource_class
  @resource_class
end | 
 
    
      
      
      
  
  
    #template  ⇒ Object 
  
  
  
  
    
Returns the value of attribute template.
   
 
  
  
    | 
4
5
6 | # File 'app/builders/releaf/builders/table_builder.rb', line 4
def template
  @template
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #association_column?(column)  ⇒ Boolean 
  
  
  
  
    | 
261
262
263 | # File 'app/builders/releaf/builders/table_builder.rb', line 261
def association_column?(column)
  !!(column =~ /_id$/) && resource_class.reflections[association_name(column).to_s].present?
end | 
 
    
      
  
  
    #association_name(column)  ⇒ Object 
  
  
  
  
    | 
188
189
190 | # File 'app/builders/releaf/builders/table_builder.rb', line 188
def association_name(column)
  column.to_s.sub(/_id$/, '').to_sym
end | 
 
    
      
  
  
    #body  ⇒ Object 
  
  
  
  
    | 
90
91
92
93
94
95
96 | # File 'app/builders/releaf/builders/table_builder.rb', line 90
def body
  tag(:tbody, class: "tbody") do
    collection.collect do |resource|
      row(resource)
    end
  end
end | 
 
    
      
  
  
    #cell(resource, column, options)  ⇒ Object 
  
  
  
  
    | 
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 | # File 'app/builders/releaf/builders/table_builder.rb', line 272
def cell(resource, column, options)
  content = cell_content(resource, column, options)
  tag(:td) do
    if options[:url].blank?
      tag(:span) do
        content
      end
    else
      tag(:a, href: options[:url]) do
        content
      end
    end
  end
end | 
 
    
      
  
  
    #cell_content(resource, column, options)  ⇒ Object 
  
  
  
  
    | 
136
137
138
139
140
141
142 | # File 'app/builders/releaf/builders/table_builder.rb', line 136
def cell_content(resource, column, options)
  if options[:content_method]
    send(options[:content_method], resource)
  else
    send(options[:format_method], resource, column)
  end
end | 
 
    
      
  
  
    #cell_content_method(column)  ⇒ Object 
  
  
  
  
    | 
201
202
203
204
205
206
207
208 | # File 'app/builders/releaf/builders/table_builder.rb', line 201
def cell_content_method(column)
  method_name = "#{column}_content"
  if respond_to? method_name
    method_name
  else
    nil
  end
end | 
 
    
      
  
  
    | 
253
254
255
256
257
258
259 | # File 'app/builders/releaf/builders/table_builder.rb', line 253
def cell_format_method(column)
  if association_column?(column)
    :format_association_content
  else
    column_type_format_method(column)
  end
end | 
 
    
      
  
  
    #cell_method(column)  ⇒ Object 
  
  
  
  
    | 
192
193
194
195
196
197
198
199 | # File 'app/builders/releaf/builders/table_builder.rb', line 192
def cell_method(column)
  method_name = "#{column}_cell"
  if respond_to? method_name
    method_name
  else
    nil
  end
end | 
 
    
      
  
  
    #column_klass(klass, column)  ⇒ Object 
  
  
  
  
    | 
236
237
238
239
240
241
242
243 | # File 'app/builders/releaf/builders/table_builder.rb', line 236
def column_klass(klass, column)
  column.to_s.split(".")[0..-2].each do|part|
    reflection = klass.reflect_on_association(part)
    klass = reflection.klass if reflection
  end
  klass
end | 
 
    
      
  
  
    #column_names  ⇒ Object 
  
  
  
  
    | 
13
14
15 | # File 'app/builders/releaf/builders/table_builder.rb', line 13
def column_names
  Releaf::ResourceTableFields.new(resource_class).values(include_associations: false)
end | 
 
    
      
  
  
    #column_type(klass, column)  ⇒ Object 
  
  
  
  
    | 
210
211
212
213
214
215
216
217 | # File 'app/builders/releaf/builders/table_builder.rb', line 210
def column_type(klass, column)
  column_description = klass.columns_hash[column.to_s]
  if column_description
    column_description.type
  else
    :string
  end
end | 
 
    
      
  
  
    | 
219
220
221
222
223
224 | # File 'app/builders/releaf/builders/table_builder.rb', line 219
def column_type_format_method(column)
  klass = column_klass(resource_class, column)
  type = column_type(klass, column)
  type_format_method(type)
end | 
 
    
      
  
  
    #column_value(resource_or_value, column)  ⇒ Object 
  
  
  
  
    | 
245
246
247
248
249
250
251 | # File 'app/builders/releaf/builders/table_builder.rb', line 245
def column_value(resource_or_value, column)
  column.to_s.split(".").each do|part|
    resource_or_value = resource_or_value.send(part) if resource_or_value.present?
  end
  resource_or_value
end | 
 
    
      
  
  
    #columns  ⇒ Object 
  
  
  
  
    | 
17
18
19 | # File 'app/builders/releaf/builders/table_builder.rb', line 17
def columns
  @columns ||= columns_schema
end | 
 
    
      
  
  
    #columns_schema  ⇒ Object 
  
  
  
  
    | 
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | # File 'app/builders/releaf/builders/table_builder.rb', line 21
def columns_schema
  data = {}
  final_column_names = []
  final_column_names += column_names
  final_column_names << :toolbox if options[:toolbox] == true
  final_column_names.map do|column|
    if cell_method(column)
      data[column] = {cell_method: cell_method(column)}
    elsif cell_content_method(column)
      data[column] = {content_method: cell_content_method(column)}
    else
      data[column] = {format_method: cell_format_method(column)}
    end
  end
  data
end | 
 
    
      
  
  
    #empty_body  ⇒ Object 
  
  
  
  
    | 
80
81
82
83
84
85
86
87
88 | # File 'app/builders/releaf/builders/table_builder.rb', line 80
def empty_body
  tag(:tr) do
    tag(:th) do
      tag(:div, class: "nothing-found") do
        t("Nothing found")
      end
    end
  end
end | 
 
    
      
  
  
    #format_association_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
184
185
186 | # File 'app/builders/releaf/builders/table_builder.rb', line 184
def format_association_content(resource, column)
  format_string_content(resource, association_name(column))
end | 
 
    
      
  
  
    #format_boolean_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
162
163
164 | # File 'app/builders/releaf/builders/table_builder.rb', line 162
def format_boolean_content(resource, column)
  t(column_value(resource, column) == true ? "Yes" : "No")
end | 
 
    
      
  
  
    #format_date_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
166
167
168
169 | # File 'app/builders/releaf/builders/table_builder.rb', line 166
def format_date_content(resource, column)
  value = column_value(resource, column)
  I18n.l(value, format: :default) unless value.nil?
end | 
 
    
      
  
  
    #format_datetime_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
171
172
173
174
175 | # File 'app/builders/releaf/builders/table_builder.rb', line 171
def format_datetime_content(resource, column)
  value = column_value(resource, column)
  format = Releaf::Builders::Utilities::DateFields.date_or_time_default_format(:datetime)
  I18n.l(value, format: format) unless value.nil?
end | 
 
    
      
  
  
    #format_richtext_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
152
153
154
155 | # File 'app/builders/releaf/builders/table_builder.rb', line 152
def format_richtext_content(resource, column)
  value = ActionView::Base.full_sanitizer.sanitize(column_value(resource, column).to_s)
  truncate(value, length: 32, separator: ' ')
end | 
 
    
      
  
  
    #format_string_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
157
158
159
160 | # File 'app/builders/releaf/builders/table_builder.rb', line 157
def format_string_content(resource, column)
  value = column_value(resource, column)
  resource_title(value)
end | 
 
    
      
  
  
    #format_text_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
144
145
146 | # File 'app/builders/releaf/builders/table_builder.rb', line 144
def format_text_content(resource, column)
  truncate(column_value(resource, column).to_s, length: 32, separator: ' ')
end | 
 
    
      
  
  
    #format_textarea_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
148
149
150 | # File 'app/builders/releaf/builders/table_builder.rb', line 148
def format_textarea_content(resource, column)
  format_text_content(resource, column)
end | 
 
    
      
  
  
    #format_time_content(resource, column)  ⇒ Object 
  
  
  
  
    | 
178
179
180
181
182 | # File 'app/builders/releaf/builders/table_builder.rb', line 178
def format_time_content(resource, column)
  value = column_value(resource, column)
  format = Releaf::Builders::Utilities::DateFields.date_or_time_default_format(:time)
  I18n.l(value, format: format) unless value.nil?
end | 
 
    
      
  
  
    #head  ⇒ Object 
  
  
  
  
    | 
55
56
57
58
59
60
61
62
63
64
65 | # File 'app/builders/releaf/builders/table_builder.rb', line 55
def head
  tag(:thead) do
    tag(:tr) do
      content = ActiveSupport::SafeBuffer.new
      columns.each_pair do|column, _options|
        content << head_cell(column)
      end
      content
    end
  end
end | 
 
    
      
  
  
    #head_cell(column)  ⇒ Object 
  
  
  
  
    | 
67
68
69
70
71 | # File 'app/builders/releaf/builders/table_builder.rb', line 67
def head_cell(column)
  tag(:th) do
    head_cell_content(column)
  end
end | 
 
    
      
  
  
    #head_cell_content(column)  ⇒ Object 
  
  
  
  
    | 
73
74
75
76
77
78 | # File 'app/builders/releaf/builders/table_builder.rb', line 73
def head_cell_content(column)
  unless column.to_sym == :toolbox
    attribute = column.to_s.tr(".", "_")
    resource_class.human_attribute_name(attribute)
  end
end | 
 
    
      
  
  
    #output  ⇒ Object 
  
  
  
  
    | 
41
42
43
44
45
46
47
48
49 | # File 'app/builders/releaf/builders/table_builder.rb', line 41
def output
  tag(:table, table_attributes) do
    if collection.empty?
      empty_body
    else
      head << body
    end
  end
end | 
 
    
      
  
  
    #row(resource)  ⇒ Object 
  
  
  
  
    | 
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 | # File 'app/builders/releaf/builders/table_builder.rb', line 120
def row(resource)
  url = row_url(resource)
  tag(:tr, row_attributes(resource)) do
    content = ActiveSupport::SafeBuffer.new
    columns.each_pair do|column, options|
      cell_options = options.merge(url: url)
      if options[:cell_method]
        content << send(options[:cell_method], resource, cell_options)
      else
        content << cell(resource, column, cell_options)
      end
    end
    content
  end
end | 
 
    
      
  
  
    #row_attributes(resource)  ⇒ Object 
  
  
  
  
    | 
111
112
113
114
115
116
117
118 | # File 'app/builders/releaf/builders/table_builder.rb', line 111
def row_attributes(resource)
  {
    class: "row",
    data: {
      id: resource.id
    }
  }
end | 
 
    
      
  
  
    #row_url(resource)  ⇒ Object 
  
  
  
  
    | 
98
99
100
101 | # File 'app/builders/releaf/builders/table_builder.rb', line 98
def row_url(resource)
  resource_action = row_url_action(resource)
  url_for(action: resource_action, id: resource.id, index_path: index_path) if resource_action
end | 
 
    
      
  
  
    #row_url_action(_resource)  ⇒ Object 
  
  
  
  
    | 
103
104
105
106
107
108
109 | # File 'app/builders/releaf/builders/table_builder.rb', line 103
def row_url_action(_resource)
  if feature_available?(:show)
    :show
  elsif feature_available?(:edit)
    :edit
  end
end | 
 
    
      
  
  
    #table_attributes  ⇒ Object 
  
  
  
  
    | 
51
52
53 | # File 'app/builders/releaf/builders/table_builder.rb', line 51
def table_attributes
  {class: ["table", resource_class.name.pluralize.underscore.dasherize]}
end | 
 
    
      
  
  
    | 
265
266
267
268
269
270 | # File 'app/builders/releaf/builders/table_builder.rb', line 265
def toolbox_cell(resource, options)
  toolbox_args = {index_path: index_path}.merge(options.fetch(:toolbox, {}))
  tag(:td, class: "only-icon toolbox-cell") do
    toolbox(resource, toolbox_args)
  end
end | 
 
    
      
  
  
    | 
226
227
228
229
230
231
232
233
234 | # File 'app/builders/releaf/builders/table_builder.rb', line 226
def type_format_method(type)
  format_method = "format_#{type}_content".to_sym
  if respond_to?(format_method)
    format_method
  else
    :format_string_content
  end
end |