Sha256: b41ba9bd4bfd894515bfcf4f16fb7d5f293ee2628bf1d56e62a4d614bd6d3648

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

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

    included do
      extend ActiveModel::Callbacks
      define_model_callbacks :validation
    end

    module ClassMethods
      def validates_embedded(*names)
        validates_each(*names) do |record, name, value|
          invalid = value.compact.select { |obj| !obj.valid? }
          if invalid.any?
            record.errors.add(name, 'is invalid')

            if logger.debug?
              invalid_messages = []
              invalid.each do |obj|
                invalid_messages << [obj.attributes, obj.errors.full_messages]
              end
              log_operation(:iem, self.name, store, record.id, invalid_messages)
            end
          end
        end
      end

      def create!(attrs={})
        new(attrs).tap { |doc| doc.save! }
      end
    end

    module InstanceMethods
      def valid?
        run_callbacks(:validation) { super }
      end

      def save(options={})
        options.assert_valid_keys(:validate)
        !options.fetch(:validate, true) || valid? ? super : false
      end

      def save!
        save || raise(RecordInvalid.new(self))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
toystore-0.8.1 lib/toy/validations.rb
toystore-0.8.0 lib/toy/validations.rb
toystore-0.7.0 lib/toy/validations.rb