Module: ActiveScaffold::Helpers::FormColumnHelpers

Included in:
ViewHelpers
Defined in:
lib/active_scaffold/bridges/file_column/form_ui.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/helpers/form_column_helpers.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

Instance Method Details

#active_scaffold_add_existing_input(options) ⇒ Object


697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 697

def active_scaffold_add_existing_input(options)
  record = options.delete(:object)
  if !ActiveScaffold.js_framework.nil? && controller.respond_to?(:record_select_config, true)
    remote_controller = active_scaffold_controller_for(record_select_config.model).controller_path
    options[:controller] = remote_controller
    options.merge!(active_scaffold_input_text_options)
    record_select_field(options[:name], nil, options)
  else
    select_options = sorted_association_options_find(nested.association, nil, record)
    select_options ||= active_scaffold_config.model.all
    select_options = options_from_collection_for_select(select_options, :id, :to_label)
    select_tag 'associated_id', ((:option, as_(:_select_), value: '') + select_options) unless select_options.empty?
  end
end

#active_scaffold_add_existing_labelObject


712
713
714
715
716
717
718
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 712

def active_scaffold_add_existing_label
  if controller.respond_to?(:record_select_config, true)
    record_select_config.model.model_name.human
  else
    active_scaffold_config.model.model_name.human
  end
end

#active_scaffold_checkbox_list(column, select_options, associated_ids, options) ⇒ Object


422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 422

def active_scaffold_checkbox_list(column, select_options, associated_ids, options)
  label_method = column.options[:label_method] || :to_label
  html = hidden_field_tag("#{options[:name]}[]", '', :id => nil)
  html << (:ul, options.merge(:class => "#{options[:class]} checkbox-list#{' draggable-lists' if column.options[:draggable_lists]}")) do
    content = []
    select_options.each_with_index do |option, i|
      content << active_scaffold_checkbox_option(option, label_method, associated_ids, :name => "#{options[:name]}[]", :id => "#{options[:id]}_#{i}_id")
    end
    safe_join content
  end
  html
end

#active_scaffold_checkbox_option(option, label_method, associated_ids, checkbox_options, li_options = {}) ⇒ Object


413
414
415
416
417
418
419
420
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 413

def active_scaffold_checkbox_option(option, label_method, associated_ids, checkbox_options, li_options = {})
  (:li, li_options) 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(checkbox_options[:name], option_id, associated_ids.include?(option_id), checkbox_options) <<
      (:label, label, :for => checkbox_options[:id])
  end
end

#active_scaffold_enum_options(column, record = nil) ⇒ Object


440
441
442
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 440

def active_scaffold_enum_options(column, record = nil)
  column.options[:options]
end

#active_scaffold_file_with_content(column, content, options, remove_file_prefix, controls_class) ⇒ Object


355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 355

def active_scaffold_file_with_content(column, content, options, remove_file_prefix, controls_class)
  required = options.delete(:required)
  case ActiveScaffold.js_framework
  when :jquery
    js_remove_file_code = "jQuery(this).prev().val('true'); jQuery(this).parent().hide().next().show()#{".find('input').attr('required', 'required')" if required}; return false;"
    js_dont_remove_file_code = "jQuery(this).parents('div.#{controls_class}').find('input.remove_file').val('false'); return false;"
  when :prototype
    js_remove_file_code = "$(this).previous().value='true'; $(this).up().hide().next().show()#{".down().writeAttribute('required', 'required')" if required}; return false;"
    js_dont_remove_file_code = "jQuery(this).parents('div.#{controls_class}').find('input.remove_file').val('false'); return false;"
  end

  object_name, method = options[:name].split(/\[(#{column.name})\]/)
  method.sub!(/#{column.name}/, "#{remove_file_prefix}\\0")
  fields = block_given? ? yield : ''
  link_key = options[:multiple] ? :remove_files : :remove_file
  input = file_field(:record, column.name, options.merge(:onchange => js_dont_remove_file_code))
  (:div, class: controls_class) do
    (:div) do
      safe_join [content, ' | ', fields,
                 hidden_field(object_name, method, :value => 'false', class: 'remove_file'),
                 (:a, as_(link_key), :href => '#', :onclick => js_remove_file_code)]
    end << (:div, input, :style => 'display: none')
  end
end

rubocop:disable Metrics/ParameterLists


346
347
348
349
350
351
352
353
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 346

def active_scaffold_file_with_remove_link(column, options, content, remove_file_prefix, controls_class, &block) # rubocop:disable Metrics/ParameterLists
  options = active_scaffold_input_text_options(options.merge(column.options))
  if content
    active_scaffold_file_with_content(column, content, options, remove_file_prefix, controls_class, &block)
  else
    file_field(:record, column.name, options)
  end
end

#active_scaffold_grouped_options(column, select_options, optgroup) ⇒ Object

Form input methods


265
266
267
268
269
270
271
272
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 265

def active_scaffold_grouped_options(column, select_options, optgroup)
  group_column = active_scaffold_config_for(column.association.klass).columns[optgroup]
  group_label = group_column.options[:label_method] if group_column
  group_label ||= group_column&.association ? :to_label : :to_s
  select_options.group_by(&optgroup.to_sym).collect do |group, options|
    [group.send(group_label), options.collect { |r| [r.send(column.options[:label_method] || :to_label), r.id] }]
  end
end

#active_scaffold_input_active_storage_has_many(column, options) ⇒ Object


12
13
14
15
16
17
18
19
# File 'lib/active_scaffold/bridges/active_storage/form_ui.rb', line 12

def active_scaffold_input_active_storage_has_many(column, options)
  record = options[:object]
  options[:multiple] = 'multiple'
  options[:name] += '[]'
  active_storage = record.send(column.name.to_s)
  content = active_scaffold_column_active_storage_has_many(record, column) if active_storage.attached?
  active_scaffold_file_with_remove_link(column, options, content, 'delete_', 'active_storage_controls')
end

#active_scaffold_input_active_storage_has_one(column, options) ⇒ Object


5
6
7
8
9
10
# File 'lib/active_scaffold/bridges/active_storage/form_ui.rb', line 5

def active_scaffold_input_active_storage_has_one(column, options)
  record = options[:object]
  active_storage = record.send(column.name.to_s)
  content = active_scaffold_column_active_storage_has_one(record, column) if active_storage.attached?
  active_scaffold_file_with_remove_link(column, options, content, 'delete_', 'active_storage_controls')
end

#active_scaffold_input_boolean(column, options) ⇒ Object

Column.type-based inputs


602
603
604
605
606
607
608
609
610
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 602

def active_scaffold_input_boolean(column, options)
  record = options.delete(:object)
  select_options = []
  select_options << [as_(:_select_), nil] if !column.virtual? && column.column.null
  select_options << [as_(:true), true] # rubocop:disable Lint/BooleanSymbol
  select_options << [as_(:false), false] # rubocop:disable Lint/BooleanSymbol

  select_tag(options[:name], options_for_select(select_options, record.send(column.name)), options)
end

#active_scaffold_input_carrierwave(column, options) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/active_scaffold/bridges/carrierwave/form_ui.rb', line 4

def active_scaffold_input_carrierwave(column, options)
  record = options[:object]
  carrierwave = record.send(column.name.to_s)
  content = get_column_value(record, column) if carrierwave.file.present?
  active_scaffold_file_with_remove_link(column, options, content, 'remove_', 'carrierwave_controls') do
    cache_field_options = {
      name: options[:name].gsub(/\[#{column.name}\]$/, "[#{column.name}_cache]"),
      id: options[:id] + '_cache'
    }
    hidden_field(:record, "#{column.name}_cache", cache_field_options)
  end
end

#active_scaffold_input_checkbox(column, options) ⇒ Object


526
527
528
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 526

def active_scaffold_input_checkbox(column, options)
  check_box(:record, column.name, options.merge(column.options))
end

#active_scaffold_input_color(column, options) ⇒ Object

A color picker


584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 584

def active_scaffold_input_color(column, options)
  html = []
  options = active_scaffold_input_text_options(options)
  if column.column&.null
    no_color = options[:object].send(column.name).nil?
    method = no_color ? :hidden_field : :color_field
    html << (:label, check_box_tag('disable', '1', no_color, id: nil, name: nil, class: 'no-color') << " #{as_ column.options[:no_color] || :no_color}")
  else
    method = :color_field
  end
  html << send(method, :record, column.name, options.merge(column.options).except(:format, :no_color))
  safe_join html
end

#active_scaffold_input_date(column, options) ⇒ Object


612
613
614
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 612

def active_scaffold_input_date(column, options)
  active_scaffold_text_input :date_field, column, options
end

#active_scaffold_input_datetime(column, options) ⇒ Object


620
621
622
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 620

def active_scaffold_input_datetime(column, options)
  active_scaffold_text_input :datetime_local_field, column, options
end

#active_scaffold_input_dragonfly(column, options) ⇒ Object


4
5
6
7
8
9
# File 'lib/active_scaffold/bridges/dragonfly/form_ui.rb', line 4

def active_scaffold_input_dragonfly(column, options)
  record = options[:object]
  dragonfly = record.send(column.name.to_s)
  content = active_scaffold_column_dragonfly(record, column) if dragonfly.present?
  active_scaffold_file_with_remove_link(column, options, content, 'remove_', 'dragonfly_controls')
end

#active_scaffold_input_email(column, options) ⇒ Object

A text box, that accepts only valid email address (in-browser validation)


546
547
548
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 546

def active_scaffold_input_email(column, options)
  active_scaffold_text_input :email_field, column, options
end

#active_scaffold_input_enum(column, html_options, options = {}) ⇒ Object


444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 444

def active_scaffold_input_enum(column, html_options, options = {})
  record = html_options.delete(:object)
  options[:selected] = record.send(column.name)
  options[:object] = record
  options_for_select = active_scaffold_enum_options(column, record).collect do |text, value|
    active_scaffold_translated_option(column, text, value)
  end
  html_options.merge!(column.options[:html_options] || {})
  options.merge!(column.options)
  active_scaffold_select_name_with_multiple html_options
  active_scaffold_translate_select_options(options)
  select(:record, column.name, options_for_select, options, html_options)
end

#active_scaffold_input_file_column(column, options) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_scaffold/bridges/file_column/form_ui.rb', line 5

def active_scaffold_input_file_column(column, options)
  record = options[:object]
  if record.send(column.name)
    # we already have a value? display the form for deletion.
    case ActiveScaffold.js_framework
    when :jquery
      remove_file_js = "jQuery(this).prev().val('true'); jQuery(this).parent().hide().next().show(); return false;"
    when :prototype
      remove_file_js = "$(this).previous().value='true'; p=$(this).up(); p.hide(); p.next().show(); return false;"
    end

    hidden_options = options.merge(:id => options[:id] + '_delete', :name => options[:name].sub("[#{column.name}]", "[delete_#{column.name}]"), :value => 'false')
    custom_hidden_field_tag = hidden_field(:record, column.name, hidden_options)

    (:div) do
      (:div) do
        safe_join [get_column_value(record, column), custom_hidden_field_tag, '|',
                   (:a, as_(:remove_file), :href => '#', :onclick => remove_file_js),
                   (:div, 'test', :style => 'display: none')], ' '
      end
    end
  else
    file_column_field('record', column.name, options)
  end
end

#active_scaffold_input_for(column, scope = nil, options = nil) ⇒ Object

This method decides which input to use for the given column. It does not do any rendering. It only decides which method is responsible for rendering.


7
8
9
10
11
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 7

def active_scaffold_input_for(column, scope = nil, options = nil)
  options ||= active_scaffold_input_options(column, scope)
  options = update_columns_options(column, scope, options)
  active_scaffold_render_input(column, options)
end

#active_scaffold_input_month(column, options) ⇒ Object


624
625
626
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 624

def active_scaffold_input_month(column, options)
  active_scaffold_text_input :month_field, column, options
end

#active_scaffold_input_number(column, options) ⇒ Object

A spinbox control for number values (in-browser validation)


561
562
563
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 561

def active_scaffold_input_number(column, options)
  active_scaffold_number_input :number_field, column, options, :format
end

#active_scaffold_input_options(column, scope = nil, options = {}) ⇒ Object

the standard active scaffold options used for class, name and scope


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 89

def active_scaffold_input_options(column, scope = nil, options = {})
  name = scope ? "record#{scope}[#{column.name}]" : "record[#{column.name}]"
  record = options[:object]

  # Add some HTML5 attributes for in-browser validation and better user experience
  if column.required? && (!@disable_required_for_new || scope.nil? || record&.persisted?)
    options[:required] = true
  end
  options[:placeholder] = column.placeholder if column.placeholder.present?

  # Fix for keeping unique IDs in subform
  id_control = "record_#{column.name}_#{[params[:eid], params[:parent_id] || params[:id]].compact.join '_'}"
  id_control += scope_id(scope) if scope

  classes = "#{column.name}-input"
  classes += ' numeric-input' if column.number?

  {:name => name, :class => classes, :id => id_control}.merge(options)
end

#active_scaffold_input_paperclip(column, options) ⇒ Object


4
5
6
7
8
9
# File 'lib/active_scaffold/bridges/paperclip/form_ui.rb', line 4

def active_scaffold_input_paperclip(column, options)
  record = options[:object]
  paperclip = record.send(column.name.to_s)
  content = active_scaffold_column_paperclip(record, column) if paperclip.file?
  active_scaffold_file_with_remove_link(column, options, content, 'delete_', 'paperclip_controls')
end

#active_scaffold_input_password(column, options) ⇒ Object


530
531
532
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 530

def active_scaffold_input_password(column, options)
  active_scaffold_text_input :password_field, column, options.reverse_merge(autocomplete: 'new-password')
end

#active_scaffold_input_plural_association(column, options) ⇒ Object


398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 398

def active_scaffold_input_plural_association(column, options)
  record = options.delete(:object)
  associated_options, select_options = active_scaffold_plural_association_options(column, record)

  html =
    if select_options.empty?
      (:span, as_(:no_options), :class => "#{options[:class]} no-options", :id => options[:id]) <<
        hidden_field_tag("#{options[:name]}[]", '', :id => nil)
    else
      active_scaffold_checkbox_list(column, select_options, associated_options.collect(&:id), options)
    end
  html << active_scaffold_refresh_link(column, options, record) if column.options[:refresh_link]
  html
end

#active_scaffold_input_radio(column, html_options) ⇒ Object


484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 484

def active_scaffold_input_radio(column, html_options)
  record = html_options[:object]
  html_options.merge!(column.options[:html_options] || {})
  options =
    if column.association
      sorted_association_options_find(column.association, nil, record)
    else
      active_scaffold_enum_options(column, record)
    end

  selected = record.send(column.association.name) if column.association
  selected_id = selected&.id
  if options.present?
    if column.options[:add_new]
      html_options[:data] ||= {}
      html_options[:data][:subform_id] = active_scaffold_subform_attributes(column)[:id]
      radio_html_options = html_options.merge(class: html_options[:class] + ' hide-new-subform')
    else
      radio_html_options = html_options
    end
    radios = options.map do |option|
      active_scaffold_radio_option(option, selected_id, column, radio_html_options)
    end
    if column.options[:include_blank]
      label = column.options[:include_blank]
      label = as_(column.options[:include_blank]) if column.options[:include_blank].is_a?(Symbol)
      radios.prepend (:label, radio_button(:record, column.name, '', html_options.merge(id: nil)) + label)
    end
    if column.options[:add_new]
      create_new_button = radio_button_tag(html_options[:name], '', selected&.new_record?, html_options.merge(id: nil, class: html_options[:class] + ' show-new-subform'))
      radios << (:label, create_new_button << as_(:create_new)) <<
        active_scaffold_new_record_subform(column, record, html_options, skip_link: true)
    end
    safe_join radios
  else
    html = (:span, as_(:no_options), :class => "#{html_options[:class]} no-options", :id => html_options[:id])
    html << hidden_field_tag(html_options[:name], '', :id => nil)
    html << active_scaffold_new_record_subform(column, record, html_options) if column.options[:add_new]
    html
  end
end

#active_scaffold_input_range(column, options) ⇒ Object

A slider control for number values (in-browser validation)


566
567
568
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 566

def active_scaffold_input_range(column, options)
  active_scaffold_number_input :range_field, column, options, :format
end

#active_scaffold_input_select(column, html_options) ⇒ Object


458
459
460
461
462
463
464
465
466
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 458

def active_scaffold_input_select(column, html_options)
  if column.association&.singular?
    active_scaffold_input_singular_association(column, html_options)
  elsif column.association&.collection?
    active_scaffold_input_plural_association(column, html_options)
  else
    active_scaffold_input_enum(column, html_options)
  end
end

#active_scaffold_input_singular_association(column, html_options, options = {}) ⇒ Object


285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 285

def active_scaffold_input_singular_association(column, html_options, options = {})
  record = html_options.delete(:object)
  associated = record.send(column.association.name)

  select_options = sorted_association_options_find(column.association, nil, record)
  select_options.unshift(associated) unless associated.nil? || select_options.include?(associated)

  method = column.name
  options.merge! :selected => associated&.id, :include_blank => as_(:_select_), :object => record

  html_options.merge!(column.options[:html_options] || {})
  options.merge!(column.options)
  active_scaffold_select_name_with_multiple html_options
  active_scaffold_translate_select_options(options)

  html =
    if (optgroup = options.delete(:optgroup))
      select(:record, method, active_scaffold_grouped_options(column, select_options, optgroup), options, html_options)
    else
      collection_select(:record, method, select_options, :id, column.options[:label_method] || :to_label, options, html_options)
    end
  html << active_scaffold_refresh_link(column, html_options, record) if column.options[:refresh_link]
  html << active_scaffold_new_record_subform(column, record, html_options) if column.options[:add_new]
  html
end

#active_scaffold_input_telephone(column, options) ⇒ Object

A text box, that accepts only valid phone-number (in-browser validation)


556
557
558
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 556

def active_scaffold_input_telephone(column, options)
  active_scaffold_text_input :telephone_field, column, options, :format
end

#active_scaffold_input_text_options(options = {}) ⇒ Object

the standard active scaffold options used for textual inputs


82
83
84
85
86
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 82

def active_scaffold_input_text_options(options = {})
  options[:autocomplete] ||= 'off'
  options[:class] = "#{options[:class]} text-input".strip
  options
end

#active_scaffold_input_textarea(column, options) ⇒ Object


534
535
536
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 534

def active_scaffold_input_textarea(column, options)
  text_area(:record, column.name, options.merge(:cols => column.options[:cols], :rows => column.options[:rows], :size => column.options[:size]))
end

#active_scaffold_input_time(column, options) ⇒ Object


616
617
618
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 616

def active_scaffold_input_time(column, options)
  active_scaffold_text_input :time_field, column, options
end

#active_scaffold_input_url(column, options) ⇒ Object

A text box, that accepts only valid URI (in-browser validation)


551
552
553
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 551

def active_scaffold_input_url(column, options)
  active_scaffold_text_input :url_field, column, options
end

#active_scaffold_input_virtual(column, options) ⇒ Object


538
539
540
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 538

def active_scaffold_input_virtual(column, options)
  active_scaffold_text_input :text_field, column, options
end

#active_scaffold_input_week(column, options) ⇒ Object


628
629
630
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 628

def active_scaffold_input_week(column, options)
  active_scaffold_text_input :week_field, column, options
end

#active_scaffold_new_record_subform(column, record, html_options, new_record_attributes: nil, locals: {}, skip_link: false) ⇒ Object

rubocop:disable Metrics/ParameterLists


311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 311

def active_scaffold_new_record_subform(column, record, html_options, new_record_attributes: nil, locals: {}, skip_link: false) # rubocop:disable Metrics/ParameterLists
  klass =
    if column.association.polymorphic? && column.association.belongs_to?
      type = record.send(column.association.foreign_type)
      column.association.klass(record) if type.present? && (column.options[:add_new] == true || type.in?(column.options[:add_new]))
    else
      column.association.klass
    end
  return (:div, '') unless klass
  subform_attrs = active_scaffold_subform_attributes(column, nil, klass)
  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 = html_options[: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), locals: locals)
  if column.options[: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 = (:div, subform, subform_attrs)
  return html if skip_link
  html << active_scaffold_show_new_subform_link(column, record, html_options[:id], subform_attrs[:id])
end

#active_scaffold_number_input(method, column, options, remove_options = nil) ⇒ Object

A slider control for number values (in-browser validation)


571
572
573
574
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 571

def active_scaffold_number_input(method, column, options, remove_options = nil)
  options = numerical_constraints_for_column(column, options)
  active_scaffold_text_input method, column, options, remove_options
end

#active_scaffold_plural_association_options(column, record = nil) ⇒ Object


393
394
395
396
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 393

def active_scaffold_plural_association_options(column, record = nil)
  associated_options = record.send(column.association.name)
  [associated_options, associated_options | sorted_association_options_find(column.association, nil, record)]
end

#active_scaffold_radio_option(option, selected, column, radio_options) ⇒ Object


468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 468

def active_scaffold_radio_option(option, selected, column, radio_options)
  if column.association
    label_method = column.options[:label_method] || :to_label
    text = option.send(label_method)
    value = option.id
    checked = {:checked => selected == value}
  else
    text, value = active_scaffold_translated_option(column, *option)
  end

  id_key = radio_options[:"data-id"] ? :"data-id" : :id
  radio_options = radio_options.merge(id_key => radio_options[id_key] + '-' + value.to_s.parameterize)
  radio_options.merge!(checked) if checked
  (:label, radio_button(:record, column.name, value, radio_options) + text)
end

380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 380

def active_scaffold_refresh_link(column, html_options, record)
  link_options = {:object => record}
  if html_options['data-update_url']
    link_options['data-update_send_form'] = html_options['data-update_send_form']
    link_options['data-update_send_form_selector'] = html_options['data-update_send_form_selector']
  else
    scope = html_options[:name].scan(/^record((\[[^\]]*\])*)\[#{column.name}\]/).dig(0, 0) if html_options[:name]
    link_options = update_columns_options(column, scope.presence, link_options, true)
  end
  link_options[:class] = 'refresh-link'
  link_to(as_(:refresh), link_options.delete('data-update_url') || html_options['data-update_url'], link_options)
end

#active_scaffold_render_input(column, options) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 13

def active_scaffold_render_input(column, options)
  record = options[:object]

  # first, check if the dev has created an override for this specific field
  if (method = override_form_field(column))
    send(method, record, options)

  # second, check if the dev has specified a valid form_ui for this column
  elsif column.form_ui && (method = override_input(column.form_ui))
    send(method, column, options)

  elsif column.association
    # if we get here, it's because the column has a form_ui but not one ActiveScaffold knows about.
    raise "Unknown form_ui `#{column.form_ui}' for column `#{column.name}'" if column.form_ui

    # its an association and nothing is specified, we will assume form_ui :select
    active_scaffold_input_select(column, options)

  elsif column.virtual?
    options[:value] = format_number_value(record.send(column.name), column.options) if column.number?
    active_scaffold_input_virtual(column, options)

  elsif (method = override_input(column.column.type)) # regular model attribute column
    # if we (or someone else) have created a custom render option for the column type, use that
    send(method, column, options)

  else # final ultimate fallback: use rails' generic input method
    # for textual fields we pass different options
    text_types = %i[text string integer float decimal]
    options = active_scaffold_input_text_options(options) if text_types.include?(column.column.type)
    if column.column.type == :string && options[:maxlength].blank?
      options[:maxlength] = column.column.limit
      options[:size] ||= options[:maxlength].to_i > 30 ? 30 : options[:maxlength]
    end
    options[:value] = format_number_value(record.send(column.name), column.options) if column.number?
    text_field(:record, column.name, options.merge(column.options).except(:format))
  end
rescue StandardError => e
  logger.error "#{e.class.name}: #{e.message} -- on the ActiveScaffold column = :#{column.name} in #{controller.class}"
  raise e
end

#active_scaffold_render_subform_column(column, scope, crud_type, readonly, add_class = false, record = nil) ⇒ Object

rubocop:disable Metrics/ParameterLists


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 55

def active_scaffold_render_subform_column(column, scope, crud_type, readonly, add_class = false, record = nil) # rubocop:disable Metrics/ParameterLists
  if add_class
    col_class = []
    col_class << 'required' if column.required?
    col_class << column.css_class unless column.css_class.nil? || column.css_class.is_a?(Proc)
    col_class << 'hidden' if column_renders_as(column) == :hidden
    col_class << 'checkbox' if column.form_ui == :checkbox
    col_class = col_class.join(' ')
  end
  if readonly && !record.new_record? || !record.authorized_for?(:crud_type => crud_type, :column => column.name)
    form_attribute(column, record, scope, true, col_class)
  else
    renders_as = column_renders_as(column)
    html = render_column(column, record, renders_as, scope, false, col_class)
    html = (:div, html, active_scaffold_subform_attributes(column)) if renders_as == :subform
    html
  end
end

#active_scaffold_select_name_with_multiple(options) ⇒ Object


280
281
282
283
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 280

def active_scaffold_select_name_with_multiple(options)
  return if !options[:multiple] || options[:name].to_s.ends_with?('[]')
  options[:name] = "#{options[:name]}[]"
end

340
341
342
343
344
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 340

def active_scaffold_show_new_subform_link(column, record, select_id, subform_id)
  data = {select_id: select_id, subform_id: subform_id, subform_text: as_(:add_existing), select_text: as_(: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_subform_attributes(column, column_css_class = nil, klass = nil) ⇒ Object


74
75
76
77
78
79
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 74

def active_scaffold_subform_attributes(column, column_css_class = nil, klass = nil)
  {
    :class => "sub-form #{active_scaffold_config_for(klass || column.association.klass).subform.layout}-sub-form #{column_css_class} #{column.name}-sub-form",
    :id => sub_form_id(:association => column.name)
  }
end

#active_scaffold_text_input(method, column, options, remove_options = nil) ⇒ Object


576
577
578
579
580
581
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 576

def active_scaffold_text_input(method, column, options, remove_options = nil)
  options = active_scaffold_input_text_options(options)
  options = options.merge(column.options)
  options = options.except(*remove_options) if remove_options.present?
  send method, :record, column.name, options
end

#active_scaffold_translate_select_options(options) ⇒ Object


274
275
276
277
278
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 274

def active_scaffold_translate_select_options(options)
  options[:include_blank] = as_(options[:include_blank].to_s) if options[:include_blank].is_a? Symbol
  options[:prompt] = as_(options[:prompt].to_s) if options[:prompt].is_a? Symbol
  options
end

#active_scaffold_translated_option(column, text, value = nil) ⇒ Object


435
436
437
438
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 435

def active_scaffold_translated_option(column, text, value = nil)
  value = text if value.nil?
  [(text.is_a?(Symbol) ? column.active_record_class.human_attribute_name(text) : text), value]
end

#column_numerical_constraints(column, options) ⇒ Object

Try to get numerical constraints from model's validators


721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 721

def column_numerical_constraints(column, options)
  validators = column.active_record_class.validators.select do |v|
    v.is_a?(ActiveModel::Validations::NumericalityValidator) && v.attributes.include?(column.name)
  end

  equal_validator = validators.find { |v| v.options[:equal_to] }
  # If there is equal_to constraint - use it (unless otherwise specified by user)
  if equal_validator && !(options[:min] || options[:max])
    equal_to = equal_validator.options[:equal_to]
    return {min: equal_to, max: equal_to}
  end

  numerical_constraints = {}

  # find minimum and maximum from validators
  # we can safely modify :min and :max by 1 for :greater_tnan or :less_than value only for integer values
  only_integer = column.column.type == :integer if column.column
  only_integer ||= validators.find { |v| v.options[:only_integer] }.present?
  margin = only_integer ? 1 : 0

  # Minimum
  unless options[:min]
    min = validators.map { |v| v.options[:greater_than_or_equal_to] }.compact.max
    greater_than = validators.map { |v| v.options[:greater_than] }.compact.max
    numerical_constraints[:min] = [min, (greater_than + margin if greater_than)].compact.max
  end

  # Maximum
  unless options[:max]
    max = validators.map { |v| v.options[:less_than_or_equal_to] }.compact.min
    less_than = validators.map { |v| v.options[:less_than] }.compact.min
    numerical_constraints[:max] = [max, (less_than - margin if less_than)].compact.min
  end

  # Set step = 2 for column values restricted to be odd or even (but only if minimum is set)
  unless options[:step]
    only_odd_valid  = validators.any? { |v| v.options[:odd] }
    only_even_valid = validators.any? { |v| v.options[:even] } unless only_odd_valid
    if !only_integer
      numerical_constraints[:step] ||= "0.#{'0' * (column.column.scale - 1)}1" if column.column&.scale.to_i.positive?
    elsif options[:min] && options[:min].respond_to?(:even?) && (only_odd_valid || only_even_valid)
      numerical_constraints[:step] = 2
      numerical_constraints[:min] += 1 if only_odd_valid  && options[:min].even?
      numerical_constraints[:min] += 1 if only_even_valid && options[:min].odd?
    end
    numerical_constraints[:step] ||= 'any' unless only_integer
  end

  numerical_constraints
end

#column_renders_as(column) ⇒ Object

Macro-level rendering decisions for columns


677
678
679
680
681
682
683
684
685
686
687
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 677

def column_renders_as(column)
  if column.respond_to? :each
    :subsection
  elsif column.active_record_class.locking_column.to_s == column.name.to_s || column.form_ui == :hidden
    :hidden
  elsif column.association.nil? || column.form_ui || !active_scaffold_config_for(column.association.klass).actions.include?(:subform) || override_form_field?(column)
    :field
  else
    :subform
  end
end

#column_scope(column, scope = nil, record = nil) ⇒ Object


689
690
691
692
693
694
695
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 689

def column_scope(column, scope = nil, record = nil)
  if column.association&.collection?
    "#{scope}[#{column.name}][#{record.id || generate_temporary_id(record)}]"
  else
    "#{scope}[#{column.name}]"
  end
end

#column_show_add_existing(column, record = nil) ⇒ Object


249
250
251
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 249

def column_show_add_existing(column, record = nil)
  column.allow_add_existing && options_for_association_count(column.association, record).positive?
end

#column_show_add_new(column, associated, record) ⇒ Object


253
254
255
256
257
258
259
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 253

def column_show_add_new(column, associated, record)
  assoc = column.association
  value = assoc.singular?
  value ||= assoc.collection? && !assoc.readonly? && (!assoc.through? || !assoc.through_reflection.collection?)
  value &&= false unless assoc.klass.authorized_for?(:crud_type => :create)
  value
end

#current_form_columns(record, scope, subform_controller = nil) ⇒ Object


109
110
111
112
113
114
115
116
117
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 109

def current_form_columns(record, scope, subform_controller = nil)
  if scope
    subform_controller.active_scaffold_config.subform.columns.visible_columns_names
  elsif %i[new create edit update render_field].include? action_name.to_sym
    # disable update_columns for inplace_edit (GET render_field)
    return if action_name == 'render_field' && request.get?
    active_scaffold_config.send(record.new_record? ? :create : :update).columns.visible_columns_names
  end
end

#field_attributes(column, record) ⇒ Object


147
148
149
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 147

def field_attributes(column, record)
  {}
end

#form_attribute(column, record, scope = nil, only_value = false, col_class = nil) ⇒ Object


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 163

def form_attribute(column, record, scope = nil, only_value = false, col_class = nil)
  column_options = active_scaffold_input_options(column, scope, :object => record)
  attributes = field_attributes(column, record)
  attributes[:class] = "#{attributes[:class]} #{col_class}" if col_class.present?
  if only_value
    field = (:span, get_column_value(record, column), column_options.except(:name, :object))
    if column.association.nil? || column.association.belongs_to?
      # hidden field probably not needed, but leaving it just in case
      # but it isn't working for assocations which are not belongs_to
      method = column.association ? column.association.foreign_key : column.name
      field << hidden_field(:record, method, column_options)
    end
  else
    field = active_scaffold_input_for column, scope, column_options
  end
  if field
    field << loading_indicator_tag(:action => :render_field, :id => params[:id]) if column.update_columns
    field << (:span, column.description, :class => 'description') if column.description.present?
  end

   :dl, attributes do
    (:dt, label_tag(label_for(column, column_options), form_column_label(column))) <<
      (:dd, field)
  end
end

#form_column_label(column) ⇒ Object


193
194
195
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 193

def form_column_label(column)
  column.label
end

#form_hidden_attribute(column, record, scope = nil) ⇒ Object


201
202
203
204
205
206
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 201

def form_hidden_attribute(column, record, scope = nil)
   :dl, style: 'display: none' do
    (:dt, '') <<
      (:dd, form_hidden_field(column, record, scope))
  end
end

#form_hidden_field(column, record, scope) ⇒ Object


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 208

def form_hidden_field(column, record, scope)
  options = active_scaffold_input_options(column, scope)
  if column.association&.collection?
    associated = record.send(column.name)
    if associated.blank?
      hidden_field_tag options[:name], '', options
    else
      options[:name] += '[]'
      fields = associated.map do |r|
        hidden_field_tag options[:name], r.id, options.merge(id: options[:id] + "_#{r.id}")
      end
      safe_join fields, ''
    end
  elsif column.association
    hidden_field_tag options[:name], record.send(column.name)&.id, options
  else
    hidden_field :record, column.name, options.merge(object: record)
  end
end

#in_subform?(column, parent_record, parent_column) ⇒ Boolean

Should this column be displayed in the subform?

Returns:

  • (Boolean)

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 229

def in_subform?(column, parent_record, parent_column)
  return true unless column.association

  if column.association.reverse.nil?
    # Polymorphic associations can't appear because they *might* be the reverse association
    return false if column.association.polymorphic?

    # A column shouldn't be in the subform if it's the reverse association to the parent
    !column.association.inverse_for?(parent_record.class)
  elsif column.association.reverse == parent_column.name
    if column.association.polymorphic?
      column.association.name != parent_column.association.as
    else
      !column.association.inverse_for?(parent_record.class)
    end
  else
    true
  end
end

#label_for(column, options) ⇒ Object


189
190
191
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 189

def label_for(column, options)
  options[:id] unless column.form_ui == :select && column.association&.collection?
end

#numerical_constraints_for_column(column, options) ⇒ Object


772
773
774
775
776
777
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 772

def numerical_constraints_for_column(column, options)
  constraints = Rails.cache.fetch("#{column.cache_key}#numerical_constarints") do
    column_numerical_constraints(column, options)
  end
  constraints.merge(options)
end

#override_form_field(column) ⇒ Object Also known as: override_form_field?


656
657
658
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 656

def override_form_field(column)
  override_helper column, 'form_column'
end

#override_form_field_partial(column) ⇒ Object

the naming convention for overriding form fields with helpers


652
653
654
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 652

def override_form_field_partial(column)
  partial_for_model(column.active_record_class, "#{clean_column_name(column.name)}_form_column")
end

#override_input(form_ui) ⇒ Object Also known as: override_input?

the naming convention for overriding form input types with helpers


662
663
664
665
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 662

def override_input(form_ui)
  method = "active_scaffold_input_#{form_ui}"
  method if respond_to? method
end

#override_subform_partial(column, subform_partial) ⇒ Object

add functionality for overriding subform partials from association class path


647
648
649
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 647

def override_subform_partial(column, subform_partial)
  partial_for_model(column.association.klass, subform_partial) if column_renders_as(column) == :subform
end

#partial_for_model(model, partial) ⇒ Object

Form column override signatures


636
637
638
639
640
641
642
643
644
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 636

def partial_for_model(model, partial)
  controller = active_scaffold_controller_for(model)
  while controller.uses_active_scaffold?
    path = File.join(controller.controller_path, partial)
    return path if template_exists?(path, true)
    controller = controller.superclass
  end
  nil
end

#render_column(column, record, renders_as, scope = nil, only_value = false, col_class = nil) ⇒ Object

rubocop:disable Metrics/ParameterLists


151
152
153
154
155
156
157
158
159
160
161
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 151

def render_column(column, record, renders_as, scope = nil, only_value = false, col_class = nil) # rubocop:disable Metrics/ParameterLists
  if (partial = override_form_field_partial(column))
    render :partial => partial, :locals => {:column => column, :only_value => only_value, :scope => scope, :col_class => col_class, :record => record}
  elsif renders_as == :field || override_form_field?(column)
    form_attribute(column, record, scope, only_value, col_class)
  elsif renders_as == :subform
    render :partial => 'form_association', :locals => {:column => column, :scope => scope, :parent_record => record}
  else
    form_hidden_attribute(column, record, scope)
  end
end

#subform_label(column, hidden) ⇒ Object


197
198
199
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 197

def subform_label(column, hidden)
  column.label unless hidden
end

#subform_partial_for_column(column, klass = nil) ⇒ Object


668
669
670
671
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 668

def subform_partial_for_column(column, klass = nil)
  subform_partial = "#{column.options[:layout] || active_scaffold_config_for(klass || column.association.klass).subform.layout}_subform"
  override_subform_partial(column, subform_partial) || subform_partial
end

#update_columns_options(column, scope, options, force = false) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 119

def update_columns_options(column, scope, options, force = false)
  record = options[:object]
  subform_controller = controller.class.active_scaffold_controller_for(record.class) if scope
  if @main_columns && (scope.nil? || subform_controller == controller.class)
    form_columns = @main_columns.visible_columns_names
  end
  form_columns ||= current_form_columns(record, scope, subform_controller)
  if force || (form_columns && column.update_columns && (column.update_columns & form_columns).present?)
    url_params = params_for(:action => 'render_field', :column => column.name, :id => record.to_param)
    if nested? && scope
      url_params[:nested] = url_params.slice(:parent_scaffold, :association, nested.param_name)
      url_params = url_params.except(:parent_scaffold, :association, nested.param_name)
    end
    if scope
      url_params[:parent_controller] ||= url_params[:controller].gsub(%r{^/}, '')
      url_params[:controller] = subform_controller.controller_path
      url_params[:scope] = scope
      url_params[:parent_id] = params[:parent_id] || params[:id]
    end

    options[:class] = "#{options[:class]} update_form".strip
    options['data-update_url'] = url_for(url_params)
    options['data-update_send_form'] = column.send_form_on_update_column
    options['data-update_send_form_selector'] = column.options[:send_form_selector] if column.options[:send_form_selector]
  end
  options
end