Class: Effective::TableBuilder

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

Constant Summary collapse

FILTER_PARAMETERS =
[:password, :password_confirmation, :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



87
88
89
# File 'app/models/effective/table_builder.rb', line 87

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

Instance Attribute Details

#content_fors ⇒ Object

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

#object ⇒ Object

Returns the value of attribute object.



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

def object
  @object
end

#options ⇒ Object

Returns the value of attribute options.



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

def options
  @options
end

#rows ⇒ Object

A Hash of :first_name => "First NameBob"



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

def rows
  @rows
end

#template ⇒ Object

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

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



96
97
98
# File 'app/models/effective/table_builder.rb', line 96

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

#build_resource_rows ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/effective/table_builder.rb', line 56

def build_resource_rows
  Effective::Resource.new(object).klass_attributes(sort: true).each do |name, options|
    case options.first
    when :boolean
      boolean_row(name)
    when :text
      text_area(name)
    when :string
      (name.to_s.include?('email') && value(name).to_s.include?('@')) ? email_field(name) : text_field(name)
    else default_row(name)
    end
  end
end

#capture(&block) ⇒ Object



47
48
49
50
51
52
53
54
# File 'app/models/effective/table_builder.rb', line 47

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



101
102
103
104
105
106
107
# File 'app/models/effective/table_builder.rb', line 101

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



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

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

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

Assign the ... to the @rows Hash



92
93
94
# File 'app/models/effective/table_builder.rb', line 92

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

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



137
138
139
# File 'app/models/effective/table_builder.rb', line 137

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



112
113
114
# File 'app/models/effective/table_builder.rb', line 112

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

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



187
188
189
190
191
# File 'app/models/effective/table_builder.rb', line 187

def fields_for(name, object, options = {}, &block)
  builder = TableBuilder.new(object, template, options.merge(prefix: human_attribute_name(name)))
  builder.render(&block)
  builder.rows.each { |child, content| rows["#{name}_#{child}".to_sym] = content }
end

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



117
118
119
# File 'app/models/effective/table_builder.rb', line 117

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

#filter_parameters ⇒ Object



70
71
72
# File 'app/models/effective/table_builder.rb', line 70

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



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

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

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

Has Many



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

def has_many(name, collection = nil, options = {}, &block)
  raise('unsupported')
end

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



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

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

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

Logics



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

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

#human_attribute_name(name) ⇒ Object



74
75
76
77
78
79
80
# File 'app/models/effective/table_builder.rb', line 74

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



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

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

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



141
142
143
# File 'app/models/effective/table_builder.rb', line 141

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

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



145
146
147
# File 'app/models/effective/table_builder.rb', line 145

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
# 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?

  # Build from the resource if we didn't do anything in the block
  build_resource_rows if rows.blank?

  only = Array(options[:only])
  except = Array(options[:except])
  filtered = filter_parameters

  content = rows.merge(content_fors)
  content = content.slice(*only) if only.present?
  content = content.except(*except) if except.present?
  content = content.except(*filtered) if filtered.present?

  (:table, class: options.fetch(:class, 'table table-striped table-hover')) do
    (:tbody, content.values.join.html_safe)
  end
end

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



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

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

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



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

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

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



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

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

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



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

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

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



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

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

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



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

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

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



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

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

#value(name) ⇒ Object



82
83
84
# File 'app/models/effective/table_builder.rb', line 82

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