validates_lengths_from_database_advanced 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Gemfile.lock +44 -0
- data/LICENSE +21 -0
- data/README.rdoc +37 -0
- data/Rakefile +18 -0
- data/init.rb +4 -0
- data/lib/validates_lengths_from_database.rb +47 -0
- data/lib/validates_lengths_from_database/railtie.rb +20 -0
- data/lib/validates_lengths_from_database/version.rb +3 -0
- data/rails/init.rb +4 -0
- data/spec/db/database.yml +21 -0
- data/spec/db/schema.rb +11 -0
- data/spec/db/test.sqlite3 +0 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/validates_lengths_from_database_spec.rb +154 -0
- metadata +101 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
validates_lengths_from_database (0.1.2)
|
5
|
+
activerecord (>= 2.3.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.6)
|
11
|
+
activesupport (= 3.0.6)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.5.0)
|
14
|
+
activerecord (3.0.6)
|
15
|
+
activemodel (= 3.0.6)
|
16
|
+
activesupport (= 3.0.6)
|
17
|
+
arel (~> 2.0.2)
|
18
|
+
tzinfo (~> 0.3.23)
|
19
|
+
activesupport (3.0.6)
|
20
|
+
arel (2.0.9)
|
21
|
+
builder (2.1.2)
|
22
|
+
diff-lcs (1.1.2)
|
23
|
+
i18n (0.5.0)
|
24
|
+
rspec (2.5.0)
|
25
|
+
rspec-core (~> 2.5.0)
|
26
|
+
rspec-expectations (~> 2.5.0)
|
27
|
+
rspec-mocks (~> 2.5.0)
|
28
|
+
rspec-core (2.5.1)
|
29
|
+
rspec-expectations (2.5.0)
|
30
|
+
diff-lcs (~> 1.1.2)
|
31
|
+
rspec-mocks (2.5.0)
|
32
|
+
sqlite3 (1.3.3)
|
33
|
+
sqlite3-ruby (1.3.3)
|
34
|
+
sqlite3 (>= 1.3.3)
|
35
|
+
tzinfo (0.3.26)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
activerecord (>= 2.3.2)
|
42
|
+
rspec (~> 2.0)
|
43
|
+
sqlite3-ruby (~> 1.3.1)
|
44
|
+
validates_lengths_from_database!
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2011 Ben Hughes
|
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.
|
21
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
== Validates Lengths from Database
|
2
|
+
|
3
|
+
Few people add length validations to fields in their database, and when saving such fields that have exhausted their length, an SQL error occurs.
|
4
|
+
This gem introspects your table schema for maximum lengths on string and text fields and automatically adds length validations to the model.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
Include the gem using bundler in your Gemfile:
|
9
|
+
|
10
|
+
gem "validates_lengths_from_database_advanced"
|
11
|
+
|
12
|
+
== Usage
|
13
|
+
|
14
|
+
In your model you can activate validations:
|
15
|
+
|
16
|
+
class Post < ActiveRecord::Base
|
17
|
+
validates_lengths_from_database
|
18
|
+
end
|
19
|
+
|
20
|
+
It also supports filter-style :only and :except options:
|
21
|
+
|
22
|
+
class Post < ActiveRecord::Base
|
23
|
+
validates_lengths_from_database :only => [:title, :contents]
|
24
|
+
end
|
25
|
+
|
26
|
+
class Post < ActiveRecord::Base
|
27
|
+
validates_lengths_from_database :except => [:other_field]
|
28
|
+
end
|
29
|
+
|
30
|
+
For set limit options
|
31
|
+
|
32
|
+
class Post < ActiveRecord::Base
|
33
|
+
validates_lengths_from_database :limit => [:string => 30, :text => 50]
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
Note that this cannot be done at a global level directly against ActiveRecord::Base, since the +validates_length_from_database+ method requires the class to have a table name (with the ability to load the schema).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
desc "Generate documentation for the plugin."
|
9
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = "rdoc"
|
11
|
+
rdoc.title = "validates_lengths_from_database"
|
12
|
+
rdoc.options << "--line-numbers" << "--inline-source"
|
13
|
+
rdoc.rdoc_files.include('README')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
Dir["#{File.dirname(__FILE__)}/lib/tasks/*.rake"].sort.each { |ext| load ext }
|
18
|
+
|
data/init.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
module ValidatesLengthsFromDatabase
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:extend, ClassMethods)
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def validates_lengths_from_database(options = {})
|
12
|
+
options.symbolize_keys!
|
13
|
+
|
14
|
+
return false unless self.table_exists?
|
15
|
+
|
16
|
+
raise ArgumentError, "The :only option to validates_lengths_from_database must be an array." if options[:only] and !options[:only].is_a?(Array)
|
17
|
+
raise ArgumentError, "The :except option to validates_lengths_from_database must be an array." if options[:except] and !options[:except].is_a?(Array)
|
18
|
+
|
19
|
+
if options[:only]
|
20
|
+
columns_to_validate = options[:only].map(&:to_s)
|
21
|
+
else
|
22
|
+
columns_to_validate = column_names.map(&:to_s)
|
23
|
+
columns_to_validate -= options[:except].map(&:to_s) if options[:except]
|
24
|
+
end
|
25
|
+
|
26
|
+
columns_to_validate.each do |column|
|
27
|
+
column_schema = columns.find {|c| c.name == column }
|
28
|
+
column_limit = options[:limit].try(:[], column_schema.type) || column_schema.limit
|
29
|
+
next if column_schema.nil?
|
30
|
+
next if ![:string, :text].include?(column_schema.type)
|
31
|
+
next if column_limit.nil?
|
32
|
+
|
33
|
+
class_eval do
|
34
|
+
validates_length_of column, :maximum => column_limit, :allow_blank => true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
module InstanceMethods
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
require "validates_lengths_from_database/railtie"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ValidatesLengthsFromDatabase
|
2
|
+
if defined?(Rails::Railtie)
|
3
|
+
require "rails"
|
4
|
+
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "validates_lengths_from_database.extend_active_record" do
|
7
|
+
ActiveSupport.on_load(:active_record) do
|
8
|
+
ValidatesLengthsFromDatabase::Railtie.insert
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Railtie
|
15
|
+
def self.insert
|
16
|
+
ActiveRecord::Base.send(:include, ValidatesLengthsFromDatabase)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data/rails/init.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
sqlite:
|
2
|
+
adapter: sqlite
|
3
|
+
database: spec/db/test.sqlite
|
4
|
+
|
5
|
+
sqlite3:
|
6
|
+
adapter: sqlite3
|
7
|
+
database: spec/db/test.sqlite3
|
8
|
+
|
9
|
+
postgresql:
|
10
|
+
adapter: postgresql
|
11
|
+
username: postgres
|
12
|
+
password: postgres
|
13
|
+
database: has_draft_plugin_test
|
14
|
+
min_messages: ERROR
|
15
|
+
|
16
|
+
mysql:
|
17
|
+
adapter: mysql
|
18
|
+
host: localhost
|
19
|
+
username: root
|
20
|
+
password:
|
21
|
+
database: has_draft_plugin_test
|
data/spec/db/schema.rb
ADDED
Binary file
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rspec"
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
# Establish DB Connection
|
6
|
+
config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml')))
|
7
|
+
ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
|
8
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
9
|
+
|
10
|
+
# Load Test Schema into the Database
|
11
|
+
load(File.dirname(__FILE__) + "/db/schema.rb")
|
12
|
+
|
13
|
+
require File.dirname(__FILE__) + '/../init'
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ValidatesLengthsFromDatabase do
|
4
|
+
|
5
|
+
LONG_ATTRIBUTES = {
|
6
|
+
:string_1 => "123456789",
|
7
|
+
:string_2 => "123456789",
|
8
|
+
:text_1 => "123456789",
|
9
|
+
:date_1 => Date.today,
|
10
|
+
:integer_1 => 123
|
11
|
+
}
|
12
|
+
|
13
|
+
SHORT_ATTRIBUTES = {
|
14
|
+
:string_1 => "12",
|
15
|
+
:string_2 => "12",
|
16
|
+
:text_1 => "12",
|
17
|
+
:date_1 => Date.today,
|
18
|
+
:integer_1 => 123
|
19
|
+
}
|
20
|
+
|
21
|
+
context "Model without associated table" do
|
22
|
+
specify "defining validates_lengths_from_database should not raise an error" do
|
23
|
+
lambda {
|
24
|
+
class InvalidTableArticle < ActiveRecord::Base
|
25
|
+
set_table_name "articles_invalid"
|
26
|
+
validates_lengths_from_database
|
27
|
+
end
|
28
|
+
}.should_not raise_error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "Model with validates_lengths_from_database" do
|
33
|
+
before do
|
34
|
+
class ArticleValidateAll < ActiveRecord::Base
|
35
|
+
set_table_name "articles"
|
36
|
+
validates_lengths_from_database
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "an article with overloaded attributes" do
|
41
|
+
before { @article = ArticleValidateAll.new(LONG_ATTRIBUTES); @article.valid? }
|
42
|
+
|
43
|
+
it "should not be valid" do
|
44
|
+
@article.should_not be_valid
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should have errors on all string/text attributes" do
|
48
|
+
@article.errors["string_1"].join.should =~ /too long/
|
49
|
+
@article.errors["string_2"].join.should =~ /too long/
|
50
|
+
@article.errors["text_1"].join.should =~ /too long/
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "an article with short attributes" do
|
55
|
+
before { @article = ArticleValidateAll.new(SHORT_ATTRIBUTES); @article.valid? }
|
56
|
+
|
57
|
+
it "should be valid" do
|
58
|
+
@article.should be_valid
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "Model with validates_lengths_from_database :only => [:string_1, :text_1]" do
|
64
|
+
before do
|
65
|
+
class ArticleValidateOnly < ActiveRecord::Base
|
66
|
+
set_table_name "articles"
|
67
|
+
validates_lengths_from_database :only => [:string_1, :text_1]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "an article with long attributes" do
|
72
|
+
before { @article = ArticleValidateOnly.new(LONG_ATTRIBUTES); @article.valid? }
|
73
|
+
|
74
|
+
it "should not be valid" do
|
75
|
+
@article.should_not be_valid
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should have errors on only string_1 and text_1" do
|
79
|
+
@article.errors["string_1"].join.should =~ /too long/
|
80
|
+
(@article.errors["string_2"] || []).should be_empty
|
81
|
+
@article.errors["text_1"].join.should =~ /too long/
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "an article with short attributes" do
|
86
|
+
before { @article = ArticleValidateOnly.new(SHORT_ATTRIBUTES); @article.valid? }
|
87
|
+
|
88
|
+
it "should be valid" do
|
89
|
+
@article.should be_valid
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "Model with validates_lengths_from_database :except => [:string_1, :text_1]" do
|
95
|
+
before do
|
96
|
+
class ArticleValidateExcept < ActiveRecord::Base
|
97
|
+
set_table_name "articles"
|
98
|
+
validates_lengths_from_database :except => [:string_1, :text_1]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "an article with long attributes" do
|
103
|
+
before { @article = ArticleValidateExcept.new(LONG_ATTRIBUTES); @article.valid? }
|
104
|
+
|
105
|
+
it "should not be valid" do
|
106
|
+
@article.should_not be_valid
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should have errors on columns other than string_1 and text_1 only" do
|
110
|
+
(@article.errors["string_1"] || []).should be_empty
|
111
|
+
(@article.errors["text_1"] || []).should be_empty
|
112
|
+
@article.errors["string_2"].join.should =~ /too long/
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context "an article with short attributes" do
|
117
|
+
before { @article = ArticleValidateOnly.new(SHORT_ATTRIBUTES); @article.valid? }
|
118
|
+
|
119
|
+
it "should be valid" do
|
120
|
+
@article.should be_valid
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "limit options" do
|
125
|
+
before do
|
126
|
+
class ArticleValidateLimitShort < ActiveRecord::Base
|
127
|
+
set_table_name "articles"
|
128
|
+
validates_lengths_from_database :limit => {:string => 2, :text => 5}
|
129
|
+
end
|
130
|
+
|
131
|
+
class ArticleValidateLimitLong < ActiveRecord::Base
|
132
|
+
set_table_name "articles"
|
133
|
+
validates_lengths_from_database :limit => {:string => 20, :text => 50}
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should not be valid" do
|
138
|
+
@article = ArticleValidateLimitShort.new(LONG_ATTRIBUTES)
|
139
|
+
@article.should_not be_valid
|
140
|
+
@article.errors[:string_1].should_not be_empty
|
141
|
+
@article.errors[:text_1].should_not be_empty
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should be valid" do
|
145
|
+
@article = ArticleValidateLimitLong.new(LONG_ATTRIBUTES)
|
146
|
+
@article.should be_valid
|
147
|
+
@article.errors[:string_1].should be_empty
|
148
|
+
@article.errors[:text_1].should be_empty
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_lengths_from_database_advanced
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Hughes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-09 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "2.0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: sqlite3-ruby
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.3.1
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
description: Introspects your database string field maximum lengths and automatically defines length validations.
|
49
|
+
email: ben@railsgarden.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- lib/validates_lengths_from_database.rb
|
58
|
+
- lib/validates_lengths_from_database/version.rb
|
59
|
+
- lib/validates_lengths_from_database/railtie.rb
|
60
|
+
- spec/db/schema.rb
|
61
|
+
- spec/db/database.yml
|
62
|
+
- spec/db/test.sqlite3
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
- spec/validates_lengths_from_database_spec.rb
|
65
|
+
- rails/init.rb
|
66
|
+
- Rakefile
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE
|
69
|
+
- README.rdoc
|
70
|
+
- Gemfile.lock
|
71
|
+
- init.rb
|
72
|
+
homepage: http://github.com/robinbortlik/validates_lengths_from_database
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 1.3.4
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project: validates_lengths_from_database_advanced
|
95
|
+
rubygems_version: 1.7.2
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Automatic maximum-length validations.
|
99
|
+
test_files: []
|
100
|
+
|
101
|
+
has_rdoc:
|