validates_lengths_from_database 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Appraisals +20 -0
- data/Gemfile.lock +30 -20
- data/README.rdoc +16 -1
- data/Rakefile +26 -10
- data/lib/validates_lengths_from_database.rb +10 -4
- data/lib/validates_lengths_from_database/version.rb +1 -1
- data/spec/db/schema.rb +8 -0
- data/spec/db/test.sqlite3 +0 -0
- data/spec/validates_lengths_from_database_spec.rb +58 -0
- metadata +86 -77
data/Appraisals
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
appraise "rails-2.3" do
|
2
|
+
gem "rails", "~> 2.3.2"
|
3
|
+
gem "validates_lengths_from_database", :path => "../"
|
4
|
+
end
|
5
|
+
|
6
|
+
appraise "rails-3.0" do
|
7
|
+
gem "rails", "~> 3.0.12"
|
8
|
+
gem "validates_lengths_from_database", :path => "../"
|
9
|
+
end
|
10
|
+
|
11
|
+
appraise "rails-3.1" do
|
12
|
+
gem "rails", "~> 3.1.4"
|
13
|
+
gem "validates_lengths_from_database", :path => "../"
|
14
|
+
end
|
15
|
+
|
16
|
+
appraise "rails-3.2" do
|
17
|
+
gem "rails", "~> 3.2.2"
|
18
|
+
gem "validates_lengths_from_database", :path => "../"
|
19
|
+
end
|
20
|
+
|
data/Gemfile.lock
CHANGED
@@ -1,26 +1,35 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
validates_lengths_from_database (0.1.
|
4
|
+
validates_lengths_from_database (0.1.3)
|
5
5
|
activerecord (>= 2.3.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (3.
|
11
|
-
activesupport (= 3.
|
12
|
-
builder (~>
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
10
|
+
activemodel (3.2.2)
|
11
|
+
activesupport (= 3.2.2)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activerecord (3.2.2)
|
14
|
+
activemodel (= 3.2.2)
|
15
|
+
activesupport (= 3.2.2)
|
16
|
+
arel (~> 3.0.2)
|
17
|
+
tzinfo (~> 0.3.29)
|
18
|
+
activesupport (3.2.2)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
appraisal (0.4.1)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
arel (3.0.2)
|
25
|
+
builder (3.0.0)
|
22
26
|
diff-lcs (1.1.2)
|
23
|
-
i18n (0.
|
27
|
+
i18n (0.6.0)
|
28
|
+
json (1.6.6)
|
29
|
+
multi_json (1.2.0)
|
30
|
+
rake (0.9.2.2)
|
31
|
+
rdoc (3.12)
|
32
|
+
json (~> 1.4)
|
24
33
|
rspec (2.5.0)
|
25
34
|
rspec-core (~> 2.5.0)
|
26
35
|
rspec-expectations (~> 2.5.0)
|
@@ -29,16 +38,17 @@ GEM
|
|
29
38
|
rspec-expectations (2.5.0)
|
30
39
|
diff-lcs (~> 1.1.2)
|
31
40
|
rspec-mocks (2.5.0)
|
32
|
-
sqlite3 (1.3.
|
33
|
-
|
34
|
-
sqlite3 (>= 1.3.3)
|
35
|
-
tzinfo (0.3.26)
|
41
|
+
sqlite3 (1.3.5)
|
42
|
+
tzinfo (0.3.32)
|
36
43
|
|
37
44
|
PLATFORMS
|
38
45
|
ruby
|
39
46
|
|
40
47
|
DEPENDENCIES
|
41
|
-
|
48
|
+
appraisal (~> 0.4.0)
|
49
|
+
i18n
|
50
|
+
rake
|
51
|
+
rdoc (~> 3.12)
|
42
52
|
rspec (~> 2.0)
|
43
|
-
sqlite3
|
53
|
+
sqlite3 (~> 1.3.4)
|
44
54
|
validates_lengths_from_database!
|
data/README.rdoc
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
{<img src="https://secure.travis-ci.org/rubiety/validates_lengths_from_database.png?branch=master" alt="Build Status" />}[http://travis-ci.org/rubiety/validates_lengths_from_database]
|
2
|
+
|
1
3
|
== Validates Lengths from Database
|
2
4
|
|
3
5
|
Few people add length validations to fields in their database, and when saving such fields that have exhausted their length, an SQL error occurs.
|
@@ -27,4 +29,17 @@ It also supports filter-style :only and :except options:
|
|
27
29
|
validates_lengths_from_database :except => [:other_field]
|
28
30
|
end
|
29
31
|
|
30
|
-
|
32
|
+
If you'd rather specify the limits explicitly instead of pulling them from the database, you can set it:
|
33
|
+
|
34
|
+
class Post < ActiveRecord::Base
|
35
|
+
validates_lengths_from_database :limit => 255
|
36
|
+
end
|
37
|
+
|
38
|
+
Or to set the limit differently for string (VARCHAR) and (TEXT) columns:
|
39
|
+
|
40
|
+
class Post < ActiveRecord::Base
|
41
|
+
validates_lengths_from_database :limit => {:string => 255, :text => 4092}
|
42
|
+
end
|
43
|
+
|
44
|
+
Note that unfortunately 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).
|
45
|
+
|
data/Rakefile
CHANGED
@@ -1,15 +1,31 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
2
4
|
require 'rake'
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
require 'appraisal'
|
7
|
+
|
8
|
+
Bundler::GemHelper.install_tasks
|
9
|
+
|
10
|
+
desc 'Default: run unit tests.'
|
11
|
+
task :default => [:clean, :all]
|
12
|
+
|
13
|
+
desc "Run Specs against all Appraisals"
|
14
|
+
task :all => :spec do
|
15
|
+
Rake::Task["appraisal:install"].execute
|
16
|
+
system("bundle exec rake -s appraisal spec")
|
12
17
|
end
|
13
18
|
|
14
|
-
|
19
|
+
desc "Run Specs"
|
20
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
21
|
+
end
|
22
|
+
|
23
|
+
task :test => :spec
|
24
|
+
|
25
|
+
desc "Clean up files."
|
26
|
+
task :clean do |t|
|
27
|
+
FileUtils.rm_rf "tmp"
|
28
|
+
Dir.glob("spec/db/*.sqlite3").each {|f| FileUtils.rm f }
|
29
|
+
Dir.glob("validates_lengths_from_database-*.gem").each {|f| FileUtils.rm f }
|
30
|
+
end
|
15
31
|
|
@@ -12,9 +12,13 @@ module ValidatesLengthsFromDatabase
|
|
12
12
|
options.symbolize_keys!
|
13
13
|
|
14
14
|
return false unless self.table_exists?
|
15
|
+
options[:only] = Array[options[:only]] if options[:only] && !options[:only].is_a?(Array)
|
16
|
+
options[:except] = Array[options[:except]] if options[:except] && !options[:except].is_a?(Array)
|
17
|
+
options[:limit] ||= {}
|
15
18
|
|
16
|
-
|
17
|
-
|
19
|
+
if options[:limit] and !options[:limit].is_a?(Hash)
|
20
|
+
options[:limit] = {:string => options[:limit], :text => options[:limit]}
|
21
|
+
end
|
18
22
|
|
19
23
|
if options[:only]
|
20
24
|
columns_to_validate = options[:only].map(&:to_s)
|
@@ -27,10 +31,12 @@ module ValidatesLengthsFromDatabase
|
|
27
31
|
column_schema = columns.find {|c| c.name == column }
|
28
32
|
next if column_schema.nil?
|
29
33
|
next if ![:string, :text].include?(column_schema.type)
|
30
|
-
|
34
|
+
|
35
|
+
column_limit = options[:limit][column_schema.type] || column_schema.limit
|
36
|
+
next unless column_limit
|
31
37
|
|
32
38
|
class_eval do
|
33
|
-
validates_length_of column, :maximum =>
|
39
|
+
validates_length_of column, :maximum => column_limit, :allow_blank => true
|
34
40
|
end
|
35
41
|
end
|
36
42
|
|
data/spec/db/schema.rb
CHANGED
@@ -8,4 +8,12 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
8
8
|
t.integer :integer_1
|
9
9
|
end
|
10
10
|
|
11
|
+
create_table :articles_high_limit, :force => true do |t|
|
12
|
+
t.string :string_1
|
13
|
+
t.string :string_2
|
14
|
+
t.text :text_1
|
15
|
+
t.date :date_1
|
16
|
+
t.integer :integer_1
|
17
|
+
end
|
18
|
+
|
11
19
|
end
|
data/spec/db/test.sqlite3
CHANGED
Binary file
|
@@ -18,6 +18,10 @@ describe ValidatesLengthsFromDatabase do
|
|
18
18
|
:integer_1 => 123
|
19
19
|
}
|
20
20
|
|
21
|
+
before(:all) do
|
22
|
+
ActiveSupport::Deprecation.silenced = true
|
23
|
+
end
|
24
|
+
|
21
25
|
context "Model without associated table" do
|
22
26
|
specify "defining validates_lengths_from_database should not raise an error" do
|
23
27
|
lambda {
|
@@ -60,6 +64,60 @@ describe ValidatesLengthsFromDatabase do
|
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
67
|
+
context "Model with validates_lengths_from_database :limit => 10" do
|
68
|
+
before do
|
69
|
+
class ArticleValidateLimit < ActiveRecord::Base
|
70
|
+
set_table_name "articles_high_limit"
|
71
|
+
validates_lengths_from_database :limit => 5
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "an article with overloaded attributes" do
|
76
|
+
before { @article = ArticleValidateLimit.new(LONG_ATTRIBUTES); @article.valid? }
|
77
|
+
|
78
|
+
it "should not be valid" do
|
79
|
+
@article.should_not be_valid
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should have errors on all string/text attributes" do
|
83
|
+
@article.errors["string_1"].join.should =~ /too long/
|
84
|
+
@article.errors["string_2"].join.should =~ /too long/
|
85
|
+
@article.errors["text_1"].join.should =~ /too long/
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "an article with short attributes" do
|
90
|
+
before { @article = ArticleValidateLimit.new(SHORT_ATTRIBUTES); @article.valid? }
|
91
|
+
|
92
|
+
it "should be valid" do
|
93
|
+
@article.should be_valid
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "Model with validates_lengths_from_database :limit => {:string => 5, :text => 100}" do
|
99
|
+
before do
|
100
|
+
class ArticleValidateSpecificLimit < ActiveRecord::Base
|
101
|
+
set_table_name "articles_high_limit"
|
102
|
+
validates_lengths_from_database :limit => {:string => 5, :text => 100}
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "an article with overloaded attributes" do
|
107
|
+
before { @article = ArticleValidateSpecificLimit.new(LONG_ATTRIBUTES); @article.valid? }
|
108
|
+
|
109
|
+
it "should not be valid" do
|
110
|
+
@article.should_not be_valid
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should have errors on all string/text attributes" do
|
114
|
+
@article.errors["string_1"].join.should =~ /too long/
|
115
|
+
@article.errors["string_2"].join.should =~ /too long/
|
116
|
+
@article.errors["text_1"].should_not be_present
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
63
121
|
context "Model with validates_lengths_from_database :only => [:string_1, :text_1]" do
|
64
122
|
before do
|
65
123
|
class ArticleValidateOnly < ActiveRecord::Base
|
metadata
CHANGED
@@ -1,79 +1,100 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_lengths_from_database
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 3
|
10
|
-
version: 0.1.3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Ben Hughes
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-04-01 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: activerecord
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70330927789240 !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 7
|
30
|
-
segments:
|
31
|
-
- 2
|
32
|
-
- 3
|
33
|
-
- 2
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
34
21
|
version: 2.3.2
|
35
22
|
type: :runtime
|
36
|
-
|
37
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70330927789240
|
25
|
+
- !ruby/object:Gem::Dependency
|
38
26
|
name: rspec
|
27
|
+
requirement: &70330927787800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.0'
|
33
|
+
type: :development
|
39
34
|
prerelease: false
|
40
|
-
|
35
|
+
version_requirements: *70330927787800
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sqlite3
|
38
|
+
requirement: &70330927781040 !ruby/object:Gem::Requirement
|
41
39
|
none: false
|
42
|
-
requirements:
|
40
|
+
requirements:
|
43
41
|
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
|
46
|
-
segments:
|
47
|
-
- 2
|
48
|
-
- 0
|
49
|
-
version: "2.0"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.3.4
|
50
44
|
type: :development
|
51
|
-
version_requirements: *id002
|
52
|
-
- !ruby/object:Gem::Dependency
|
53
|
-
name: sqlite3-ruby
|
54
45
|
prerelease: false
|
55
|
-
|
46
|
+
version_requirements: *70330927781040
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: appraisal
|
49
|
+
requirement: &70330927779540 !ruby/object:Gem::Requirement
|
56
50
|
none: false
|
57
|
-
requirements:
|
51
|
+
requirements:
|
58
52
|
- - ~>
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
|
61
|
-
segments:
|
62
|
-
- 1
|
63
|
-
- 3
|
64
|
-
- 1
|
65
|
-
version: 1.3.1
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.4.0
|
66
55
|
type: :development
|
67
|
-
|
68
|
-
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70330927779540
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rdoc
|
60
|
+
requirement: &70330927777720 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '3.12'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70330927777720
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: &70330927776440 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70330927776440
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: i18n
|
82
|
+
requirement: &70330927774820 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70330927774820
|
91
|
+
description: Introspects your database string field maximum lengths and automatically
|
92
|
+
defines length validations.
|
69
93
|
email: ben@railsgarden.com
|
70
94
|
executables: []
|
71
|
-
|
72
95
|
extensions: []
|
73
|
-
|
74
96
|
extra_rdoc_files: []
|
75
|
-
|
76
|
-
files:
|
97
|
+
files:
|
77
98
|
- lib/validates_lengths_from_database/railtie.rb
|
78
99
|
- lib/validates_lengths_from_database/version.rb
|
79
100
|
- lib/validates_lengths_from_database.rb
|
@@ -83,47 +104,35 @@ files:
|
|
83
104
|
- spec/spec_helper.rb
|
84
105
|
- spec/validates_lengths_from_database_spec.rb
|
85
106
|
- rails/init.rb
|
107
|
+
- Appraisals
|
86
108
|
- Gemfile
|
87
109
|
- Gemfile.lock
|
88
110
|
- LICENSE
|
89
111
|
- Rakefile
|
90
112
|
- README.rdoc
|
91
113
|
- init.rb
|
92
|
-
has_rdoc: true
|
93
114
|
homepage: http://github.com/rubiety/validates_lengths_from_database
|
94
115
|
licenses: []
|
95
|
-
|
96
116
|
post_install_message:
|
97
117
|
rdoc_options: []
|
98
|
-
|
99
|
-
require_paths:
|
118
|
+
require_paths:
|
100
119
|
- lib
|
101
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
121
|
none: false
|
103
|
-
requirements:
|
104
|
-
- -
|
105
|
-
- !ruby/object:Gem::Version
|
106
|
-
|
107
|
-
|
108
|
-
- 0
|
109
|
-
version: "0"
|
110
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
127
|
none: false
|
112
|
-
requirements:
|
113
|
-
- -
|
114
|
-
- !ruby/object:Gem::Version
|
115
|
-
hash: 19
|
116
|
-
segments:
|
117
|
-
- 1
|
118
|
-
- 3
|
119
|
-
- 4
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
120
131
|
version: 1.3.4
|
121
132
|
requirements: []
|
122
|
-
|
123
133
|
rubyforge_project: validates_lengths_from_database
|
124
|
-
rubygems_version: 1.
|
134
|
+
rubygems_version: 1.8.10
|
125
135
|
signing_key:
|
126
136
|
specification_version: 3
|
127
137
|
summary: Automatic maximum-length validations.
|
128
138
|
test_files: []
|
129
|
-
|