validates_lengths_from_database_tmp 0.1.2

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/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,30 @@
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"
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
+ 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,45 @@
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
+ return unless self.table_exists? #return if table does not exist. Happens in test env during migration
13
+ options.symbolize_keys!
14
+
15
+ raise ArgumentError, "The :only option to validates_lengths_from_database must be an array." if options[:only] and !options[:only].is_a?(Array)
16
+ raise ArgumentError, "The :except option to validates_lengths_from_database must be an array." if options[:except] and !options[:except].is_a?(Array)
17
+
18
+ if options[:only]
19
+ columns_to_validate = options[:only].map(&:to_s)
20
+ else
21
+ columns_to_validate = column_names.map(&:to_s)
22
+ columns_to_validate -= options[:except].map(&:to_s) if options[:except]
23
+ end
24
+
25
+ columns_to_validate.each do |column|
26
+ column_schema = columns.find {|c| c.name == column }
27
+ next if column_schema.nil?
28
+ next if ![:string, :text].include?(column_schema.type)
29
+ next if column_schema.limit.nil?
30
+
31
+ class_eval do
32
+ validates_length_of column, :maximum => column_schema.limit, :allow_blank => true
33
+ end
34
+ end
35
+
36
+ nil
37
+ end
38
+
39
+ end
40
+
41
+ module InstanceMethods
42
+ end
43
+ end
44
+
45
+ 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.1.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
@@ -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,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,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_lengths_from_database_tmp
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.2
6
+ platform: ruby
7
+ authors:
8
+ - Ben Hughes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-11 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.3.2
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "2.0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: sqlite3-ruby
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.1
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: Introspects your database string field maximum lengths and automatically defines length validations.
50
+ email: ben@railsgarden.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - lib/validates_lengths_from_database/railtie.rb
59
+ - lib/validates_lengths_from_database/version.rb
60
+ - lib/validates_lengths_from_database.rb
61
+ - spec/db/database.yml
62
+ - spec/db/schema.rb
63
+ - spec/spec_helper.rb
64
+ - spec/validates_lengths_from_database_spec.rb
65
+ - rails/init.rb
66
+ - Gemfile
67
+ - LICENSE
68
+ - Rakefile
69
+ - README.rdoc
70
+ - init.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/rubiety/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_tmp
95
+ rubygems_version: 1.6.2
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Automatic maximum-length validations.
99
+ test_files: []
100
+