Module: Releaf::Builders::FormBuilder::AssociatedSetField

Included in:
Fields
Defined in:
app/builders/releaf/builders/form_builder/associated_set_field.rb

Instance Method Summary collapse

Instance Method Details

#releaf_associated_set_content(name, options: {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/builders/releaf/builders/form_builder/associated_set_field.rb', line 8

def releaf_associated_set_content(name, options: {})
  association_options = options[:association]
  association = object.send(name)
  key_field = association_options[:field]

  list = []

  association_options[:items].each_pair do|value, label_text|
    item = association.find_by(key_field => value) || association.build(key_field => value)

    list << fields_for(name, item, relation_name: name, builder: self.class) do |builder|
      builder.releaf_associated_set_item(association_options, label_text)
    end
  end

  safe_join do
    list
  end
end

#releaf_associated_set_field(name, label: {}, field: {}, options: {}, &block) ⇒ Object



2
3
4
5
6
# File 'app/builders/releaf/builders/form_builder/associated_set_field.rb', line 2

def releaf_associated_set_field(name, label: {}, field: {}, options: {}, &block)
  options = {field: {type: "associated-set"}}.deep_merge(options)
  content = releaf_associated_set_content(name, options: options)
  input_wrapper_with_label(name, content, label: label, field: field, options: options, &block)
end

#releaf_associated_set_item(association_options, label_text) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'app/builders/releaf/builders/form_builder/associated_set_field.rb', line 28

def releaf_associated_set_item(association_options, label_text)
  wrapper(class: "type-associated-set-item") do
    [hidden_field(:_destroy, value: object.new_record?, class: "destroy"),
     check_box(association_options[:field], {class: "keep", name: "keep"}, (object.send(association_options[:field]) if object.persisted?)),
     label(association_options[:field], label_text),
     hidden_field(association_options[:field])
    ]
  end
end