Sha256: abb1de3bf065a4d2789c2efa1b7c9d809888f0eb12518b3753718e712c86e87e

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

require 'active_support/concern'
require 'active_model/validations'
require 'ripple/translation'
require 'ripple/validations/associated_validator'

module Ripple
  # Raised by <tt>save!</tt> when the document is invalid.  Use the
  # +document+ method to retrieve the document which did not validate.
  #   begin
  #     invalid_document.save!
  #   rescue Ripple::DocumentInvalid => invalid
  #     puts invalid.document.errors
  #   end
  class DocumentInvalid < StandardError
    include Translation
    attr_reader :document
    def initialize(document)
      @document = document
      errors = @document.errors.full_messages.join(", ")
      super(t("document_invalid", :errors => errors))
    end
  end

  # Adds validations to {Ripple::Document} models. Validations are
  # executed before saving the document.
  module Validations
    extend ActiveSupport::Concern
    include ActiveModel::Validations

    module ClassMethods
      # @private
      def property(key, type, options={})
        prop = super
        validates key, prop.validation_options unless prop.validation_options.blank?
      end

      # Instantiates a new document, applies attributes from a block, and saves it
      # Raises Ripple::DocumentInvalid if the record did not save
      def create!(*args, &block)
        obj = create(*args, &block)
        (raise Ripple::DocumentInvalid.new(obj) if obj.new?) || obj
      end
    end

    # @private
    def save(options={:validate => true})
      return false if options[:validate] && !valid?
      super()
    end
    
    # Saves the document and raises {DocumentInvalid} exception if
    # validations fail.
    def save!
      (raise Ripple::DocumentInvalid.new(self) unless save) || true
    end
    
    # Sets the passed attributes and saves the document, raising a
    # {DocumentInvalid} exception if the validations fail.
    def update_attributes!(attrs)
      self.attributes = attrs
      save!
    end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
wyngle-ripple-0.1.0 lib/ripple/validations.rb
better-ripple-1.0.0 lib/ripple/validations.rb
ripple-1.0.0.beta2 lib/ripple/validations.rb