loose_change 0.4.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 1.0.0
data/lib/loose_change.rb CHANGED
@@ -21,6 +21,7 @@ module LooseChange
21
21
  autoload :Pagination, File.dirname(__FILE__) + '/loose_change/pagination'
22
22
  autoload :Helpers, File.dirname(__FILE__) + '/loose_change/helpers'
23
23
  autoload :Spatial, File.dirname(__FILE__) + '/loose_change/spatial'
24
+ autoload :Validations, File.dirname(__FILE__) + '/loose_change/validations'
24
25
  end
25
26
 
26
27
 
@@ -24,6 +24,7 @@ module LooseChange
24
24
  extend Persistence
25
25
  include PersistenceClassMethods
26
26
  extend Views
27
+ extend Validations
27
28
  extend Pagination
28
29
  extend Spatial
29
30
  include Helpers
@@ -0,0 +1,28 @@
1
+ module LooseChange
2
+ module Validations
3
+
4
+ # Ensure that only one document of this class can be created with
5
+ # each value. The <tt>:allow_nil</tt> option can be set to true
6
+ # to allow any number of documents with a blank value for this property.
7
+ def validates_uniqueness_of(property, opts = {})
8
+ view_by property
9
+ validates_with UniquenessValidator, :property => property, :allow_nil => opts[:allow_nil]
10
+ end
11
+
12
+ class UniquenessValidator < ActiveModel::Validator
13
+
14
+ def validate(record)
15
+ record.errors[@options[:property]] << "must be present" unless record.send(@options[:property]) || @options[:allow_nil]
16
+ record.errors[@options[:property]] << "is already taken" if record.send(@options[:property]).present? && duplicate_exists?(record)
17
+ end
18
+
19
+ private
20
+
21
+ def duplicate_exists?(record)
22
+ record.class.send(:view, "by_#{ @options[:property] }", :key => record.send(@options[:property]), :limit => 1).size > 0
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
data/loose_change.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{loose_change}
8
- s.version = "0.4.2"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joshua Miller"]
12
- s.date = %q{2010-12-31}
12
+ s.date = %q{2011-01-01}
13
13
  s.email = %q{josh@joshinharrisburg.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
36
36
  "lib/loose_change/railtie.rb",
37
37
  "lib/loose_change/server.rb",
38
38
  "lib/loose_change/spatial.rb",
39
+ "lib/loose_change/validations.rb",
39
40
  "lib/loose_change/views.rb",
40
41
  "lib/tasks/loose_change.rb",
41
42
  "loose_change.gemspec",
@@ -49,6 +50,7 @@ Gem::Specification.new do |s|
49
50
  "test/resources/couchdb.png",
50
51
  "test/spatial_test.rb",
51
52
  "test/test_helper.rb",
53
+ "test/validations_test.rb",
52
54
  "test/view_test.rb"
53
55
  ]
54
56
  s.homepage = %q{http://github.com/joshuamiller/loose_change}
@@ -66,6 +68,7 @@ Gem::Specification.new do |s|
66
68
  "test/persistence_test.rb",
67
69
  "test/spatial_test.rb",
68
70
  "test/test_helper.rb",
71
+ "test/validations_test.rb",
69
72
  "test/view_test.rb"
70
73
  ]
71
74
 
@@ -0,0 +1,48 @@
1
+ require './test/test_helper'
2
+
3
+ class ValidationsTest < ActiveSupport::TestCase
4
+
5
+ context "a model that validates uniqueness without allow_nil" do
6
+ setup do
7
+ class UniqueModel < LooseChange::Base
8
+ use_database "test_db"
9
+ property :name
10
+ validates_uniqueness_of :name
11
+ end
12
+ end
13
+
14
+ should "be valid if no others have been created" do
15
+ assert UniqueModel.new(:name => "Josh").valid?
16
+ end
17
+
18
+ should "not be valid attempting to create a second" do
19
+ UniqueModel.create(:name => "Josh")
20
+ model = UniqueModel.new(:name => "Josh")
21
+ assert !model.valid?
22
+ assert model.errors.has_key?(:name)
23
+ end
24
+
25
+ should "not be valid without a value" do
26
+ model = UniqueModel.new
27
+ assert !model.valid?
28
+ assert model.errors.has_key?(:name)
29
+ end
30
+ end
31
+
32
+ context "a model that validates uniqueness with allow_nil as true" do
33
+ setup do
34
+ class AllowNilModel < LooseChange::Base
35
+ use_database "test_db"
36
+ property :name
37
+ validates_uniqueness_of :name, :allow_nil => true
38
+ end
39
+ end
40
+
41
+ should "be valid if the property is nil" do
42
+ assert AllowNilModel.create
43
+ model = AllowNilModel.new
44
+ assert model.valid?
45
+ end
46
+ end
47
+
48
+ end
metadata CHANGED
@@ -3,10 +3,10 @@ name: loose_change
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
+ - 1
6
7
  - 0
7
- - 4
8
- - 2
9
- version: 0.4.2
8
+ - 0
9
+ version: 1.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joshua Miller
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-31 00:00:00 -05:00
17
+ date: 2011-01-01 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -117,6 +117,7 @@ files:
117
117
  - lib/loose_change/railtie.rb
118
118
  - lib/loose_change/server.rb
119
119
  - lib/loose_change/spatial.rb
120
+ - lib/loose_change/validations.rb
120
121
  - lib/loose_change/views.rb
121
122
  - lib/tasks/loose_change.rb
122
123
  - loose_change.gemspec
@@ -130,6 +131,7 @@ files:
130
131
  - test/resources/couchdb.png
131
132
  - test/spatial_test.rb
132
133
  - test/test_helper.rb
134
+ - test/validations_test.rb
133
135
  - test/view_test.rb
134
136
  has_rdoc: true
135
137
  homepage: http://github.com/joshuamiller/loose_change
@@ -145,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
147
  requirements:
146
148
  - - ">="
147
149
  - !ruby/object:Gem::Version
148
- hash: 322710298869901246
150
+ hash: 2822351378024837532
149
151
  segments:
150
152
  - 0
151
153
  version: "0"
@@ -174,4 +176,5 @@ test_files:
174
176
  - test/persistence_test.rb
175
177
  - test/spatial_test.rb
176
178
  - test/test_helper.rb
179
+ - test/validations_test.rb
177
180
  - test/view_test.rb