Class: Formwandler::Form
- Inherits:
-
Object
- Object
- Formwandler::Form
- Includes:
- ActiveModel::Model, ActiveModel::Validations::Callbacks
- Defined in:
- lib/formwandler/form.rb
Class Attribute Summary collapse
-
.model_save_order ⇒ Object
readonly
Returns the value of attribute model_save_order.
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Class Method Summary collapse
- .attribute_accessor(name) ⇒ Object
- .field(name, opts = {}, &block) ⇒ Object
- .field_definitions ⇒ Object
- .model_name ⇒ Object
- .save_order(*model_names) ⇒ Object
Instance Method Summary collapse
- #field(name) ⇒ Object
- #fields(*names) ⇒ Object
- #fields_for_model(model) ⇒ Object
-
#initialize(models: {}, controller:) ⇒ Form
constructor
A new instance of Form.
- #models_valid? ⇒ Boolean
- #persisted? ⇒ Boolean
- #submit ⇒ Object
- #to_param ⇒ Object
Constructor Details
#initialize(models: {}, controller:) ⇒ Form
Returns a new instance of Form.
49 50 51 52 53 54 55 56 57 |
# File 'lib/formwandler/form.rb', line 49 def initialize(models: {}, controller:) @models = models.symbolize_keys @controller = controller initialize_fields initialize_models assign_defaults assign_params end |
Class Attribute Details
.model_save_order ⇒ Object (readonly)
Returns the value of attribute model_save_order.
44 45 46 |
# File 'lib/formwandler/form.rb', line 44 def model_save_order @model_save_order end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
47 48 49 |
# File 'lib/formwandler/form.rb', line 47 def controller @controller end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
47 48 49 |
# File 'lib/formwandler/form.rb', line 47 def models @models end |
Class Method Details
.attribute_accessor(name) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/formwandler/form.rb', line 30 def attribute_accessor(name) define_method "#{name}=" do |value| field(name).value = value end define_method name do field(name).value end end |
.field(name, opts = {}, &block) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/formwandler/form.rb', line 21 def field(name, opts = {}, &block) field_definition = FieldDefinition.new(name, opts) field_definitions[name] = field_definition field_definition.instance_exec(&block) if block_given? attribute_accessor(name) end |
.field_definitions ⇒ Object
17 18 19 |
# File 'lib/formwandler/form.rb', line 17 def field_definitions @field_definitions ||= {} end |
.model_name ⇒ Object
13 14 15 |
# File 'lib/formwandler/form.rb', line 13 def model_name ActiveModel::Name.new(self, nil, name.chomp('Form').underscore) end |
.save_order(*model_names) ⇒ Object
40 41 42 |
# File 'lib/formwandler/form.rb', line 40 def save_order(*model_names) @model_save_order = model_names end |
Instance Method Details
#field(name) ⇒ Object
67 68 69 |
# File 'lib/formwandler/form.rb', line 67 def field(name) @fields.fetch(name) end |
#fields(*names) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/formwandler/form.rb', line 71 def fields(*names) if names.any? @fields.fetch_values(*names) else @fields.values end end |
#fields_for_model(model) ⇒ Object
79 80 81 |
# File 'lib/formwandler/form.rb', line 79 def fields_for_model(model) fields.select { |field| field.model == model } end |
#models_valid? ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/formwandler/form.rb', line 83 def models_valid? all_valid = true models.each do |name, model| all_valid = false if model.invalid? next if model.valid? fields_for_model(name).each do |field| model.errors[field.source].each do || errors.add(field.name, ) end end end all_valid end |
#persisted? ⇒ Boolean
59 60 61 |
# File 'lib/formwandler/form.rb', line 59 def persisted? models.values.first&.persisted? || false end |
#submit ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/formwandler/form.rb', line 97 def submit if valid? && models_valid? ActiveRecord::Base.transaction do save_models! end load_results else false end end |
#to_param ⇒ Object
63 64 65 |
# File 'lib/formwandler/form.rb', line 63 def to_param models.values.first.to_param end |