Sha256: 23b425c684aea7759911beb1525915ca434419a166b10d04ccb05c5d92674cc5

Contents?: true

Size: 873 Bytes

Versions: 78

Compression:

Stored size: 873 Bytes

Contents

module CassandraObject
  class RecordInvalid < StandardError
    attr_reader :record
    def initialize(record)
      @record = record
      super("Invalid record: #{@record.errors.full_messages.to_sentence}")
    end
  end

  module Validations
    extend ActiveSupport::Concern
    include ActiveModel::Validations
    
    module ClassMethods
      def create!(attributes = {})
        new(attributes).tap do |object|
          object.save!
        end
      end
    end

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

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

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

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
sessionm-cassandra_object-4.0.32 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.31 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.30 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.29 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.28 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.27 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.26 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.25 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.24 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.23 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.22 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.21 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.20 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.7.5 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.7.4 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.18 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.17 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.16 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.15 lib/cassandra_object/validations.rb
sessionm-cassandra_object-4.0.14 lib/cassandra_object/validations.rb