Class: FormProps::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/form_props/form_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FormBuilder

:rich_text_area



12
13
14
15
16
17
# File 'lib/form_props/form_builder.rb', line 12

def initialize(*args)
  super
  options = args.last || {}
  @default_options[:controlled] = options[:controlled]
  @default_html_options = @default_html_options.except(:controlled)
end

Instance Method Details

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



100
101
102
# File 'lib/form_props/form_builder.rb', line 100

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  Inputs::CheckBox.new(@object_name, method, @template, checked_value, unchecked_value, objectify_options(options)).render
end

#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



112
113
114
# File 'lib/form_props/form_builder.rb', line 112

def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  Inputs::CollectionCheckBoxes.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options), &block).render
end

#collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



116
117
118
# File 'lib/form_props/form_builder.rb', line 116

def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  Inputs::CollectionRadioButtons.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options), &block).render
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



108
109
110
# File 'lib/form_props/form_builder.rb', line 108

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  Inputs::CollectionSelect.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_html_options.merge(html_options)).render
end

#color_field(method, options = {}) ⇒ Object



72
73
74
# File 'lib/form_props/form_builder.rb', line 72

def color_field(method, options = {})
  Inputs::ColorField.new(@object_name, method, @template, objectify_options(options)).render
end

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



40
41
42
# File 'lib/form_props/form_builder.rb', line 40

def date_field(method, options = {})
  Inputs::DateField.new(@object_name, method, @template, objectify_options(options)).render
end

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



56
57
58
# File 'lib/form_props/form_builder.rb', line 56

def datetime_field(method, options = {})
  Inputs::DatetimeLocalField.new(@object_name, method, @template, objectify_options(options)).render
end

#datetime_local_field(method, options = {}) ⇒ Object



60
61
62
# File 'lib/form_props/form_builder.rb', line 60

def datetime_local_field(method, options = {})
  Inputs::DatetimeLocalField.new(@object_name, method, @template, objectify_options(options)).render
end

#default_form_builder_classObject



221
222
223
# File 'lib/form_props/form_builder.rb', line 221

def default_form_builder_class
  self.class
end

#email_field(method, options = {}) ⇒ Object



88
89
90
# File 'lib/form_props/form_builder.rb', line 88

def email_field(method, options = {})
  Inputs::EmailField.new(@object_name, method, @template, objectify_options(options)).render
end

#fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/form_props/form_builder.rb', line 185

def fields_for(record_name, record_object = nil, fields_options = {}, &block)
  fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
  fields_options[:builder] ||= self.class
  fields_options[:namespace] = options[:namespace]
  fields_options[:parent_builder] = self

  case record_name
  when String, Symbol
    if nested_attributes_association?(record_name)
      return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
    end
  else
    record_object = record_name.is_a?(Array) ? record_name.last : record_name
    record_name = model_name_from_record_or_class(record_object).param_key
  end

  object_name = @object_name
  index = if options.has_key?(:index)
    options[:index]
  elsif defined?(@auto_index)
    object_name = object_name.to_s.delete_suffix("[]")
    @auto_index
  end

  record_name = if index
    "#{object_name}[#{index}][#{record_name}]"
  elsif record_name.end_with?("[]")
    "#{object_name}[#{record_name[0..-3]}][#{record_object.id}]"
  else
    "#{object_name}[#{record_name}]"
  end
  fields_options[:child_index] = index

  @template.fields_for(record_name, record_object, fields_options, &block)
end

#fields_for_nested_model(name, object, fields_options, block) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/form_props/form_builder.rb', line 173

def fields_for_nested_model(name, object, fields_options, block)
  object = convert_to_model(object)
  emit_hidden_id = object.persisted? && fields_options.fetch(:include_id) {
    options.fetch(:include_id, true)
  }

  @template.fields_for(name, object, fields_options) do |f|
    block.call(f)
    f.hidden_field(:id) if emit_hidden_id && !f.emitted_hidden_id?
  end
end

#fields_for_with_nested_attributes(association_name, association, options, block) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/form_props/form_builder.rb', line 140

def fields_for_with_nested_attributes(association_name, association, options, block)
  name = "#{object_name}[#{association_name}_attributes]"
  association_key = "#{association_name.to_s.camelize(:lower)}Attributes"
  association = convert_to_model(association)
  json = @template.instance_variable_get(:@__json)

  if association.respond_to?(:persisted?)
    association = [association] if @object.public_send(association_name).respond_to?(:to_ary)
  elsif !association.respond_to?(:to_ary)
    association = @object.public_send(association_name)
  end

  if association.respond_to?(:to_ary)
    explicit_child_index = options[:child_index]

    json.set!(association_key) do
      json.array! association do |child|
        if explicit_child_index
          options[:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call)
        else
          options[:child_index] = nested_child_index(name)
        end

        fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block)
      end
    end
  elsif association
    json.set!(association_key) do
      fields_for_nested_model(name, association, options, block)
    end
  end
end

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



19
20
21
22
23
24
25
26
# File 'lib/form_props/form_builder.rb', line 19

def file_field(method, options = {})
  Inputs::FileField.new(
    @object_name,
    method,
    @template,
    @template.send(:convert_direct_upload_option_to_url, objectify_options(options).dup)
  ).render
end

#grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



120
121
122
# File 'lib/form_props/form_builder.rb', line 120

def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  Inputs::GroupedCollectionSelect.new(@object_name, method, @template, collection, group_method, group_label_method, option_key_method, option_value_method, objectify_options(options), @default_html_options.merge(html_options)).render
end

#hidden_field(method, options = {}) ⇒ Object



32
33
34
# File 'lib/form_props/form_builder.rb', line 32

def hidden_field(method, options = {})
  Inputs::HiddenField.new(@object_name, method, @template, objectify_options(options)).render
end

#month_field(method, options = {}) ⇒ Object



52
53
54
# File 'lib/form_props/form_builder.rb', line 52

def month_field(method, options = {})
  Inputs::MonthField.new(@object_name, method, @template, objectify_options(options)).render
end

#number_field(method, options = {}) ⇒ Object



80
81
82
# File 'lib/form_props/form_builder.rb', line 80

def number_field(method, options = {})
  Inputs::NumberField.new(@object_name, method, @template, objectify_options(options)).render
end

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



76
77
78
# File 'lib/form_props/form_builder.rb', line 76

def password_field(method, options = {})
  Inputs::PasswordField.new(@object_name, method, @template, objectify_options(options)).render
end

#radio_button(method, tag_value, options = {}) ⇒ Object



104
105
106
# File 'lib/form_props/form_builder.rb', line 104

def radio_button(method, tag_value, options = {})
  Inputs::RadioButton.new(@object_name, method, @template, tag_value, objectify_options(options)).render
end

#range_field(method, options = {}) ⇒ Object



84
85
86
# File 'lib/form_props/form_builder.rb', line 84

def range_field(method, options = {})
  Inputs::RangeField.new(@object_name, method, @template, objectify_options(options)).render
end

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



92
93
94
# File 'lib/form_props/form_builder.rb', line 92

def search_field(method, options = {})
  Inputs::SearchField.new(@object_name, method, @template, objectify_options(options)).render
end

#select(method, choices = [], options = {}, html_options = {}) ⇒ Object



28
29
30
# File 'lib/form_props/form_builder.rb', line 28

def select(method, choices = [], options = {}, html_options = {})
  Inputs::Select.new(@object_name, method, @template, choices, objectify_options(options), @default_html_options.merge(html_options)).render
end

#submit(value = nil, options = {}) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/form_props/form_builder.rb', line 132

def submit(value = nil, options = {})
  value, options = nil, value if value.is_a?(Hash)
  value ||= submit_default_value
  options = {name: "commit", text: value}.update(options)

  Inputs::Submit.new(@template, options).render
end

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



68
69
70
# File 'lib/form_props/form_builder.rb', line 68

def tel_field(method, options = {})
  Inputs::TelField.new(@object_name, method, @template, objectify_options(options)).render
end

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



96
97
98
# File 'lib/form_props/form_builder.rb', line 96

def text_area(method, options = {})
  Inputs::TextArea.new(@object_name, method, @template, objectify_options(options)).render
end

#text_field(method, options = {}) ⇒ Object



36
37
38
# File 'lib/form_props/form_builder.rb', line 36

def text_field(method, options = {})
  Inputs::TextField.new(@object_name, method, @template, objectify_options(options)).render
end

#time_field(method, options = {}) ⇒ Object



44
45
46
# File 'lib/form_props/form_builder.rb', line 44

def time_field(method, options = {})
  Inputs::TimeField.new(@object_name, method, @template, objectify_options(options)).render
end

#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



124
125
126
# File 'lib/form_props/form_builder.rb', line 124

def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  Inputs::TimeZoneSelect.new(@object_name, method, @template, priority_zones, objectify_options(options), @default_html_options.merge(html_options)).render
end

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



64
65
66
# File 'lib/form_props/form_builder.rb', line 64

def url_field(method, options = {})
  Inputs::UrlField.new(@object_name, method, @template, objectify_options(options)).render
end

#week_field(method, options = {}) ⇒ Object



48
49
50
# File 'lib/form_props/form_builder.rb', line 48

def week_field(method, options = {})
  Inputs::WeekField.new(@object_name, method, @template, objectify_options(options)).render
end

#weekday_select(method, options = {}, html_options = {}) ⇒ Object



128
129
130
# File 'lib/form_props/form_builder.rb', line 128

def weekday_select(method, options = {}, html_options = {})
  Inputs::WeekdaySelect.new(@object_name, method, @template, objectify_options(options), @default_html_options.merge(html_options)).render
end