Class: Releaf::Builders::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
Base, Associations, Fields, Label
Defined in:
app/builders/releaf/builders/form_builder.rb

Defined Under Namespace

Modules: AssociatedSetField, Associations, BooleanFields, DateFields, Fields, FileFields, I18nFields, Label, NumberFields, RichtextFields, TextFields

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Associations

#association_reflector, #field_type_add_nested, #field_type_remove_nested, #reflect_on_association, #relation_name, #releaf_association_fields, #releaf_belongs_to_association, #releaf_has_many_association, #releaf_has_many_association_attributes, #releaf_has_many_association_body, #releaf_has_many_association_field, #releaf_has_many_association_fields, #releaf_has_many_association_footer, #releaf_has_many_association_header, #releaf_has_one_association, #releaf_has_one_or_belongs_to_association, #releaf_item_field, #releaf_item_field_choices, #releaf_item_field_collection

Methods included from BooleanFields

#releaf_boolean_field

Methods included from FileFields

#releaf_file_field, #releaf_file_remove_button, #releaf_image_field

Methods included from NumberFields

#releaf_number_field

Methods included from RichtextFields

#releaf_richtext_field, #richtext_input_attributes, #richtext_options

Methods included from I18nFields

#default_locale, #locales, #localization_switch, #localized_field, #releaf_link_i18n_field, #releaf_richtext_i18n_field, #releaf_text_i18n_field, #releaf_textarea_i18n_field

Methods included from DateFields

#date_or_time_field, #date_or_time_field_input_attributes, #releaf_date_field, #releaf_datetime_field, #releaf_time_field

Methods included from AssociatedSetField

#releaf_associated_set_content, #releaf_associated_set_field, #releaf_associated_set_item

Methods included from TextFields

#releaf_email_field, #releaf_link_field, #releaf_password_field, #releaf_text_field, #releaf_textarea_field

Methods included from Label

#label_attributes, #label_text, #releaf_label

Methods included from Base

#default_translation_scope, #html_escape, #icon, #layout_settings, #locale_options, #resource_title, #safe_join, #t, #tag, #template_variable, #translate_locale, #wrapper

Instance Attribute Details

#templateObject

Returns the value of attribute template


6
7
8
# File 'app/builders/releaf/builders/form_builder.rb', line 6

def template
  @template
end

Instance Method Details

#association_collection(reflector) ⇒ Object


106
107
108
# File 'app/builders/releaf/builders/form_builder.rb', line 106

def association_collection(reflector)
  object.send(reflector.name)
end

#field(name, attributes, options, &block) ⇒ Object


85
86
87
# File 'app/builders/releaf/builders/form_builder.rb', line 85

def field(name, attributes, options, &block)
  tag(:div, field_attributes(name, attributes, options), nil, nil, &block)
end

#field_attributes(name, attributes, options) ⇒ Object


89
90
91
92
93
94
95
96
# File 'app/builders/releaf/builders/form_builder.rb', line 89

def field_attributes(name, attributes, options)
  type = options.fetch(:field, {}).fetch(:type, nil)

  classes = ["field", "type-#{type}"]
  classes << "i18n" if options.key? :i18n

  merge_attributes({class: classes, data: {name: name.to_s}}, attributes)
end

#field_namesObject


8
9
10
# File 'app/builders/releaf/builders/form_builder.rb', line 8

def field_names
  resource_fields.values
end

#field_render_method_name(name) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/builders/releaf/builders/form_builder.rb', line 16

def field_render_method_name(name)
  parts = [name]

  builder = self
  until builder.options[:parent_builder].nil? do
    parts << builder.options[:relation_name] if builder.options[:relation_name]
    builder = builder.options[:parent_builder]
  end

  parts << "render"
  parts.reverse.join("_")
end

#field_type_method(name) ⇒ Object


69
70
71
# File 'app/builders/releaf/builders/form_builder.rb', line 69

def field_type_method(name)
  Releaf::Builders::Utilities::ResolveAttributeFieldMethodName.call(object: object, attribute_name: name.to_s)
end

#input_attributes(_name, attributes, _options) ⇒ Object


98
99
100
# File 'app/builders/releaf/builders/form_builder.rb', line 98

def input_attributes(_name, attributes, _options)
  attributes
end

#input_wrapper_with_label(name, input_content, label: {}, field: {}, options: {}) ⇒ Object


78
79
80
81
82
83
# File 'app/builders/releaf/builders/form_builder.rb', line 78

def input_wrapper_with_label(name, input_content, label: {}, field: {}, options: {})
  field(name, field, options) do
    input_content = safe_join{[input_content, yield.to_s]} if block_given?
    releaf_label(name, label, options) << wrapper(input_content, class: "value")
  end
end

#normalize_field(field, subfields) ⇒ Object


41
42
43
44
45
46
47
# File 'app/builders/releaf/builders/form_builder.rb', line 41

def normalize_field(field, subfields)
  {
    render_method: field_render_method_name(field),
    field: field,
    subfields: subfields
  }
end

#normalize_fields(fields) ⇒ Object


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

def normalize_fields(fields)
  fields.flatten.map do |item|
    if item.is_a? Hash
      item.each_pair.map do |(association, subfields)|
        normalize_field(association, subfields)
      end
    else
      normalize_field(item, nil)
    end
  end.flatten
end

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


73
74
75
76
# File 'app/builders/releaf/builders/form_builder.rb', line 73

def releaf_field(name, input: {}, label: {}, field: {}, options: {}, &block)
  method_name = field_type_method(name)
  send(method_name, name, input: input, label: label, field: field, options: options, &block)
end

#releaf_fields(*fields) ⇒ Object


49
50
51
52
53
# File 'app/builders/releaf/builders/form_builder.rb', line 49

def releaf_fields(*fields)
  safe_join do
    normalize_fields(fields).collect{|field_option| render_field_by_options(field_option) }
  end
end

#render_field_by_options(options) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/builders/releaf/builders/form_builder.rb', line 55

def render_field_by_options(options)
  if respond_to? options[:render_method]
    send(options[:render_method])
  else
    reflection = reflect_on_association(options[:field])

    if reflection
      releaf_association_fields(reflection, options[:subfields])
    else
      releaf_field(options[:field])
    end
  end
end

#resource_fieldsObject


12
13
14
# File 'app/builders/releaf/builders/form_builder.rb', line 12

def resource_fields
  Releaf::ResourceFields.new(object.class)
end

#sortable_column_nameObject


110
111
112
# File 'app/builders/releaf/builders/form_builder.rb', line 110

def sortable_column_name
  'item_position'
end

#translate_attribute(attribute) ⇒ Object


102
103
104
# File 'app/builders/releaf/builders/form_builder.rb', line 102

def translate_attribute(attribute)
  object.class.human_attribute_name(attribute)
end