validation_scopes 0.4.0 → 0.4.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/CHANGELOG +5 -0
- data/README.md +11 -4
- data/Rakefile +1 -28
- data/VERSION +1 -1
- data/lib/validation_scopes.rb +11 -0
- data/test/db/schema.rb +8 -1
- data/test/fixtures/books.yml +12 -0
- data/test/helper.rb +5 -3
- data/test/models/book.rb +13 -0
- data/test/models/user.rb +1 -0
- data/test/test_validation_scopes.rb +17 -0
- data/validation_scopes.gemspec +10 -14
- metadata +9 -12
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -46,6 +46,9 @@ previous example:
|
|
46
46
|
film.errors.full_messages
|
47
47
|
=> []
|
48
48
|
|
49
|
+
film.class.all_scopes
|
50
|
+
=> [:warnings]
|
51
|
+
|
49
52
|
film.save
|
50
53
|
=> true
|
51
54
|
|
@@ -57,7 +60,11 @@ does not accept an `ActiveRecord::Errors` object directly. Instead you need to
|
|
57
60
|
|
58
61
|
## Compatibility
|
59
62
|
|
60
|
-
|
63
|
+
The current version should work for Rails 3 on Ruby 1.8 or 1.9; Battle-tested in the former.
|
64
|
+
|
65
|
+
For Rails 2 see the 0.3.x version of the gem which is maintained on the [rails2
|
66
|
+
branch](https://github.com/dasil003/validation_scopes/tree/rails2)
|
67
|
+
|
61
68
|
|
62
69
|
## Installation
|
63
70
|
|
@@ -69,7 +76,7 @@ In your Gemfile:
|
|
69
76
|
|
70
77
|
gem 'validation_scopes'
|
71
78
|
|
72
|
-
Or old-school
|
79
|
+
Or old-school-Rails-style, in your environment.rb:
|
73
80
|
|
74
81
|
config.gem 'validation_scopes'
|
75
82
|
|
@@ -99,8 +106,8 @@ about the design process](http://www.darwinweb.net/articles/80) is on my blog.
|
|
99
106
|
|
100
107
|
## TODO
|
101
108
|
|
102
|
-
* In Rails 3 validations are no longer coupled to ActiveRecord.
|
103
|
-
|
109
|
+
* In Rails 3 validations are no longer coupled to ActiveRecord. Although the current version of the gem uses
|
110
|
+
ActiveModel, it hasn't been tested against arbitrary objects.
|
104
111
|
|
105
112
|
|
106
113
|
## Copyright
|
data/Rakefile
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
|
4
1
|
begin
|
5
2
|
require 'jeweler'
|
6
3
|
Jeweler::Tasks.new do |gem|
|
@@ -11,7 +8,6 @@ begin
|
|
11
8
|
gem.homepage = "http://github.com/dasil003/validation_scopes"
|
12
9
|
gem.authors = ["Gabe da Silveira"]
|
13
10
|
gem.add_development_dependency "shoulda", ">= 0"
|
14
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
11
|
end
|
16
12
|
Jeweler::GemcutterTasks.new
|
17
13
|
rescue LoadError
|
@@ -20,34 +16,11 @@ end
|
|
20
16
|
|
21
17
|
require 'rake/testtask'
|
22
18
|
Rake::TestTask.new(:test) do |test|
|
23
|
-
test.libs <<
|
19
|
+
test.libs << %w(lib test)
|
24
20
|
test.pattern = 'test/**/test_*.rb'
|
25
21
|
test.verbose = true
|
26
22
|
end
|
27
23
|
|
28
|
-
begin
|
29
|
-
require 'rcov/rcovtask'
|
30
|
-
Rcov::RcovTask.new do |test|
|
31
|
-
test.libs << 'test'
|
32
|
-
test.pattern = 'test/**/test_*.rb'
|
33
|
-
test.verbose = true
|
34
|
-
end
|
35
|
-
rescue LoadError
|
36
|
-
task :rcov do
|
37
|
-
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
24
|
task :test => :check_dependencies
|
42
25
|
|
43
26
|
task :default => :test
|
44
|
-
|
45
|
-
require 'rake/rdoctask'
|
46
|
-
Rake::RDocTask.new do |rdoc|
|
47
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
-
|
49
|
-
rdoc.rdoc_dir = 'rdoc'
|
50
|
-
rdoc.title = "validation_scopes #{version}"
|
51
|
-
rdoc.rdoc_files.include('README*')
|
52
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
-
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/lib/validation_scopes.rb
CHANGED
@@ -9,6 +9,17 @@ module ValidationScopes
|
|
9
9
|
module ClassMethods
|
10
10
|
def validation_scope(scope)
|
11
11
|
base_class = self
|
12
|
+
@all_scopes ||= []
|
13
|
+
class << self
|
14
|
+
def all_scopes=(scope)
|
15
|
+
@all_scopes << scope
|
16
|
+
end
|
17
|
+
|
18
|
+
def all_scopes
|
19
|
+
@all_scopes
|
20
|
+
end
|
21
|
+
end
|
22
|
+
self.all_scopes = scope
|
12
23
|
deferred_proxy_class_declaration = Proc.new do
|
13
24
|
proxy_class = Class.new(DelegateClass(base_class)) do
|
14
25
|
include ActiveModel::Validations
|
data/test/db/schema.rb
CHANGED
@@ -6,4 +6,11 @@ ActiveRecord::Schema.define do
|
|
6
6
|
t.column "bio", :text
|
7
7
|
t.column "sponsor_id", :integer
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
|
+
create_table "books" do |t|
|
11
|
+
t.column "title", :string
|
12
|
+
t.column "author", :string
|
13
|
+
t.column "isbn", :integer
|
14
|
+
t.column "user_id", :integer
|
15
|
+
end
|
16
|
+
end
|
data/test/helper.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'test/unit'
|
3
2
|
require 'shoulda'
|
4
3
|
|
@@ -13,7 +12,10 @@ ActiveRecord::Base.establish_connection(
|
|
13
12
|
|
14
13
|
require 'db/schema.rb'
|
15
14
|
|
16
|
-
Dir['test/models/*.rb'].each { |f| require f }
|
15
|
+
Dir['./test/models/*.rb'].each { |f| require f }
|
17
16
|
|
18
17
|
require 'active_record/fixtures'
|
19
|
-
|
18
|
+
|
19
|
+
fixtures_constant = defined?(ActiveRecord::Fixtures) ? ActiveRecord::Fixtures : Fixtures
|
20
|
+
|
21
|
+
fixtures_constant.create_fixtures('test/fixtures/', ActiveRecord::Base.connection.tables)
|
data/test/models/book.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
class Book < ActiveRecord::Base
|
2
|
+
belongs_to :user
|
3
|
+
|
4
|
+
validates_presence_of :title
|
5
|
+
|
6
|
+
validation_scope :warnings_book do |s|
|
7
|
+
s.validates_presence_of :author
|
8
|
+
end
|
9
|
+
|
10
|
+
validation_scope :alerts_book do |s|
|
11
|
+
s.validates_presence_of :isbn
|
12
|
+
end
|
13
|
+
end
|
data/test/models/user.rb
CHANGED
@@ -73,4 +73,21 @@ class TestValidationScopes < Test::Unit::TestCase
|
|
73
73
|
@user.validation_scope_proxy_for_warnings.class.model_name
|
74
74
|
end
|
75
75
|
end
|
76
|
+
|
77
|
+
context "scopes per model" do
|
78
|
+
setup do
|
79
|
+
@user = User.find(1)
|
80
|
+
@book = Book.find(1)
|
81
|
+
@book2 = Book.find(2)
|
82
|
+
end
|
83
|
+
|
84
|
+
should "return all the scopes declared in User model" do
|
85
|
+
assert_equal [:warnings, :alerts], @user.class.all_scopes
|
86
|
+
end
|
87
|
+
|
88
|
+
should "return all the scopes declared in Book model" do
|
89
|
+
assert_equal [:warnings_book, :alerts_book], @book.class.all_scopes
|
90
|
+
assert_equal [:warnings_book, :alerts_book], @book2.class.all_scopes
|
91
|
+
end
|
92
|
+
end
|
76
93
|
end
|
data/validation_scopes.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.4.
|
7
|
+
s.name = "validation_scopes"
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Gabe da Silveira"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2012-04-15"
|
13
|
+
s.description = "Define additional sets of validations beyond the standard \"errors\" that is tied to the ActiveRecord life-cycle. These additional sets can be defined with all the standard ActiveRecord::Validation macros, and the resulting collection is a standard ActiveRecord::Errors object."
|
14
|
+
s.email = "gabe@websaviour.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.md"
|
@@ -25,22 +25,18 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"lib/validation_scopes.rb",
|
27
27
|
"test/db/schema.rb",
|
28
|
+
"test/fixtures/books.yml",
|
28
29
|
"test/fixtures/users.yml",
|
29
30
|
"test/helper.rb",
|
31
|
+
"test/models/book.rb",
|
30
32
|
"test/models/user.rb",
|
31
33
|
"test/test_validation_scopes.rb",
|
32
34
|
"validation_scopes.gemspec"
|
33
35
|
]
|
34
|
-
s.homepage =
|
36
|
+
s.homepage = "http://github.com/dasil003/validation_scopes"
|
35
37
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version =
|
37
|
-
s.summary =
|
38
|
-
s.test_files = [
|
39
|
-
"test/db/schema.rb",
|
40
|
-
"test/helper.rb",
|
41
|
-
"test/models/user.rb",
|
42
|
-
"test/test_validation_scopes.rb"
|
43
|
-
]
|
38
|
+
s.rubygems_version = "1.8.22"
|
39
|
+
s.summary = "Create sets of validations independent of the life-cycle of an ActiveRecord object"
|
44
40
|
|
45
41
|
if s.respond_to? :specification_version then
|
46
42
|
s.specification_version = 3
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validation_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gabe da Silveira
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2012-04-15 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: shoulda
|
@@ -50,12 +49,13 @@ files:
|
|
50
49
|
- VERSION
|
51
50
|
- lib/validation_scopes.rb
|
52
51
|
- test/db/schema.rb
|
52
|
+
- test/fixtures/books.yml
|
53
53
|
- test/fixtures/users.yml
|
54
54
|
- test/helper.rb
|
55
|
+
- test/models/book.rb
|
55
56
|
- test/models/user.rb
|
56
57
|
- test/test_validation_scopes.rb
|
57
58
|
- validation_scopes.gemspec
|
58
|
-
has_rdoc: true
|
59
59
|
homepage: http://github.com/dasil003/validation_scopes
|
60
60
|
licenses: []
|
61
61
|
|
@@ -85,12 +85,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements: []
|
86
86
|
|
87
87
|
rubyforge_project:
|
88
|
-
rubygems_version: 1.
|
88
|
+
rubygems_version: 1.8.22
|
89
89
|
signing_key:
|
90
90
|
specification_version: 3
|
91
91
|
summary: Create sets of validations independent of the life-cycle of an ActiveRecord object
|
92
|
-
test_files:
|
93
|
-
|
94
|
-
- test/helper.rb
|
95
|
-
- test/models/user.rb
|
96
|
-
- test/test_validation_scopes.rb
|
92
|
+
test_files: []
|
93
|
+
|