From 66d713fc8f62e270ac21f6c413d6527fbf30dc52 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 31 Aug 2009 19:09:16 -0500 Subject: License, version, and gemspec for ActiveModel. Ship it! --- activemodel/lib/active_model/version.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 activemodel/lib/active_model/version.rb (limited to 'activemodel/lib/active_model') 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 -- cgit v1.2.3 From 723a47bfb3708f968821bc969a9a3fc873a3ed58 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 2 Sep 2009 11:44:36 -0500 Subject: Kill AMo observing wrap_with_notifications since ARes was only using it --- activemodel/lib/active_model/observing.rb | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'activemodel/lib/active_model') 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 -- cgit v1.2.3 From 6dc9ad80e6ee4a581c5ace005632373fe7275c03 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 5 Sep 2009 19:10:21 -0500 Subject: Fix warnings in AMo --- activemodel/lib/active_model/attribute_methods.rb | 2 +- activemodel/lib/active_model/validations_repair_helper.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'activemodel/lib/active_model') 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/validations_repair_helper.rb b/activemodel/lib/active_model/validations_repair_helper.rb index 432e411308..0809e7c0d1 100644 --- a/activemodel/lib/active_model/validations_repair_helper.rb +++ b/activemodel/lib/active_model/validations_repair_helper.rb @@ -7,7 +7,8 @@ module ActiveModel 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") + ivar = "@#{callback.to_s}_callbacks" + the_callback = klass.instance_variable_get(ivar) if klass.instance_variable_defined?(ivar) repair[klass][callback] = (the_callback.nil? ? nil : the_callback.dup) end repair -- cgit v1.2.3 From 4f37b97033f596ec2c95eb53e9964e051c224981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 8 Sep 2009 10:10:14 -0500 Subject: Changed ActiveRecord to use new callbacks and speed up observers by only notifying events that are actually being consumed. Signed-off-by: Joshua Peek --- activemodel/lib/active_model/validations.rb | 16 +++++---- .../lib/active_model/validations/presence.rb | 2 +- activemodel/lib/active_model/validations/with.rb | 2 +- .../lib/active_model/validations_repair_helper.rb | 42 +++++++++------------- 4 files changed, 28 insertions(+), 34 deletions(-) (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 7d49e60790..72898726d1 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -6,7 +6,7 @@ require 'active_support/callbacks' module ActiveModel module Validations extend ActiveSupport::Concern - include ActiveSupport::Callbacks + include ActiveSupport::NewCallbacks included do define_callbacks :validate @@ -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, :before, *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 0809e7c0d1..40741e6dbe 100644 --- a/activemodel/lib/active_model/validations_repair_helper.rb +++ b/activemodel/lib/active_model/validations_repair_helper.rb @@ -2,44 +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| - ivar = "@#{callback.to_s}_callbacks" - the_callback = klass.instance_variable_get(ivar) if klass.instance_variable_defined?(ivar) - 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 -- cgit v1.2.3 From 2ea1d684d93bd59887a9fd12e647941f0d1f4868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 8 Sep 2009 10:22:45 -0500 Subject: Refactor new callbacks and AR implementation. Signed-off-by: Joshua Peek --- activemodel/lib/active_model/validations.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 72898726d1..edeb508a08 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -9,7 +9,7 @@ module ActiveModel include ActiveSupport::NewCallbacks included do - define_callbacks :validate + define_callbacks :validate, :scope => :name end module ClassMethods @@ -79,7 +79,7 @@ module ActiveModel options[:if] = Array(options[:if]) options[:if] << "@_on_validate == :#{options[:on]}" end - set_callback(:validate, :before, *args, &block) + set_callback(:validate, *args, &block) end end -- cgit v1.2.3 From 0990a13500d036f9b8cf4c11eb1056069357fca7 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 19 Mar 2009 14:42:08 +0900 Subject: Ensure validation errors to be ordered in declared order [#2301 state:committed milestone:2.3.5] Signed-off-by: Jeremy Kemper --- activemodel/lib/active_model/errors.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 7a3001174f..590420de0b 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) -- cgit v1.2.3 From f183288050beedf3a89e0bf5b9059bb014346097 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Thu, 17 Sep 2009 18:08:20 -0700 Subject: Fix typo. Signed-off-by: Yehuda Katz --- activemodel/lib/active_model/errors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 590420de0b..7a48960f89 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -114,7 +114,7 @@ module ActiveModel full_messages end - # Translates an error message in it's default scope (activemodel.errrors.messages). + # Translates an error message in its default scope (activemodel.errors.messages). # Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it's not there, # it's looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the # default message (e.g. activemodel.errors.messages.MESSAGE). The translated model name, -- cgit v1.2.3 From e2d0b0ee61c5a8c2626abb5ac1029b48ec1965eb Mon Sep 17 00:00:00 2001 From: lakshan Date: Mon, 21 Sep 2009 09:49:43 -0500 Subject: fixed ActiveModel::Lint typos [#3236 state:resolved] Signed-off-by: Joshua Peek --- activemodel/lib/active_model/lint.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'activemodel/lib/active_model') 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 -- cgit v1.2.3