Class: Aform::Form
- Inherits:
-
Object
- Object
- Aform::Form
- Defined in:
- lib/aform/form.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#form_model ⇒ Object
readonly
Returns the value of attribute form_model.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#nested_forms ⇒ Object
readonly
Returns the value of attribute nested_forms.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #errors ⇒ Object
-
#initialize(model, attributes, parent = nil, opts = {}) ⇒ Form
constructor
A new instance of Form.
- #invalid? ⇒ Boolean
- #save ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(model, attributes, parent = nil, opts = {}) ⇒ Form
Returns a new instance of Form.
7 8 9 10 11 12 13 14 |
# File 'lib/aform/form.rb', line 7 def initialize(model, attributes, parent = nil, opts = {}) @opts = opts @attributes = attributes @model = model @parent = parent assign_opts_instances initialize_nested end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/aform/form.rb', line 5 def attributes @attributes end |
#form_model ⇒ Object (readonly)
Returns the value of attribute form_model.
5 6 7 |
# File 'lib/aform/form.rb', line 5 def form_model @form_model end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/aform/form.rb', line 5 def model @model end |
#nested_forms ⇒ Object (readonly)
Returns the value of attribute nested_forms.
5 6 7 |
# File 'lib/aform/form.rb', line 5 def nested_forms @nested_forms end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/aform/form.rb', line 5 def parent @parent end |
Class Method Details
.method_missing(meth, *args, &block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aform/form.rb', line 54 def method_missing(meth, *args, &block) if meth.to_s.start_with?("validate") = {method: meth, options: args} .merge!(block: block) if block_given? self.validations ||= [] self.validations << elsif meth == :has_many define_nested_form(args, &block) else super end end |
.param(*args) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/aform/form.rb', line 44 def param(*args) self.params ||= [] = args. elements = args.map do |a| field = {field: a} .present? ? field.merge({options: }) : field end self.params += elements end |
.primary_key(key) ⇒ Object
40 41 42 |
# File 'lib/aform/form.rb', line 40 def primary_key(key) self.pkey = key end |
Instance Method Details
#errors ⇒ Object
35 36 37 |
# File 'lib/aform/form.rb', line 35 def errors @errors. end |
#invalid? ⇒ Boolean
27 28 29 |
# File 'lib/aform/form.rb', line 27 def invalid? !valid? end |
#save ⇒ Object
31 32 33 |
# File 'lib/aform/form.rb', line 31 def save self.valid? && @form_saver.save end |
#valid? ⇒ Boolean
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/aform/form.rb', line 16 def valid? if @nested_forms main = @form_model.valid? #all? don't invoike method on each element nested = @nested_forms.values.flatten.map(&:valid?).all? main && nested else @form_model.valid? end end |