Sha256: 23ef799c7466e7b62654b1944d97026ac05d6e2dc80d455af198cd902f8224bb

Contents?: true

Size: 870 Bytes

Versions: 31

Compression:

Stored size: 870 Bytes

Contents

module Superstore
  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

    included do
      define_callbacks :validate, scope: :name
    end

    module ClassMethods
      def create!(attributes = {})
        new(attributes).tap do |object|
          object.save!
        end
      end
    end

    def save(options={})
      if perform_validations(options)
        super
        true
      else
        false
      end
    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

31 entries across 31 versions & 1 rubygems

Version Path
superstore-2.4.4 lib/superstore/validations.rb
superstore-2.4.3 lib/superstore/validations.rb
superstore-2.4.2 lib/superstore/validations.rb
superstore-2.4.1 lib/superstore/validations.rb
superstore-2.4.0 lib/superstore/validations.rb
superstore-2.3.0 lib/superstore/validations.rb
superstore-2.2.0 lib/superstore/validations.rb
superstore-2.1.3 lib/superstore/validations.rb
superstore-2.1.2 lib/superstore/validations.rb
superstore-2.1.1 lib/superstore/validations.rb
superstore-2.1.0 lib/superstore/validations.rb
superstore-2.0.1 lib/superstore/validations.rb
superstore-2.0.0 lib/superstore/validations.rb
superstore-1.2.0 lib/superstore/validations.rb
superstore-1.1.4 lib/superstore/validations.rb
superstore-1.1.3 lib/superstore/validations.rb
superstore-1.1.2 lib/superstore/validations.rb
superstore-1.1.1 lib/superstore/validations.rb
superstore-1.1.0 lib/superstore/validations.rb
superstore-1.0.12 lib/superstore/validations.rb