Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/record_collection/rails/form_options_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_collection_ids(content) ⇒ Object

This method adds the collection ids to the form if not already added. For the lazy peaple that forget to add them manually and use an optional tag



6
7
8
9
# File 'lib/record_collection/rails/form_options_helper.rb', line 6

def add_collection_ids(content)
  return content if @collection_ids_already_added
  collection_ids + content
end

#collection_idsObject

Return inputs for the collection ids



12
13
14
15
16
# File 'lib/record_collection/rails/form_options_helper.rb', line 12

def collection_ids
  @collection_ids_already_added = true
  return "".html_safe unless object.respond_to?(:map)
  object.map{|record| @template.hidden_field_tag('ids[]', record.id, id: nil) }.join.html_safe
end

#get_data_attributes(attr, options = {}) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/record_collection/rails/form_options_helper.rb', line 44

def get_data_attributes(attr, options = {})
  one = true
  one = object.one? if object.is_a? RecordCollection::Base
  attrs = {attribute: attr, one: one}
  attrs[:disabled] = true if options[:disabled]
  attrs
end

#get_optional_classes(attr, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/record_collection/rails/form_options_helper.rb', line 52

def get_optional_classes(attr, options = {})
  active = false
  if object.respond_to?(:uniform_collection_attribute)
    uniform_value = object.uniform_collection_attribute(attr, set_if_nil: true)
    # The field is active when the uniform value is not nil
    active = !uniform_value.nil?
  end
  active = true unless object.public_send(attr).nil? # Activate if collection attribute is not nil
  classes = [options[:base_class] || 'optional-input', 'optional-attribute-container', attr]
  classes << (active ? 'active' : 'inactive')
  classes << 'disabled' if options[:disabled]
  if object.is_a? RecordCollection::Base
    classes << 'one' if object.one?
  else
    # Assume normal record, this always behaves like one
    classes << 'one'
  end
  classes << 'error' if object.errors[attr].present?
  classes
end

#optional_boolean(attr, options = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/record_collection/rails/form_options_helper.rb', line 18

def optional_boolean(attr, options = {})
  classes = get_optional_classes(attr, options.merge(base_class: 'optional-boolean'))
  label_tag = label(attr, options[:label])
  check_box_tag = check_box(attr, options)
  add_collection_ids @template.(:div, label_tag + check_box_tag , class: classes, data: get_data_attributes(attr, options))
end

#optional_check_box(attr, options = {}) ⇒ Object



25
26
27
# File 'lib/record_collection/rails/form_options_helper.rb', line 25

def optional_check_box(attr, options = {})
  optional_boolean(attr, options)
end

#optional_input(attr, options = {}) ⇒ Object



29
30
31
32
# File 'lib/record_collection/rails/form_options_helper.rb', line 29

def optional_input(attr, options = {})
  classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-input'))
  add_collection_ids @template.(:div, input(attr, options), class: classes, data: get_data_attributes(attr))
end

#optional_text_area(attr, options = {}) ⇒ Object



39
40
41
42
# File 'lib/record_collection/rails/form_options_helper.rb', line 39

def optional_text_area(attr, options={})
  classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-text-area'))
  add_collection_ids @template.(:div, label(attr, options[:label]) + text_area(attr, options), class: classes, data: get_data_attributes(attr))
end

#optional_text_field(attr, options = {}) ⇒ Object



34
35
36
37
# File 'lib/record_collection/rails/form_options_helper.rb', line 34

def optional_text_field(attr, options={})
  classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-text-field'))
  add_collection_ids @template.(:div, label(attr, options[:label]) + text_field(attr, options), class: classes, data: get_data_attributes(attr))
end