Sha256: 74acc2dec712d5bb71799289283d53df722031d2ed23dc1847b59f56a1baf32b

Contents?: true

Size: 1.67 KB

Versions: 18

Compression:

Stored size: 1.67 KB

Contents

module ActiveRemote
  module Validations
    extend ActiveSupport::Concern

    # Attempts to save the record like Persistence, but will run
    # validations and return false if the record is invalid
    #
    # Validations can be skipped by passing :validate => false
    #
    # example Save a record
    #   post.save
    #
    # example Save a record, skip validations
    #  post.save(:validate => false)
    #
    def save(options = {})
      perform_validations(options) ? super : false
    end

    # Attempts to save the record like Persistence, but will raise
    # ActiveRemote::RemoteRecordInvalid if the record is not valid
    #
    # Validations can be skipped by passing :validate => false
    #
    # example Save a record, raise and error if invalid
    #   post.save!
    #
    # example Save a record, skip validations
    #  post.save!(:validate => false)
    #
    def save!(options = {})
      perform_validations(options) ? super : raise_validation_error
    end

    # Runs all the validations within the specified context. Returns true if
    # no errors are found, false otherwise.
    #
    # Aliased as validate.
    #
    # example Is the record valid?
    #   post.valid?
    #
    # example Is the record valid for creation?
    #   post.valid?(:create)
    #
    def valid?(context = nil)
      context ||= (new_record? ? :create : :update)
      output = super(context)
      errors.empty? && output
    end

    alias_method :validate, :valid?

  protected

    def raise_validation_error
      fail ActiveRemote::RemoteRecordInvalid, self
    end

    def perform_validations(options = {})
      options[:validate] == false || valid?(options[:context])
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
active_remote-6.0.3 lib/active_remote/validations.rb
active_remote-6.1.2 lib/active_remote/validations.rb
active_remote-7.0.0 lib/active_remote/validations.rb
active_remote-6.1.1 lib/active_remote/validations.rb
active_remote-6.1.0 lib/active_remote/validations.rb
active_remote-6.0.2 lib/active_remote/validations.rb
active_remote-6.0.1 lib/active_remote/validations.rb
active_remote-6.0.0.beta lib/active_remote/validations.rb
active_remote-5.2.0 lib/active_remote/validations.rb
active_remote-5.2.0.beta lib/active_remote/validations.rb
active_remote-5.2.0.alpha lib/active_remote/validations.rb
active_remote-5.0.1 lib/active_remote/validations.rb
active_remote-5.1.1 lib/active_remote/validations.rb
active_remote-5.1.0 lib/active_remote/validations.rb
active_remote-5.0.0 lib/active_remote/validations.rb
active_remote-5.1.0.rc1 lib/active_remote/validations.rb
active_remote-5.0.0.rc1 lib/active_remote/validations.rb
active_remote-5.0.0.pre lib/active_remote/validations.rb