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
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activemodel-3.2.22.5/lib/active_model/validations/callbacks.rb
activemodel-3.2.22.5 lib/active_model/validations/callbacks.rb
activemodel-3.2.22.4 lib/active_model/validations/callbacks.rb
activemodel-3.2.22.3 lib/active_model/validations/callbacks.rb
activemodel-3.2.22.2 lib/active_model/validations/callbacks.rb
activemodel-3.2.22.1 lib/active_model/validations/callbacks.rb
classiccms-0.7.5 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.7.4 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
classiccms-0.7.3 vendor/bundle/gems/activemodel-3.2.3/lib/active_model/validations/callbacks.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.12/lib/active_model/validations/callbacks.rb
activemodel-3.2.22 lib/active_model/validations/callbacks.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activemodel-3.2.12/lib/active_model/validations/callbacks.rb
activemodel-3.2.21 lib/active_model/validations/callbacks.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activemodel-3.2.18/lib/active_model/validations/callbacks.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.18/lib/active_model/validations/callbacks.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activemodel-3.2.18/lib/active_model/validations/callbacks.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activemodel-3.2.18/lib/active_model/validations/callbacks.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activemodel-3.2.18/lib/active_model/validations/callbacks.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activemodel-3.2.18/lib/active_model/validations/callbacks.rb
activemodel-3.2.20 lib/active_model/validations/callbacks.rb