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

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,options={})
  decode_remote_errors(data,options).each do |key,values|
    Array.wrap(values).each do |value|
      remote_errors.add key, value.to_s, *([options[:options]] if Hash===options[:options])
    end
  end
end

#remote_errorsObject

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(options={})
  perform_validations(options) && super && remote_errors.empty?
end

#save!(options = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/cardiac/model/validations.rb', line 37

def save!(options={})
  raise RecordInvalid.new(self) unless perform_validations(options)
  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.

Returns:

  • (Boolean)


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