Sha256: 2a15637f4d504949f7e916f6480c04c791bfeec57ebc4e7c99609455adb87709
Contents?: true
Size: 1.09 KB
Versions: 3
Compression:
Stored size: 1.09 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 { |o| !o.valid? } if invalid.any? record.errors.add(name, 'is invalid') logger.debug("ToyStore #{self.name} IEM") invalid.each do |o| logger.debug(" #{o.attributes.inspect} - #{o.errors.full_messages.inspect}") 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.6.6 | lib/toy/validations.rb |
toystore-0.6.5 | lib/toy/validations.rb |
toystore-0.6.4 | lib/toy/validations.rb |