uniqueable 0.0.5 → 0.0.6
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 +73 -42
- data/lib/uniqueable/validates_uniqueable.rb +2 -2
- data/lib/uniqueable/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -22,59 +22,90 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
Uniqueable adds a uniqueable method and validator to your models. Uniqueable is most useful when validating uniqueness across multiple Models and fields.
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
25
|
+
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
include Uniqueable
|
28
|
+
uniquable :username
|
29
|
+
validates :username, :uniqueable => true
|
30
|
+
end
|
31
|
+
|
32
|
+
class Organsation < ActiveRecord::Base
|
33
|
+
include Uniqueable
|
34
|
+
uniquable :alias
|
35
|
+
validates :alias, :uniqueable => true
|
36
|
+
end
|
37
|
+
|
38
|
+
user = User.create(:username => "gazler") # Works
|
39
|
+
organisation = Organisation.create(:alias => "gazler") # Does not work
|
40
|
+
organisation.errors.messages # {:alias => ["has already been taken"]}
|
41
|
+
```
|
40
42
|
|
41
43
|
### Groups
|
42
44
|
In a large application, you may want to check uniqueness for multiple groups of things. Uniqueable includes groups for this:
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
```ruby
|
47
|
+
class User < ActiveRecord::Base
|
48
|
+
include Uniqueable
|
49
|
+
uniquable :username, :group => :names_for_stuff
|
50
|
+
validates :username, :uniqueable => {:group => :names_for_stuff}
|
51
|
+
end
|
52
|
+
|
53
|
+
class Organsation < ActiveRecord::Base
|
54
|
+
include Uniqueable
|
55
|
+
uniquable :alias, :group => :names_for_stuff
|
56
|
+
validates :alias, :uniqueable => {:group => :names_for_stuff}
|
57
|
+
end
|
58
|
+
|
59
|
+
class AdminUser < ActiveRecord::Base
|
60
|
+
include Uniqueable
|
61
|
+
uniquable :alias, :group => :names_for_stuff
|
62
|
+
uniquable :alias
|
63
|
+
validates :uniquable
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
admin_user = AdminUser.create(:alias => "gazler") # Works
|
68
|
+
user = User.create(:username => "gazler") # Does not work
|
69
|
+
organisation = Organisation.create(:alias => "gazler") # Does not work
|
70
|
+
admin_user = AdminUser.create(:alias => "gazler") # Does not work
|
71
|
+
```
|
72
|
+
|
73
|
+
### Conditions
|
74
|
+
|
75
|
+
Sometimes you will only want a field to be checked if certain conditions are satisfied. Let's say we only want public Organisations to be validated against with a user.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class User < ActiveRecord::Base
|
79
|
+
include Uniqueable
|
80
|
+
uniquable :username, :group => :names_for_stuff
|
81
|
+
validates :username, :uniqueable => {:group => :names_for_stuff}
|
82
|
+
end
|
83
|
+
|
84
|
+
class Organsation < ActiveRecord::Base
|
85
|
+
include Uniqueable
|
86
|
+
uniquable :alias, :group => :names_for_stuff, :conditions => ["public = ?", true]
|
87
|
+
validates :alias, :uniqueable => {:group => :names_for_stuff}
|
88
|
+
end
|
89
|
+
|
90
|
+
Organisation.create(:alias => "gazler", :public => true) # Works
|
91
|
+
Organisation.create(:alias => "powershift", :public => false) # Works
|
92
|
+
User.create(:username => "powershift") # Works
|
93
|
+
User.create(:username => "gazler") # Does Not Work
|
94
|
+
```
|
49
95
|
|
50
|
-
class Organsation < ActiveRecord::Base
|
51
|
-
include Uniqueable
|
52
|
-
uniquable :alias, :group => :names_for_stuff
|
53
|
-
validates :alias, :uniqueable => {:group => :names_for_stuff}
|
54
|
-
end
|
55
96
|
|
56
|
-
class Organsation < ActiveRecord::Base
|
57
|
-
include Uniqueable
|
58
|
-
uniquable :alias, :group => :names_for_stuff
|
59
|
-
uniquable :alias
|
60
|
-
validates :uniquable
|
61
|
-
end
|
62
|
-
|
63
|
-
|
64
|
-
admin_user = AdminUser.create(:alias => "gazler") # Works
|
65
|
-
user = User.create(:username => "gazler") # Does not work
|
66
|
-
organisation = Organisation.create(:alias => "gazler") # Does not work
|
67
|
-
admin_user = AdminUser.create(:alias => "gazler") # Does not work
|
68
97
|
|
69
98
|
### Other options
|
70
99
|
|
71
100
|
The `case_sensitive` option can be sent through with the validator. It defaults to true.
|
72
101
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
102
|
+
```ruby
|
103
|
+
class User < ActiveRecord::Base
|
104
|
+
include Uniqueable
|
105
|
+
uniquable :username, :group => :names_for_stuff
|
106
|
+
validates :username, :uniqueable => {:group => :names_for_stuff, :case_sensitive => false}
|
107
|
+
end
|
108
|
+
```
|
78
109
|
|
79
110
|
### Notes
|
80
111
|
|
@@ -6,7 +6,7 @@ module Uniqueable
|
|
6
6
|
|
7
7
|
def validate_each(record, attribute, value)
|
8
8
|
checks = record.class.uniqueable_checks[options[:group]]
|
9
|
-
#Make the check atomic
|
9
|
+
#TODO: Make the check atomic
|
10
10
|
return unless checks
|
11
11
|
checks.each do |klass, keys|
|
12
12
|
keys.each do |key|
|
@@ -25,7 +25,7 @@ module Uniqueable
|
|
25
25
|
|
26
26
|
unless result.empty?
|
27
27
|
#Check that the result isn't the record we are validating
|
28
|
-
unless result.first == record
|
28
|
+
unless result.first.attributes.except(:updated_at) == record.attributes.except(:updated_at)
|
29
29
|
return record.errors.add(attribute, :taken)
|
30
30
|
end
|
31
31
|
end
|
data/lib/uniqueable/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uniqueable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
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: 2012-10-
|
12
|
+
date: 2012-10-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -127,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
127
127
|
version: '0'
|
128
128
|
segments:
|
129
129
|
- 0
|
130
|
-
hash: -
|
130
|
+
hash: -2060387812442946570
|
131
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
132
|
none: false
|
133
133
|
requirements:
|
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
136
|
version: '0'
|
137
137
|
segments:
|
138
138
|
- 0
|
139
|
-
hash: -
|
139
|
+
hash: -2060387812442946570
|
140
140
|
requirements: []
|
141
141
|
rubyforge_project:
|
142
142
|
rubygems_version: 1.8.24
|