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
- .human_attribute_name(attr, options = {}) ⇒ 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.
- #persisted? ⇒ Boolean
- #submit ⇒ Object
- #to_param ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(models: {}, controller:) ⇒ Form
58 59 60 61 62 63 64 65 66 |
# File 'lib/formwandler/form.rb', line 58 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.
53 54 55 |
# File 'lib/formwandler/form.rb', line 53 def model_save_order @model_save_order end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
56 57 58 |
# File 'lib/formwandler/form.rb', line 56 def controller @controller end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
56 57 58 |
# File 'lib/formwandler/form.rb', line 56 def models @models end |
Class Method Details
.attribute_accessor(name) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/formwandler/form.rb', line 39 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
31 32 33 34 35 36 37 |
# File 'lib/formwandler/form.rb', line 31 def field(name, opts = {}, &block) field_definition = field_definitions[name] ||= FieldDefinition.new(name) field_definition.configure(opts) field_definition.instance_exec(&block) if block_given? attribute_accessor(name) end |
.field_definitions ⇒ Object
27 28 29 |
# File 'lib/formwandler/form.rb', line 27 def field_definitions @field_definitions ||= {} end |
.human_attribute_name(attr, options = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/formwandler/form.rb', line 17 def human_attribute_name(attr, = {}) field_definition = @field_definitions.fetch(attr) { nil } if [:default].nil? && field_definition && field_definition.model_class not_found = '__formwandler_not_found__' model_translation = field_definition.model_class.human_attribute_name(field_definition.source, default: not_found) [:default] = model_translation unless model_translation == not_found end super(attr, ) 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
49 50 51 |
# File 'lib/formwandler/form.rb', line 49 def save_order(*model_names) @model_save_order = model_names end |
Instance Method Details
#field(name) ⇒ Object
76 77 78 |
# File 'lib/formwandler/form.rb', line 76 def field(name) @fields.fetch(name) end |
#fields(*names) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/formwandler/form.rb', line 80 def fields(*names) if names.any? @fields.fetch_values(*names) else @fields.values end end |
#fields_for_model(model) ⇒ Object
88 89 90 |
# File 'lib/formwandler/form.rb', line 88 def fields_for_model(model) fields.select { |field| field.model == model } end |
#persisted? ⇒ Boolean
68 69 70 |
# File 'lib/formwandler/form.rb', line 68 def persisted? models.values.first&.persisted? || false end |
#submit ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/formwandler/form.rb', line 98 def submit if valid? ActiveRecord::Base.transaction do save_models! end load_results else false end end |
#to_param ⇒ Object
72 73 74 |
# File 'lib/formwandler/form.rb', line 72 def to_param models.values.first.to_param end |
#valid? ⇒ Boolean
92 93 94 95 96 |
# File 'lib/formwandler/form.rb', line 92 def valid? form_valid = super models_valid = models_valid? form_valid && models_valid end |