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.3.7 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.3.6 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.3.5 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.3.4 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.3.3 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.3.2 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.3.1 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
dirty_history-0.5.4 dirty_history/ruby/1.9.1/gems/activemodel-3.1.1/lib/active_model/validations/callbacks.rb
activemodel-3.2.5 lib/active_model/validations/callbacks.rb
activemodel-3.2.4 lib/active_model/validations/callbacks.rb
activemodel-3.1.5 lib/active_model/validations/callbacks.rb
classiccms-0.3.0 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
activemodel-3.2.4.rc1 lib/active_model/validations/callbacks.rb
activemodel-3.1.5.rc1 lib/active_model/validations/callbacks.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/rails-uploader-0.0.1/vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
rails-uploader-0.0.1 vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.2.4.pre vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
challah-0.6.0 vendor/bundle/gems/activemodel-3.2.1/lib/active_model/validations/callbacks.rb
challah-0.6.0 vendor/bundle/gems/activemodel-3.2.2/lib/active_model/validations/callbacks.rb