validates_blacklist 0.0.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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +28 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/generators/blacklists/blacklists_generator.rb +7 -0
- data/generators/blacklists/templates/blacklists.rb +8 -0
- data/lib/validates_blacklist.rb +4 -0
- data/lib/validates_blacklist/blacklist.rb +7 -0
- data/lib/validates_blacklist/validates.rb +47 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +42 -0
- data/spec/validates_blacklist_spec.rb +104 -0
- data/validates_blacklist.gemspec +59 -0
- metadata +80 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Bart Zonneveld
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
= validates_blacklist
|
2
|
+
|
3
|
+
Validation of Rails models against a database, supporting scoped validation.
|
4
|
+
|
5
|
+
= Install
|
6
|
+
Add the following to config/environment.rb
|
7
|
+
|
8
|
+
config.gem "bartzon-validates_blacklist", :lib => "validates_blacklist"
|
9
|
+
|
10
|
+
And run
|
11
|
+
|
12
|
+
rake gems:install
|
13
|
+
|
14
|
+
After these steps, generate a migration with:
|
15
|
+
|
16
|
+
script/generate blacklists
|
17
|
+
|
18
|
+
= Usage
|
19
|
+
Add the follow to each model you want to have validated:
|
20
|
+
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
|
23
|
+
validates_blacklist
|
24
|
+
:fields => [:name] # required, specify the fields to validate
|
25
|
+
:message => "" # optional, defaults to "is not allowed"
|
26
|
+
:scope => nil # optional, set this to for instance a class name to allow blacklist validation per class
|
27
|
+
|
28
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "validates_blacklist"
|
8
|
+
gem.summary = %Q{Validate Rails models against a database table}
|
9
|
+
gem.description = %Q{Validate Rails models against a database table}
|
10
|
+
gem.email = "loop@superinfinite.com"
|
11
|
+
gem.homepage = "http://github.com/bartzon/validates_blacklist"
|
12
|
+
gem.authors = ["Bart Zonneveld"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "validates_blacklist #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ValidatesBlacklist
|
2
|
+
module Validates
|
3
|
+
def validates_blacklist(options={})
|
4
|
+
options.reverse_merge!(
|
5
|
+
:message => "is not allowed",
|
6
|
+
:columns => [],
|
7
|
+
:scope => nil)
|
8
|
+
|
9
|
+
unless included_modules.include? InstanceMethods
|
10
|
+
class_inheritable_accessor :blacklist_options
|
11
|
+
|
12
|
+
extend ClassMethods
|
13
|
+
include InstanceMethods
|
14
|
+
|
15
|
+
validate :blacklisted_columns
|
16
|
+
end
|
17
|
+
|
18
|
+
self.blacklist_options = options
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def blacklisted?(value)
|
23
|
+
blacklist.include?(value)
|
24
|
+
end
|
25
|
+
|
26
|
+
def blacklist
|
27
|
+
if scope = blacklist_options[:scope]
|
28
|
+
list = ValidatesBlacklist::Blacklist.by_scope(scope.to_s)
|
29
|
+
else
|
30
|
+
list = ValidatesBlacklist::Blacklist.all
|
31
|
+
end
|
32
|
+
list.map(&:value)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module InstanceMethods
|
37
|
+
private
|
38
|
+
def blacklisted_columns
|
39
|
+
blacklist_options[:columns].each do |field|
|
40
|
+
if self.class.blacklisted?(self.send(field))
|
41
|
+
errors.add(field, blacklist_options[:message])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
require 'validates_blacklist'
|
7
|
+
begin
|
8
|
+
require 'sqlite3'
|
9
|
+
rescue MissingSourceFile => e
|
10
|
+
puts "Gem sqlite3-ruby could not be found, please install it"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'spec'
|
15
|
+
require 'spec/autorun'
|
16
|
+
|
17
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
18
|
+
|
19
|
+
# AR keeps printing annoying schema statements
|
20
|
+
$stdout = StringIO.new
|
21
|
+
|
22
|
+
def setup_db
|
23
|
+
ActiveRecord::Base.logger
|
24
|
+
ActiveRecord::Schema.define(:version => 1) do
|
25
|
+
create_table :users do |t|
|
26
|
+
t.string :name
|
27
|
+
end
|
28
|
+
create_table :blacklists do |t|
|
29
|
+
t.string :value
|
30
|
+
t.string :scope
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def teardown_db
|
36
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
37
|
+
ActiveRecord::Base.connection.drop_table(table)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Spec::Runner.configure do |config|
|
42
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class UserWithoutOptions < ActiveRecord::Base
|
4
|
+
set_table_name 'users'
|
5
|
+
validates_blacklist
|
6
|
+
end
|
7
|
+
|
8
|
+
class User < UserWithoutOptions
|
9
|
+
validates_blacklist :columns => [:name]
|
10
|
+
end
|
11
|
+
|
12
|
+
class UserWithAllOptions < UserWithoutOptions
|
13
|
+
validates_blacklist :columns => [:name],
|
14
|
+
:message => 'is blacklisted',
|
15
|
+
:scope => 'User'
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "ValidatesBlacklist" do
|
19
|
+
def create_blacklist(value, scope=nil)
|
20
|
+
ValidatesBlacklist::Blacklist.create!(:value => value, :scope => scope)
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete_blacklists
|
24
|
+
ValidatesBlacklist::Blacklist.destroy_all
|
25
|
+
end
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
setup_db
|
29
|
+
end
|
30
|
+
|
31
|
+
after(:all) do
|
32
|
+
teardown_db
|
33
|
+
end
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
delete_blacklists
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "validating a model" do
|
40
|
+
it "should not have any errors if no columns are checked" do
|
41
|
+
user = UserWithoutOptions.new
|
42
|
+
user.valid?
|
43
|
+
user.errors.should be_empty
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should not have any errors without blacklists" do
|
47
|
+
user = User.new
|
48
|
+
user.errors.should be_empty
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "a model with blacklists" do
|
52
|
+
before(:each) do
|
53
|
+
create_blacklist('bad')
|
54
|
+
@user = User.new(:name => 'bad')
|
55
|
+
@user.valid?
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should not be valid with a blacklisted value" do
|
59
|
+
@user.should_not be_valid
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should add the default error message" do
|
63
|
+
@user.errors.on(:name).should == 'is not allowed'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should allow setting of custom error messages" do
|
67
|
+
create_blacklist('bad', 'User')
|
68
|
+
user = UserWithAllOptions.new(:name => 'bad')
|
69
|
+
user.should_not be_valid
|
70
|
+
user.errors.on(:name).should == 'is blacklisted'
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should be valid if the blacklisted value has a different scope" do
|
74
|
+
create_blacklist('scoped', 'Article')
|
75
|
+
user = UserWithAllOptions.new(:name => 'scoped')
|
76
|
+
user.should be_valid
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "Class methods" do
|
82
|
+
before(:each) do
|
83
|
+
create_blacklist('bad')
|
84
|
+
create_blacklist('a_scoped', 'Article')
|
85
|
+
create_blacklist('u_scoped', 'User')
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return true if a value is blacklisted" do
|
89
|
+
User.blacklisted?('bad').should be_true
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return false if a value isn't blacklisted" do
|
93
|
+
User.blacklisted?('good').should be_false
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should return the correct blacklist" do
|
97
|
+
User.blacklist.should == ['bad', 'a_scoped', 'u_scoped']
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should return the correct scoped blacklist" do
|
101
|
+
UserWithAllOptions.blacklist.should == ['u_scoped']
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{validates_blacklist}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Bart Zonneveld"]
|
12
|
+
s.date = %q{2010-02-19}
|
13
|
+
s.description = %q{Validate Rails models against a database table}
|
14
|
+
s.email = %q{loop@superinfinite.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"generators/blacklists/blacklists_generator.rb",
|
27
|
+
"generators/blacklists/templates/blacklists.rb",
|
28
|
+
"lib/validates_blacklist.rb",
|
29
|
+
"lib/validates_blacklist/blacklist.rb",
|
30
|
+
"lib/validates_blacklist/validates.rb",
|
31
|
+
"spec/spec.opts",
|
32
|
+
"spec/spec_helper.rb",
|
33
|
+
"spec/validates_blacklist_spec.rb",
|
34
|
+
"validates_blacklist.gemspec"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/bartzon/validates_blacklist}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{Validate Rails models against a database table}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/spec_helper.rb",
|
43
|
+
"spec/validates_blacklist_spec.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_blacklist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bart Zonneveld
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-19 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: Validate Rails models against a database table
|
26
|
+
email: loop@superinfinite.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- generators/blacklists/blacklists_generator.rb
|
42
|
+
- generators/blacklists/templates/blacklists.rb
|
43
|
+
- lib/validates_blacklist.rb
|
44
|
+
- lib/validates_blacklist/blacklist.rb
|
45
|
+
- lib/validates_blacklist/validates.rb
|
46
|
+
- spec/spec.opts
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
- spec/validates_blacklist_spec.rb
|
49
|
+
- validates_blacklist.gemspec
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/bartzon/validates_blacklist
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.5
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Validate Rails models against a database table
|
78
|
+
test_files:
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
- spec/validates_blacklist_spec.rb
|