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 | ||||
-rwxr-xr-x | activemodel/bin/test | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 6 | ||||
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 31 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 68 | ||||
-rw-r--r-- | activemodel/test/cases/helper.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/serializers/xml_serialization_test.rb | 1 | ||||
-rw-r--r-- | activemodel/test/cases/validations/i18n_validation_test.rb | 100 | ||||
-rw-r--r-- | activemodel/test/cases/validations/with_validation_test.rb | 8 |
12 files changed, 163 insertions, 84 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/bin/test b/activemodel/bin/test new file mode 100755 index 0000000000..404cabba51 --- /dev/null +++ b/activemodel/bin/test @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +COMPONENT_ROOT = File.expand_path("../../", __FILE__) +require File.expand_path("../tools/test", COMPONENT_ROOT) +exit Minitest.run(ARGV) diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index ff7280f9e5..286cd6c206 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -225,7 +225,7 @@ module ActiveModel end # Declares the attributes that should be prefixed and suffixed by - # ActiveModel::AttributeMethods. + # <tt>ActiveModel::AttributeMethods</tt>. # # To use, pass attribute names (as strings or symbols). Be sure to declare # +define_attribute_methods+ after you define any prefix, suffix or affix @@ -253,7 +253,7 @@ module ActiveModel end # Declares an attribute that should be prefixed and suffixed by - # ActiveModel::AttributeMethods. + # <tt>ActiveModel::AttributeMethods</tt>. # # To use, pass an attribute name (as string or symbol). Be sure to declare # +define_attribute_method+ after you define any prefix, suffix or affix diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 287a2559d2..29e0c977ce 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -43,11 +43,11 @@ module ActiveModel # end # end # - # The last three methods are required in your object for Errors to be + # The last three methods are required in your object for +Errors+ to be # able to generate error messages correctly and also handle multiple - # languages. Of course, if you extend your object with ActiveModel::Translation + # languages. Of course, if you extend your object with <tt>ActiveModel::Translation</tt> # you will not need to implement the last two. Likewise, using - # ActiveModel::Validations will handle the validation related methods + # <tt>ActiveModel::Validations</tt> will handle the validation related methods # for you. # # The above allows you to do: diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index c06a64c75f..f95849eb84 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -94,6 +94,37 @@ module ActiveModel # person.serializable_hash(except: :name) # => {"age"=>22} # person.serializable_hash(methods: :capitalized_name) # # => {"name"=>"bob", "age"=>22, "capitalized_name"=>"Bob"} + # + # Example with <tt>:include</tt> option + # + # class User + # include ActiveModel::Serializers::JSON + # attr_accessor :name, :notes # Emulate has_many :notes + # def attributes + # {'name' => nil} + # end + # end + # + # class Note + # include ActiveModel::Serializers::JSON + # attr_accessor :title, :text + # def attributes + # {'title' => nil, 'text' => nil} + # end + # end + # + # note = Note.new + # note.title = 'Battle of Austerlitz' + # note.text = 'Some text here' + # + # user = User.new + # user.name = 'Napoleon' + # user.notes = [note] + # + # user.serializable_hash + # # => {"name" => "Napoleon"} + # user.serializable_hash(include: { notes: { only: 'title' }}) + # # => {"name" => "Napoleon", "notes" => [{"title"=>"Battle of Austerlitz"}]} def serializable_hash(options = nil) options ||= {} diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index d10c20caad..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' @@ -299,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/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb index 22fca5bd17..37faf6cef8 100644 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -2,6 +2,7 @@ require 'cases/helper' require 'models/contact' require 'active_support/core_ext/object/instance_variables' require 'ostruct' +require 'yaml' module Admin class Contact < ::Contact diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 70ee7afecc..70b93a202b 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,18 +88,22 @@ 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 # validates_length_of :within too short w/ mocha COMMON_CASES.each do |name, validation_options, generate_message_options| - test "validates_length_of for :withing on generated message when too short #{name}" do + test "validates_length_of for :within 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 |