Module: ActiveScaffold::Helpers::FormUiHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/helpers/form_ui_helpers.rb,
lib/active_scaffold/bridges/dragonfly/form_ui.rb,
lib/active_scaffold/bridges/paperclip/form_ui.rb,
lib/active_scaffold/bridges/carrierwave/form_ui.rb,
lib/active_scaffold/bridges/file_column/form_ui.rb,
lib/active_scaffold/bridges/active_storage/form_ui.rb
Overview
Helpers that assist with the rendering of a Form Column
Instance Method Summary collapse
- #active_scaffold_add_new(column, record, html_options, ui_options: column.options, skip_link: false) ⇒ Object
- #active_scaffold_add_new_text(options, key, default) ⇒ Object
- #active_scaffold_check_all_buttons(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_checkbox_list(column, select_options, associated_ids, options, ui_options: column.options) ⇒ Object
- #active_scaffold_checkbox_option(option, label_method, associated_ids, checkbox_options, li_options = {}) ⇒ Object
- #active_scaffold_enum_options(column, record = nil, ui_options: column.options) ⇒ Object
- #active_scaffold_file_with_content(column, options, content, remove_file_prefix, controls_class, ui_options: column.options) ⇒ Object
- #active_scaffold_file_with_remove_link(content, options, controls_class, link_key = nil) ⇒ Object
- #active_scaffold_grouped_options(column, select_options, optgroup) ⇒ Object
- #active_scaffold_input_active_storage_has_many(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_active_storage_has_one(column, options, ui_options: column.options) ⇒ Object
-
#active_scaffold_input_boolean(column, html_options, ui_options: column.options) ⇒ Object
Column.type-based inputs.
- #active_scaffold_input_carrierwave(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_checkbox(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_checkboxes(column, options, ui_options: column.options) ⇒ Object
-
#active_scaffold_input_color(column, options, ui_options: column.options) ⇒ Object
A color picker.
- #active_scaffold_input_date(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_datetime(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_draggable(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_dragonfly(column, options, ui_options: column.options) ⇒ Object
-
#active_scaffold_input_email(column, options, ui_options: column.options) ⇒ Object
A text box, that accepts only valid email address (in-browser validation).
- #active_scaffold_input_enum(column, html_options, options = {}, ui_options: column.options) ⇒ Object
- #active_scaffold_input_file_column(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_month(column, options, ui_options: column.options) ⇒ Object
-
#active_scaffold_input_number(column, options, ui_options: column.options) ⇒ Object
A spinbox control for number values (in-browser validation).
- #active_scaffold_input_paperclip(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_password(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_plural_association(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_radio(column, html_options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_radio_content(column, record, options, html_options, ui_options) ⇒ Object
-
#active_scaffold_input_range(column, options, ui_options: column.options) ⇒ Object
A slider control for number values (in-browser validation).
- #active_scaffold_input_select(column, html_options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_select_multiple(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_singular_association(column, html_options, options = {}, ui_options: column.options) ⇒ Object
-
#active_scaffold_input_telephone(column, options, ui_options: column.options) ⇒ Object
A text box, that accepts only valid phone-number (in-browser validation).
- #active_scaffold_input_textarea(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_input_time(column, options, ui_options: column.options) ⇒ Object
-
#active_scaffold_input_url(column, options, ui_options: column.options) ⇒ Object
A text box, that accepts only valid URI (in-browser validation).
- #active_scaffold_input_virtual(column, options) ⇒ Object
- #active_scaffold_input_week(column, options, ui_options: column.options) ⇒ Object
- #active_scaffold_new_record_klass(column, record, **options) ⇒ Object
- #active_scaffold_new_record_popup(column, record, html_options, options: {}) ⇒ Object
- #active_scaffold_new_record_subform(column, record, html_options, options: {}, new_record_attributes: nil, locals: {}, skip_link: false) ⇒ Object
- #active_scaffold_new_record_url_options(column, record) ⇒ Object
-
#active_scaffold_number_input(method, column, options, remove_options = nil, ui_options: column.options) ⇒ Object
A slider control for number values (in-browser validation).
- #active_scaffold_plural_association_options(column, record = nil) ⇒ Object
- #active_scaffold_radio_option(option, selected, column, radio_options, ui_options: column.options) ⇒ Object
- #active_scaffold_refresh_link(column, html_options, record, ui_options = {}) ⇒ Object
- #active_scaffold_select_name_with_multiple(options) ⇒ Object
- #active_scaffold_show_new_subform_link(column, record, select_id, subform_id, options: {}) ⇒ Object
- #active_scaffold_text_input(method, column, options, remove_options = nil, ui_options: column.options) ⇒ Object
- #active_scaffold_translate_select_options(options) ⇒ Object
- #active_scaffold_translated_enum_options(column, record, ui_options: column.options) ⇒ Object
- #active_scaffold_translated_option(column, text, value = nil) ⇒ Object
Instance Method Details
#active_scaffold_add_new(column, record, html_options, ui_options: column.options, skip_link: false) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 64 def active_scaffold_add_new(column, record, , ui_options: column., skip_link: false) = [:add_new] == true ? {} : [:add_new] [:mode] = :popup if column.association&.collection? case [:mode] when nil, :subform active_scaffold_new_record_subform(column, record, , options: , skip_link: skip_link) when :popup active_scaffold_new_record_popup(column, record, , options: ) unless skip_link else raise ArgumentError, "unsupported mode for add_new: #{[:mode].inspect}" end end |
#active_scaffold_add_new_text(options, key, default) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 132 def active_scaffold_add_new_text(, key, default) text = [key] unless == true return text if text.is_a? String as_(text || default) end |
#active_scaffold_check_all_buttons(column, options, ui_options: column.options) ⇒ Object
249 250 251 252 253 254 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 249 def (column, , ui_options: column.) content_tag(:div, class: 'check-buttons') do link_to(as_(:check_all), '#', class: 'check-all') << link_to(as_(:uncheck_all), '#', class: 'uncheck-all') end end |
#active_scaffold_checkbox_list(column, select_options, associated_ids, options, ui_options: column.options) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 256 def active_scaffold_checkbox_list(column, , associated_ids, , ui_options: column.) label_method = [:label_method] || :to_label html = (column, , ui_options: ) html << hidden_field_tag("#{[:name]}[]", '', id: nil) draggable = .delete(:draggable_lists) || [:draggable_lists] html << content_tag(:ul, .merge(class: "#{[:class]} checkbox-list#{' draggable-lists' if draggable}")) do content = [] .each_with_index do |option, i| content << active_scaffold_checkbox_option(option, label_method, associated_ids, name: "#{[:name]}[]", id: "#{[:id]}_#{i}_id") end safe_join content end html end |
#active_scaffold_checkbox_option(option, label_method, associated_ids, checkbox_options, li_options = {}) ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 240 def active_scaffold_checkbox_option(option, label_method, associated_ids, , = {}) content_tag(:li, ) do option_id = option.is_a?(Array) ? option[1] : option.id label = option.is_a?(Array) ? option[0] : option.send(label_method) check_box_tag([:name], option_id, associated_ids.include?(option_id), ) << content_tag(:label, label, for: [:id]) end end |
#active_scaffold_enum_options(column, record = nil, ui_options: column.options) ⇒ Object
281 282 283 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 281 def (column, record = nil, ui_options: column.) [:options] end |
#active_scaffold_file_with_content(column, options, content, remove_file_prefix, controls_class, ui_options: column.options) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 147 def active_scaffold_file_with_content(column, , content, remove_file_prefix, controls_class, ui_options: column.) = (.merge()) [:style] = 'display: none' if content field = file_field(:record, column.name, ) if content content = [content, ' | '] content << yield if block_given? object_name, method = [:name].split(/\[(#{column.name})\]/) method.sub!(/#{column.name}/, "#{remove_file_prefix}\\0") content << hidden_field(object_name, method, value: 'false', class: 'remove_file') active_scaffold_file_with_remove_link(safe_join(content), , controls_class) { field } else field end end |
#active_scaffold_file_with_remove_link(content, options, controls_class, link_key = nil) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 164 def active_scaffold_file_with_remove_link(content, , controls_class, link_key = nil) required = .delete(:required) link_key ||= [:multiple] ? :remove_files : :remove_file content_tag(:div, class: "#{controls_class} file-input-controls", data: {required: required}) do content_line = content_tag(:div) do safe_join [content, content_tag(:a, as_(link_key), href: '#', class: 'remove-file-btn')] end content_line << yield if block_given? content_line end end |
#active_scaffold_grouped_options(column, select_options, optgroup) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 7 def (column, , optgroup) group_column = active_scaffold_config_for(column.association.klass).columns[optgroup] group_label = group_column.[:label_method] if group_column group_label ||= group_column&.association ? :to_label : :to_s .group_by(&optgroup.to_sym).collect do |group, | [group.send(group_label), .collect { |r| [r.send(column.[:label_method] || :to_label), r.id] }] end end |
#active_scaffold_input_active_storage_has_many(column, options, ui_options: column.options) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_scaffold/bridges/active_storage/form_ui.rb', line 14 def active_scaffold_input_active_storage_has_many(column, , ui_options: column.) record = [:object] [:multiple] = 'multiple' [:include_hidden] = false [:name] += '[]' active_storage = record.send(column.name.to_s) existing = active_storage.map do |file| content = hidden_field_tag([:name], file.signed_id) << (file, column, ui_options: ) << h(' | ') active_scaffold_file_with_remove_link(content, , 'active_storage_controls', :remove) end safe_join existing << hidden_field_tag([:name]) << file_field(:record, column.name, ) end |
#active_scaffold_input_active_storage_has_one(column, options, ui_options: column.options) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/active_scaffold/bridges/active_storage/form_ui.rb', line 7 def active_scaffold_input_active_storage_has_one(column, , ui_options: column.) record = [:object] active_storage = record.send(column.name.to_s) content = active_scaffold_column_active_storage_has_one(record, column, ui_options: ) if active_storage.attached? active_scaffold_file_with_content(column, , content, 'delete_', 'active_storage_controls', ui_options: ) end |
#active_scaffold_input_boolean(column, html_options, ui_options: column.options) ⇒ Object
Column.type-based inputs
496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 496 def active_scaffold_input_boolean(column, , ui_options: column.) record = .delete(:object) .merge!([:html_options] || {}) = {selected: record.send(column.name), object: record} [:include_blank] = :_select_ if column.null? .merge!() () = [[as_(:true), true], [as_(:false), false]] # rubocop:disable Lint/BooleanSymbol select(:record, column.name, , , ) end |
#active_scaffold_input_carrierwave(column, options, ui_options: column.options) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_scaffold/bridges/carrierwave/form_ui.rb', line 6 def active_scaffold_input_carrierwave(column, , ui_options: column.) record = [:object] carrierwave = record.send(column.name.to_s) content = get_column_value(record, column) if carrierwave.file.present? active_scaffold_file_with_content(column, , content, 'remove_', 'carrierwave_controls', ui_options: ) do = { name: [:name].gsub(/\[#{column.name}\]$/, "[#{column.name}_cache]"), id: "#{[:id]}_cache" } hidden_field(:record, "#{column.name}_cache", ) end end |
#active_scaffold_input_checkbox(column, options, ui_options: column.options) ⇒ Object
420 421 422 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 420 def active_scaffold_input_checkbox(column, , ui_options: column.) check_box(:record, column.name, .merge()) end |
#active_scaffold_input_checkboxes(column, options, ui_options: column.options) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 223 def active_scaffold_input_checkboxes(column, , ui_options: column.) if column.association&.singular? raise ArgumentError, "association #{column.association.name} is singular, but checkboxes form_ui expects a collection" end if column.association # collection active_scaffold_input_plural_association(column, , ui_options: ) else record = .delete(:object) = record.send(column.name) || [] raise ArgumentError, 'checkboxes form_ui expect getter to return an Array' unless .is_a?(Array) = (column, [:object], ui_options: ) active_scaffold_checkbox_list(column, , , , ui_options: ) end end |
#active_scaffold_input_color(column, options, ui_options: column.options) ⇒ Object
A color picker
478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 478 def active_scaffold_input_color(column, , ui_options: column.) html = [] = () if column.null? no_color = [:object].send(column.name).nil? method = no_color ? :hidden_field : :color_field html << content_tag(:label, check_box_tag('disable', '1', no_color, id: nil, name: nil, class: 'no-color') << " #{as_ [:no_color] || :no_color}") else method = :color_field end html << send(method, :record, column.name, .merge().except(:format, :no_color)) safe_join html end |
#active_scaffold_input_date(column, options, ui_options: column.options) ⇒ Object
509 510 511 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 509 def active_scaffold_input_date(column, , ui_options: column.) active_scaffold_text_input :date_field, column, , ui_options: end |
#active_scaffold_input_datetime(column, options, ui_options: column.options) ⇒ Object
517 518 519 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 517 def active_scaffold_input_datetime(column, , ui_options: column.) active_scaffold_text_input :datetime_local_field, column, , ui_options: end |
#active_scaffold_input_draggable(column, options, ui_options: column.options) ⇒ Object
219 220 221 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 219 def active_scaffold_input_draggable(column, , ui_options: column.) active_scaffold_input_checkboxes(column, .merge(draggable_lists: true), ui_options: ) end |
#active_scaffold_input_dragonfly(column, options, ui_options: column.options) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/active_scaffold/bridges/dragonfly/form_ui.rb', line 6 def active_scaffold_input_dragonfly(column, , ui_options: column.) record = [:object] dragonfly = record.send(column.name.to_s) content = active_scaffold_column_dragonfly(record, column, ui_options: ) if dragonfly.present? active_scaffold_file_with_content(column, , content, 'remove_', 'dragonfly_controls', ui_options: ) end |
#active_scaffold_input_email(column, options, ui_options: column.options) ⇒ Object
A text box, that accepts only valid email address (in-browser validation)
440 441 442 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 440 def active_scaffold_input_email(column, , ui_options: column.) active_scaffold_text_input :email_field, column, , ui_options: end |
#active_scaffold_input_enum(column, html_options, options = {}, ui_options: column.options) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 295 def active_scaffold_input_enum(column, , = {}, ui_options: column.) record = .delete(:object) [:selected] = record.send(column.name) [:object] = record = (column, record, ui_options: ) .merge!([:html_options] || {}) .merge!() active_scaffold_select_name_with_multiple () html = select(:record, column.name, , , ) html << active_scaffold_refresh_link(column, , record, ) if [:refresh_link] html end |
#active_scaffold_input_file_column(column, options, ui_options: column.options) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_scaffold/bridges/file_column/form_ui.rb', line 7 def active_scaffold_input_file_column(column, , ui_options: column.) record = [:object] if record.send(column.name) # we already have a value? display the form for deletion. remove_file_js = "jQuery(this).prev().val('true'); jQuery(this).parent().hide().next().show(); return false;" = .merge(id: "#{[:id]}_delete", name: [:name].sub("[#{column.name}]", "[delete_#{column.name}]"), value: 'false') custom_hidden_field_tag = hidden_field(:record, column.name, ) content_tag(:div) do content_tag(:div) do safe_join [get_column_value(record, column), custom_hidden_field_tag, '|', content_tag(:a, as_(:remove_file), href: '#', onclick: remove_file_js), content_tag(:div, 'test', style: 'display: none')], ' ' end end else file_column_field('record', column.name, ) end end |
#active_scaffold_input_month(column, options, ui_options: column.options) ⇒ Object
521 522 523 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 521 def active_scaffold_input_month(column, , ui_options: column.) active_scaffold_text_input :month_field, column, , ui_options: end |
#active_scaffold_input_number(column, options, ui_options: column.options) ⇒ Object
A spinbox control for number values (in-browser validation)
455 456 457 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 455 def active_scaffold_input_number(column, , ui_options: column.) active_scaffold_number_input :number_field, column, , :format, ui_options: end |
#active_scaffold_input_paperclip(column, options, ui_options: column.options) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/active_scaffold/bridges/paperclip/form_ui.rb', line 6 def active_scaffold_input_paperclip(column, , ui_options: column.) record = [:object] paperclip = record.send(column.name.to_s) content = active_scaffold_column_paperclip(record, column, ui_options: ) if paperclip.file? active_scaffold_file_with_content(column, , content, 'delete_', 'paperclip_controls', ui_options: ) end |
#active_scaffold_input_password(column, options, ui_options: column.options) ⇒ Object
424 425 426 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 424 def active_scaffold_input_password(column, , ui_options: column.) active_scaffold_text_input :password_field, column, .reverse_merge(autocomplete: 'new-password'), ui_options: end |
#active_scaffold_input_plural_association(column, options, ui_options: column.options) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 200 def active_scaffold_input_plural_association(column, , ui_options: column.) record = .delete(:object) , = (column, record) html = if [:multiple] || .dig(:html_options, :multiple) = .merge([:html_options] || {}) active_scaffold_select_name_with_multiple collection_select(:record, column.name, , :id, [:label_method] || :to_label, .merge(object: record), ) elsif .empty? content_tag(:span, as_(:no_options), class: "#{[:class]} no-options", id: [:id]) << hidden_field_tag("#{[:name]}[]", '', id: nil) else active_scaffold_checkbox_list(column, , .collect(&:id), , ui_options: ) end html << active_scaffold_refresh_link(column, , record, ) if [:refresh_link] html end |
#active_scaffold_input_radio(column, html_options, ui_options: column.options) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 394 def active_scaffold_input_radio(column, , ui_options: column.) record = [:object] .merge!([:html_options] || {}) = if column.association helper_method = association_helper_method(column.association, :sorted_association_options_find) send(helper_method, column.association, nil, record) else (column, record, ui_options: ) end if .present? html = active_scaffold_input_radio_content(column, record, , , ) else html = content_tag(:span, as_(:no_options), class: "#{[:class]} no-options") html << hidden_field_tag([:name], '', id: [:id]) if [:add_new] html = content_tag(:div, html, class: 'select-field') << active_scaffold_add_new(column, record, , ui_options: ) end html end html << active_scaffold_refresh_link(column, , record, .merge(field_selector: "[name=\"#{[:name]}\"]")) if [:refresh_link] html end |
#active_scaffold_input_radio_content(column, record, options, html_options, ui_options) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 346 def active_scaffold_input_radio_content(column, record, , , ) if [:add_new] add_new_subform = [:add_new] == true || [:add_new][:mode].in?([nil, :subform]) if add_new_subform [:data] ||= {} [:data][:subform_id] = active_scaffold_subform_attributes(column, ui_options: )[:id] end = .merge(class: "#{[:class]} hide-new-subform") else = end selected = record.send(column.association.name) if column.association radios = .map do |option| active_scaffold_radio_option(option, selected&.id, column, , ui_options: ) end if [:include_blank] label = [:include_blank] label = as_([:include_blank]) if [:include_blank].is_a?(Symbol) radio_id = "#{[:id]}-" radios.prepend content_tag(:label, (:record, column.name, '', .merge(id: radio_id)) + label) end if [:add_new] if add_new_subform create_new = content_tag(:label) do ([:name], '', selected&.new_record?, .merge( id: "#{[:id]}-create_new", class: "#{[:class]} show-new-subform" ).except(:object)) << active_scaffold_add_new_text([:add_new], :add_new_text, :create_new) end radios << create_new skip_link = true else = .merge(add_new: [:add_new].merge( url_options: { parent_scope: [:name].gsub(/^record|\[[^\]]*\]$/, '').presence, radio_data: .slice(*.keys.grep(/^data-update_/)) } )) radios << content_tag(:span, '', class: 'new-radio-container', id: [:id]) end radios << active_scaffold_add_new(column, record, , ui_options: , skip_link: skip_link) end safe_join radios end |
#active_scaffold_input_range(column, options, ui_options: column.options) ⇒ Object
A slider control for number values (in-browser validation)
460 461 462 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 460 def active_scaffold_input_range(column, , ui_options: column.) active_scaffold_number_input :range_field, column, , :format, ui_options: end |
#active_scaffold_input_select(column, html_options, ui_options: column.options) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 309 def active_scaffold_input_select(column, , ui_options: column.) record = [:object] html = if column.association&.singular? active_scaffold_input_singular_association(column, , ui_options: ) elsif column.association&.collection? active_scaffold_input_plural_association(column, , ui_options: ) else active_scaffold_input_enum(column, , ui_options: ) end if [:add_new] html = content_tag(:div, html, class: 'select-field') << active_scaffold_add_new(column, record, , ui_options: ) end html end |
#active_scaffold_input_select_multiple(column, options, ui_options: column.options) ⇒ Object
326 327 328 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 326 def active_scaffold_input_select_multiple(column, , ui_options: column.) active_scaffold_input_select(column, .merge(multiple: true), ui_options: ) end |
#active_scaffold_input_singular_association(column, html_options, options = {}, ui_options: column.options) ⇒ Object
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 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 28 def active_scaffold_input_singular_association(column, , = {}, ui_options: column.) record = .delete(:object) associated = .include?(:associated) ? .delete(:associated) : record.send(column.association.name) helper_method = association_helper_method(column.association, :sorted_association_options_find) = send(helper_method, column.association, nil, record) .unshift(associated) if associated&.persisted? && .exclude?(associated) method = column.name .merge! selected: associated&.id, include_blank: as_(:_select_), object: record .merge!([:html_options] || {}) .merge!() .delete(:multiple) # no point using multiple in a form for singular assoc, but may be set for field search () html = if (optgroup = .delete(:optgroup)) select(:record, method, (column, , optgroup), , ) else collection_select(:record, method, , :id, [:label_method] || :to_label, , ) end html << active_scaffold_refresh_link(column, , record, ) if [:refresh_link] html end |
#active_scaffold_input_telephone(column, options, ui_options: column.options) ⇒ Object
A text box, that accepts only valid phone-number (in-browser validation)
450 451 452 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 450 def active_scaffold_input_telephone(column, , ui_options: column.) active_scaffold_text_input :telephone_field, column, , :format, ui_options: end |
#active_scaffold_input_textarea(column, options, ui_options: column.options) ⇒ Object
428 429 430 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 428 def active_scaffold_input_textarea(column, , ui_options: column.) text_area(:record, column.name, .merge(cols: [:cols], rows: [:rows], size: [:size])) end |
#active_scaffold_input_time(column, options, ui_options: column.options) ⇒ Object
513 514 515 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 513 def active_scaffold_input_time(column, , ui_options: column.) active_scaffold_text_input :time_field, column, , ui_options: end |
#active_scaffold_input_url(column, options, ui_options: column.options) ⇒ Object
A text box, that accepts only valid URI (in-browser validation)
445 446 447 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 445 def active_scaffold_input_url(column, , ui_options: column.) active_scaffold_text_input :url_field, column, , ui_options: end |
#active_scaffold_input_virtual(column, options) ⇒ Object
432 433 434 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 432 def active_scaffold_input_virtual(column, ) active_scaffold_text_input :text_field, column, end |
#active_scaffold_input_week(column, options, ui_options: column.options) ⇒ Object
525 526 527 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 525 def active_scaffold_input_week(column, , ui_options: column.) active_scaffold_text_input :week_field, column, , ui_options: end |
#active_scaffold_new_record_klass(column, record, **options) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 54 def active_scaffold_new_record_klass(column, record, **) if column.association.polymorphic? && column.association.belongs_to? type = record.send(column.association.foreign_type) = [:types] column.association.klass(record) if type.present? && (.nil? || .include?(type)) else column.association.klass end end |
#active_scaffold_new_record_popup(column, record, html_options, options: {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 89 def active_scaffold_new_record_popup(column, record, , options: {}) klass = send(override_helper_per_model(:active_scaffold_new_record_klass, record.class), column, record, **) klass = nil if [:security_method] && !controller.send([:security_method]) klass = nil if klass && [:security_method].nil? && !klass.(crud_type: :create) return h('') unless klass link_text = active_scaffold_add_new_text(, :add_new_text, :add) = override_helper_per_model(:active_scaffold_new_record_url_options, record.class) = send(, column, record) [:controller] ||= active_scaffold_controller_for(klass).controller_path [:action] ||= :new [:from_field] ||= [:id] [:parent_model] ||= record.class.name [:parent_column] ||= column.name .reverse_merge! [:url_options] if [:url_options] link_to(link_text, , remote: true, data: {position: :popup}, class: 'as_action') end |
#active_scaffold_new_record_subform(column, record, html_options, options: {}, new_record_attributes: nil, locals: {}, skip_link: false) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 107 def active_scaffold_new_record_subform(column, record, , options: {}, new_record_attributes: nil, locals: {}, skip_link: false) klass = send(override_helper_per_model(:active_scaffold_new_record_klass, record.class), column, record, **) return content_tag(:div, '') unless klass subform_attrs = active_scaffold_subform_attributes(column, nil, klass, ui_options: ) if record.send(column.name)&.new_record? new_record = record.send(column.name) else subform_attrs[:style] = 'display: none' end subform_attrs[:class] << ' optional' scope = [:name].scan(/record(.*)\[#{column.name}\]/).dig(0, 0) new_record ||= klass.new(new_record_attributes) locals = locals.reverse_merge(column: column, parent_record: record, associated: [], show_blank_record: new_record, scope: scope) subform = render(partial: subform_partial_for_column(column, klass, ui_options: ), locals: locals) if [:hide_subgroups] toggable_id = "#{sub_form_id(association: column.name, id: record.id || generated_id(record) || 99_999_999_999)}-div" subform << link_to_visibility_toggle(toggable_id, default_visible: false) end html = content_tag(:div, subform, subform_attrs) return html if skip_link html << active_scaffold_show_new_subform_link(column, record, [:id], subform_attrs[:id], options: ) end |
#active_scaffold_new_record_url_options(column, record) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 77 def (column, record) if column.association.reverse constraint = [record.id] constraint.unshift record.class.name if column.association.reverse_association.polymorphic? {embedded: {constraints: {column.association.reverse => constraint}}} else raise "can't add constraint to create new record with :popup, no reverse association for " \ "\"#{column.name}\" in #{column.association.klass}, add the reverse association " \ 'or override active_scaffold_new_record_url_options helper.' end end |
#active_scaffold_number_input(method, column, options, remove_options = nil, ui_options: column.options) ⇒ Object
A slider control for number values (in-browser validation)
465 466 467 468 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 465 def active_scaffold_number_input(method, column, , = nil, ui_options: column.) = numerical_constraints_for_column(column, ) active_scaffold_text_input method, column, , , ui_options: end |
#active_scaffold_plural_association_options(column, record = nil) ⇒ Object
194 195 196 197 198 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 194 def (column, record = nil) = record.send(column.association.name) helper_method = association_helper_method(column.association, :sorted_association_options_find) [, | send(helper_method, column.association, nil, record)] end |
#active_scaffold_radio_option(option, selected, column, radio_options, ui_options: column.options) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 330 def active_scaffold_radio_option(option, selected, column, , ui_options: column.) if column.association label_method = [:label_method] || :to_label text = option.send(label_method) value = option.id checked = {checked: selected == value} else text, value = option end id_key = [:'data-id'] ? :'data-id' : :id = .merge(id_key => "#{[id_key]}-#{value.to_s.parameterize}") .merge!(checked) if checked content_tag(:label, (:record, column.name, value, ) + text) end |
#active_scaffold_refresh_link(column, html_options, record, ui_options = {}) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 176 def active_scaffold_refresh_link(column, , record, = {}) = {object: record, data: {field_selector: [:field_selector] || "##{[:id]}"}} if ['data-update_url'] ['data-update_send_form'] = ['data-update_send_form'] ['data-update_send_form_selector'] = ['data-update_send_form_selector'] else scope = [:name].scan(/^record((\[[^\]]*\])*)\[#{column.name}\]/).dig(0, 0) if [:name] = (column, scope.presence, , true) end [:class] = 'refresh-link' if [:refresh_link].is_a?(Hash) text = .dig(:refresh_link, :text) text = as_(text) if text.is_a?(Symbol) .merge! [:refresh_link].except(:text) end link_to(text || as_(:refresh), .delete('data-update_url') || ['data-update_url'], .except(:object)) end |
#active_scaffold_select_name_with_multiple(options) ⇒ Object
22 23 24 25 26 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 22 def active_scaffold_select_name_with_multiple() return if ![:multiple] || [:name].to_s.ends_with?('[]') [:name] = "#{[:name]}[]" end |
#active_scaffold_show_new_subform_link(column, record, select_id, subform_id, options: {}) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 139 def active_scaffold_show_new_subform_link(column, record, select_id, subform_id, options: {}) add_existing = active_scaffold_add_new_text(, :add_existing_text, :add_existing) create_new = active_scaffold_add_new_text(, :add_new_text, :create_new) data = {select_id: select_id, subform_id: subform_id, subform_text: add_existing, select_text: create_new} label = data[record.send(column.name)&.new_record? ? :subform_text : :select_text] link_to(label, '#', data: data, class: 'show-new-subform') end |
#active_scaffold_text_input(method, column, options, remove_options = nil, ui_options: column.options) ⇒ Object
470 471 472 473 474 475 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 470 def active_scaffold_text_input(method, column, , = nil, ui_options: column.) = () = .merge() = .except(*) if .present? send method, :record, column.name, end |
#active_scaffold_translate_select_options(options) ⇒ Object
16 17 18 19 20 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 16 def () [:include_blank] = as_([:include_blank].to_s) if [:include_blank].is_a? Symbol [:prompt] = as_([:prompt].to_s) if [:prompt].is_a? Symbol end |
#active_scaffold_translated_enum_options(column, record, ui_options: column.options) ⇒ Object
285 286 287 288 289 290 291 292 293 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 285 def (column, record, ui_options: column.) = override_helper_per_model(:active_scaffold_enum_options, record.class) # e.g. setting form_ui = :select, set the options in column.options, and set search_ui = :select, {null_comparators: false} = column. if [:options].nil? = send(, column, record, ui_options: )&.collect do |text, value| active_scaffold_translated_option(column, text, value) end || [] end |
#active_scaffold_translated_option(column, text, value = nil) ⇒ Object
271 272 273 274 275 276 277 278 279 |
# File 'lib/active_scaffold/helpers/form_ui_helpers.rb', line 271 def active_scaffold_translated_option(column, text, value = nil) value = text if value.nil? if text.is_a?(Symbol) klass = column.active_record_class text = I18n.t "#{klass.i18n_scope}.attributes.#{klass.model_name.i18n_key}.#{column.name}.#{text}", default: klass.human_attribute_name(text) end [text, value] end |