diff options
Diffstat (limited to 'activemodel')
24 files changed, 186 insertions, 133 deletions
diff --git a/activemodel/MIT-LICENSE b/activemodel/MIT-LICENSE new file mode 100644 index 0000000000..e7accc5ea1 --- /dev/null +++ b/activemodel/MIT-LICENSE @@ -0,0 +1,21 @@ +Copyright (c) 2004-2009 David Heinemeier Hansson + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/activemodel/Rakefile b/activemodel/Rakefile index ae2fbdb002..4a64c90dd9 100755 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -1,7 +1,13 @@ -#!/usr/bin/env ruby -require 'rake' +require File.join(File.dirname(__FILE__), 'lib', 'active_model', 'version') + +PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : '' +PKG_NAME = 'activemodel' +PKG_VERSION = ActiveModel::VERSION::STRING + PKG_BUILD +PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}" +RELEASE_NAME = "REL #{PKG_VERSION}" + + require 'rake/testtask' -require 'rake/rdoctask' task :default => :test @@ -9,14 +15,19 @@ Rake::TestTask.new do |t| t.libs << "test" t.test_files = Dir.glob("test/cases/**/*_test.rb").sort t.verbose = true + t.warning = true end + task :isolated_test do ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME')) Dir.glob("test/**/*_test.rb").all? do |file| - system(ruby, '-Ilib:test', file) + system(ruby, '-w', '-Ilib:test', file) end or raise "Failures" end + +require 'rake/rdoctask' + # Generate the RDoc documentation Rake::RDocTask.new do |rdoc| rdoc.rdoc_dir = 'doc' @@ -27,3 +38,39 @@ Rake::RDocTask.new do |rdoc| rdoc.rdoc_files.include('README', 'CHANGES') rdoc.rdoc_files.include('lib/**/*.rb') end + + +require 'rake/packagetask' +require 'rake/gempackagetask' + +spec = Gem::Specification.new do |s| + s.platform = Gem::Platform::RUBY + s.name = PKG_NAME + s.version = PKG_VERSION + s.summary = "A toolkit for building other modeling frameworks like ActiveRecord" + s.description = %q{Extracts common modeling concerns from ActiveRecord to share between similar frameworks like ActiveResource.} + + s.author = "David Heinemeier Hansson" + s.email = "david@loudthinking.com" + s.rubyforge_project = "activemodel" + s.homepage = "http://www.rubyonrails.org" + + s.has_rdoc = true + + s.add_dependency('activesupport', '= 3.0.pre' + PKG_BUILD) + + s.require_path = 'lib' + s.files = Dir["CHANGELOG", "MIT-LICENSE", "README", "Rakefile", "lib/**/*", "test/**/*"] +end + +Rake::GemPackageTask.new(spec) do |p| + p.gem_spec = spec + p.need_tar = true + p.need_zip = true +end + +task :gemspec do + File.open(File.join(File.dirname(__FILE__), "#{spec.name}.gemspec"), "w") do |file| + file.puts spec.to_ruby + end +end
\ No newline at end of file diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec new file mode 100644 index 0000000000..e96718e6fd --- /dev/null +++ b/activemodel/activemodel.gemspec @@ -0,0 +1,31 @@ +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{activemodel} + s.version = "3.0.pre" + + s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version= + s.authors = ["David Heinemeier Hansson"] + s.date = %q{2009-09-01} + s.description = %q{Extracts common modeling concerns from ActiveRecord to share between similar frameworks like ActiveResource.} + s.email = %q{david@loudthinking.com} + s.files = ["CHANGELOG", "MIT-LICENSE", "README", "Rakefile", "lib/active_model", "lib/active_model/attribute_methods.rb", "lib/active_model/conversion.rb", "lib/active_model/deprecated_error_methods.rb", "lib/active_model/dirty.rb", "lib/active_model/errors.rb", "lib/active_model/lint.rb", "lib/active_model/locale", "lib/active_model/locale/en.yml", "lib/active_model/naming.rb", "lib/active_model/observing.rb", "lib/active_model/serialization.rb", "lib/active_model/serializers", "lib/active_model/serializers/json.rb", "lib/active_model/serializers/xml.rb", "lib/active_model/state_machine", "lib/active_model/state_machine/event.rb", "lib/active_model/state_machine/machine.rb", "lib/active_model/state_machine/state.rb", "lib/active_model/state_machine/state_transition.rb", "lib/active_model/state_machine.rb", "lib/active_model/test_case.rb", "lib/active_model/validations", "lib/active_model/validations/acceptance.rb", "lib/active_model/validations/confirmation.rb", "lib/active_model/validations/exclusion.rb", "lib/active_model/validations/format.rb", "lib/active_model/validations/inclusion.rb", "lib/active_model/validations/length.rb", "lib/active_model/validations/numericality.rb", "lib/active_model/validations/presence.rb", "lib/active_model/validations/with.rb", "lib/active_model/validations.rb", "lib/active_model/validations_repair_helper.rb", "lib/active_model/version.rb", "lib/active_model.rb", "lib/activemodel.rb", "test/cases", "test/cases/helper.rb", "test/cases/lint_test.rb", "test/cases/naming_test.rb", "test/cases/observing_test.rb", "test/cases/serializeration", "test/cases/serializeration/json_serialization_test.rb", "test/cases/serializeration/xml_serialization_test.rb", "test/cases/state_machine", "test/cases/state_machine/event_test.rb", "test/cases/state_machine/machine_test.rb", "test/cases/state_machine/state_test.rb", "test/cases/state_machine/state_transition_test.rb", "test/cases/state_machine_test.rb", "test/cases/tests_database.rb", "test/cases/validations", "test/cases/validations/acceptance_validation_test.rb", "test/cases/validations/conditional_validation_test.rb", "test/cases/validations/confirmation_validation_test.rb", "test/cases/validations/exclusion_validation_test.rb", "test/cases/validations/format_validation_test.rb", "test/cases/validations/i18n_generate_message_validation_test.rb", "test/cases/validations/i18n_validation_test.rb", "test/cases/validations/inclusion_validation_test.rb", "test/cases/validations/length_validation_test.rb", "test/cases/validations/numericality_validation_test.rb", "test/cases/validations/presence_validation_test.rb", "test/cases/validations/with_validation_test.rb", "test/cases/validations_test.rb", "test/config.rb", "test/fixtures", "test/fixtures/topics.yml", "test/models", "test/models/contact.rb", "test/models/custom_reader.rb", "test/models/developer.rb", "test/models/person.rb", "test/models/reply.rb", "test/models/topic.rb", "test/schema.rb"] + s.homepage = %q{http://www.rubyonrails.org} + s.require_paths = ["lib"] + s.rubyforge_project = %q{activemodel} + s.rubygems_version = %q{1.3.5} + s.summary = %q{A toolkit for building other modeling frameworks like ActiveRecord} + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + s.add_runtime_dependency(%q<activesupport>, ["= 3.0.pre"]) + else + s.add_dependency(%q<activesupport>, ["= 3.0.pre"]) + end + else + s.add_dependency(%q<activesupport>, ["= 3.0.pre"]) + end +end diff --git a/activemodel/examples/validations.rb b/activemodel/examples/validations.rb index 3f8311ff96..b039897ea5 100644 --- a/activemodel/examples/validations.rb +++ b/activemodel/examples/validations.rb @@ -1,4 +1,4 @@ -require 'activemodel' +require 'active_model' class Person include ActiveModel::Conversion 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' diff --git a/activemodel/test/cases/observing_test.rb b/activemodel/test/cases/observing_test.rb index fbf93c19ef..e23bda0528 100644 --- a/activemodel/test/cases/observing_test.rb +++ b/activemodel/test/cases/observing_test.rb @@ -78,6 +78,7 @@ class ObserverTest < ActiveModel::TestCase def teardown FooObserver.instance_eval do + undef_method :observed_classes alias_method :observed_classes, :original_observed_classes end end diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb index 6227aedc39..81df52fcb9 100644 --- a/activemodel/test/cases/serializeration/json_serialization_test.rb +++ b/activemodel/test/cases/serializeration/json_serialization_test.rb @@ -1,12 +1,14 @@ require 'cases/helper' require 'models/contact' +require 'active_support/core_ext/object/instance_variables' class Contact + extend ActiveModel::Naming include ActiveModel::Serializers::JSON def attributes instance_values - end + end unless method_defined?(:attributes) end class JsonSerializationTest < ActiveModel::TestCase diff --git a/activemodel/test/cases/serializeration/xml_serialization_test.rb b/activemodel/test/cases/serializeration/xml_serialization_test.rb index 428e5a6bd1..6340aad531 100644 --- a/activemodel/test/cases/serializeration/xml_serialization_test.rb +++ b/activemodel/test/cases/serializeration/xml_serialization_test.rb @@ -1,12 +1,14 @@ require 'cases/helper' require 'models/contact' +require 'active_support/core_ext/object/instance_variables' class Contact + extend ActiveModel::Naming include ActiveModel::Serializers::Xml def attributes instance_values - end + end unless method_defined?(:attributes) end module Admin diff --git a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb index 37bba5e95e..07e4341289 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -5,7 +5,7 @@ require 'models/person' class I18nGenerateMessageValidationTest < Test::Unit::TestCase def setup - reset_callbacks Person + Person.reset_callbacks(:validate) @person = Person.new @old_load_path, @old_backend = I18n.load_path, I18n.backend @@ -45,12 +45,6 @@ class I18nGenerateMessageValidationTest < Test::Unit::TestCase I18n.backend = @old_backend end - def reset_callbacks(*models) - models.each do |model| - model.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - end - end - # validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value) def test_generate_message_inclusion_with_default_message assert_equal 'is not included in the list', @person.errors.generate_message(:title, :inclusion, :default => nil, :value => 'title') diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index cc68d847a2..fc4f1926b0 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -7,8 +7,7 @@ class I18nValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase def setup - reset_callbacks Person - + Person.reset_callbacks(:validate) @person = Person.new @old_load_path, @old_backend = I18n.load_path, I18n.backend @@ -18,17 +17,11 @@ class I18nValidationTest < ActiveModel::TestCase end def teardown - reset_callbacks Person + Person.reset_callbacks(:validate) I18n.load_path.replace @old_load_path I18n.backend = @old_backend end - def reset_callbacks(*models) - models.each do |model| - model.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new) - end - end - def test_percent_s_interpolation_syntax_in_error_messages_was_deprecated assert_not_deprecated do default = "%s interpolation syntax was deprecated" @@ -107,32 +100,6 @@ class I18nValidationTest < ActiveModel::TestCase @person.valid? end - def test_validates_length_of_within_generates_message_with_title_too_short - Person.validates_length_of :title, :within => 3..5 - @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil}) - @person.valid? - end - - def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message - Person.validates_length_of :title, :within => 3..5, :too_short => 'custom' - @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'}) - @person.valid? - end - - def test_validates_length_of_within_generates_message_with_title_too_long - Person.validates_length_of :title, :within => 3..5 - @person.title = 'this title is too long' - @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil}) - @person.valid? - end - - def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message - Person.validates_length_of :title, :within => 3..5, :too_long => 'custom' - @person.title = 'this title is too long' - @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'}) - @person.valid? - end - # validates_length_of :within w/ mocha def test_validates_length_of_within_generates_message_with_title_too_short @@ -280,7 +247,7 @@ class I18nValidationTest < ActiveModel::TestCase @person.valid? end - def test_validates_numericality_of_odd_generates_message_with_custom_default_message + def test_validates_numericality_of_less_than_odd_generates_message_with_custom_default_message Person.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom' @person.title = 1 @person.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => 'custom'}) @@ -384,24 +351,6 @@ class I18nValidationTest < ActiveModel::TestCase assert_equal ['global message'], @person.errors[:title] end - def test_validates_length_of_is_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}} - - Person.validates_length_of :title, :is => 5 - @person.valid? - assert_equal ['custom message'], @person.errors[:title] - end - - def test_validates_length_of_is_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}} - - Person.validates_length_of :title, :is => 5 - @person.valid? - assert_equal ['global message'], @person.errors[:title] - end - - # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation @@ -576,4 +525,4 @@ class I18nValidationTest < ActiveModel::TestCase assert_equal ["I am a custom error"], @person.errors[:title] end -end
\ No newline at end of file +end diff --git a/activemodel/test/cases/validations/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb index bb6fb91774..90b0951a77 100644 --- a/activemodel/test/cases/validations/presence_validation_test.rb +++ b/activemodel/test/cases/validations/presence_validation_test.rb @@ -5,6 +5,7 @@ require 'cases/tests_database' require 'models/topic' require 'models/developer' require 'models/person' +require 'models/custom_reader' class PresenceValidationTest < ActiveModel::TestCase include ActiveModel::TestsDatabase diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index f55fdc5864..c290b49a28 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -1,9 +1,11 @@ # encoding: utf-8 require 'cases/helper' +require 'cases/tests_database' require 'models/topic' class ValidatesWithTest < ActiveRecord::TestCase + include ActiveModel::TestsDatabase include ActiveModel::ValidationsRepairHelper repair_validations(Topic) diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 0b340e68bf..78565177d8 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -121,8 +121,8 @@ class ValidationsTest < ActiveModel::TestCase end def test_invalid_validator - Topic.validate 3 - assert_raise(ArgumentError) { t = Topic.create } + Topic.validate :i_dont_exist + assert_raise(NameError) { t = Topic.create } end def test_errors_to_xml @@ -141,6 +141,22 @@ class ValidationsTest < ActiveModel::TestCase t = Topic.new("title" => "") assert !t.valid? assert_equal "can't be blank", t.errors["title"].first + Topic.validates_presence_of :title, :author_name + Topic.validate {|topic| topic.errors.add('author_email_address', 'will never be valid')} + Topic.validates_length_of :title, :content, :minimum => 2 + + t = Topic.new :title => '' + assert !t.valid? + + assert_equal :title, key = t.errors.keys.first + assert_equal "can't be blank", t.errors[key].first + assert_equal 'is too short (minimum is 2 characters)', t.errors[key].second + assert_equal :author_name, key = t.errors.keys.second + assert_equal "can't be blank", t.errors[key].first + assert_equal :author_email_address, key = t.errors.keys.third + assert_equal 'will never be valid', t.errors[key].first + assert_equal :content, key = t.errors.keys.fourth + assert_equal 'is too short (minimum is 2 characters)', t.errors[key].first end def test_invalid_should_be_the_opposite_of_valid @@ -189,4 +205,4 @@ class ValidationsTest < ActiveModel::TestCase all_errors = t.errors.to_a assert_deprecated { assert_equal all_errors, t.errors.each_full{|err| err} } end -end
\ No newline at end of file +end diff --git a/activemodel/test/models/reply.rb b/activemodel/test/models/reply.rb index acfd801674..e86692677f 100644 --- a/activemodel/test/models/reply.rb +++ b/activemodel/test/models/reply.rb @@ -2,11 +2,11 @@ require 'models/topic' class Reply < Topic validate :errors_on_empty_content - validate_on_create :title_is_wrong_create + validate :title_is_wrong_create, :on => :create validate :check_empty_title - validate_on_create :check_content_mismatch - validate_on_update :check_wrong_update + validate :check_content_mismatch, :on => :create + validate :check_wrong_update, :on => :update attr_accessible :title, :author_name, :author_email_address, :written_on, :content, :last_read |