Sha256: 083c1cace575bbfde562197ebab6773a98a9d8076807526834d07329bd7fac1f

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module MassiveRecord
  module ORM

    # 
    # Raised when save! or create! fails due to invalid record
    # A rescued error contains record which you can read errors
    # from as normal
    #
    class RecordInvalid < MassiveRecordError
      attr_reader :record
      def initialize(record)
        @record = record
        errors = @record.errors.full_messages.join(", ")
        super(I18n.t("activemodel.errors.messages.record_invalid", :errors => errors))
      end
    end




    module Validations
      extend ActiveSupport::Concern
      include ActiveModel::Validations


      module ClassMethods
        def create!(*args)
          record = new(*args)
          record.save!
          record
        end
      end


      def save(options = {})
        perform_validation(options) ? super : false
      end

      def save!(options = {})
        perform_validation(options) ? super : raise(RecordInvalid.new(self))
      end

      def valid?(context = nil)
        context ||= (new_record? ? :create : :update)
        super
      end

      private

      def perform_validation(options = {})
        perform_validation = options[:validate] != false
        perform_validation ? valid? : true
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
massive_record-0.2.2 lib/massive_record/orm/validations.rb
massive_record-0.2.2.rc2 lib/massive_record/orm/validations.rb
massive_record-0.2.2.rc1 lib/massive_record/orm/validations.rb