From 29dcf096b01d0720136716dc08736e6fef331fa2 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Fri, 15 Mar 2013 11:55:03 +0530 Subject: Small typos here and there. --- activemodel/CHANGELOG.md | 2 +- activemodel/test/cases/attribute_methods_test.rb | 2 +- activemodel/test/cases/naming_test.rb | 2 +- activemodel/test/cases/validations_test.rb | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index c6d7b0b5d3..6ba0c7cd6b 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -111,7 +111,7 @@ * Changed `ActiveModel::Serializers::Xml::Serializer#add_associations` to by default propagate `:skip_types, :dasherize, :camelize` keys to included associations. - It can be overriden on each association by explicitly specifying the option on one + It can be overridden on each association by explicitly specifying the option on one or more associations *Anthony Alberto* diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index baaf842222..d3ec78157c 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -194,7 +194,7 @@ class AttributeMethodsTest < ActiveModel::TestCase assert_raises(NoMethodError) { ModelWithAttributes.new.foo } end - test 'acessing a suffixed attribute' do + test 'accessing a suffixed attribute' do m = ModelWithAttributes2.new m.attributes = { 'foo' => 'bar' } diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 38ba3cc152..aa683f4152 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -245,7 +245,7 @@ class NamingHelpersTest < ActiveModel::TestCase end def test_uncountable - assert uncountable?(@uncountable), "Expected 'sheep' to be uncoutable" + assert uncountable?(@uncountable), "Expected 'sheep' to be uncountable" assert !uncountable?(@klass), "Expected 'contact' to be countable" end diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index a9d32808da..d3723fa45b 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -213,7 +213,7 @@ class ValidationsTest < ActiveModel::TestCase assert_equal 'is too short (minimum is 2 characters)', t.errors[key][0] end - def test_validaton_with_if_and_on + def test_validation_with_if_and_on Topic.validates_presence_of :title, :if => Proc.new{|x| x.author_name = "bad"; true }, :on => :update t = Topic.new(:title => "") @@ -361,7 +361,7 @@ class ValidationsTest < ActiveModel::TestCase def test_dup_validity_is_independent Topic.validates_presence_of :title - topic = Topic.new("title" => "Litterature") + topic = Topic.new("title" => "Literature") topic.valid? duped = topic.dup -- cgit v1.2.3 From 233737706cfe1a9b0d50140ca5c814a070b4b1be Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Mon, 18 Mar 2013 17:20:05 +0530 Subject: fix some typos found in activemodel --- activemodel/test/cases/callbacks_test.rb | 2 +- activemodel/test/cases/validations/with_validation_test.rb | 2 +- activemodel/test/cases/validations_test.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'activemodel') diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb index 086e7266ff..c4c34b0be7 100644 --- a/activemodel/test/cases/callbacks_test.rb +++ b/activemodel/test/cases/callbacks_test.rb @@ -107,7 +107,7 @@ class CallbacksTest < ActiveModel::TestCase test "after_create callbacks with both callbacks declared in one line" do assert_equal ["callback1", "callback2"], Violin1.new.create.history end - test "after_create callbacks with both callbacks declared in differnt lines" do + test "after_create callbacks with both callbacks declared in different lines" do assert_equal ["callback1", "callback2"], Violin2.new.create.history end diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index 457f553661..daa7a8d5c4 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -50,7 +50,7 @@ class ValidatesWithTest < ActiveModel::TestCase end end - test "vaidation with class that adds errors" do + test "validation with class that adds errors" do Topic.validates_with(ValidatorThatAddsErrors) topic = Topic.new assert topic.invalid?, "A class that adds errors causes the record to be invalid" diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index d3723fa45b..2934e70c2f 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -26,11 +26,11 @@ class ValidationsTest < ActiveModel::TestCase def test_single_field_validation r = Reply.new r.title = "There's no content!" - assert r.invalid?, "A reply without content shouldn't be saveable" + assert r.invalid?, "A reply without content shouldn't be savable" assert r.after_validation_performed, "after_validation callback should be called" r.content = "Messa content!" - assert r.valid?, "A reply with content should be saveable" + assert r.valid?, "A reply with content should be savable" assert r.after_validation_performed, "after_validation callback should be called" end -- cgit v1.2.3 From c24528fbc94dea9946a563be3bed9559583bdc57 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 18 Mar 2013 20:35:49 +0100 Subject: instruct RDoc to only parse Ruby files under lib [Fixes #9779] --- activemodel/activemodel.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index 51655fe3da..f6f727dab8 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] + s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*.rb'] s.require_path = 'lib' s.add_dependency 'activesupport', version -- cgit v1.2.3 From ddd2c75b2668d4a22681c0c39221bbdfb516cd06 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 18 Mar 2013 21:18:36 +0100 Subject: Revert "instruct RDoc to only parse Ruby files under lib [Fixes #9779]" This reverts commit c24528fbc94dea9946a563be3bed9559583bdc57. --- activemodel/activemodel.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index f6f727dab8..51655fe3da 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*.rb'] + s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] s.require_path = 'lib' s.add_dependency 'activesupport', version -- cgit v1.2.3 From 3ee30ca44a6f965c2e9a60bcf84b45d9be726509 Mon Sep 17 00:00:00 2001 From: wangjohn Date: Thu, 21 Feb 2013 13:40:48 -0500 Subject: The repair_validations helper was not working correctly before because it only cleared the validations that created :validate callbacks. This didn't include the validates created by validates_with, so I've added a method to clear all validations. --- activemodel/CHANGELOG.md | 21 ++++++++++++++ activemodel/lib/active_model/validations.rb | 43 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 6ba0c7cd6b..62d684fd0b 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,5 +1,26 @@ ## Rails 4.0.0 (unreleased) ## +* Added a method so that validations can be easily cleared on a model. + For example: + + class Person + include ActiveModel::Validations + + validates_uniqueness_of :first_name + validate :cannot_be_robot + + def cannot_be_robot + errors.add(:base, 'A person cannot be a robot') if person_is_robot + end + end + + Now, if someone runs `Person.clear_validators!`, then the following occurs: + + Person.validators # => [] + Person._validate_callbacks.empty? # => true + + *John Wang* + * `has_secure_password` does not fail the confirmation validation when assigning empty String to `password` and `password_confirmation`. diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 2db4a25f61..72e3cf310a 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -169,6 +169,49 @@ module ActiveModel _validators.values.flatten.uniq end + # Clears all of the validators and validations. + # + # Note that this will clear anything that is being used to validate + # the model for both the +validates_with+ and +validate+ methods. + # It clears the validators that are created with an invocation of + # +validates_with+ and the callbacks that are set by an invocation + # of +validate+. + # + # class Person + # include ActiveModel::Validations + # + # validates_with MyValidator + # validates_with OtherValidator, on: :create + # validates_with StrictValidator, strict: true + # validate :cannot_be_robot + # + # def cannot_be_robot + # errors.add(:base, 'A person cannot be a robot') if person_is_robot + # end + # end + # + # Person.validators + # # => [ + # # #, + # # #, + # # # + # # ] + # + # If one runs Person.clear_validators! and then checks to see what + # validators this class has, you would obtain: + # + # Person.validators # => [] + # + # Also, the callback set by +validate :cannot_be_robot+ will be erased + # so that: + # + # Person._validate_callbacks.empty? # => true + # + def clear_validators! + reset_callbacks(:validate) + _validators.clear + end + # List all validators that are being used to validate a specific attribute. # # class Person -- cgit v1.2.3 From 9c025ab6e9731dde56186b41ba5d4f216a48c831 Mon Sep 17 00:00:00 2001 From: Intrepidd Date: Wed, 20 Mar 2013 18:24:48 +0000 Subject: Tidying up some require : removing useless sort and homogenizing with the rest of the code the wat the includes are done --- activemodel/lib/active_model/validations.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 72e3cf310a..714cc95b9a 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -376,7 +376,4 @@ module ActiveModel end end -Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path| - filename = File.basename(path) - require "active_model/validations/#{filename}" -end +Dir[File.dirname(__FILE__) + "/validations/*.rb"].each { |file| require file } -- cgit v1.2.3 From c07e1515f7c66f5599cbb3c7e9fe42e166bf3edc Mon Sep 17 00:00:00 2001 From: Charlie Somerville Date: Wed, 20 Mar 2013 20:42:05 +1100 Subject: Add version method to top level modules --- activemodel/lib/active_model/version.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/version.rb b/activemodel/lib/active_model/version.rb index 7586b02037..35c4b30999 100644 --- a/activemodel/lib/active_model/version.rb +++ b/activemodel/lib/active_model/version.rb @@ -1,10 +1,11 @@ module ActiveModel - module VERSION #:nodoc: - MAJOR = 4 - MINOR = 0 - TINY = 0 - PRE = "beta1" + # Returns the version of the currently loaded ActiveModel as a Gem::Version + def self.version + Gem::Version.new "4.0.0.beta1" + end - STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') + module VERSION #:nodoc: + MAJOR, MINOR, TINY, PRE = ActiveModel.version.segments + STRING = ActiveModel.version.to_s end end -- cgit v1.2.3 From 7e07b7bb5da02f63d830eb70aae2835aa7d024ed Mon Sep 17 00:00:00 2001 From: robertomiranda Date: Thu, 21 Mar 2013 22:51:08 -0500 Subject: Change Standard error when is required bcrypt-ruby and fails --- activemodel/lib/active_model/secure_password.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index 9324a1ad0a..abbf96650e 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -43,8 +43,13 @@ module ActiveModel # Load bcrypt-ruby only when has_secure_password is used. # This is to avoid ActiveModel (and by extension the entire framework) # being dependent on a binary library. - gem 'bcrypt-ruby', '~> 3.0.0' - require 'bcrypt' + begin + gem 'bcrypt-ruby', '~> 3.0.0' + require 'bcrypt' + rescue LoadError => e + $stderr.puts "You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install" + raise e + end attr_reader :password -- cgit v1.2.3 From 24fee97581824e6c8fde166ef15b3a28038a9ab8 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Fri, 22 Mar 2013 10:00:17 +0530 Subject: Add Error#full_message test; Fix typos Introduce test on Error#full_message for attribute with underscores; Fix some typos --- activemodel/test/cases/errors_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activemodel') diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 51dcfc37d8..7fae688770 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -225,6 +225,7 @@ class ErrorsTest < ActiveModel::TestCase test 'full_message should return the given message with the attribute name included' do person = Person.new assert_equal "name can not be blank", person.errors.full_message(:name, "can not be blank") + assert_equal "name_test can not be blank", person.errors.full_message(:name_test, "can not be blank") end test 'should return a JSON hash representation of the errors' do -- cgit v1.2.3 From a03ab8cef8c712cad3dafd06c1b483782a1c0eb5 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 22 Mar 2013 14:00:06 -0300 Subject: Remove exception variable from rescue block Just raising from the rescue block will re-raise the exception, so the variable is not necessary. --- activemodel/lib/active_model/secure_password.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index abbf96650e..474cb0aea0 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -46,9 +46,9 @@ module ActiveModel begin gem 'bcrypt-ruby', '~> 3.0.0' require 'bcrypt' - rescue LoadError => e + rescue LoadError $stderr.puts "You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install" - raise e + raise end attr_reader :password -- cgit v1.2.3 From 5f59317ec65655766b12f4ac80ea70dea6b3f945 Mon Sep 17 00:00:00 2001 From: Prathamesh Sonpatki Date: Sun, 24 Mar 2013 02:25:22 +0530 Subject: Fixed typo in activemodel/CHANGELOG --- activemodel/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 62d684fd0b..b4ada0e0b5 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -91,7 +91,7 @@ *Yves Senn* -* Use BCrypt's `MIN_COST` in the test environment for speedier tests when using `has_secure_pasword`. +* Use BCrypt's `MIN_COST` in the test environment for speedier tests when using `has_secure_password`. *Brian Cardarella + Jeremy Kemper + Trevor Turk* -- cgit v1.2.3 From ec1b715b0e6f0a345b94a44b2a03b6044091a706 Mon Sep 17 00:00:00 2001 From: shock_one Date: Sun, 24 Mar 2013 09:34:27 +0200 Subject: Add a method full_messages_for to the Errors class --- activemodel/CHANGELOG.md | 5 +++++ activemodel/lib/active_model/errors.rb | 14 ++++++++++++++ activemodel/test/cases/errors_test.rb | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index b4ada0e0b5..e4c80b1bf8 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Add ActiveModel::Errors#full_messages_for, a method that returns all the error + messages for a given attribute. + + *Volodymyr Shatsky* + * Added a method so that validations can be easily cleared on a model. For example: diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 963e52bff3..485eb1a40a 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -352,6 +352,20 @@ module ActiveModel map { |attribute, message| full_message(attribute, message) } end + # Returns all the full error messages for a given attribute in an array. + # + # class Person + # validates_presence_of :name, :email + # validates_length_of :name, in: 5..30 + # end + # + # person = Person.create() + # person.errors.full_messages_for(:name) + # # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"] + def full_messages_for(attribute) + (get(attribute) || []).map { |message| full_message(attribute, message) } + end + # Returns a full message for a given attribute. # # person.errors.full_message(:name, 'is invalid') # => "Name is invalid" diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 51dcfc37d8..5a9abe0204 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -216,6 +216,26 @@ class ErrorsTest < ActiveModel::TestCase person.errors.add(:name, "can not be nil") assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages end + + test 'full_messages_for should contain all the messages for a given attribute' do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:name, "can not be nil") + assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages_for(:name) + end + + test 'full_messages_for should not contain messages for another attributes' do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:email, "can not be blank") + assert_equal ["name can not be blank"], person.errors.full_messages_for(:name) + end + + test "full_messages_for should return an empty array in case if errors hash doesn't contain a given attribute" do + person = Person.new + person.errors.add(:name, "can not be blank") + assert_equal [], person.errors.full_messages_for(:email) + end test 'full_message should return the given message if attribute equals :base' do person = Person.new -- cgit v1.2.3