Module: Cardiac::Model::Validations
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Validations
- Included in:
- Base
- Defined in:
- lib/cardiac/model/validations.rb
Overview
Cardiac::Model finder methods. Most of this has been “borrowed” from ActiveRecord.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#assign_remote_errors(data, options = {}) ⇒ Object
(also: #remote_errors=)
Stores the errors returned by the remote, after performing any unpacking/decoding.
-
#remote_errors ⇒ Object
Like ActiveModel::Validations#errors, but used for remote errors.
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
-
#valid?(context = nil) ⇒ Boolean
Runs all the validations within the specified context.
Instance Method Details
#assign_remote_errors(data, options = {}) ⇒ Object Also known as: remote_errors=
Stores the errors returned by the remote, after performing any unpacking/decoding. To customize the options used to add the error:
<code>assign_remote_errors(data,options: {foo: :bar})</code>
63 64 65 66 67 68 69 |
# File 'lib/cardiac/model/validations.rb', line 63 def assign_remote_errors(data,={}) decode_remote_errors(data,).each do |key,values| Array.wrap(values).each do |value| remote_errors.add key, value.to_s, *([[:options]] if Hash===[:options]) end end end |
#remote_errors ⇒ Object
Like ActiveModel::Validations#errors, but used for remote errors.
74 75 76 |
# File 'lib/cardiac/model/validations.rb', line 74 def remote_errors @remote_errors ||= (self.class.remote_errors_class || ::ActiveModel::Errors).new(self) end |
#save(options = {}) ⇒ Object
33 34 35 |
# File 'lib/cardiac/model/validations.rb', line 33 def save(={}) perform_validations() && super && remote_errors.empty? end |
#save!(options = {}) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/cardiac/model/validations.rb', line 37 def save!(={}) raise RecordInvalid.new(self) unless perform_validations() super ensure raise RecordInvalid.new(self) unless remote_errors.empty? end |
#valid?(context = nil) ⇒ Boolean
Runs all the validations within the specified context. Returns true
if no errors are found, false
otherwise.
If the argument is false
(default is nil
), the context is set to :create
if new_record?
is true
, and to :update
if it is not.
Validations with no :on
option will run no matter the context. Validations with some :on
option will only run in the specified context.
52 53 54 55 56 |
# File 'lib/cardiac/model/validations.rb', line 52 def valid?(context = nil) context ||= (new_record? ? :create : :update) output = super(context) errors.empty? && output end |