daily_affirmation 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 067ab891f1fade3f4a26150786cfcc87ee472280
4
- data.tar.gz: f05e7558ace028cfecd5b4d9d43220943bf1eaae
3
+ metadata.gz: 8bebab94b6507fded2c3c6b8620064c31753fbd7
4
+ data.tar.gz: 7ec0f8b962d6981a99f62d3475524437ecdec690
5
5
  SHA512:
6
- metadata.gz: cb9978830c16cbce9d3d7e27c028ade54c5ffd3a567600e32a36c1598b1636b105392a44217f75654f09f80c392b063907bb8c7e196b5a0852496ede8dfb794c
7
- data.tar.gz: e3288aef6c9dd1ff25b5742199dd2e184973e8fb10d6f4818c5f0f7facf3f8f5a084acb51e337731d3f6b3d900f21566f4dc1ca15a94bbb1422516458d78994f
6
+ metadata.gz: b73c9f489458982f747535fa715dd90f6a10d8f7cf7814b9f00c247b99aa521f4b53a1a70d56794b38961aabeef41f4722ac98931afd535bc160a21460aa6dbf
7
+ data.tar.gz: 3cd11055f9b7fc591796320fb951324d522481835e69e8e588bf6360a425f9e642945d8929ce86189d1f13b1c984ba0d8df4022ca8013793aa83a4627ec17e99
data/README.md CHANGED
@@ -51,12 +51,12 @@ TeamAffirmation.new(team1).valid? #=> false
51
51
 
52
52
  ## Roadmap
53
53
 
54
- - Provide all the validations ActiveModel does
55
- - Add a way to build custom validations (example validating a password)
54
+ - Allow error messages to use i18n.
55
+ - Add a way to build custom validations (example validating a password).
56
56
 
57
57
  ## Contributing
58
58
 
59
- 1. Fork it
59
+ 1. Fork it ( http://github.com/teamsnap/daily_affirmation/fork )
60
60
  2. Create your feature branch (`git checkout -b my-new-feature`)
61
61
  3. Commit your changes (`git commit -am 'Add some feature'`)
62
62
  4. Push to the branch (`git push origin my-new-feature`)
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec", "~> 3.0.0.beta1"
24
+ spec.add_development_dependency "i18n", "~> 0.6.9"
24
25
  end
@@ -16,11 +16,28 @@ module DailyAffirmation
16
16
  end
17
17
 
18
18
  def error_message
19
- raise StandardError, "must implement #error_message"
19
+ i18n_error_message(:none)
20
20
  end
21
21
 
22
22
  private
23
23
 
24
24
  attr_accessor :object, :attribute, :value, :opts
25
+
26
+ def i18n_error_message(type, default: "#{attribute} failed validation")
27
+ if defined?(I18n)
28
+ args = opts.merge(
29
+ {
30
+ :default => [
31
+ :"daily_affirmation.errors.messages.#{type}", default
32
+ ],
33
+ :attribute => attribute,
34
+ :value => value
35
+ }
36
+ )
37
+ I18n.t(:"daily_affirmation.errors.messages.#{type}.#{attribute}", args)
38
+ else
39
+ default
40
+ end
41
+ end
25
42
  end
26
43
  end
@@ -9,7 +9,9 @@ module DailyAffirmation
9
9
  end
10
10
 
11
11
  def error_message
12
- @error_message ||= "#{attribute} must be blank"
12
+ @error_message ||= i18n_error_message(
13
+ :absence, :default => "#{attribute} must be blank"
14
+ )
13
15
  end
14
16
  end
15
17
  end
@@ -8,7 +8,9 @@ module DailyAffirmation
8
8
  end
9
9
 
10
10
  def error_message
11
- @error_message ||= "#{attribute} must be accepted"
11
+ @error_message ||= i18n_error_message(
12
+ :acceptance, :default => "#{attribute} must be accepted"
13
+ )
12
14
  end
13
15
  end
14
16
  end
@@ -8,7 +8,9 @@ module DailyAffirmation
8
8
  end
9
9
 
10
10
  def error_message
11
- @error_message ||= "#{attribute} doesn't match confirmation"
11
+ @error_message ||= i18n_error_message(
12
+ :confirmation, :default => "#{attribute} doesn't match confirmation"
13
+ )
12
14
  end
13
15
  end
14
16
  end
@@ -9,7 +9,9 @@ module DailyAffirmation
9
9
  end
10
10
 
11
11
  def error_message
12
- @error_message ||= "#{attribute} is reserved"
12
+ @error_message ||= i18n_error_message(
13
+ :exclusion, :default => "#{attribute} is reserved"
14
+ )
13
15
  end
14
16
  end
15
17
  end
@@ -8,7 +8,9 @@ module DailyAffirmation
8
8
  end
9
9
 
10
10
  def error_message
11
- @error_message ||= "#{attribute} is invalid"
11
+ @error_message ||= i18n_error_message(
12
+ :format, :default => "#{attribute} is invalid"
13
+ )
12
14
  end
13
15
  end
14
16
  end
@@ -8,7 +8,10 @@ module DailyAffirmation
8
8
  end
9
9
 
10
10
  def error_message
11
- @error_message ||= "#{attribute} is not included in #{opts[:list]}"
11
+ @error_message ||= i18n_error_message(
12
+ :inclusion,
13
+ :default => "#{attribute} is not included in #{opts[:list]}"
14
+ )
12
15
  end
13
16
  end
14
17
  end
@@ -8,7 +8,10 @@ module DailyAffirmation
8
8
  end
9
9
 
10
10
  def error_message
11
- @error_message ||= "#{attribute} is the wrong length (allowed #{opts[:range]})"
11
+ @error_message ||= i18n_error_message(
12
+ :length,
13
+ :default => "#{attribute} is the wrong length (allowed #{opts[:range]})"
14
+ )
12
15
  end
13
16
  end
14
17
  end
@@ -8,7 +8,9 @@ module DailyAffirmation
8
8
  end
9
9
 
10
10
  def error_message
11
- @error_message ||= "#{attribute} is not a number"
11
+ @error_message ||= i18n_error_message(
12
+ :numericality, :default => "#{attribute} is not a number"
13
+ )
12
14
  end
13
15
  end
14
16
  end
@@ -8,7 +8,9 @@ module DailyAffirmation
8
8
  end
9
9
 
10
10
  def error_message
11
- @error_message ||= "#{attribute} can't be blank"
11
+ @error_message ||= i18n_error_message(
12
+ :presence, :default => "#{attribute} can't be blank"
13
+ )
12
14
  end
13
15
 
14
16
  private
@@ -1,3 +1,3 @@
1
1
  module DailyAffirmation
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,4 @@
1
+ en:
2
+ daily_affirmation:
3
+ errors:
4
+ messages:
@@ -0,0 +1,5 @@
1
+ en:
2
+ daily_affirmation:
3
+ errors:
4
+ messages:
5
+ none: "egads man, you must have %{attribute} (%{range}, %{value})"
@@ -0,0 +1,6 @@
1
+ en:
2
+ daily_affirmation:
3
+ errors:
4
+ messages:
5
+ none:
6
+ age: must include age yo!
@@ -11,14 +11,55 @@ describe "Validator" do
11
11
  )
12
12
  end
13
13
 
14
- it "requires #error_message to be implemented" do
14
+ it "uses a generic error message if one is not provied" do
15
15
  cls = Class.new(DailyAffirmation::Validator) do
16
16
  def valid?; false; end
17
17
  end
18
18
  obj = double(:age => 13)
19
19
 
20
- expect { cls.new(obj, :age).affirm }.to raise_error(
21
- StandardError, "must implement #error_message"
22
- )
20
+ expect(cls.new(obj, :age).error_message).to eq("age failed validation")
21
+ end
22
+
23
+ context "i18n required" do
24
+ before(:all) { require "i18n" }
25
+ after(:all) { Object.send(:remove_const, :I18n) if defined?(I18n) }
26
+
27
+ it "uses specific translation if available" do
28
+ I18n.load_path = [File.expand_path("../support/specific.yml", __FILE__)]
29
+ I18n.backend.load_translations
30
+
31
+ cls = Class.new(DailyAffirmation::Validator) do
32
+ def valid?; false; end
33
+ end
34
+ obj = double(:age => 13)
35
+
36
+ expect(cls.new(obj, :age).error_message).to eq("must include age yo!")
37
+ end
38
+
39
+ it "uses generic translation if specific is not available" do
40
+ I18n.load_path = [File.expand_path("../support/generic.yml", __FILE__)]
41
+ I18n.backend.load_translations
42
+
43
+ cls = Class.new(DailyAffirmation::Validator) do
44
+ def valid?; false; end
45
+ end
46
+ obj = double(:age => 13)
47
+
48
+ expect(cls.new(obj, :age, :range => 10..20).error_message).to eq(
49
+ "egads man, you must have age (10..20, 13)"
50
+ )
51
+ end
52
+
53
+ it "uses default if both translations are unavailable" do
54
+ I18n.load_path = [File.expand_path("../support/default.yml", __FILE__)]
55
+ I18n.backend.load_translations
56
+
57
+ cls = Class.new(DailyAffirmation::Validator) do
58
+ def valid?; false; end
59
+ end
60
+ obj = double(:age => 13)
61
+
62
+ expect(cls.new(obj, :age).error_message).to eq("age failed validation")
63
+ end
23
64
  end
24
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daily_affirmation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.0.0.beta1
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.6.9
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.6.9
55
69
  description: A simple library for external validations of POROs
56
70
  email:
57
71
  - oss@teamsnap.com
@@ -80,6 +94,9 @@ files:
80
94
  - lib/daily_affirmation/version.rb
81
95
  - spec/daily_affirmation_spec.rb
82
96
  - spec/spec_helper.rb
97
+ - spec/support/default.yml
98
+ - spec/support/generic.yml
99
+ - spec/support/specific.yml
83
100
  - spec/validator_spec.rb
84
101
  - spec/validators/absence_validator_spec.rb
85
102
  - spec/validators/acceptance_validator_spec.rb
@@ -117,6 +134,9 @@ summary: A simple library for external validations of POROs
117
134
  test_files:
118
135
  - spec/daily_affirmation_spec.rb
119
136
  - spec/spec_helper.rb
137
+ - spec/support/default.yml
138
+ - spec/support/generic.yml
139
+ - spec/support/specific.yml
120
140
  - spec/validator_spec.rb
121
141
  - spec/validators/absence_validator_spec.rb
122
142
  - spec/validators/acceptance_validator_spec.rb