Module: Formality
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/formality.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
-
#assign(new_attributes) ⇒ Object
Assigns a hash of attributes to the form.
-
#attribute?(name) ⇒ Boolean
Returns a Boolean that answers the question: Is this ‘name` a declared attribute?.
-
#attribute_names ⇒ Object
Returns an Array of attribute names (Strings).
-
#attributes ⇒ Object
Returns a HashWithIndifferentAccess of all the defined attributes and their values.
-
#invalid ⇒ Object
Same as :valid, but in reverse: only yields to the block if the form is invalid.
-
#nested_forms_valid?(context) ⇒ Boolean
If there are nested forms, call :valid? on them.
-
#persisted? ⇒ Boolean
:form_for calls :persisted? on the object it receives to determine whether to :post or :put.
-
#to_key ⇒ Object
More ActiveModel compliance shenanigans.
- #to_param ⇒ Object
- #to_partial_path ⇒ Object
-
#valid ⇒ Object
Yields to its block if the form is valid.
-
#valid?(context = nil) ⇒ Boolean
A Formality form object is valid if its attributes validate and all of its children are valid.
Instance Method Details
#assign(new_attributes) ⇒ Object
Assigns a hash of attributes to the form. Only assigns values if the key for that value is a declared attribute. It silently ignores non-declared keys.
112 113 114 115 116 117 118 119 |
# File 'lib/formality.rb', line 112 def assign(new_attributes) new_attributes.each do |name, value| next unless attribute?(name) send("#{name}=", value) end self.id = new_attributes[:id] self end |
#attribute?(name) ⇒ Boolean
Returns a Boolean that answers the question: Is this ‘name` a declared attribute?
123 124 125 |
# File 'lib/formality.rb', line 123 def attribute?(name) attribute_names.include?(name.to_s) end |
#attribute_names ⇒ Object
Returns an Array of attribute names (Strings).
95 96 97 |
# File 'lib/formality.rb', line 95 def attribute_names self.class.attributes.to_a end |
#attributes ⇒ Object
Returns a HashWithIndifferentAccess of all the defined attributes and their values.
101 102 103 104 105 106 107 |
# File 'lib/formality.rb', line 101 def attributes hash = ActiveSupport::HashWithIndifferentAccess.new attribute_names.each_with_object({}) do |name| hash[name] = send(name) end hash end |
#invalid ⇒ Object
Same as :valid, but in reverse: only yields to the block if the form is invalid.
183 |
# File 'lib/formality.rb', line 183 def invalid; yield if invalid? end |
#nested_forms_valid?(context) ⇒ Boolean
If there are nested forms, call :valid? on them.
278 279 280 281 282 283 |
# File 'lib/formality.rb', line 278 def nested_forms_valid?(context) self.class.nested_forms.all? do |name| nested = Array(send(name)) nested.all? { |form| form.valid?(context) } end end |
#persisted? ⇒ Boolean
:form_for calls :persisted? on the object it receives to determine whether to :post or :put.
We assume we’re persisted (i.e. editing an object) if we have an id.
50 51 52 |
# File 'lib/formality.rb', line 50 def persisted? id.present? end |
#to_key ⇒ Object
More ActiveModel compliance shenanigans.
33 |
# File 'lib/formality.rb', line 33 def to_key; end |
#to_param ⇒ Object
34 |
# File 'lib/formality.rb', line 34 def to_param; end |
#to_partial_path ⇒ Object
35 |
# File 'lib/formality.rb', line 35 def to_partial_path; "" end |
#valid ⇒ Object
Yields to its block if the form is valid.
179 |
# File 'lib/formality.rb', line 179 def valid; yield if valid? end |
#valid?(context = nil) ⇒ Boolean
A Formality form object is valid if its attributes validate and all of its children are valid.
273 274 275 |
# File 'lib/formality.rb', line 273 def valid?(context=nil) nested_forms_valid?(context) && super(context) end |