Class: FormProps::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- FormProps::FormBuilder
- Defined in:
- lib/form_props/form_builder.rb
Instance Method Summary collapse
- #check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
- #collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object
- #collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #color_field(method, options = {}) ⇒ Object
- #date_field(method, options = {}) ⇒ Object
- #datetime_field(method, options = {}) ⇒ Object
- #datetime_local_field(method, options = {}) ⇒ Object
- #default_form_builder_class ⇒ Object
- #email_field(method, options = {}) ⇒ Object
- #fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object
- #fields_for_nested_model(name, object, fields_options, block) ⇒ Object
- #fields_for_with_nested_attributes(association_name, association, options, block) ⇒ Object
- #file_field(method, options = {}) ⇒ Object
- #grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object
- #hidden_field(method, options = {}) ⇒ Object
-
#initialize(*args) ⇒ FormBuilder
constructor
:rich_text_area.
- #month_field(method, options = {}) ⇒ Object
- #number_field(method, options = {}) ⇒ Object
- #password_field(method, options = {}) ⇒ Object
- #radio_button(method, tag_value, options = {}) ⇒ Object
- #range_field(method, options = {}) ⇒ Object
- #search_field(method, options = {}) ⇒ Object
- #select(method, choices = [], options = {}, html_options = {}) ⇒ Object
- #submit(value = nil, options = {}) ⇒ Object
- #tel_field(method, options = {}) ⇒ Object
- #text_area(method, options = {}) ⇒ Object
- #text_field(method, options = {}) ⇒ Object
- #time_field(method, options = {}) ⇒ Object
- #time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
- #url_field(method, options = {}) ⇒ Object
- #week_field(method, options = {}) ⇒ Object
- #weekday_select(method, options = {}, html_options = {}) ⇒ Object
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 = args.last || {} @default_options[:controlled] = [: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, = {}, checked_value = "1", unchecked_value = "0") Inputs::CheckBox.new(@object_name, method, @template, checked_value, unchecked_value, ()).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, = {}, = {}, &block) Inputs::CollectionCheckBoxes.new(@object_name, method, @template, collection, value_method, text_method, (), @default_html_options.merge(), &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 (method, collection, value_method, text_method, = {}, = {}, &block) Inputs::CollectionRadioButtons.new(@object_name, method, @template, collection, value_method, text_method, (), @default_html_options.merge(), &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, = {}, = {}) Inputs::CollectionSelect.new(@object_name, method, @template, collection, value_method, text_method, (), @default_html_options.merge()).render end |
#color_field(method, options = {}) ⇒ Object
72 73 74 |
# File 'lib/form_props/form_builder.rb', line 72 def color_field(method, = {}) Inputs::ColorField.new(@object_name, method, @template, ()).render end |
#date_field(method, options = {}) ⇒ Object
40 41 42 |
# File 'lib/form_props/form_builder.rb', line 40 def date_field(method, = {}) Inputs::DateField.new(@object_name, method, @template, ()).render end |
#datetime_field(method, options = {}) ⇒ Object
56 57 58 |
# File 'lib/form_props/form_builder.rb', line 56 def datetime_field(method, = {}) Inputs::DatetimeLocalField.new(@object_name, method, @template, ()).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, = {}) Inputs::DatetimeLocalField.new(@object_name, method, @template, ()).render end |
#default_form_builder_class ⇒ Object
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, = {}) Inputs::EmailField.new(@object_name, method, @template, ()).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, = {}, &block) , record_object = record_object, nil if record_object.is_a?(Hash) && record_object. [:builder] ||= self.class [:namespace] = [:namespace] [: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, , 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 .has_key?(:index) [: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 [:child_index] = index @template.fields_for(record_name, record_object, , &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, , block) object = convert_to_model(object) emit_hidden_id = object.persisted? && .fetch(:include_id) { .fetch(:include_id, true) } @template.fields_for(name, object, ) 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, , 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 = [:child_index] json.set!(association_key) do json.array! association do |child| if explicit_child_index [:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call) else [:child_index] = nested_child_index(name) end fields_for_nested_model("#{name}[#{[:child_index]}]", child, , block) end end elsif association json.set!(association_key) do fields_for_nested_model(name, association, , 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, = {}) Inputs::FileField.new( @object_name, method, @template, @template.send(:convert_direct_upload_option_to_url, ().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, = {}, = {}) Inputs::GroupedCollectionSelect.new(@object_name, method, @template, collection, group_method, group_label_method, option_key_method, option_value_method, (), @default_html_options.merge()).render end |
#hidden_field(method, options = {}) ⇒ Object
32 33 34 |
# File 'lib/form_props/form_builder.rb', line 32 def hidden_field(method, = {}) Inputs::HiddenField.new(@object_name, method, @template, ()).render end |
#month_field(method, options = {}) ⇒ Object
52 53 54 |
# File 'lib/form_props/form_builder.rb', line 52 def month_field(method, = {}) Inputs::MonthField.new(@object_name, method, @template, ()).render end |
#number_field(method, options = {}) ⇒ Object
80 81 82 |
# File 'lib/form_props/form_builder.rb', line 80 def number_field(method, = {}) Inputs::NumberField.new(@object_name, method, @template, ()).render end |
#password_field(method, options = {}) ⇒ Object
76 77 78 |
# File 'lib/form_props/form_builder.rb', line 76 def password_field(method, = {}) Inputs::PasswordField.new(@object_name, method, @template, ()).render end |
#radio_button(method, tag_value, options = {}) ⇒ Object
104 105 106 |
# File 'lib/form_props/form_builder.rb', line 104 def (method, tag_value, = {}) Inputs::RadioButton.new(@object_name, method, @template, tag_value, ()).render end |
#range_field(method, options = {}) ⇒ Object
84 85 86 |
# File 'lib/form_props/form_builder.rb', line 84 def range_field(method, = {}) Inputs::RangeField.new(@object_name, method, @template, ()).render end |
#search_field(method, options = {}) ⇒ Object
92 93 94 |
# File 'lib/form_props/form_builder.rb', line 92 def search_field(method, = {}) Inputs::SearchField.new(@object_name, method, @template, ()).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 = [], = {}, = {}) Inputs::Select.new(@object_name, method, @template, choices, (), @default_html_options.merge()).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, = {}) value, = nil, value if value.is_a?(Hash) value ||= submit_default_value = {name: "commit", text: value}.update() Inputs::Submit.new(@template, ).render end |
#tel_field(method, options = {}) ⇒ Object
68 69 70 |
# File 'lib/form_props/form_builder.rb', line 68 def tel_field(method, = {}) Inputs::TelField.new(@object_name, method, @template, ()).render end |
#text_area(method, options = {}) ⇒ Object
96 97 98 |
# File 'lib/form_props/form_builder.rb', line 96 def text_area(method, = {}) Inputs::TextArea.new(@object_name, method, @template, ()).render end |
#text_field(method, options = {}) ⇒ Object
36 37 38 |
# File 'lib/form_props/form_builder.rb', line 36 def text_field(method, = {}) Inputs::TextField.new(@object_name, method, @template, ()).render end |
#time_field(method, options = {}) ⇒ Object
44 45 46 |
# File 'lib/form_props/form_builder.rb', line 44 def time_field(method, = {}) Inputs::TimeField.new(@object_name, method, @template, ()).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, = {}, = {}) Inputs::TimeZoneSelect.new(@object_name, method, @template, priority_zones, (), @default_html_options.merge()).render end |
#url_field(method, options = {}) ⇒ Object
64 65 66 |
# File 'lib/form_props/form_builder.rb', line 64 def url_field(method, = {}) Inputs::UrlField.new(@object_name, method, @template, ()).render end |
#week_field(method, options = {}) ⇒ Object
48 49 50 |
# File 'lib/form_props/form_builder.rb', line 48 def week_field(method, = {}) Inputs::WeekField.new(@object_name, method, @template, ()).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, = {}, = {}) Inputs::WeekdaySelect.new(@object_name, method, @template, (), @default_html_options.merge()).render end |