Sha256: 9d9e96fd2b02f67b6a83333bc4048a45aab3a9f61fcbd38f23d281851bf4bf2e

Contents?: true

Size: 1.83 KB

Versions: 266

Compression:

Stored size: 1.83 KB

Contents

require 'active_support/callbacks'

module ActiveModel
  module Validations
    module Callbacks
      # == Active Model Validation callbacks
      #
      # Provides an interface for any class to have <tt>before_validation</tt> and
      # <tt>after_validation</tt> callbacks.
      #
      # First, extend ActiveModel::Callbacks from the class you are creating:
      #
      #   class MyModel
      #     include ActiveModel::Validations::Callbacks
      #
      #     before_validation :do_stuff_before_validation
      #     after_validation  :do_stuff_after_validation
      #   end
      #
      #   Like other before_* callbacks if <tt>before_validation</tt> returns false
      #   then <tt>valid?</tt> will not be called.
      extend ActiveSupport::Concern

      included do
        include ActiveSupport::Callbacks
        define_callbacks :validation, :terminator => "result == false", :scope => [:kind, :name]
      end

      module ClassMethods
        def before_validation(*args, &block)
          options = args.last
          if options.is_a?(Hash) && options[:on]
            options[:if] = Array.wrap(options[:if])
            options[:if].unshift("self.validation_context == :#{options[:on]}")
          end
          set_callback(:validation, :before, *args, &block)
        end

        def after_validation(*args, &block)
          options = args.extract_options!
          options[:prepend] = true
          options[:if] = Array.wrap(options[:if])
          options[:if] << "!halted"
          options[:if].unshift("self.validation_context == :#{options[:on]}") if options[:on]
          set_callback(:validation, :after, *(args << options), &block)
        end
      end

    protected

      # Overwrite run validations to include callbacks.
      def run_validations!
        run_callbacks(:validation) { super }
      end
    end
  end
end

Version data entries

266 entries across 222 versions & 23 rubygems

Version Path
classiccms-0.5.6 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
activemodel-3.2.8.rc2 lib/active_model/validations/callbacks.rb
classiccms-0.5.5 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
challah-0.7.0.pre vendor/bundle/gems/activemodel-3.2.7/lib/active_model/validations/callbacks.rb
classiccms-0.5.2 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.5.1 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.5.0 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
activemodel-3.2.8.rc1 lib/active_model/validations/callbacks.rb
activemodel-3.2.7 lib/active_model/validations/callbacks.rb
activemodel-3.1.7 lib/active_model/validations/callbacks.rb
activemodel-3.2.7.rc1 lib/active_model/validations/callbacks.rb
classiccms-0.4.2 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.4.1 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.4.0 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.3.9 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
challah-0.6.2 vendor/bundle/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb
challah-0.6.2 vendor/bundle/gems/activemodel-3.2.6/lib/active_model/validations/callbacks.rb
challah-0.6.2 vendor/bundle/gems/activemodel-3.2.5/lib/active_model/validations/callbacks.rb
challah-0.6.2 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb