Class: Phlexi::Form::Base
Overview
A form component for building flexible and customizable forms.
Defined Under Namespace
Classes: Builder, Errors, Namespace
Instance Attribute Summary collapse
-
#key ⇒ Symbol
readonly
The form’s key, derived from the record or explicitly set.
-
#object ⇒ ActiveModel::Model?
readonly
The form’s associated object.
Class Method Summary collapse
Instance Method Summary collapse
- #error_message ⇒ Object
- #errors ⇒ Object
- #extract_input(params, view_context: nil) ⇒ Object
- #form_errors ⇒ Object
-
#form_template ⇒ void
Executes the form’s content block.
-
#initialize(record, action: nil, method: nil, attributes: {}, **options) ⇒ Base
constructor
Initializes a new Form instance.
-
#view_template ⇒ void
Renders the form template.
Constructor Details
#initialize(record, action: nil, method: nil, attributes: {}, **options) ⇒ Base
Initializes a new Form instance.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/phlexi/form/base.rb', line 49 def initialize(record, action: nil, method: nil, attributes: {}, **) @form_action = action @form_method = method @dom_id = attributes.delete(:id) @attributes = attributes @namespace_klass = .delete(:namespace_klass) || default_namespace_klass @builder_klass = .delete(:builder_klass) || default_builder_klass @options = initialize_object_and_key(record) initialize_namespace initialize_attributes super() end |
Instance Attribute Details
#key ⇒ Symbol (readonly)
The form’s key, derived from the record or explicitly set
20 21 22 |
# File 'lib/phlexi/form/base.rb', line 20 def key @key end |
#object ⇒ ActiveModel::Model? (readonly)
The form’s associated object
20 21 22 |
# File 'lib/phlexi/form/base.rb', line 20 def object @object end |
Class Method Details
.inline(&block) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/phlexi/form/base.rb', line 27 def self.inline(*, **, &block) raise ArgumentError, "block is required" unless block new(*, **) do |f| f.instance_exec(&block) end end |
Instance Method Details
#error_message ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/phlexi/form/base.rb', line 81 def lookups = [] lookups << :"#{key}" lookups << :default_message lookups << .fetch(:error_message) { "Please review the problems below" } I18n.t(lookups.shift, scope: :"phlexi_form.error_notification", default: lookups) end |
#errors ⇒ Object
89 90 91 |
# File 'lib/phlexi/form/base.rb', line 89 def errors @errors ||= .fetch(:errors) { object.respond_to?(:errors) && object.errors. } end |
#extract_input(params, view_context: nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/phlexi/form/base.rb', line 101 def extract_input(params, view_context: nil) params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h) params = {} unless params.is_a?(Hash) unless @_state raise ArgumentError, "view_context is required if Form has not been rendered" unless view_context view_context.render(self) end @namespace.extract_input(params) end |
#form_errors ⇒ Object
75 76 77 78 79 |
# File 'lib/phlexi/form/base.rb', line 75 def form_errors return unless errors.present? render self.class::Errors.new , errors end |
#form_template ⇒ void
This method returns an undefined value.
Executes the form’s content block. Override this in subclasses to defie a static form.
97 98 99 |
# File 'lib/phlexi/form/base.rb', line 97 def form_template yield if block_given? end |
#view_template ⇒ void
This method returns an undefined value.
Renders the form template.
67 68 69 70 71 72 73 |
# File 'lib/phlexi/form/base.rb', line 67 def view_template(&) captured = capture { form_template(&) } form_tag do form_errors raw(safe(captured)) end end |