Class: Phlexi::Form::Builder

Overview

Builder class is responsible for building form fields with various options and components.

Direct Known Subclasses

Phlexi::Form::Base::Builder

Defined Under Namespace

Classes: FieldCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Options::Step

#step

Methods included from Options::Limit

#limit

Methods included from Options::Pattern

#pattern

Methods included from Options::Min

#min

Methods included from Options::Max

#max

Methods included from Options::Length

#maxlength, #minlength

Methods included from Options::Readonly

#readonly!, #readonly?

Methods included from Options::Disabled

#disabled!, #disabled?

Methods included from Options::Autofocus

#focused!, #focused?

Methods included from Options::Required

#required!, #required?

Methods included from Options::Hints

#show_hint?

Methods included from Options::Choices

#choices

Methods included from Options::Errors

#can_show_errors?, #custom_error, #error, #full_error, #has_errors?, #object_valid?, #show_errors?, #valid?

Constructor Details

#initialize(input_attributes: {}) ⇒ Builder

Initializes a new Builder instance.

Parameters:

  • key (Symbol, String)

    The key for the field.

  • parent (Structure::Namespace)

    The parent object.

  • object (Object, nil)

    The associated object.

  • value (Object)

    The initial value for the field.

  • input_attributes (Hash) (defaults to: {})

    Default attributes to apply to input fields.

  • options (Hash)

    Additional options for the field.



36
37
38
39
40
# File 'lib/phlexi/form/builder.rb', line 36

def initialize(*, input_attributes: {}, **)
  super(*, **)

  @input_attributes = input_attributes
end

Instance Attribute Details

#input_attributesObject (readonly)

Returns the value of attribute input_attributes.



26
27
28
# File 'lib/phlexi/form/builder.rb', line 26

def input_attributes
  @input_attributes
end

Instance Method Details

#belongs_to_tag(**options) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/phlexi/form/builder.rb', line 164

def belongs_to_tag(**options, &)
  options.fetch(:input_param) {
    options[:input_param] = if association_reflection.respond_to?(:options) && association_reflection.options[:foreign_key]
      association_reflection.options[:foreign_key]
    else
      :"#{association_reflection.name}_id"
    end
  }
  select_tag(**options, &)
end

#boolean_tagObject



107
108
109
# File 'lib/phlexi/form/builder.rb', line 107

def boolean_tag(**, &)
  checkbox_tag(**, theme: :boolean, &)
end

#checkbox_tagComponents::Checkbox

Creates a checkbox tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the checkbox.

Returns:



103
104
105
# File 'lib/phlexi/form/builder.rb', line 103

def checkbox_tag(**, &)
  create_component(Components::Checkbox, :checkbox, **, &)
end

#collection_checkboxes_tag {|block| ... } ⇒ Components::CollectionCheckboxes

Creates collection checkboxes for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the collection checkboxes.

Yields:

  • (block)

    The block to be executed for each checkbox.

Returns:



121
122
123
# File 'lib/phlexi/form/builder.rb', line 121

def collection_checkboxes_tag(**, &)
  create_component(Components::CollectionCheckboxes, :collection_checkboxes, **, &)
end

#collection_radio_buttons_tag {|block| ... } ⇒ Components::CollectionRadioButtons

Creates collection radio buttons for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the collection radio buttons.

Yields:

  • (block)

    The block to be executed for each radio button.

Returns:



138
139
140
# File 'lib/phlexi/form/builder.rb', line 138

def collection_radio_buttons_tag(**, &)
  create_component(Components::CollectionRadioButtons, :collection_radio_buttons, **, &)
end

#color_tagObject



83
84
85
# File 'lib/phlexi/form/builder.rb', line 83

def color_tag(**, &)
  input_tag(type: :color, theme: :color, **, &)
end

#date_tagObject



58
59
60
# File 'lib/phlexi/form/builder.rb', line 58

def date_tag(**, &)
  input_tag(type: :date, theme: :date, **, &)
end

#datetime_local_tagObject Also known as: datetime_tag



66
67
68
# File 'lib/phlexi/form/builder.rb', line 66

def datetime_local_tag(**, &)
  input_tag(type: :"datetime-local", theme: :datetime, **, &)
end

#email_tagObject



71
72
73
# File 'lib/phlexi/form/builder.rb', line 71

def email_tag(**, &)
  input_tag(type: :email, theme: :email, **, &)
end

#error_tagComponents::Error

Creates an error tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the error.

Returns:



218
219
220
# File 'lib/phlexi/form/builder.rb', line 218

def error_tag(**, &)
  create_component(Components::Error, :error, **, &)
end

#extract_input(params) ⇒ Object



250
251
252
253
254
# File 'lib/phlexi/form/builder.rb', line 250

def extract_input(params)
  raise "field##{dom.name} did not define an input component" unless @field_input_extractor

  @field_input_extractor.extract_input(params)
end

#file_input_tagObject Also known as: file_tag



111
112
113
# File 'lib/phlexi/form/builder.rb', line 111

def file_input_tag(**, &)
  create_component(Components::FileInput, :file, **, &)
end

#full_error_tagComponents::FullError

Creates a full error tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the full error.

Returns:



226
227
228
# File 'lib/phlexi/form/builder.rb', line 226

def full_error_tag(**, &)
  create_component(Components::FullError, :full_error, **, &)
end

#has_file_input!Object



256
257
258
# File 'lib/phlexi/form/builder.rb', line 256

def has_file_input!
  parent.has_file_input!
end

#has_many_tag(**options) ⇒ Object Also known as: has_and_belongs_to_many_tag



185
186
187
188
189
190
191
# File 'lib/phlexi/form/builder.rb', line 185

def has_many_tag(**options, &)
  options.fetch(:input_param) {
    options[:input_param] = :"#{association_reflection.name.to_s.singularize}_ids"
  }

  select_tag(**options, &)
end

#has_one_tagObject

Raises:

  • (NotImplementedError)


181
182
183
# File 'lib/phlexi/form/builder.rb', line 181

def has_one_tag(**, &)
  raise NotImplementedError, "has_one associations are NOT supported"
end

#hidden_input_tagObject Also known as: hidden_tag



95
96
97
# File 'lib/phlexi/form/builder.rb', line 95

def hidden_input_tag(**, &)
  input_tag(type: :hidden, theme: nil, **, &)
end

#hint_tagComponents::Hint

Creates a hint tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the hint.

Returns:



210
211
212
# File 'lib/phlexi/form/builder.rb', line 210

def hint_tag(**, &)
  create_component(Components::Hint, :hint, **, &)
end

#hstore_tagObject



151
152
153
154
# File 'lib/phlexi/form/builder.rb', line 151

def hstore_tag(**, &)
  @value = @value.to_s.tr("{}", "")
  textarea_tag(**, theme: :hstore, &)
end

#input_array_tagObject



194
195
196
# File 'lib/phlexi/form/builder.rb', line 194

def input_array_tag(**, &)
  create_component(Components::InputArray, :array, **, &)
end

#input_tag(theme: :input) ⇒ Components::Input

Creates an input tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the input.

Returns:



46
47
48
# File 'lib/phlexi/form/builder.rb', line 46

def input_tag(theme: :input, **, &)
  create_component(Components::Input, theme, **, &)
end

#label_tagComponents::Label

Creates a label tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the label.

Returns:



202
203
204
# File 'lib/phlexi/form/builder.rb', line 202

def label_tag(**, &)
  create_component(Components::Label, :label, **, &)
end

#number_tagObject



54
55
56
# File 'lib/phlexi/form/builder.rb', line 54

def number_tag(**, &)
  input_tag(type: :number, theme: :number, **, &)
end

#password_tagObject



75
76
77
# File 'lib/phlexi/form/builder.rb', line 75

def password_tag(**, &)
  input_tag(type: :password, theme: :password, **, &)
end

#phone_tagObject



79
80
81
# File 'lib/phlexi/form/builder.rb', line 79

def phone_tag(**, &)
  input_tag(type: :tel, theme: :phone, **, &)
end

#polymorphic_belongs_to_tagObject

Raises:

  • (NotImplementedError)


175
176
177
178
179
# File 'lib/phlexi/form/builder.rb', line 175

def polymorphic_belongs_to_tag(**, &)
  # TODO: this requires a grouped_select component
  # see: Plutonium::Core::Fields::Inputs::PolymorphicBelongsToAssociationInput
  raise NotImplementedError, "polymorphic belongs_to associations are not YET supported"
end

#radio_button_tagComponents::RadioButton

Creates a radio button tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the radio button.

Returns:



129
130
131
# File 'lib/phlexi/form/builder.rb', line 129

def radio_button_tag(**, &)
  create_component(Components::RadioButton, :radio, **, &)
end

#search_tagObject



91
92
93
# File 'lib/phlexi/form/builder.rb', line 91

def search_tag(**, &)
  input_tag(type: :search, theme: :search, **, &)
end

#select_tagComponents::Select

Creates a select tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the select.

Returns:



160
161
162
# File 'lib/phlexi/form/builder.rb', line 160

def select_tag(**, &)
  create_component(Components::Select, :select, **, &)
end

#string_tagObject



50
51
52
# File 'lib/phlexi/form/builder.rb', line 50

def string_tag(**, &)
  input_tag(type: :text, theme: :string, **, &)
end

#submit_button_tagComponents::SubmitButton

Creates a submit button

Parameters:

  • attributes (Hash)

    Additional attributes for the submit.

Returns:



246
247
248
# File 'lib/phlexi/form/builder.rb', line 246

def submit_button_tag(**, &)
  create_component(Components::SubmitButton, :submit_button, **, &)
end

#textarea_tagComponents::Textarea Also known as: text_tag

Creates a textarea tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the textarea.

Returns:



146
147
148
# File 'lib/phlexi/form/builder.rb', line 146

def textarea_tag(**, &)
  create_component(Components::Textarea, :textarea, **, &)
end

#time_tagObject



62
63
64
# File 'lib/phlexi/form/builder.rb', line 62

def time_tag(**, &)
  input_tag(type: :time, theme: :time, **, &)
end

#url_tagObject



87
88
89
# File 'lib/phlexi/form/builder.rb', line 87

def url_tag(**, &)
  input_tag(type: :url, theme: :url, **, &)
end

#wrapped(inner: {}, **attributes) {|block| ... } ⇒ Components::Wrapper

Wraps the field with additional markup.

Parameters:

  • inner (Hash) (defaults to: {})

    Attributes for the inner wrapper.

  • attributes (Hash)

    Additional attributes for the wrapper.

Yields:

  • (block)

    The block to be executed within the wrapper.

Returns:



236
237
238
239
240
# File 'lib/phlexi/form/builder.rb', line 236

def wrapped(inner: {}, **attributes, &)
  attributes = apply_component_theme(attributes, :wrapper)
  inner = apply_component_theme(inner, :inner_wrapper)
  Components::Wrapper.new(self, inner: inner, **attributes, &)
end