Class: Phlexi::Form::Builder

Inherits:
Phlexi::Field::Builder
  • Object
show all
Includes:
Phlexi::Field::Common::Tokens, HTML::Behaviour, Options::Autofocus, Options::Choices, Options::Disabled, Options::Errors, Options::Hints, Options::InferredTypes, Options::Length, Options::Limit, Options::Max, Options::Min, Options::Pattern, Options::Readonly, Options::Required, Options::Step, Options::Validators
Defined in:
lib/phlexi/form/builder.rb

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.



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

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

  @input_attributes = input_attributes
end

Instance Attribute Details

#input_attributesObject (readonly)

Returns the value of attribute input_attributes.



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

def input_attributes
  @input_attributes
end

Instance Method Details

#belongs_to_tagObject



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

def belongs_to_tag(**, &)
  create_component(Components::BelongsTo, :belongs_to, **, &)
end

#boolean_tagObject



109
110
111
# File 'lib/phlexi/form/builder.rb', line 109

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:



105
106
107
# File 'lib/phlexi/form/builder.rb', line 105

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:



131
132
133
# File 'lib/phlexi/form/builder.rb', line 131

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:



149
150
151
# File 'lib/phlexi/form/builder.rb', line 149

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

#color_tagObject



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

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

#date_tagObject



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

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

#datetime_local_tagObject Also known as: datetime_tag



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

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

#email_tagObject



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

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



113
114
115
# File 'lib/phlexi/form/builder.rb', line 113

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_tagObject Also known as: has_and_belongs_to_many_tag



189
190
191
# File 'lib/phlexi/form/builder.rb', line 189

def has_many_tag(**, &)
  create_component(Components::HasMany, :has_many, **, &)
end

#has_one_tagObject

Raises:

  • (NotImplementedError)


185
186
187
# File 'lib/phlexi/form/builder.rb', line 185

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

#hidden_input_tagObject Also known as: hidden_tag



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

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



162
163
164
165
# File 'lib/phlexi/form/builder.rb', line 162

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:



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

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



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

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

#password_tagObject



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

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

#phone_tagObject



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

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

#polymorphic_belongs_to_tagObject

Raises:

  • (NotImplementedError)


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

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 Also known as: radio_tag

Creates a radio button tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the radio button.

Returns:



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

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

#range_tagComponents::Checkbox

Creates a checkbox tag for the field.

Parameters:

  • attributes (Hash)

    Additional attributes for the checkbox.

Returns:



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

def range_tag(**, &)
  create_component(Components::Range, :range, **, &)
end

#search_tagObject



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

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:



171
172
173
# File 'lib/phlexi/form/builder.rb', line 171

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

#string_tagObject



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

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:



157
158
159
# File 'lib/phlexi/form/builder.rb', line 157

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

#time_tagObject



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

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

#url_tagObject



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

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