diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 2 | ||||
-rw-r--r-- | activemodel/README.rdoc | 2 | ||||
-rw-r--r-- | activemodel/Rakefile | 19 | ||||
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/attribute_assignment_test.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 74 | ||||
-rw-r--r-- | activemodel/test/cases/helper.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/validations/i18n_validation_test.rb | 98 | ||||
-rw-r--r-- | activemodel/test/cases/validations/with_validation_test.rb | 8 |
10 files changed, 130 insertions, 83 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 3c1510b31d..dddfd940bb 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -23,7 +23,7 @@ *Wojciech Wnętrzak* * Deprecate `ActiveModel::Errors#get`, `ActiveModel::Errors#set` and - `ActiveModel::Errors#[]=` methods that have inconsistent behaviour. + `ActiveModel::Errors#[]=` methods that have inconsistent behavior. *Wojciech Wnętrzak* diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc index 4920666f27..5c36b1277e 100644 --- a/activemodel/README.rdoc +++ b/activemodel/README.rdoc @@ -242,7 +242,7 @@ behavior out of the box: The latest version of Active Model can be installed with RubyGems: - % [sudo] gem install activemodel + % gem install activemodel Source code can be downloaded as part of the Rails project on GitHub diff --git a/activemodel/Rakefile b/activemodel/Rakefile index 7256285a41..5a67f0a151 100644 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -1,7 +1,7 @@ -dir = File.dirname(__FILE__) - require 'rake/testtask' +dir = File.dirname(__FILE__) + task :default => :test Rake::TestTask.new do |t| @@ -19,18 +19,3 @@ namespace :test do end or raise "Failures" end end - -require 'rubygems/package_task' - -spec = eval(File.read("#{dir}/activemodel.gemspec")) - -Gem::PackageTask.new(spec) do |p| - p.gem_spec = spec -end - -desc "Release to rubygems" -task :release => :package do - require 'rake/gemcutter' - Rake::Gemcutter::Tasks.new(spec).define - Rake::Task['gem:push'].invoke -end diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index c0fc507286..0169c20e0b 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -102,10 +102,10 @@ module ActiveModel # person.changes # => {"name" => ["Bill", "Bob"]} # # If an attribute is modified in-place then make use of - # +[attribute_name]_will_change!+ to mark that the attribute is changing. + # <tt>[attribute_name]_will_change!</tt> to mark that the attribute is changing. # Otherwise \Active \Model can't track changes to in-place attributes. Note # that Active Record can detect in-place modifications automatically. You do - # not need to call +[attribute_name]_will_change!+ on Active Record models. + # not need to call <tt>[attribute_name]_will_change!</tt> on Active Record models. # # person.name_will_change! # person.name_change # => ["Bill", "Bill"] diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index f843b279ce..287a2559d2 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -452,7 +452,6 @@ module ActiveModel defaults = [] end - defaults << options.delete(:message) defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}" if @base.class.respond_to?(:i18n_scope) defaults << :"errors.attributes.#{attribute}.#{type}" defaults << :"errors.messages.#{type}" @@ -461,6 +460,7 @@ module ActiveModel defaults.flatten! key = defaults.shift + defaults = options.delete(:message) if options[:message] value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil) options = { diff --git a/activemodel/test/cases/attribute_assignment_test.rb b/activemodel/test/cases/attribute_assignment_test.rb index 3b01644dd1..64a85e01eb 100644 --- a/activemodel/test/cases/attribute_assignment_test.rb +++ b/activemodel/test/cases/attribute_assignment_test.rb @@ -58,8 +58,6 @@ class AttributeAssignmentTest < ActiveModel::TestCase end test "assign private attribute" do - rubinius_skip "https://github.com/rubinius/rubinius/issues/3328" - model = Model.new assert_raises(ActiveModel::UnknownAttributeError) do model.assign_attributes(metadata: { a: 1 }) diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index f781a0017f..3fa63917d0 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -27,6 +27,14 @@ class ErrorsTest < ActiveModel::TestCase end end + def setup + @mock_generator = MiniTest::Mock.new + end + + def teardown + @mock_generator.verify + end + def test_delete errors = ActiveModel::Errors.new(self) errors[:foo] << 'omg' @@ -149,6 +157,12 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["cannot be blank"], person.errors[:name] end + test "add an error message on a specific attribute with a defined type" do + person = Person.new + person.errors.add(:name, :blank, message: "cannot be blank") + assert_equal ["cannot be blank"], person.errors[:name] + end + test "add an error with a symbol" do person = Person.new person.errors.add(:name, :blank) @@ -293,60 +307,74 @@ class ErrorsTest < ActiveModel::TestCase test "add_on_empty generates message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :empty, {}) - assert_deprecated do - person.errors.add_on_empty :name + @mock_generator.expect(:call, nil, [:name, :empty, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty :name + end end end test "add_on_empty generates message for multiple attributes" do person = Person.new - person.errors.expects(:generate_message).with(:name, :empty, {}) - person.errors.expects(:generate_message).with(:age, :empty, {}) - assert_deprecated do - person.errors.add_on_empty [:name, :age] + @mock_generator.expect(:call, nil, [:name, :empty, {}]) + @mock_generator.expect(:call, nil, [:age, :empty, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty [:name, :age] + end end end test "add_on_empty generates message with custom default message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :empty, { message: 'custom' }) - assert_deprecated do - person.errors.add_on_empty :name, message: 'custom' + @mock_generator.expect(:call, nil, [:name, :empty, { message: 'custom' }]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty :name, message: 'custom' + end end end test "add_on_empty generates message with empty string value" do person = Person.new person.name = '' - person.errors.expects(:generate_message).with(:name, :empty, {}) - assert_deprecated do - person.errors.add_on_empty :name + @mock_generator.expect(:call, nil, [:name, :empty, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty :name + end end end test "add_on_blank generates message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :blank, {}) - assert_deprecated do - person.errors.add_on_blank :name + @mock_generator.expect(:call, nil, [:name, :blank, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_blank :name + end end end test "add_on_blank generates message for multiple attributes" do person = Person.new - person.errors.expects(:generate_message).with(:name, :blank, {}) - person.errors.expects(:generate_message).with(:age, :blank, {}) - assert_deprecated do - person.errors.add_on_blank [:name, :age] + @mock_generator.expect(:call, nil, [:name, :blank, {}]) + @mock_generator.expect(:call, nil, [:age, :blank, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_blank [:name, :age] + end end end test "add_on_blank generates message with custom default message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :blank, { message: 'custom' }) - assert_deprecated do - person.errors.add_on_blank :name, message: 'custom' + @mock_generator.expect(:call, nil, [:name, :blank, { message: 'custom' }]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_blank :name, message: 'custom' + end end end diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb index 2b9de5e5d2..0d179ea9ad 100644 --- a/activemodel/test/cases/helper.rb +++ b/activemodel/test/cases/helper.rb @@ -12,7 +12,7 @@ I18n.enforce_available_locales = false require 'active_support/testing/autorun' -require 'mocha/setup' # FIXME: stop using mocha +require 'minitest/mock' # Skips the current run on Rubinius using Minitest::Assertions#skip def rubinius_skip(message = '') diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 70ee7afecc..be17575402 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -11,6 +11,7 @@ class I18nValidationTest < ActiveModel::TestCase I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new I18n.backend.store_translations('en', errors: { messages: { custom: nil } }) + @mock_generator = MiniTest::Mock.new end def teardown @@ -18,6 +19,7 @@ class I18nValidationTest < ActiveModel::TestCase I18n.load_path.replace @old_load_path I18n.backend = @old_backend I18n.backend.reload! + @mock_generator.verify end def test_full_message_encoding @@ -30,8 +32,10 @@ class I18nValidationTest < ActiveModel::TestCase def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @person.errors.add(:name, 'not found') - Person.expects(:human_attribute_name).with(:name, default: 'Name').returns("Person's name") - assert_equal ["Person's name not found"], @person.errors.full_messages + @mock_generator.expect(:call, "Person's name", [:name, default: 'Name']) + Person.stub(:human_attribute_name, @mock_generator) do + assert_equal ["Person's name not found"], @person.errors.full_messages + end end def test_errors_full_messages_uses_format @@ -60,8 +64,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_confirmation_of on generated message #{name}" do Person.validates_confirmation_of :title, validation_options @person.title_confirmation = 'foo' - @person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')) - @person.valid? + @mock_generator.expect(:call, nil, [:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -70,8 +76,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_acceptance_of on generated message #{name}" do Person.validates_acceptance_of :title, validation_options.merge(allow_nil: false) - @person.errors.expects(:generate_message).with(:title, :accepted, generate_message_options) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :accepted, generate_message_options]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -80,8 +88,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_presence_of on generated message #{name}" do Person.validates_presence_of :title, validation_options - @person.errors.expects(:generate_message).with(:title, :blank, generate_message_options) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :blank, generate_message_options]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -90,8 +100,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_length_of for :withing on generated message when too short #{name}" do Person.validates_length_of :title, validation_options.merge(within: 3..5) - @person.errors.expects(:generate_message).with(:title, :too_short, generate_message_options.merge(count: 3)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :too_short, generate_message_options.merge(count: 3)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -101,8 +113,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_length_of for :too_long generated message #{name}" do Person.validates_length_of :title, validation_options.merge(within: 3..5) @person.title = 'this title is too long' - @person.errors.expects(:generate_message).with(:title, :too_long, generate_message_options.merge(count: 5)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :too_long, generate_message_options.merge(count: 5)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -111,8 +125,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_length_of for :is on generated message #{name}" do Person.validates_length_of :title, validation_options.merge(is: 5) - @person.errors.expects(:generate_message).with(:title, :wrong_length, generate_message_options.merge(count: 5)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :wrong_length, generate_message_options.merge(count: 5)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -122,8 +138,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_format_of on generated message #{name}" do Person.validates_format_of :title, validation_options.merge(with: /\A[1-9][0-9]*\z/) @person.title = '72x' - @person.errors.expects(:generate_message).with(:title, :invalid, generate_message_options.merge(value: '72x')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :invalid, generate_message_options.merge(value: '72x')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -133,8 +151,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_inclusion_of on generated message #{name}" do Person.validates_inclusion_of :title, validation_options.merge(in: %w(a b c)) @person.title = 'z' - @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(value: 'z')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :inclusion, generate_message_options.merge(value: 'z')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -144,8 +164,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_inclusion_of using :within on generated message #{name}" do Person.validates_inclusion_of :title, validation_options.merge(within: %w(a b c)) @person.title = 'z' - @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(value: 'z')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :inclusion, generate_message_options.merge(value: 'z')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -155,8 +177,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_exclusion_of generated message #{name}" do Person.validates_exclusion_of :title, validation_options.merge(in: %w(a b c)) @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(value: 'a')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :exclusion, generate_message_options.merge(value: 'a')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -166,8 +190,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_exclusion_of using :within generated message #{name}" do Person.validates_exclusion_of :title, validation_options.merge(within: %w(a b c)) @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(value: 'a')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :exclusion, generate_message_options.merge(value: 'a')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -177,8 +203,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of generated message #{name}" do Person.validates_numericality_of :title, validation_options @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :not_a_number, generate_message_options.merge(value: 'a')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :not_a_number, generate_message_options.merge(value: 'a')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -188,8 +216,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of for :only_integer on generated message #{name}" do Person.validates_numericality_of :title, validation_options.merge(only_integer: true) @person.title = '0.0' - @person.errors.expects(:generate_message).with(:title, :not_an_integer, generate_message_options.merge(value: '0.0')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :not_an_integer, generate_message_options.merge(value: '0.0')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -199,8 +229,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of for :odd on generated message #{name}" do Person.validates_numericality_of :title, validation_options.merge(only_integer: true, odd: true) @person.title = 0 - @person.errors.expects(:generate_message).with(:title, :odd, generate_message_options.merge(value: 0)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :odd, generate_message_options.merge(value: 0)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -210,8 +242,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of for :less_than on generated message #{name}" do Person.validates_numericality_of :title, validation_options.merge(only_integer: true, less_than: 0) @person.title = 1 - @person.errors.expects(:generate_message).with(:title, :less_than, generate_message_options.merge(value: 1, count: 0)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :less_than, generate_message_options.merge(value: 1, count: 0)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index 01804032f0..9ee8b79da9 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -97,12 +97,14 @@ class ValidatesWithTest < ActiveModel::TestCase test "passes all configuration options to the validator class" do topic = Topic.new - validator = mock() - validator.expects(:new).with(foo: :bar, if: "1 == 1", class: Topic).returns(validator) - validator.expects(:validate).with(topic) + validator = MiniTest::Mock.new + validator.expect(:new, validator, [{foo: :bar, if: "1 == 1", class: Topic}]) + validator.expect(:validate, nil, [topic]) + validator.expect(:is_a?, false, [Symbol]) Topic.validates_with(validator, if: "1 == 1", foo: :bar) assert topic.valid? + validator.verify end test "validates_with with options" do |