Sha256: 8a653d66d78bf0c99abf774430146edfd3817de17f1a6eaff422335172a922b0

Contents?: true

Size: 1.67 KB

Versions: 24

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.new(self))
    end

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

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
active_remote-3.3.3 lib/active_remote/validations.rb
active_remote-3.3.2 lib/active_remote/validations.rb
active_remote-3.3.1 lib/active_remote/validations.rb
active_remote-3.3.0 lib/active_remote/validations.rb
active_remote-3.2.2 lib/active_remote/validations.rb
active_remote-3.2.1 lib/active_remote/validations.rb
active_remote-3.2.0 lib/active_remote/validations.rb
active_remote-3.2.0.pre lib/active_remote/validations.rb
active_remote-3.1.3 lib/active_remote/validations.rb
active_remote-3.1.2 lib/active_remote/validations.rb
active_remote-3.1.2.pre lib/active_remote/validations.rb
active_remote-3.1.1 lib/active_remote/validations.rb
active_remote-3.1.0 lib/active_remote/validations.rb
active_remote-3.0.0 lib/active_remote/validations.rb
active_remote-3.0.0.pre1 lib/active_remote/validations.rb
active_remote-2.4.0 lib/active_remote/validations.rb
active_remote-2.3.5 lib/active_remote/validations.rb
active_remote-2.3.4 lib/active_remote/validations.rb
active_remote-2.3.3 lib/active_remote/validations.rb
active_remote-2.3.3.pre lib/active_remote/validations.rb