validates_lengths_from_database 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in testgem.gemspec
4
+ gemspec
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,32 @@
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
+ This gem is testing working with Rails 3.0 only!
9
+
10
+ Include the gem using bundler in your Gemfile:
11
+
12
+ gem "validates_lengths_from_database"
13
+
14
+ == Usage
15
+
16
+ In your model you can activate validations:
17
+
18
+ class Post < ActiveRecord::Base
19
+ validates_lengths_from_database
20
+ end
21
+
22
+ It also supports filter-style :only and :except options:
23
+
24
+ class Post < ActiveRecord::Base
25
+ validates_lengths_from_database :only => [:title, :contents]
26
+ end
27
+
28
+ class Post < ActiveRecord::Base
29
+ validates_lengths_from_database :except => [:other_field]
30
+ end
31
+
32
+ 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,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+
5
+ desc "Generate documentation for the plugin."
6
+ Rake::RDocTask.new(:rdoc) do |rdoc|
7
+ rdoc.rdoc_dir = "rdoc"
8
+ rdoc.title = "validates_lengths_from_database"
9
+ rdoc.options << "--line-numbers" << "--inline-source"
10
+ rdoc.rdoc_files.include('README')
11
+ rdoc.rdoc_files.include('lib/**/*.rb')
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/lib/tasks/*.rake"].sort.each { |ext| load ext }
15
+
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "validates_lengths_from_database"
2
+
3
+ ValidatesLengthsFromDatabase::Railtie.insert
4
+
@@ -0,0 +1,41 @@
1
+ require "rubygems"
2
+ require "active_record"
3
+
4
+ module ValidatesLengthsFromDatabase
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def validates_lengths_from_database(options = {})
9
+ options.symbolize_keys!
10
+
11
+ raise ArgumentError, "The :only option to validates_lengths_from_database must be an array." if options[:only] and !options[:only].is_a?(Array)
12
+ raise ArgumentError, "The :except option to validates_lengths_from_database must be an array." if options[:except] and !options[:except].is_a?(Array)
13
+
14
+ if options[:only]
15
+ columns_to_validate = options[:only].map(&:to_s)
16
+ else
17
+ columns_to_validate = column_names.map(&:to_s)
18
+ columns_to_validate -= options[:except].map(&:to_s) if options[:except]
19
+ end
20
+
21
+ columns_to_validate.each do |column|
22
+ column_schema = columns.find {|c| c.name == column }
23
+ next if column_schema.nil?
24
+ next if ![:string, :text].include?(column_schema.type)
25
+ next if column_schema.limit.nil?
26
+
27
+ class_eval do
28
+ validates_length_of column, :maximum => column_schema.limit
29
+ end
30
+ end
31
+
32
+ nil
33
+ end
34
+
35
+ end
36
+
37
+ module InstanceMethods
38
+ end
39
+ end
40
+
41
+ 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
+
@@ -0,0 +1,3 @@
1
+ module ValidatesLengthsFromDatabase
2
+ VERSION = "0.0.2"
3
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "validates_lengths_from_database"
2
+
3
+ ValidatesLengthsFromDatabase::Railtie.insert
4
+
@@ -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
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table :articles, :force => true do |t|
4
+ t.string :string_1, :limit => 5
5
+ t.string :string_2, :limit => 5
6
+ t.text :text_1, :limit => 5
7
+ t.date :date_1
8
+ t.integer :integer_1
9
+ end
10
+
11
+ end
Binary file
@@ -0,0 +1,20 @@
1
+ require "rubygems"
2
+ require "rspec"
3
+ require "factory_girl"
4
+ require "faker"
5
+ require "rails"
6
+ require "active_record"
7
+ require "active_support"
8
+
9
+ # Establish DB Connection
10
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml')))
11
+ ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
12
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
13
+
14
+ # Load Test Schema into the Database
15
+ load(File.dirname(__FILE__) + "/db/schema.rb")
16
+
17
+ require File.dirname(__FILE__) + '/../init'
18
+
19
+ # Load Factories:
20
+ Dir[File.join(File.dirname(__FILE__), "factories/**/*.rb")].each {|f| require f}
@@ -0,0 +1,114 @@
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 with validates_lengths_from_database" do
22
+ before do
23
+ class ArticleValidateAll < ActiveRecord::Base
24
+ set_table_name "articles"
25
+ validates_lengths_from_database
26
+ end
27
+ end
28
+
29
+ context "an article with overloaded attributes" do
30
+ before { @article = ArticleValidateAll.new(LONG_ATTRIBUTES); @article.valid? }
31
+
32
+ it "should not be valid" do
33
+ @article.should_not be_valid
34
+ end
35
+
36
+ it "should have errors on all string/text attributes" do
37
+ @article.errors["string_1"].join.should =~ /too long/
38
+ @article.errors["string_2"].join.should =~ /too long/
39
+ @article.errors["text_1"].join.should =~ /too long/
40
+ end
41
+ end
42
+
43
+ context "an article with short attributes" do
44
+ before { @article = ArticleValidateAll.new(SHORT_ATTRIBUTES); @article.valid? }
45
+
46
+ it "should be valid" do
47
+ @article.should be_valid
48
+ end
49
+ end
50
+ end
51
+
52
+ context "Model with validates_lengths_from_database :only => [:string_1, :text_1]" do
53
+ before do
54
+ class ArticleValidateOnly < ActiveRecord::Base
55
+ set_table_name "articles"
56
+ validates_lengths_from_database :only => [:string_1, :text_1]
57
+ end
58
+ end
59
+
60
+ context "an article with long attributes" do
61
+ before { @article = ArticleValidateOnly.new(LONG_ATTRIBUTES); @article.valid? }
62
+
63
+ it "should not be valid" do
64
+ @article.should_not be_valid
65
+ end
66
+
67
+ it "should have errors on only string_1 and text_1" do
68
+ @article.errors["string_1"].join.should =~ /too long/
69
+ @article.errors["string_2"].should be_empty
70
+ @article.errors["text_1"].join.should =~ /too long/
71
+ end
72
+ end
73
+
74
+ context "an article with short attributes" do
75
+ before { @article = ArticleValidateOnly.new(SHORT_ATTRIBUTES); @article.valid? }
76
+
77
+ it "should be valid" do
78
+ @article.should be_valid
79
+ end
80
+ end
81
+ end
82
+
83
+ context "Model with validates_lengths_from_database :except => [:string_1, :text_1]" do
84
+ before do
85
+ class ArticleValidateExcept < ActiveRecord::Base
86
+ set_table_name "articles"
87
+ validates_lengths_from_database :except => [:string_1, :text_1]
88
+ end
89
+ end
90
+
91
+ context "an article with long attributes" do
92
+ before { @article = ArticleValidateExcept.new(LONG_ATTRIBUTES); @article.valid? }
93
+
94
+ it "should not be valid" do
95
+ @article.should_not be_valid
96
+ end
97
+
98
+ it "should have errors on columns other than string_1 and text_1 only" do
99
+ @article.errors["string_1"].should be_empty
100
+ @article.errors["text_1"].should be_empty
101
+ @article.errors["string_2"].join.should =~ /too long/
102
+ end
103
+ end
104
+
105
+ context "an article with short attributes" do
106
+ before { @article = ArticleValidateOnly.new(SHORT_ATTRIBUTES); @article.valid? }
107
+
108
+ it "should be valid" do
109
+ @article.should be_valid
110
+ end
111
+ end
112
+ end
113
+
114
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_lengths_from_database
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Ben Hughes
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-30 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: active_record
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 2
48
+ - 0
49
+ version: "2.0"
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: sqlite3-ruby
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 25
61
+ segments:
62
+ - 1
63
+ - 3
64
+ - 1
65
+ version: 1.3.1
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: Introspects your database string field maximum lengths and automatically defines length validations.
69
+ email: ben@railsgarden.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - lib/validates_lengths_from_database/railtie.rb
78
+ - lib/validates_lengths_from_database/version.rb
79
+ - lib/validates_lengths_from_database.rb
80
+ - spec/db/database.yml
81
+ - spec/db/schema.rb
82
+ - spec/db/test.sqlite3
83
+ - spec/spec_helper.rb
84
+ - spec/validates_lengths_from_database_spec.rb
85
+ - rails/init.rb
86
+ - Gemfile
87
+ - LICENSE
88
+ - Rakefile
89
+ - README.rdoc
90
+ - init.rb
91
+ has_rdoc: true
92
+ homepage: http://github.com/rubiety/validates_lengths_from_database
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options: []
97
+
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 19
115
+ segments:
116
+ - 1
117
+ - 3
118
+ - 4
119
+ version: 1.3.4
120
+ requirements: []
121
+
122
+ rubyforge_project: validates_lengths_from_database
123
+ rubygems_version: 1.3.7
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Automatic maximum-length validations.
127
+ test_files: []
128
+