Class: Practical::Views::FormBuilders::Base

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ElementHelper
Defined in:
app/lib/practical/views/form_builders/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ElementHelper

#grab, #mix

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'app/lib/practical/views/form_builders/base.rb', line 5

def object
  @object
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'app/lib/practical/views/form_builders/base.rb', line 5

def template
  @template
end

Instance Method Details

#button_component(options = {}, &block) ⇒ Object



66
67
68
# File 'app/lib/practical/views/form_builders/base.rb', line 66

def button_component(options = {}, &block)
  template.render(Practical::Views::ButtonComponent.new(**options), &block)
end

#check_box_collection(field_method:, options:, collection_check_boxes_options: {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/lib/practical/views/form_builders/base.rb', line 70

def check_box_collection(field_method:, options:, collection_check_boxes_options: {})
  collection_check_boxes(field_method,
                         options,
                         :value,
                         :title,
                         collection_check_boxes_options
  ) do |collection_builder|
    collection_builder.label(class: "wa-flank") do
      template.safe_join([
        collection_builder.checkbox,
        template.render(Practical::Views::Form::OptionLabelComponent.new) do |component|
          component.with_title do
            template.icon_text(
              icon: collection_builder.object.icon,
              text: collection_builder.object.title
            )
          end

          component.with_description{ collection_builder.object.description }
        end
      ])
    end
  end
end

#errors_for(object_method) ⇒ Object



144
145
146
147
# File 'app/lib/practical/views/form_builders/base.rb', line 144

def errors_for(object_method)
  return unless object.present? && object_method.present?
  object.errors.group_by_attribute[object_method]
end

#fallback_error_section(blurb_key: :"practical_framework.forms.generic_error_blurb", id:, options: {}) ⇒ Object



136
137
138
139
140
141
142
# File 'app/lib/practical/views/form_builders/base.rb', line 136

def fallback_error_section(blurb_key: :"practical_framework.forms.generic_error_blurb", id:, options: {})
  template.render Practical::Views::Form::FallbackErrorsSectionComponent.new(
    f: self,
    blurb: template.translate(blurb_key, raise: true),
    options: options
  )
end

#field_errors(object_method, options = {}) ⇒ Object



128
129
130
131
132
133
134
# File 'app/lib/practical/views/form_builders/base.rb', line 128

def field_errors(object_method, options = {})
  template.render Practical::Views::Form::FieldErrorsComponent.new(
    f: self,
    object_method: object_method,
    options: options
  )
end

#field_errors_id(object_method) ⇒ Object



149
150
151
# File 'app/lib/practical/views/form_builders/base.rb', line 149

def field_errors_id(object_method)
  field_id(object_method, :errors)
end

#field_title(icon:, title:) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'app/lib/practical/views/form_builders/base.rb', line 27

def field_title(icon:, title:)
  template.render(Practical::Views::Form::FieldTitleComponent.new) do |component|
    component.with_icon {
      template.render icon
    }

    title
  end
end

#fieldset(options = {}, &block) ⇒ Object



7
8
9
10
# File 'app/lib/practical/views/form_builders/base.rb', line 7

def fieldset(options = {}, &block)
  finalized_options = mix({class: 'wa-stack'}, options)
  template.tag.fieldset(**finalized_options, &block)
end

#fieldset_title(icon:, title:) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'app/lib/practical/views/form_builders/base.rb', line 17

def fieldset_title(icon:, title:)
  template.render(Practical::Views::Form::FieldsetTitleComponent.new) do |component|
    component.with_icon {
      template.render icon
    }

    title
  end
end

#input_component(object_method, label_options: {}, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'app/lib/practical/views/form_builders/base.rb', line 37

def input_component(object_method, label_options: {}, &block)
  template.render(Practical::Views::Form::InputComponent.new(
    f: self,
    object_method: object_method,
    label_options: label_options
  ), &block)
end

#legend(options = {}, &block) ⇒ Object



12
13
14
15
# File 'app/lib/practical/views/form_builders/base.rb', line 12

def legend(options = {}, &block)
  finalized_options = mix({}, options)
  template.tag.legend(**finalized_options, &block)
end

#practical_editor_field(object_method:, direct_upload_url:, hidden_input_options: {}, options: {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/lib/practical/views/form_builders/base.rb', line 45

def practical_editor_field(object_method:, direct_upload_url:, hidden_input_options: {}, options: {})
  aria_describedby_id = field_errors_id(object_method)
  input_id = field_id(object_method)

  finalized_hidden_input_options = mix({
    "aria-describedby": aria_describedby_id,
    "aria-invalid": errors_for(object_method).present?,
    "id": input_id,
  }, hidden_input_options)

  template.safe_join([
    self.hidden_field(object_method, **finalized_hidden_input_options),
    template.render(Practical::Views::Form::PracticalEditorComponent.new(
      input_id: input_id,
      aria_describedby_id: aria_describedby_id,
      direct_upload_url: direct_upload_url,
      options: options
    ))
  ])
end

#radio_collection(field_method:, options:, collection_check_boxes_options: {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/lib/practical/views/form_builders/base.rb', line 95

def radio_collection(field_method:, options:, collection_check_boxes_options: {})
  collection_radio_buttons(field_method,
                         options,
                         :value,
                         :title,
                         collection_check_boxes_options
  ) do |collection_builder|
    collection_builder.label(class: "wa-flank") do
      template.safe_join([
        collection_builder.radio_button,
        template.render(Practical::Views::Form::OptionLabelComponent.new) do |component|
          component.with_title do
            template.icon_text(
              icon: collection_builder.object.icon,
              text: collection_builder.object.title
            )
          end

          component.with_description{ collection_builder.object.description }
        end
      ])
    end
  end
end

#required_radio_collection_wrapper(object_method, options = {}, &block) ⇒ Object



120
121
122
123
124
125
126
# File 'app/lib/practical/views/form_builders/base.rb', line 120

def required_radio_collection_wrapper(object_method, options = {}, &block)
  template.render(Practical::Views::Form::RequiredRadioCollectionWrapperComponent.new(
    f: self,
    object_method: object_method,
    options: options
  ), &block)
end