uniqueable 0.0.3 → 0.0.4
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.
@@ -11,11 +11,16 @@ module Uniqueable
|
|
11
11
|
checks.each do |klass, keys|
|
12
12
|
keys.each do |key|
|
13
13
|
#Check nothing exists with this pair
|
14
|
+
conditions = []
|
15
|
+
if key.kind_of?(Hash)
|
16
|
+
conditions = key[key.keys.first][:conditions]
|
17
|
+
key = key.keys.first
|
18
|
+
end
|
14
19
|
if options[:case_sensitive]
|
15
|
-
result = klass.where(key
|
20
|
+
result = klass.where(["#{key} = ?", value]).where(conditions).all
|
16
21
|
else
|
17
22
|
value = value.downcase if value.kind_of?(String)
|
18
|
-
result = klass.
|
23
|
+
result = klass.where(["lower(#{key}) = ?", value]).where(conditions).all
|
19
24
|
end
|
20
25
|
|
21
26
|
unless result.empty?
|
data/lib/uniqueable/version.rb
CHANGED
data/lib/uniqueable.rb
CHANGED
@@ -17,11 +17,14 @@ module Uniqueable
|
|
17
17
|
attr_accessor :uniqueable_checks
|
18
18
|
def uniqueable(*values)
|
19
19
|
options = values.last.kind_of?(Hash) ? values.pop : {}
|
20
|
-
group = options
|
20
|
+
group = options.delete(:group)
|
21
21
|
klass = self
|
22
22
|
uniqueable_checks[group] ||= {}
|
23
23
|
uniqueable_checks[group][klass] ||= []
|
24
24
|
values.each do |value|
|
25
|
+
unless options.empty?
|
26
|
+
value = {value => options}
|
27
|
+
end
|
25
28
|
uniqueable_checks[group][klass] << value
|
26
29
|
end
|
27
30
|
uniqueable_checks[group][klass].uniq!
|
data/spec/schema.rb
CHANGED
@@ -60,4 +60,17 @@ describe Uniqueable do
|
|
60
60
|
Tester.uniqueable_checks.should eql({:aliases => {Tester => [:name], AnotherTester=> [:name]}})
|
61
61
|
end
|
62
62
|
end
|
63
|
+
|
64
|
+
context "coniditionals" do
|
65
|
+
it "should store the conditions if applicable" do
|
66
|
+
Tester.uniqueable(:name, :group => :aliases, :conditions => ["admin = ?", true])
|
67
|
+
AnotherTester.uniqueable(:name, :group => :aliases)
|
68
|
+
Tester.uniqueable_checks.should eql({
|
69
|
+
:aliases => {
|
70
|
+
Tester => [{:name => {:conditions => ["admin = ?", true]}}],
|
71
|
+
AnotherTester=> [:name]
|
72
|
+
}
|
73
|
+
})
|
74
|
+
end
|
75
|
+
end
|
63
76
|
end
|
@@ -128,5 +128,71 @@ describe Uniqueable::UniqueableValidator do
|
|
128
128
|
user.should_not be_valid
|
129
129
|
end
|
130
130
|
end
|
131
|
+
|
132
|
+
context "Conditionals" do
|
133
|
+
before(:each) do
|
134
|
+
Uniqueable.instance_variable_set(:@uniqueable_checks, nil)
|
135
|
+
class User < ActiveRecord::Base
|
136
|
+
include Uniqueable
|
137
|
+
uniqueable :username
|
138
|
+
validates :username, :uniqueable => true
|
139
|
+
end
|
140
|
+
|
141
|
+
class Organisation < ActiveRecord::Base
|
142
|
+
include Uniqueable
|
143
|
+
uniqueable :alias, :conditions => ["public = ?", true]
|
144
|
+
validates :alias, :uniqueable => true
|
145
|
+
end
|
146
|
+
User.delete_all
|
147
|
+
Organisation.delete_all
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should ignore non-public organisations" do
|
151
|
+
organisation = Organisation.create(:name => "Gazler", :alias => "gazler", :public => false)
|
152
|
+
organisation.should be_valid
|
153
|
+
user = User.create(:name => "Gazler", :username => "gazler")
|
154
|
+
user.should be_valid
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should validate when public is true" do
|
158
|
+
organisation = Organisation.create(:name => "Gazler", :alias => "gazler", :public => true)
|
159
|
+
organisation.should be_valid
|
160
|
+
user = User.create(:name => "Gazler", :username => "gazler")
|
161
|
+
user.should_not be_valid
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "Case Sensitive with Conditionals" do
|
166
|
+
before(:each) do
|
167
|
+
Uniqueable.instance_variable_set(:@uniqueable_checks, nil)
|
168
|
+
class User < ActiveRecord::Base
|
169
|
+
include Uniqueable
|
170
|
+
uniqueable :username
|
171
|
+
validates :username, :uniqueable => {:case_sensitive => false}
|
172
|
+
end
|
173
|
+
|
174
|
+
class Organisation < ActiveRecord::Base
|
175
|
+
include Uniqueable
|
176
|
+
uniqueable :alias, :conditions => ["public = ?", true]
|
177
|
+
validates :alias, :uniqueable => {:case_sensitive => false}
|
178
|
+
end
|
179
|
+
User.delete_all
|
180
|
+
Organisation.delete_all
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should ignore non-public organisations" do
|
184
|
+
organisation = Organisation.create(:name => "Gazler", :alias => "Gazler", :public => false)
|
185
|
+
organisation.should be_valid
|
186
|
+
user = User.create(:name => "Gazler", :username => "gazler")
|
187
|
+
user.should be_valid
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should validate when public is true" do
|
191
|
+
organisation = Organisation.create(:name => "Gazler", :alias => "Gazler", :public => true)
|
192
|
+
organisation.should be_valid
|
193
|
+
user = User.create(:name => "Gazler", :username => "gazler")
|
194
|
+
user.should_not be_valid
|
195
|
+
end
|
196
|
+
end
|
131
197
|
|
132
198
|
end
|
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.4
|
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-03 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: -2039842943075345764
|
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: -2039842943075345764
|
140
140
|
requirements: []
|
141
141
|
rubyforge_project:
|
142
142
|
rubygems_version: 1.8.24
|