judge 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,83 +1,72 @@
1
- require "spec_helper"
1
+ require "support/spec_helper"
2
2
 
3
3
  describe Judge::MessageCollection do
4
4
 
5
- before(:each) do
6
- @user = FactoryGirl.build(:user)
7
- end
8
-
9
- it "has hash of messages in messages attr" do
10
- amv = User.validators_on(:name).first
11
- message_collection = Judge::MessageCollection.new(@user, :name, amv)
12
- message_collection.messages.should be_a Hash
13
- end
5
+ let(:user) { FactoryGirl.build(:user) }
14
6
 
15
7
  it "has to_hash method which returns messages hash" do
16
8
  amv = User.validators_on(:name).first
17
- message_collection = Judge::MessageCollection.new(@user, :name, amv)
9
+ message_collection = Judge::MessageCollection.new(user, :name, amv)
18
10
  message_collection.should respond_to :to_hash
19
11
  message_collection.to_hash.should be_a Hash
20
12
  end
21
13
 
22
- describe "#generate_base!" do
14
+ describe "base messages" do
23
15
  it "adds correct base message to messages hash" do
24
16
  amv = User.validators_on(:name).first
25
- message_collection = Judge::MessageCollection.new(@user, :name, amv, :generate => false)
26
- message_collection.to_hash.should be_empty
27
- message_collection.send(:generate_base!)
28
- message_collection.to_hash[:blank].should be_a String
17
+ messages = Judge::MessageCollection.new(user, :name, amv).to_hash
18
+ messages[:blank].should eql "Name must not be blank"
29
19
  end
30
20
  end
31
21
 
32
- describe "#generate_options!" do
22
+ describe "options messages" do
33
23
  it "adds correct optional messages to messages hash when present (length)" do
34
24
  amv = User.validators_on(:username).first
35
- message_collection = Judge::MessageCollection.new(@user, :username, amv, :generate => false)
36
- message_collection.to_hash.should be_empty
37
- message_collection.send(:generate_options!)
38
- message_collection.to_hash[:too_long].should be_a String
25
+ messages = Judge::MessageCollection.new(user, :username, amv).to_hash
26
+ messages[:too_long].should eql "Username is too long (must be less than 10 characters)"
39
27
  end
40
28
 
41
29
  it "adds correct optional messages to messages hash when present (numericality)" do
42
30
  amv = User.validators_on(:age).first
43
- message_collection = Judge::MessageCollection.new(@user, :age, amv, :generate => false)
44
- message_collection.to_hash.should be_empty
45
- message_collection.send(:generate_options!)
46
- message_collection.to_hash[:greater_than].should be_a String
31
+ messages = Judge::MessageCollection.new(user, :age, amv).to_hash
32
+ messages[:greater_than].should eql "Age must be greater than 13"
47
33
  end
48
34
 
49
35
  it "adds nothing to messages hash when optional messages not present" do
50
36
  amv = User.validators_on(:name).first
51
- message_collection = Judge::MessageCollection.new(@user, :name, amv, :generate => false)
52
- message_collection.send(:generate_options!)
53
- message_collection.to_hash.should be_empty
37
+ messages = Judge::MessageCollection.new(user, :name, amv).to_hash
38
+ messages[:too_long].should be_nil
39
+ messages[:greater_than].should be_nil
54
40
  end
55
41
  end
56
42
 
57
- describe "#generate_blank!" do
43
+ describe "blank messages" do
58
44
  it "adds blank message to messages hash if applicable" do
59
45
  amv = User.validators_on(:username).first
60
- message_collection = Judge::MessageCollection.new(@user, :username, amv, :generate => false)
61
- message_collection.to_hash.should be_empty
62
- message_collection.send(:generate_blank!)
63
- message_collection.to_hash[:blank].should be_a String
46
+ messages = Judge::MessageCollection.new(user, :username, amv).to_hash
47
+ messages[:blank].should eql "Username must not be blank"
64
48
  end
65
49
 
66
50
  it "does not add blank message to messages hash if allow_blank is true" do
67
51
  amv = User.validators_on(:country).first
68
- message_collection = Judge::MessageCollection.new(@user, :country, amv, :generate => false)
69
- message_collection.send(:generate_blank!)
70
- message_collection.to_hash.should be_empty
52
+ messages = Judge::MessageCollection.new(user, :country, amv).to_hash
53
+ messages[:blank].should be_nil
71
54
  end
72
55
  end
73
56
 
74
- describe "#generate_integer!" do
57
+ describe "integer messages" do
75
58
  it "adds not_an_integer message to messages hash if only_integer is true" do
76
59
  amv = User.validators_on(:age).first
77
- message_collection = Judge::MessageCollection.new(@user, :age, amv, :generate => false)
78
- message_collection.to_hash.should be_empty
79
- message_collection.send(:generate_integer!)
80
- message_collection.to_hash[:not_an_integer].should be_a String
60
+ messages = Judge::MessageCollection.new(user, :age, amv).to_hash
61
+ messages[:not_an_integer].should eql "Age must be an integer"
62
+ end
63
+ end
64
+
65
+ describe "custom messages" do
66
+ it "adds custom messages to messages hash if declared inside EachValidator" do
67
+ amv = User.validators_on(:city).first
68
+ messages = Judge::MessageCollection.new(user, :city, amv).to_hash
69
+ messages[:not_valid_city].should eql "City must be an approved city"
81
70
  end
82
71
  end
83
72
 
data/spec/setup.rb CHANGED
@@ -1,4 +1,4 @@
1
- # setup fake ActiveRecord class for tests
1
+ # setup ActiveRecord classes for tests
2
2
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
3
3
  ActiveRecord::Schema.define(:version => 1) do
4
4
  create_table :users do |t|
@@ -14,6 +14,7 @@ ActiveRecord::Schema.define(:version => 1) do
14
14
  t.integer :team_id
15
15
  t.string :time_zone
16
16
  t.integer :discipline_id
17
+ t.string :city
17
18
  end
18
19
  create_table :teams do |t|
19
20
  t.string :name
@@ -46,6 +47,7 @@ class User < ActiveRecord::Base
46
47
  validates :team_id, :presence => true
47
48
  validates :time_zone, :presence => true
48
49
  validates :discipline_id, :presence => true
50
+ validates :city, :city => true
49
51
  end
50
52
 
51
53
  class Team < ActiveRecord::Base; end
@@ -62,6 +64,9 @@ class Discipline < ActiveRecord::Base
62
64
  belongs_to :user
63
65
  end
64
66
 
67
+ # i18n locale file
68
+ I18n.load_path << File.expand_path("spec/support/locale/en.yml")
69
+
65
70
  # hack to stop #url_for error
66
71
  module ActionDispatch::Routing::PolymorphicRoutes
67
72
  def polymorphic_path(record_or_hash_or_array, options = {})
File without changes
@@ -0,0 +1,18 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ models:
5
+ user:
6
+ attributes:
7
+ city:
8
+ not_valid_city: "%{attribute} must be an approved city"
9
+ errors:
10
+ attributes:
11
+ city:
12
+ not_valid_city: "This is never reached"
13
+
14
+ messages:
15
+ not_an_integer: "%{attribute} must be an integer"
16
+ blank: "%{attribute} must not be blank"
17
+ greater_than: "%{attribute} must be greater than %{count}"
18
+ too_long: "%{attribute} is too long (must be less than %{count} characters)"
@@ -0,0 +1,72 @@
1
+ # setup fake ActiveRecord class for tests
2
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
3
+ ActiveRecord::Schema.define(:version => 1) do
4
+ create_table :users do |t|
5
+ t.string :name
6
+ t.string :username
7
+ t.string :country
8
+ t.integer :age
9
+ t.text :bio
10
+ t.string :password
11
+ t.boolean :accepted
12
+ t.text :gender
13
+ t.date :dob
14
+ t.integer :team_id
15
+ t.string :time_zone
16
+ t.integer :discipline_id
17
+ t.string :foo
18
+ end
19
+ create_table :teams do |t|
20
+ t.string :name
21
+ end
22
+ create_table :categories do |t|
23
+ t.string :name
24
+ end
25
+ create_table :sports do |t|
26
+ t.string :name
27
+ t.integer :category_id
28
+ end
29
+ create_table :disciplines do |t|
30
+ t.string :name
31
+ t.integer :sport_id
32
+ end
33
+ end
34
+
35
+ class User < ActiveRecord::Base
36
+ belongs_to :team
37
+
38
+ validates :name, :presence => true
39
+ validates :username, :length => { :maximum => 10 }
40
+ validates :country, :format => { :with => /[A-Za-z]/, :allow_blank => true }
41
+ validates :age, :numericality => { :only_integer => true, :greater_than => 13 }
42
+ validates :bio, :presence => true
43
+ validates :password, :format => { :with => /.+/ }, :confirmation => true
44
+ validates :accepted, :acceptance => true
45
+ validates :gender, :inclusion => { :in => ["male", "female", "other", "withheld"] }
46
+ validates :dob, :presence => true
47
+ validates :team_id, :presence => true
48
+ validates :time_zone, :presence => true
49
+ validates :discipline_id, :presence => true
50
+ validates :foo, :foo => true
51
+ end
52
+
53
+ class Team < ActiveRecord::Base; end
54
+
55
+ class Category < ActiveRecord::Base
56
+ has_many :sports
57
+ end
58
+ class Sport < ActiveRecord::Base
59
+ belongs_to :category
60
+ has_many :disciplines
61
+ end
62
+ class Discipline < ActiveRecord::Base
63
+ belongs_to :sport
64
+ belongs_to :user
65
+ end
66
+
67
+ # hack to stop #url_for error
68
+ module ActionDispatch::Routing::PolymorphicRoutes
69
+ def polymorphic_path(record_or_hash_or_array, options = {})
70
+ ""
71
+ end
72
+ end
@@ -4,9 +4,9 @@ require "action_view"
4
4
  require "judge"
5
5
  require "rspec"
6
6
  require "factory_girl"
7
+ require "support/validators/city_validator"
7
8
  require "setup"
8
-
9
- require_relative "factories"
9
+ require "support/factories"
10
10
 
11
11
  RSpec.configure do |config|
12
12
  config.color_enabled = true
@@ -0,0 +1,9 @@
1
+ class CityValidator < ActiveModel::EachValidator
2
+ declare_messages :not_valid_city
3
+
4
+ def validate_each(record, attribute, value)
5
+ unless ["London", "New York City"].include? value
6
+ record.errors.add :attribute, :not_valid_city
7
+ end
8
+ end
9
+ end
@@ -1,4 +1,4 @@
1
- require "spec_helper"
1
+ require "support/spec_helper"
2
2
 
3
3
  describe Judge::ValidatorCollection do
4
4
 
@@ -1,15 +1,11 @@
1
- require "spec_helper"
1
+ require "support/spec_helper"
2
2
 
3
3
  describe Judge::Validator do
4
4
 
5
5
  before(:each) do
6
6
  user = FactoryGirl.build(:user)
7
7
  amv = User.validators_on(:name).first
8
- @validator = Judge::Validator.new(amv, :name, Judge::MessageCollection.new(user, :name, amv))
9
- end
10
-
11
- it "has an original validator in validator attr" do
12
- @validator.active_model_validator.should be_a ActiveModel::Validations::PresenceValidator
8
+ @validator = Judge::Validator.new(user, :name, amv)
13
9
  end
14
10
 
15
11
  it "has correct kind attr" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: judge
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-01 00:00:00.000000000 Z
12
+ date: 2012-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jasmine
16
- requirement: &2151873000 !ruby/object:Gem::Requirement
16
+ requirement: &2168506340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.1.2
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2151873000
24
+ version_requirements: *2168506340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &2151872120 !ruby/object:Gem::Requirement
27
+ requirement: &2168505620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.2'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2151872120
35
+ version_requirements: *2168505620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2151871360 !ruby/object:Gem::Requirement
38
+ requirement: &2168504460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,21 +43,21 @@ dependencies:
43
43
  version: '2.8'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2151871360
46
+ version_requirements: *2168504460
47
47
  - !ruby/object:Gem::Dependency
48
- name: sqlite3-ruby
49
- requirement: &2151870580 !ruby/object:Gem::Requirement
48
+ name: sqlite3
49
+ requirement: &2168503260 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 1.3.3
54
+ version: 1.3.5
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2151870580
57
+ version_requirements: *2168503260
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: factory_girl
60
- requirement: &2151869300 !ruby/object:Gem::Requirement
60
+ requirement: &2168501980 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '2.6'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2151869300
68
+ version_requirements: *2168501980
69
69
  description: Validate Rails 3 forms on the client side, cleanly
70
70
  email: joe@tribesports.com
71
71
  executables: []
@@ -84,14 +84,16 @@ files:
84
84
  - lib/generators/judge/templates/judge.js
85
85
  - lib/generators/judge/templates/underscore.js
86
86
  - lib/judge.rb
87
+ - lib/judge/each_validator.rb
87
88
  - lib/judge/form_builder.rb
88
89
  - lib/judge/html.rb
89
90
  - lib/judge/message_collection.rb
91
+ - lib/judge/message_config.rb
90
92
  - lib/judge/validator.rb
91
93
  - lib/judge/validator_collection.rb
92
94
  - lib/judge/version.rb
93
95
  - lib/tasks/js.rake
94
- - spec/factories.rb
96
+ - spec/each_validator_spec.rb
95
97
  - spec/form_builder_spec.rb
96
98
  - spec/html_spec.rb
97
99
  - spec/javascripts/JudgeSpec.js
@@ -107,7 +109,11 @@ files:
107
109
  - spec/javascripts/support/runner.js
108
110
  - spec/message_collection_spec.rb
109
111
  - spec/setup.rb
110
- - spec/spec_helper.rb
112
+ - spec/support/factories.rb
113
+ - spec/support/locale/en.yml
114
+ - spec/support/setup.rb
115
+ - spec/support/spec_helper.rb
116
+ - spec/support/validators/city_validator.rb
111
117
  - spec/validator_collection_spec.rb
112
118
  - spec/validator_spec.rb
113
119
  homepage: http://github.com/joecorcoran/judge
@@ -124,7 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
130
  version: '0'
125
131
  segments:
126
132
  - 0
127
- hash: -196777786245842786
133
+ hash: -172077303514846172
128
134
  required_rubygems_version: !ruby/object:Gem::Requirement
129
135
  none: false
130
136
  requirements:
@@ -133,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
139
  version: '0'
134
140
  segments:
135
141
  - 0
136
- hash: -196777786245842786
142
+ hash: -172077303514846172
137
143
  requirements: []
138
144
  rubyforge_project:
139
145
  rubygems_version: 1.8.15
@@ -141,7 +147,7 @@ signing_key:
141
147
  specification_version: 3
142
148
  summary: Simple client side ActiveModel::Validators
143
149
  test_files:
144
- - spec/factories.rb
150
+ - spec/each_validator_spec.rb
145
151
  - spec/form_builder_spec.rb
146
152
  - spec/html_spec.rb
147
153
  - spec/javascripts/JudgeSpec.js
@@ -157,6 +163,10 @@ test_files:
157
163
  - spec/javascripts/support/runner.js
158
164
  - spec/message_collection_spec.rb
159
165
  - spec/setup.rb
160
- - spec/spec_helper.rb
166
+ - spec/support/factories.rb
167
+ - spec/support/locale/en.yml
168
+ - spec/support/setup.rb
169
+ - spec/support/spec_helper.rb
170
+ - spec/support/validators/city_validator.rb
161
171
  - spec/validator_collection_spec.rb
162
172
  - spec/validator_spec.rb