Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add a better custom validator example, to be more descriptive in the …

…specs; load a locale file too
  • Loading branch information...
commit 1b288db40c9e5d536f1fe4f5a4e4f892da27ec9a 1 parent a8dcb25
@joecorcoran authored
View
7 spec/each_validator_spec.rb
@@ -2,17 +2,16 @@
describe Judge::EachValidator do
- let(:amv) { User.validators_on(:foo).first }
+ let(:amv) { User.validators_on(:city).first }
specify "custom validators include Judge::EachValidator" do
- FooValidator.include?(Judge::EachValidator).should be_true
+ CityValidator.include?(Judge::EachValidator).should be_true
end
specify "#messages_to_lookup method should return array of messages" do
amv.should respond_to :messages_to_lookup
amv.messages_to_lookup.should be_an Array
- amv.messages_to_lookup.should include :not_foo
- amv.messages_to_lookup.should include "must_be_a_foo"
+ amv.messages_to_lookup.should include :not_valid_city
end
end
View
37 spec/message_collection_spec.rb
@@ -2,13 +2,11 @@
describe Judge::MessageCollection do
- before(:each) do
- @user = FactoryGirl.build(:user)
- end
+ let(:user) { FactoryGirl.build(:user) }
it "has to_hash method which returns messages hash" do
amv = User.validators_on(:name).first
- message_collection = Judge::MessageCollection.new(@user, :name, amv)
+ message_collection = Judge::MessageCollection.new(user, :name, amv)
message_collection.should respond_to :to_hash
message_collection.to_hash.should be_a Hash
end
@@ -16,27 +14,27 @@
describe "base messages" do
it "adds correct base message to messages hash" do
amv = User.validators_on(:name).first
- messages = Judge::MessageCollection.new(@user, :name, amv).to_hash
- messages[:blank].should be_a String
+ messages = Judge::MessageCollection.new(user, :name, amv).to_hash
+ messages[:blank].should eql "Name must not be blank"
end
end
describe "options messages" do
it "adds correct optional messages to messages hash when present (length)" do
amv = User.validators_on(:username).first
- messages = Judge::MessageCollection.new(@user, :username, amv).to_hash
- messages[:too_long].should be_a String
+ messages = Judge::MessageCollection.new(user, :username, amv).to_hash
+ messages[:too_long].should eql "Username is too long (must be less than 10 characters)"
end
it "adds correct optional messages to messages hash when present (numericality)" do
amv = User.validators_on(:age).first
- messages = Judge::MessageCollection.new(@user, :age, amv).to_hash
- messages[:greater_than].should be_a String
+ messages = Judge::MessageCollection.new(user, :age, amv).to_hash
+ messages[:greater_than].should eql "Age must be greater than 13"
end
it "adds nothing to messages hash when optional messages not present" do
amv = User.validators_on(:name).first
- messages = Judge::MessageCollection.new(@user, :name, amv).to_hash
+ messages = Judge::MessageCollection.new(user, :name, amv).to_hash
messages[:too_long].should be_nil
messages[:greater_than].should be_nil
end
@@ -45,13 +43,13 @@
describe "blank messages" do
it "adds blank message to messages hash if applicable" do
amv = User.validators_on(:username).first
- messages = Judge::MessageCollection.new(@user, :username, amv).to_hash
- messages[:blank].should be_a String
+ messages = Judge::MessageCollection.new(user, :username, amv).to_hash
+ messages[:blank].should eql "Username must not be blank"
end
it "does not add blank message to messages hash if allow_blank is true" do
amv = User.validators_on(:country).first
- messages = Judge::MessageCollection.new(@user, :country, amv).to_hash
+ messages = Judge::MessageCollection.new(user, :country, amv).to_hash
messages[:blank].should be_nil
end
end
@@ -59,17 +57,16 @@
describe "integer messages" do
it "adds not_an_integer message to messages hash if only_integer is true" do
amv = User.validators_on(:age).first
- messages = Judge::MessageCollection.new(@user, :age, amv).to_hash
- messages[:not_an_integer].should be_a String
+ messages = Judge::MessageCollection.new(user, :age, amv).to_hash
+ messages[:not_an_integer].should eql "Age must be an integer"
end
end
describe "custom messages" do
it "adds custom messages to messages hash if declared inside EachValidator" do
- amv = User.validators_on(:foo).first
- messages = Judge::MessageCollection.new(@user, :foo, amv).to_hash
- messages[:not_foo].should be_a String
- messages[:must_be_a_foo].should be_a String
+ amv = User.validators_on(:city).first
+ messages = Judge::MessageCollection.new(user, :city, amv).to_hash
+ messages[:not_valid_city].should eql "City must be an approved city"
end
end
View
7 spec/setup.rb
@@ -14,7 +14,7 @@
t.integer :team_id
t.string :time_zone
t.integer :discipline_id
- t.string :foo
+ t.string :city
end
create_table :teams do |t|
t.string :name
@@ -47,7 +47,7 @@ class User < ActiveRecord::Base
validates :team_id, :presence => true
validates :time_zone, :presence => true
validates :discipline_id, :presence => true
- validates :foo, :foo => true
+ validates :city, :city => { :hello => ["abc"] }
end
class Team < ActiveRecord::Base; end
@@ -64,6 +64,9 @@ class Discipline < ActiveRecord::Base
belongs_to :user
end
+# i18n locale file
+I18n.load_path << File.expand_path("spec/support/locale/en.yml")
+
# hack to stop #url_for error
module ActionDispatch::Routing::PolymorphicRoutes
def polymorphic_path(record_or_hash_or_array, options = {})
View
18 spec/support/locale/en.yml
@@ -0,0 +1,18 @@
+en:
+ activerecord:
+ errors:
+ models:
+ user:
+ attributes:
+ city:
+ not_valid_city: "%{attribute} must be an approved city"
+ errors:
+ attributes:
+ city:
+ not_valid_city: "This is never reached"
+
+ messages:
+ not_an_integer: "%{attribute} must be an integer"
+ blank: "%{attribute} must not be blank"
+ greater_than: "%{attribute} must be greater than %{count}"
+ too_long: "%{attribute} is too long (must be less than %{count} characters)"
View
2  spec/support/spec_helper.rb
@@ -4,7 +4,7 @@
require "judge"
require "rspec"
require "factory_girl"
-require "support/validators/foo_validator"
+require "support/validators/city_validator"
require "setup"
require "support/factories"
View
3  spec/support/validators/city_validator.rb
@@ -0,0 +1,3 @@
+class CityValidator < ActiveModel::EachValidator
+ declare_messages :not_valid_city
+end
View
3  spec/support/validators/foo_validator.rb
@@ -1,3 +0,0 @@
-class FooValidator < ActiveModel::EachValidator
- declare_messages :not_foo, "must_be_a_foo"
-end
Please sign in to comment.
Something went wrong with that request. Please try again.