diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-09-21 21:14:04 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-09-21 21:14:04 +0100 |
commit | 340be9bddd8e5902e0218a0101a40a17a4afd558 (patch) | |
tree | ef4de25f3f8eb610dc2235f0762b01cb1d464efd /activemodel/lib | |
parent | b31cdb55422226cd45a2234a4b54986f1f611151 (diff) | |
parent | 1bbb9b2db05730194edfd7d2cef9f5fcb9d79e50 (diff) | |
download | rails-340be9bddd8e5902e0218a0101a40a17a4afd558.tar.gz rails-340be9bddd8e5902e0218a0101a40a17a4afd558.tar.bz2 rails-340be9bddd8e5902e0218a0101a40a17a4afd558.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model.rb | 1 | ||||
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 3 | ||||
-rw-r--r-- | activemodel/lib/active_model/lint.rb | 7 | ||||
-rw-r--r-- | activemodel/lib/active_model/observing.rb | 17 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 18 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/presence.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/with.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations_repair_helper.rb | 41 | ||||
-rw-r--r-- | activemodel/lib/active_model/version.rb | 9 | ||||
-rw-r--r-- | activemodel/lib/activemodel.rb | 1 |
11 files changed, 45 insertions, 58 deletions
diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 5bb931be7f..67f529262d 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -41,6 +41,7 @@ module ActiveModel autoload :TestCase, 'active_model/test_case' autoload :Validations, 'active_model/validations' autoload :ValidationsRepairHelper, 'active_model/validations_repair_helper' + autoload :VERSION, 'active_model/version' module Serializers autoload :JSON, 'active_model/serializers/json' diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 1091ad3095..aa35a2726e 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -181,7 +181,7 @@ module ActiveModel end def attribute_methods_generated? - @attribute_methods_generated ? true : false + @attribute_methods_generated ||= nil end protected diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 1a3d120761..7a48960f89 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -1,7 +1,8 @@ require 'active_support/core_ext/string/inflections' +require 'active_support/ordered_hash' module ActiveModel - class Errors < Hash + class Errors < ActiveSupport::OrderedHash include DeprecatedErrorMethods def initialize(base) diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb index 46af8ca9de..478f887043 100644 --- a/activemodel/lib/active_model/lint.rb +++ b/activemodel/lib/active_model/lint.rb @@ -2,7 +2,7 @@ require "test/unit" require "test/unit/ui/console/testrunner" # You can test whether an object is compliant with the ActiveModel API by -# calling ActiveModel::Compliance.test(object). It will emit a Test::Unit +# calling ActiveModel::Lint.test(object). It will emit a Test::Unit # output that tells you whether your object is fully compliant, or if not, # which aspects of the API are not implemented. # @@ -13,7 +13,6 @@ require "test/unit/ui/console/testrunner" # # Objects you pass in are expected to return a compliant object from a # call to to_model. It is perfectly fine for to_model to return self. - module ActiveModel module Lint def self.test(object, verbosity = 2, output = STDOUT) @@ -59,7 +58,7 @@ module ActiveModel end def test_destroyed? - assert @object.respond_to?(:new_record?), "The model should respond to destroyed?" + assert @object.respond_to?(:destroyed?), "The model should respond to destroyed?" assert_boolean "destroyed?", @object.destroyed? end @@ -93,4 +92,4 @@ module ActiveModel include Errors end end -end
\ No newline at end of file +end diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index 3b230c43b9..d9d1ab8967 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -40,23 +40,6 @@ module ActiveModel observers.each { |o| instantiate_observer(o) } end - # Wraps methods with before and after notifications. - # - # wrap_with_notifications :create, :save, :update, :destroy - def wrap_with_notifications(*methods) - methods.each do |method| - class_eval(<<-EOS, __FILE__, __LINE__ + 1) - def #{method}_with_notifications(*args, &block) - notify_observers(:before_#{method}) - result = #{method}_without_notifications(*args, &block) - notify_observers(:after_#{method}) - result - end - EOS - alias_method_chain(method, :notifications) - end - end - protected def instantiate_observer(observer) #:nodoc: # string/symbol diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 7d49e60790..edeb508a08 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -6,10 +6,10 @@ require 'active_support/callbacks' module ActiveModel module Validations extend ActiveSupport::Concern - include ActiveSupport::Callbacks + include ActiveSupport::NewCallbacks included do - define_callbacks :validate + define_callbacks :validate, :scope => :name end module ClassMethods @@ -64,7 +64,7 @@ module ActiveModel attrs = attrs.flatten # Declare the validation. - send(validation_method(options[:on]), options) do |record| + validate options do |record| attrs.each do |attr| value = record.send(:read_attribute_for_validation, attr) next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) @@ -73,10 +73,14 @@ module ActiveModel end end - private - def validation_method(on) - :validate + def validate(*args, &block) + options = args.last + if options.is_a?(Hash) && options.key?(:on) + options[:if] = Array(options[:if]) + options[:if] << "@_on_validate == :#{options[:on]}" end + set_callback(:validate, *args, &block) + end end # Returns the Errors object that holds all information about attribute error messages. @@ -87,7 +91,7 @@ module ActiveModel # Runs all the specified validations and returns true if no errors were added otherwise false. def valid? errors.clear - run_callbacks(:validate) + _run_validate_callbacks errors.empty? end diff --git a/activemodel/lib/active_model/validations/presence.rb b/activemodel/lib/active_model/validations/presence.rb index 72d6b1c6f0..3ff677c137 100644 --- a/activemodel/lib/active_model/validations/presence.rb +++ b/activemodel/lib/active_model/validations/presence.rb @@ -32,7 +32,7 @@ module ActiveModel # can't use validates_each here, because it cannot cope with nonexistent attributes, # while errors.add_on_empty can - send(validation_method(configuration[:on]), configuration) do |record| + validate configuration do |record| record.errors.add_on_blank(attr_names, configuration[:message]) end end diff --git a/activemodel/lib/active_model/validations/with.rb b/activemodel/lib/active_model/validations/with.rb index 851cdfebf0..edc2133ddc 100644 --- a/activemodel/lib/active_model/validations/with.rb +++ b/activemodel/lib/active_model/validations/with.rb @@ -51,7 +51,7 @@ module ActiveModel def validates_with(*args) configuration = args.extract_options! - send(validation_method(configuration[:on]), configuration) do |record| + validate configuration do |record| args.each do |klass| klass.new(record, configuration.except(:on, :if, :unless)).validate end diff --git a/activemodel/lib/active_model/validations_repair_helper.rb b/activemodel/lib/active_model/validations_repair_helper.rb index 432e411308..40741e6dbe 100644 --- a/activemodel/lib/active_model/validations_repair_helper.rb +++ b/activemodel/lib/active_model/validations_repair_helper.rb @@ -2,43 +2,34 @@ module ActiveModel module ValidationsRepairHelper extend ActiveSupport::Concern - module Toolbox - def self.record_validations(*model_classes) - model_classes.inject({}) do |repair, klass| - repair[klass] ||= {} - [:validate, :validate_on_create, :validate_on_update].each do |callback| - the_callback = klass.instance_variable_get("@#{callback.to_s}_callbacks") - repair[klass][callback] = (the_callback.nil? ? nil : the_callback.dup) - end - repair - end - end - - def self.reset_validations(recorded) - recorded.each do |klass, repairs| - [:validate, :validate_on_create, :validate_on_update].each do |callback| - klass.instance_variable_set("@#{callback.to_s}_callbacks", repairs[callback]) - end - end - end - end - module ClassMethods def repair_validations(*model_classes) setup do - @validation_repairs = Toolbox.record_validations(*model_classes) + @_stored_callbacks = {} + model_classes.each do |k| + @_stored_callbacks[k] = k._validate_callbacks.dup + end end teardown do - Toolbox.reset_validations(@validation_repairs) + model_classes.each do |k| + k._validate_callbacks = @_stored_callbacks[k] + k.__update_callbacks(:validate) + end end end end def repair_validations(*model_classes, &block) - validation_repairs = Toolbox.record_validations(*model_classes) + @__stored_callbacks = {} + model_classes.each do |k| + @__stored_callbacks[k] = k._validate_callbacks.dup + end return block.call ensure - Toolbox.reset_validations(validation_repairs) + model_classes.each do |k| + k._validate_callbacks = @__stored_callbacks[k] + k.__update_callbacks(:validate) + end end end end diff --git a/activemodel/lib/active_model/version.rb b/activemodel/lib/active_model/version.rb new file mode 100644 index 0000000000..0c233b7b4f --- /dev/null +++ b/activemodel/lib/active_model/version.rb @@ -0,0 +1,9 @@ +module ActiveModel + module VERSION #:nodoc: + MAJOR = 3 + MINOR = 0 + TINY = "pre" + + STRING = [MAJOR, MINOR, TINY].join('.') + end +end diff --git a/activemodel/lib/activemodel.rb b/activemodel/lib/activemodel.rb deleted file mode 100644 index da3133103b..0000000000 --- a/activemodel/lib/activemodel.rb +++ /dev/null @@ -1 +0,0 @@ -require 'active_model' |