activemodel-validators 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 795554ae591fe014e540d898361f2a4aaa2a588a
4
- data.tar.gz: c33cdc6d6417a0dfddec20ed28b0f3c43032b3eb
3
+ metadata.gz: 09e9d34a28a1db45b7ff83d9c3fb77e92625a333
4
+ data.tar.gz: d42c29a1b6138e1302bba11d9a1c89a8f4eb8e37
5
5
  SHA512:
6
- metadata.gz: b3e2e2d049271ea0b24acfce842c5db974f11ff81203faed628c688823ec30d8b5113b8f973b288482a29a82fa271aee6011406d884fe462592c5b2606cbd794
7
- data.tar.gz: e307cc4d0699d8d541bbb0c7a8b021f765c005023c5840222ad9e8b98d7c1679ca400d238c4c87f5109b13624883d4e948d9e9fa87cc0e3e4d3a702474017d85
6
+ metadata.gz: 852cc4d4c762f37c00ae02cea0fb5dd5970301e1ce114f9b2e47473e1deaa01396e1eb734f4b96ac8edc5113953cad4c108d7facebac4d464d6fad97910abd3f
7
+ data.tar.gz: 447e539fb66b6451c13962167e96b6d73a25337a48630493b4fbb9a216204523a07b5df109b1545023188b4d48cbb420bb6f8befcc0356c88e6f8692fded844b
data/Readme.md CHANGED
@@ -20,6 +20,7 @@ class Thing < ActiveRecord::Base
20
20
 
21
21
  validates :model, restrict_to: {allowed_options: ['Model A', 'Model B']}, allow_blank: true
22
22
 
23
+ validates_with AtLeastOnePresentValidator, at_least_one_of: [:home_phone, :work_phone]
23
24
  validates_with AtLeastOnePresentValidator, at_least_one_of: [:parent_1_home_phone, :parent_1_work_phone, :parent_1_mobile_phone], message: :at_least_one_phone_parent_1
24
25
 
25
26
  validates :option_b, blank: {message: :must_be_blank_if_option_a}, if: :option_a?
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_runtime_dependency "activemodel"
22
+ s.add_development_dependency "activerecord"
23
+ s.add_development_dependency "sqlite3"
22
24
 
23
25
  # For config/locales
24
26
  #s.add_runtime_dependency "railties"
@@ -10,3 +10,9 @@ en:
10
10
  sum_of_with_addends: "must be the sum of %{attr_names} (%{addends} = %{sum}) but was %{value}"
11
11
 
12
12
  must_be_blank: "must be blank"
13
+
14
+ at_least_one_field_must_be_present: "at least 1 of these fields must be present: %{attr_names_list}"
15
+
16
+ year_invalid: "is not a valid year (must be a number between %{min} and %{max})"
17
+ date_invalid: "is not a valid date (must be in %{expected_format} format)"
18
+ must_have_at_least_10_digits: "must have at least 10 digits"
@@ -5,7 +5,8 @@ module ActiveModel
5
5
  attr_names = options[:at_least_one_of]
6
6
  #puts %(attr_names=#{attr_names.inspect})
7
7
  unless attr_names.map {|attr_name| record[attr_name]}.any?(&:present?)
8
- record.errors.add :base, options[:message] || "at least 1 of these fields must be present: #{attr_names.to_sentence(last_word_connector: ', or ')}"
8
+ record.errors.add :base, options[:message] || :at_least_one_field_must_be_present,
9
+ attr_names_list: attr_names.to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')
9
10
  end
10
11
  end
11
12
  end
@@ -8,7 +8,8 @@ module ActiveModel
8
8
  raw_value ||= value
9
9
 
10
10
  if raw_value.present? and !value.is_a?(Date)
11
- record.errors.add attr_name, :date_invalid
11
+ record.errors.add attr_name, :date_invalid,
12
+ expected_format: expected_format
12
13
  return # don't want it to show *both* errors (:date_invalid and :blank)
13
14
  end
14
15
 
@@ -17,6 +18,15 @@ module ActiveModel
17
18
  record.errors.add attr_name, message
18
19
  end
19
20
  end
21
+
22
+ private
23
+ # Note that the format given here only affects the error message that you show your users. It
24
+ # is up to you to actually change your model to understand or convert from values supplied by
25
+ # users in a particular format.
26
+ # Some formats that ActiveRecord already understands include 'yyyy-mm-dd' and 'dd/mm/yyyy'.
27
+ def expected_format
28
+ options[:expected_format] || 'yyyy-mm-dd'
29
+ end
20
30
  end
21
31
  end
22
32
  end
@@ -2,7 +2,7 @@ class PhoneValidator < ActiveModel::EachValidator
2
2
  def validate_each(record, attribute, value)
3
3
  return if value.blank?
4
4
  unless value.to_s.gsub(/[\s-]+/, '').length >= 10
5
- record.errors[attribute] << "must have at least 10 digits"
5
+ record.errors.add attribute, :must_have_at_least_10_digits
6
6
  end
7
7
  end
8
8
  end
@@ -1,5 +1,5 @@
1
1
  module Activemodel
2
2
  module Validators
3
- Version = "1.0.0"
3
+ Version = "1.1.0"
4
4
  end
5
5
  end
@@ -1,8 +1,21 @@
1
- class YearValidator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- return if value.blank?
4
- unless ((min=1700) .. (max=2200)).include? value
5
- record.errors.add attribute, :year_invalid, min: min, max: max
1
+ module ActiveModel
2
+ module Validations
3
+ class YearValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ return if value.blank?
6
+ unless (min .. max).include? value
7
+ record.errors.add attribute, :year_invalid, min: min, max: max
8
+ end
9
+ end
10
+
11
+ private
12
+ def min
13
+ options[:min] || 1700
14
+ end
15
+
16
+ def max
17
+ options[:max] || 2200
18
+ end
6
19
  end
7
20
  end
8
21
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ ActiveModel::Validations::AtLeastOnePresentValidator
4
+ class ActiveModel::Validations::AtLeastOnePresentValidator
5
+
6
+ class TestModel < Struct.new(:home_phone, :work_phone)
7
+ include ActiveModel::Validations
8
+
9
+ validates_with AtLeastOnePresentValidator, at_least_one_of: [:home_phone, :work_phone]
10
+ end
11
+
12
+ describe TestModel do
13
+
14
+ context 'when we supply none of the required fields' do
15
+ before { subject.valid? }
16
+ its('errors.messages') { should == {base: ["at least 1 of these fields must be present: home_phone or work_phone"]} }
17
+ end
18
+
19
+ context 'when we only have home_phone' do
20
+ before { subject.home_phone = '555-555-5555' }
21
+ before { subject.valid? }
22
+ its('errors.messages') { should == {} }
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::User, 'ActiveModel::Validations::DateValidator' do
4
+
5
+ shared_examples_for 'should not allow invalid dates' do |
6
+ attr_name: nil,
7
+ message: "is not a valid date (must be in yyyy-mm-dd format)"
8
+ |
9
+ it { should_not allow_value('non-date'). for(attr_name).with_message(message) }
10
+ it { should allow_value('2013-01-01').for(attr_name) }
11
+ it { should allow_value('30/12/2013').for(attr_name) }
12
+ end
13
+
14
+ describe 'date: true' do
15
+ it { should allow_value(nil).for(:optional_date) }
16
+ it { should allow_value(''). for(:optional_date) }
17
+ it_behaves_like 'should not allow invalid dates', attr_name: :optional_date
18
+ end
19
+
20
+ describe "date: {expected_format: 'dd/mm/yyyy'}" do
21
+ it { should allow_value(nil).for(:custom_format_date) }
22
+ it { should allow_value(''). for(:custom_format_date) }
23
+ it_behaves_like 'should not allow invalid dates', attr_name: :custom_format_date,
24
+ message: "is not a valid date (must be in dd/mm/yyyy format)"
25
+ end
26
+
27
+ describe 'date: {required: true}' do
28
+ it { should_not allow_value(nil).for(:end_date).with_message("can't be blank") }
29
+ it { should_not allow_value(''). for(:end_date).with_message("can't be blank") }
30
+ it_behaves_like 'should not allow invalid dates', attr_name: :end_date
31
+ end
32
+
33
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection \
4
+ adapter: "sqlite3",
5
+ database: ":memory:"
@@ -1,3 +1,11 @@
1
- class User < Struct.new(:name, :age)
2
- include ActiveModel::Validations
1
+ ActiveRecord::Migration.create_table :users do |t|
2
+ t.date :optional_date
3
+ t.date :end_date
4
+ t.date :custom_format_date
5
+ end
6
+
7
+ class User < ActiveRecord::Base
8
+ validates :optional_date, date: true
9
+ validates :custom_format_date, date: {expected_format: 'dd/mm/yyyy'}
10
+ validates :end_date, date: {required: true}
3
11
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ ActiveModel::Validations::YearValidator
4
+ class ActiveModel::Validations::YearValidator
5
+
6
+ class TestModel < Struct.new(:graduation_year, :ancient_person_birth_year)
7
+ include ActiveModel::Validations
8
+
9
+ validates :graduation_year, year: true
10
+ validates :ancient_person_birth_year, year: {min: 1000, max: 2000}
11
+ end
12
+
13
+ describe TestModel do
14
+
15
+ it { should_not allow_value(1699).for(:graduation_year).with_message('is not a valid year (must be a number between 1700 and 2200)') }
16
+ it { should allow_value(1700).for(:graduation_year) }
17
+ it { should allow_value(2200).for(:graduation_year) }
18
+ it { should_not allow_value(2201).for(:graduation_year).with_message('is not a valid year (must be a number between 1700 and 2200)') }
19
+
20
+ it { should_not allow_value( 999).for(:ancient_person_birth_year).with_message('is not a valid year (must be a number between 1000 and 2000)') }
21
+ it { should allow_value(1000).for(:ancient_person_birth_year) }
22
+ it { should allow_value(2000).for(:ancient_person_birth_year) }
23
+ it { should_not allow_value(2001).for(:ancient_person_birth_year).with_message('is not a valid year (must be a number between 1000 and 2000)') }
24
+
25
+ end
26
+
27
+ end
28
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel-validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Rick
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-18 00:00:00.000000000 Z
11
+ date: 2013-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  description: 'Some reusable ActiveModel validations, including greater_than, boolean_presence,
28
56
  and at_least_one_present '
29
57
  email:
@@ -58,10 +86,14 @@ files:
58
86
  - lib/activemodel-validators/sum_of_validator.rb
59
87
  - lib/activemodel-validators/version.rb
60
88
  - lib/activemodel-validators/year_validator.rb
89
+ - spec/at_least_one_present_validator_spec.rb
90
+ - spec/date_validator_spec.rb
61
91
  - spec/less_or_greater_than_validator_spec.rb
62
92
  - spec/spec_helper.rb
93
+ - spec/support/active_record.rb
63
94
  - spec/support/models/response.rb
64
95
  - spec/support/models/user.rb
96
+ - spec/year_validator_spec.rb
65
97
  homepage: ''
66
98
  licenses: []
67
99
  metadata: {}
@@ -81,9 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
113
  version: '0'
82
114
  requirements: []
83
115
  rubyforge_project: activemodel-validators
84
- rubygems_version: 2.0.3
116
+ rubygems_version: 2.1.5
85
117
  signing_key:
86
118
  specification_version: 4
87
119
  summary: Some reusable ActiveModel validations
88
120
  test_files: []
89
- has_rdoc: