Class: Effective::TableBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/table_builder.rb

Constant Summary collapse

FILTER_PARAMETERS =
[:password, :password_confirmation, :status_steps, :wizard_steps, :token, :created_at, :updated_at]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, template, options = {}, &block) ⇒ TableBuilder

Returns a new instance of TableBuilder.



15
16
17
18
19
20
21
22
23
24
# File 'app/models/effective/table_builder.rb', line 15

def initialize(object, template, options = {}, &block)
  raise('resource must be an ActiveRecord::Base') unless object.is_a?(ActiveRecord::Base)

  @object = object
  @template = template
  @options = options

  @rows = {}
  @content_fors = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object

Call default_row for any form field



120
121
122
# File 'app/models/effective/table_builder.rb', line 120

def method_missing(method, *args, **kwargs, &block)
  default_row(args[0], **kwargs, &block)
end

Instance Attribute Details

#content_forsObject

Returns the value of attribute content_fors.



11
12
13
# File 'app/models/effective/table_builder.rb', line 11

def content_fors
  @content_fors
end

#objectObject

Returns the value of attribute object.



7
8
9
# File 'app/models/effective/table_builder.rb', line 7

def object
  @object
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'app/models/effective/table_builder.rb', line 7

def options
  @options
end

#rowsObject

A Hash of :first_name => “<tr><td>First Name</td><td>Bob</td></tr>”



10
11
12
# File 'app/models/effective/table_builder.rb', line 10

def rows
  @rows
end

#templateObject

Returns the value of attribute template.



7
8
9
# File 'app/models/effective/table_builder.rb', line 7

def template
  @template
end

Instance Method Details

#article_editor(name, options = {}) ⇒ Object



129
130
131
# File 'app/models/effective/table_builder.rb', line 129

def article_editor(name, options = {})
  rows[name] = TableRows::ArticleEditor.new(name, options, builder: self).to_html
end

#belongs_to(name, options = {}) ⇒ Object



149
150
151
# File 'app/models/effective/table_builder.rb', line 149

def belongs_to(name, options = {})
  rows[name] = TableRows::BelongsTo.new(name, options, builder: self).to_html
end

#boolean_row(name, options = {}) ⇒ Object Also known as: check_box



133
134
135
# File 'app/models/effective/table_builder.rb', line 133

def boolean_row(name, options = {})
  rows[name] = TableRows::Boolean.new(name, options, builder: self).to_html
end

#build_resource_rows(only: nil, except: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/effective/table_builder.rb', line 72

def build_resource_rows(only: nil, except: nil)
  Effective::Resource.new(object).resource_attributes.each do |name, options|
    next if only.present? && only.exclude?(name)
    next if except.present? && except.include?(name)

    case options.first
    when :active_storage
      file_field(name)
    when :belongs_to
      belongs_to(name)
    when :effective_address
      effective_address(name)
    when :boolean
      boolean_row(name)
    when :integer
      if ['price', 'discount', '_fee'].any? { |p| name.to_s.include?(p) }
        price_field(name)
      elsif ['_percent'].any? { |p| name.to_s.include?(p) }
        percent_field(name)
      else
        text_field(name)
      end
    when :string
      name.to_s.include?('email') ? email_field(name) : text_field(name)
    when :text
      text_area(name)
    else default_row(name)
    end
  end
end

#capture(&block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/models/effective/table_builder.rb', line 63

def capture(&block)
  begin
    template.instance_variable_set(:@_effective_table_builder, self)
    template.capture(self, &block)
  ensure
    template.instance_variable_set(:@_effective_table_builder, nil)
  end
end

#collection_row(name, collection, options = {}, &block) ⇒ Object Also known as: select, checks, radios



138
139
140
141
142
143
144
# File 'app/models/effective/table_builder.rb', line 138

def collection_row(name, collection, options = {}, &block)
  rows[name] = if [true, false].include?(value(name))
    TableRows::Boolean.new(name, options, builder: self).to_html
  else
    TableRows::Collection.new(name, collection, options, builder: self).to_html
  end
end

#content_for(name, options = {}, &block) ⇒ Object



226
227
228
# File 'app/models/effective/table_builder.rb', line 226

def content_for(name, options = {}, &block)
  content_fors[name] = TableRows::ContentFor.new(name, options, builder: self).to_html(&block)
end

#date_field(name, options = {}) ⇒ Object



153
154
155
# File 'app/models/effective/table_builder.rb', line 153

def date_field(name, options = {})
  rows[name] = TableRows::DateField.new(name, options, builder: self).to_html
end

#datetime_field(name, options = {}) ⇒ Object



157
158
159
# File 'app/models/effective/table_builder.rb', line 157

def datetime_field(name, options = {})
  rows[name] = TableRows::DatetimeField.new(name, options, builder: self).to_html
end

#default_row(name, options = {}, &block) ⇒ Object

Assign the <tr><td>…</td></tr> to the @rows Hash



125
126
127
# File 'app/models/effective/table_builder.rb', line 125

def default_row(name, options = {}, &block)
  rows[name] = TableRow.new(name, options, builder: self).to_html
end

#effective_address(name, options = {}) ⇒ Object



190
191
192
# File 'app/models/effective/table_builder.rb', line 190

def effective_address(name, options = {})
  rows[name] = TableRows::EffectiveAddress.new(name, options, builder: self).to_html
end

#email_field(name, options = {}) ⇒ Object Also known as: email_cc_field



161
162
163
# File 'app/models/effective/table_builder.rb', line 161

def email_field(name, options = {})
  rows[name] = TableRows::EmailField.new(name, options, builder: self).to_html
end

#error(name, options = {}) ⇒ Object



166
167
168
# File 'app/models/effective/table_builder.rb', line 166

def error(name, options = {})
  # Nothing to do
end

#fields_for(name, collection = nil, options = {}, &block) ⇒ Object Also known as: effective_fields_for



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'app/models/effective/table_builder.rb', line 265

def fields_for(name, collection = nil, options = {}, &block)
  options = collection if options.blank? && collection.kind_of?(Hash)

  values = value(name)

  if values.respond_to?(:each_with_index)
    values.each_with_index do |object, index|
      builder = TableBuilder.new(object, template, options.reverse_merge(prefix: human_attribute_name(name).singularize + " ##{index+1}"))
      builder.render(&block)
      builder.rows.each { |child, content| rows["#{name}_#{child}_#{index}".to_sym] = content }
    end
  else
    builder = TableBuilder.new(object.send(name), template, options)
    builder.render(&block)
    builder.rows.each { |child, content| rows["#{name}_#{child}".to_sym] = content }
  end
end

#file_field(name, options = {}) ⇒ Object



170
171
172
# File 'app/models/effective/table_builder.rb', line 170

def file_field(name, options = {})
  rows[name] = TableRows::FileField.new(name, options, builder: self).to_html
end

#filter_parametersObject



103
104
105
# File 'app/models/effective/table_builder.rb', line 103

def filter_parameters
  FILTER_PARAMETERS + Array(object.class.try(:filter_parameters)) + Array(Rails.application.config.filter_parameters)
end

#form_group(name = nil, options = {}, &block) ⇒ Object



174
175
176
# File 'app/models/effective/table_builder.rb', line 174

def form_group(name = nil, options = {}, &block)
  # Nothing to do
end

#has_many(name, collection = nil, options = {}, &block) ⇒ Object

Has Many



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/models/effective/table_builder.rb', line 244

def has_many(name, collection = nil, options = {}, &block)
  options = collection if options.blank? && collection.kind_of?(Hash)

  if options[:cards]
    value(name).each_with_index do |object, index|
      builder = TableBuilder.new(object, template, options)
      rows["#{name}_#{index}".to_sym] = TableRows::HasManyCard.new(name, options.merge(index: index), builder: builder).to_html(&block)
    end
  else
    value(name).each_with_index do |object, index|
      builder = TableBuilder.new(object, template, options.reverse_merge(prefix: template.et(object) + " ##{index+1}"))
      builder.render(&block)
      builder.rows.each { |child, content| rows["#{name}_#{child}_#{index}".to_sym] = content }
    end
  end

  rows[name] = "None" if value(name).blank?

  nil
end

#hidden_field(name = nil, options = {}) ⇒ Object



178
179
180
# File 'app/models/effective/table_builder.rb', line 178

def hidden_field(name = nil, options = {})
  # Nothing to do
end

#hide_if(name, selected, &block) ⇒ Object

Logics



231
232
233
# File 'app/models/effective/table_builder.rb', line 231

def hide_if(name, selected, &block)
  template.capture(self, &block) unless value(name) == selected
end

#human_attribute_name(name) ⇒ Object



107
108
109
110
111
112
113
# File 'app/models/effective/table_builder.rb', line 107

def human_attribute_name(name)
  if object.respond_to?(:human_attribute_name)
    object.human_attribute_name(name)
  else
    (name.to_s.split('.').last.to_s.titleize || '')
  end
end

#password_field(name, options = {}) ⇒ Object



186
187
188
# File 'app/models/effective/table_builder.rb', line 186

def password_field(name, options = {})
  # Nothing to do
end

#percent_field(name, options = {}) ⇒ Object



194
195
196
# File 'app/models/effective/table_builder.rb', line 194

def percent_field(name, options = {})
  rows[name] = TableRows::PercentField.new(name, options, builder: self).to_html
end

#price_field(name, options = {}) ⇒ Object



202
203
204
# File 'app/models/effective/table_builder.rb', line 202

def price_field(name, options = {})
  rows[name] = TableRows::PriceField.new(name, options, builder: self).to_html
end

#render(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/effective/table_builder.rb', line 26

def render(&block)
  # This runs the form partial over this table builder
  capture(&block) if block_given?

  # Consider content
  only = Array(options[:only])
  except = Array(options[:except])
  additionally = Array(options[:additionally])
  filtered = filter_parameters

  # Rows are created when we use the block syntax and call a f.text_field or other f.form_field
  # Using = f.content_for does not create a row.
  # So rows wille be blank when using the default syntax
  # And rows will be present when we render a form, or any form fields
  build_resource_rows(only: only, except: except) unless block_given?

  # This gives us a second run through the rows, if you pass additionally
  build_resource_rows(only: additionally) if additionally.present?

  # Use f.content_for :name to override the content
  content = rows.merge(content_fors)

  # Filter out some hardcoded ones
  content = content.except(*filtered) if filtered.present?

  if except.present?
    content = content.except(*except)
    content = content.reject { |key, _| except.any? { |ex| key.to_s.start_with?(ex.to_s + '_') } }
  end

  if content.present?
    (:table, class: options.fetch(:class, 'table table-sm table-striped table-hover effective-table-summary')) do
      (:tbody, content.values.join.html_safe)
    end
  end
end

#save(name = nil, options = {}) ⇒ Object



206
207
208
# File 'app/models/effective/table_builder.rb', line 206

def save(name = nil, options = {})
  # Nothing to do
end

#search_field(name, options = {}) ⇒ Object



182
183
184
# File 'app/models/effective/table_builder.rb', line 182

def search_field(name, options = {})
  # Nothing to do
end

#show_if(name, selected, &block) ⇒ Object



235
236
237
# File 'app/models/effective/table_builder.rb', line 235

def show_if(name, selected, &block)
  template.capture(self, &block) if value(name) == selected
end

#show_if_any(name, options, &block) ⇒ Object



239
240
241
# File 'app/models/effective/table_builder.rb', line 239

def show_if_any(name, options, &block)
  template.capture(self, &block) if (Array(options) & Array(value(name))).present?
end

#static_field(name, options = {}, &block) ⇒ Object



214
215
216
# File 'app/models/effective/table_builder.rb', line 214

def static_field(name, options = {}, &block)
  rows[name] = (value(name).is_a?(ActiveRecord::Base)) ? belongs_to(name, options) : default_row(name, options)
end

#submit(name = nil, options = {}, &block) ⇒ Object



210
211
212
# File 'app/models/effective/table_builder.rb', line 210

def submit(name = nil, options = {}, &block)
  # Nothing to do
end

#tel_field(name, options = {}) ⇒ Object



198
199
200
# File 'app/models/effective/table_builder.rb', line 198

def tel_field(name, options = {})
  rows[name] = TableRows::PhoneField.new(name, options, builder: self).to_html
end

#text_area(name, options = {}) ⇒ Object



218
219
220
# File 'app/models/effective/table_builder.rb', line 218

def text_area(name, options = {})
  rows[name] = TableRows::TextArea.new(name, options, builder: self).to_html
end

#url_field(name, options = {}) ⇒ Object



222
223
224
# File 'app/models/effective/table_builder.rb', line 222

def url_field(name, options = {})
  rows[name] = TableRows::UrlField.new(name, options, builder: self).to_html
end

#value(name) ⇒ Object



115
116
117
# File 'app/models/effective/table_builder.rb', line 115

def value(name)
  object.send(name)
end