can_has_validations 0.2.0 → 0.2.1

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.
data/README.md CHANGED
@@ -18,7 +18,16 @@ All validators use the newer Rails 3 syntax:
18
18
  (That is, there's not a validates_email_of :some_attribute helper.)
19
19
 
20
20
 
21
- ## Email ##
21
+ ## Installation ##
22
+
23
+ Add it to your `Gemfile`:
24
+
25
+ gem 'can_has_validations'
26
+
27
+
28
+
29
+
30
+ ## Email validator ##
22
31
 
23
32
  Ensures an attribute is generally formatted as an email. It uses a basic regex
24
33
  that's designed to match something that looks like an email. It allows for any
@@ -27,7 +36,7 @@ TLD, so as to not fail as ICANN continues to add TLDs.
27
36
  validates :user_email, :email=>true
28
37
 
29
38
 
30
- ## Grandparent ##
39
+ ## Grandparent validator ##
31
40
 
32
41
  Ensures two (or more) associations share a common parent value.
33
42
 
@@ -77,7 +86,7 @@ or the database foreign key (`:user_id`). You can also use any other field. The
77
86
  test is merely that they match, not that they are associations.
78
87
 
79
88
 
80
- ## Ordering ##
89
+ ## Ordering validators ##
81
90
 
82
91
  Ensures two attribute values maintain a relative order to one another. This is
83
92
  often useful when two date or range values. Validations can be written using
@@ -95,7 +104,7 @@ Always skips over nil values; use `:presence` to validate those.
95
104
  validates :finish_at, :after => {:values_of => [:start_at, :alt_start_at], :if=>... }
96
105
 
97
106
 
98
- ## URL ##
107
+ ## URL validator ##
99
108
 
100
109
  Ensure an attribute is generally formatted as a URL. If `addressable/uri` is
101
110
  already loaded, it will be used to parse IDN's.
@@ -106,8 +115,12 @@ already loaded, it will be used to parse IDN's.
106
115
  require 'addressable/uri'
107
116
  validates :website, :url=>true
108
117
 
118
+ # Or, as part of your Gemfile:
119
+ gem 'addressable', :require=>'addressable/uri'
120
+ gem 'can_has_validations'
121
+
109
122
 
110
- ## Write Once ##
123
+ ## Write Once validator ##
111
124
 
112
125
  Ensure that once a value is written, it becomes readonly. There are two uses
113
126
  for this.
@@ -3,8 +3,11 @@
3
3
 
4
4
  module ActiveModel::Validations
5
5
  class EmailValidator < ActiveModel::EachValidator
6
+
7
+ EMAIL_REGEXP = /\A([a-z0-9._+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
8
+
6
9
  def validate_each(record, attribute, value)
7
- unless value =~ /\A([a-z0-9._+-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
10
+ unless value =~ EMAIL_REGEXP
8
11
  record.errors.add(attribute, :invalid_email, options)
9
12
  end
10
13
  end
@@ -33,4 +33,26 @@ module ActiveModel::Validations
33
33
  end
34
34
  end
35
35
  end
36
+ # class OrderingValidator < ActiveModel::EachValidator
37
+ # def validate_each(record, attribute, value)
38
+ # Array(options[:before]).each do |attr_name|
39
+ # greater = record.send attr_name
40
+ # next unless value && greater
41
+ # unless value < greater
42
+ # attr2 = record.class.human_attribute_name attr_name
43
+ # record.errors.add(attribute, :before, options.except(:before).merge!(:attribute2=>attr2))
44
+ # # record.errors[attribute] << (options[:message] || :before"must be before #{record.class.human_attribute_name attr_name}")
45
+ # end
46
+ # end
47
+ # Array(options[:after]).each do |attr_name|
48
+ # lesser = record.send attr_name
49
+ # next unless value && lesser
50
+ # unless value > lesser
51
+ # attr2 = record.class.human_attribute_name attr_name
52
+ # record.errors.add(attribute, :after, options.except(:after).merge!(:attribute2=>attr2))
53
+ # # record.errors[attribute] << (options[:message] || :after"must be after #{record.class.human_attribute_name attr_name}")
54
+ # end
55
+ # end
56
+ # end
57
+ # end
36
58
  end
@@ -0,0 +1,15 @@
1
+ # better behaved version of the uniqueness validator
2
+ # Difference is it doesn't puke if the foreign_key is an invalid type (for
3
+ # example, an invalid string being provided for UUID type key). Instead,
4
+ # returns an error about being unable to find the key value.
5
+ # eg: validates :user_id, :safe_uniqueness=>true
6
+
7
+ class SafeUniquenessValidator < ActiveRecord::Validations::UniquenessValidator
8
+ def validate_each(record, attribute, value)
9
+ record.transaction(:requires_new=>true) do
10
+ super
11
+ end
12
+ rescue ActiveRecord::StatementInvalid
13
+ record.errors.add(attribute, "%{attribute} %{value} not found", options.except(:case_sensitive, :scope).merge(:value=>value))
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module CanHasValidations
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: can_has_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
12
+ date: 2013-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 3.2.0
22
22
  - - <
23
23
  - !ruby/object:Gem::Version
24
- version: '4.0'
24
+ version: '5.0'
25
25
  type: :runtime
26
26
  prerelease: false
27
27
  version_requirements: !ruby/object:Gem::Requirement
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.2.0
33
33
  - - <
34
34
  - !ruby/object:Gem::Version
35
- version: '4.0'
35
+ version: '5.0'
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
38
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +60,7 @@ files:
60
60
  - lib/can_has_validations/validators/email_validator.rb
61
61
  - lib/can_has_validations/validators/grandparent_validator.rb
62
62
  - lib/can_has_validations/validators/ordering_validator.rb
63
+ - lib/can_has_validations/validators/safe_uniqueness_validator.rb
63
64
  - lib/can_has_validations/validators/url_validator.rb
64
65
  - lib/can_has_validations/validators/write_once_validator.rb
65
66
  - lib/can_has_validations/version.rb
@@ -110,12 +111,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
111
  - - ! '>='
111
112
  - !ruby/object:Gem::Version
112
113
  version: '0'
114
+ segments:
115
+ - 0
116
+ hash: 908909344838106920
113
117
  required_rubygems_version: !ruby/object:Gem::Requirement
114
118
  none: false
115
119
  requirements:
116
120
  - - ! '>='
117
121
  - !ruby/object:Gem::Version
118
122
  version: '0'
123
+ segments:
124
+ - 0
125
+ hash: 908909344838106920
119
126
  requirements: []
120
127
  rubyforge_project:
121
128
  rubygems_version: 1.8.24