validates_lengths_from_database 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36ab4fa806bb72ce68f69c1bfe9375ea5b8a72aa
4
- data.tar.gz: 28f5c8941b6717983b9b0f7ba236695b36d2a551
3
+ metadata.gz: 11cac7bb47bda407a7ef1ed3c928d7a364c9522c
4
+ data.tar.gz: eb71d89711455e1c869155f98694fc1029a9f68c
5
5
  SHA512:
6
- metadata.gz: 3d5895cc5180acdf4aa522e5350c6edadcec087fac8b882159748f2b063060a0e70a7b19b9e16e8211d64a1c0d5e88726fe0d5114f6ed6981cb58b885e53ec86
7
- data.tar.gz: 6e445869d1cae0997f61ebb2cb43113acef46cf76f118242a9dad53bc7139f0851bb995a5216c7ea2b227c381135292c6157a39c0abed2de695a87437e095e39
6
+ metadata.gz: e4ebd4890711a54a80410beccff3ac20e7e20ee9ca8626c46c40b553d6b0f9e028d0b6a227f28f763ca6f3c8c1f6eb5b09d83d0a5d70c956c233ed336e533be7
7
+ data.tar.gz: 2c82869d46cea0bcbdc51485b8b7f2e0c579a3060580eff52c82538d294849110d5bceaa726512a95ac9e11a90b5bfa8b37da87e9ac6519bec4d6c676b31f2aa
data/Appraisals CHANGED
@@ -1,7 +1,3 @@
1
- appraise "2.3" do
2
- gem "rails", "~> 2.3.2"
3
- end
4
-
5
1
  appraise "3.0" do
6
2
  gem "rails", "~> 3.0.12"
7
3
  end
File without changes
@@ -31,7 +31,7 @@ GEM
31
31
  diff-lcs (1.1.2)
32
32
  i18n (0.7.0)
33
33
  iconv (1.0.4)
34
- json (1.6.6)
34
+ json (1.8.3)
35
35
  minitest (4.7.5)
36
36
  multi_json (1.10.1)
37
37
  pg (0.17.1)
@@ -46,7 +46,7 @@ GEM
46
46
  rspec-expectations (2.5.0)
47
47
  diff-lcs (~> 1.1.2)
48
48
  rspec-mocks (2.5.0)
49
- sqlite3 (1.3.5)
49
+ sqlite3 (1.3.11)
50
50
  thor (0.19.1)
51
51
  thread_safe (0.3.4)
52
52
  tzinfo (0.3.42)
@@ -65,3 +65,6 @@ DEPENDENCIES
65
65
  rspec (~> 2.0)
66
66
  sqlite3 (~> 1.3.4)
67
67
  validates_lengths_from_database!
68
+
69
+ BUNDLED WITH
70
+ 1.10.6
@@ -9,6 +9,8 @@ This gem introspects your table schema for maximum lengths on string and text fi
9
9
 
10
10
  == Requirements
11
11
 
12
+ As of v0.5.0, validates_lengths_from_database lazily loads column information and requires Rails 3+. If you are using Rails 2, please use the v0.4.0 release.
13
+
12
14
  Due to issues maintaining backwards compatibility, as of v0.4.0 validates_lengths_from_database requires ruby 1.9.3 or higher.
13
15
  If you are still using 1.8.7 or 1.9.2, please use the v0.3.0 release.
14
16
 
@@ -58,5 +60,6 @@ This gem uses appraisal to test with different versions of the dependencies. See
58
60
  $ rake test
59
61
 
60
62
  # All of the Appraisals:
61
- $ rake appraisal test
63
+ $ appraisal install
64
+ $ appraisal rake test
62
65
 
@@ -11,7 +11,6 @@ module ValidatesLengthsFromDatabase
11
11
  def validates_lengths_from_database(options = {})
12
12
  options.symbolize_keys!
13
13
 
14
- return false unless self.table_exists?
15
14
  options[:only] = Array[options[:only]] if options[:only] && !options[:only].is_a?(Array)
16
15
  options[:except] = Array[options[:except]] if options[:except] && !options[:except].is_a?(Array)
17
16
  options[:limit] ||= {}
@@ -19,16 +18,31 @@ module ValidatesLengthsFromDatabase
19
18
  if options[:limit] and !options[:limit].is_a?(Hash)
20
19
  options[:limit] = {:string => options[:limit], :text => options[:limit]}
21
20
  end
21
+ @@validate_lengths_from_database_options = options
22
+
23
+ validate :validate_lengths_from_database
24
+
25
+ nil
26
+ end
27
+
28
+ def validate_lengths_from_database_options
29
+ @@validate_lengths_from_database_options
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ def validate_lengths_from_database
35
+ options = self.class.validate_lengths_from_database_options
22
36
 
23
37
  if options[:only]
24
38
  columns_to_validate = options[:only].map(&:to_s)
25
39
  else
26
- columns_to_validate = column_names.map(&:to_s)
40
+ columns_to_validate = self.class.column_names.map(&:to_s)
27
41
  columns_to_validate -= options[:except].map(&:to_s) if options[:except]
28
42
  end
29
43
 
30
44
  columns_to_validate.each do |column|
31
- column_schema = columns.find {|c| c.name == column }
45
+ column_schema = self.class.columns.find {|c| c.name == column }
32
46
  next if column_schema.nil?
33
47
  next if ![:string, :text].include?(column_schema.type)
34
48
  next if column_schema.respond_to?(:array) && column_schema.array
@@ -36,17 +50,9 @@ module ValidatesLengthsFromDatabase
36
50
  column_limit = options[:limit][column_schema.type] || column_schema.limit
37
51
  next unless column_limit
38
52
 
39
- class_eval do
40
- validates_length_of column, :maximum => column_limit, :allow_blank => true
41
- end
53
+ ActiveModel::Validations::LengthValidator.new(:maximum => column_limit, :allow_blank => true, :attributes => [column]).validate(self)
42
54
  end
43
-
44
- nil
45
55
  end
46
-
47
- end
48
-
49
- module InstanceMethods
50
56
  end
51
57
  end
52
58
 
@@ -1,3 +1,3 @@
1
1
  module ValidatesLengthsFromDatabase
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
Binary file
@@ -33,6 +33,69 @@ describe ValidatesLengthsFromDatabase do
33
33
  end
34
34
  end
35
35
 
36
+ context "Model without a table yet" do
37
+ before do
38
+ class LazyTableArticle < ActiveRecord::Base
39
+ self.table_name = "articles_lazy"
40
+ validates_lengths_from_database
41
+ end
42
+ end
43
+
44
+ context "before the table is created" do
45
+ specify "should fail to create records" do
46
+ lambda { LazyTableArticle.new }.should raise_error
47
+ end
48
+ end
49
+
50
+ context "after the table is created" do
51
+ before do
52
+ ActiveRecord::Schema.define do
53
+ create_table :articles_lazy, :force => false do |t|
54
+ t.string :string_1, :limit => 5
55
+ end
56
+ end
57
+ end
58
+
59
+ after do
60
+ ActiveRecord::Schema.define do
61
+ drop_table :articles_lazy
62
+ end
63
+ end
64
+
65
+ context "an article with long attributes" do
66
+ before { @article = LazyTableArticle.new(LONG_ATTRIBUTES.slice(:string_1)); @article.valid? }
67
+
68
+ specify "should not be valid" do
69
+ @article.should_not be_valid
70
+ end
71
+ end
72
+
73
+ context "an article with short attributes" do
74
+ before { @article = LazyTableArticle.new(SHORT_ATTRIBUTES.slice(:string_1)); @article.valid? }
75
+
76
+ specify "should be valid" do
77
+ @article.should be_valid
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ context "Model without validates_lengths_from_database" do
84
+ before do
85
+ class ArticleValidateAll < ActiveRecord::Base
86
+ self.table_name = "articles"
87
+ end
88
+ end
89
+
90
+ context "an article with overloaded attributes" do
91
+ before { @article = ArticleValidateAll.new(LONG_ATTRIBUTES); @article.valid? }
92
+
93
+ it "should be valid" do
94
+ @article.should be_valid
95
+ end
96
+ end
97
+ end
98
+
36
99
  context "Model with validates_lengths_from_database" do
37
100
  before do
38
101
  class ArticleValidateAll < ActiveRecord::Base
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validates_lengths_from_database/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "validates_lengths_from_database"
7
+ s.version = ValidatesLengthsFromDatabase::VERSION
8
+ s.author = "Ben Hughes"
9
+ s.email = "ben@railsgarden.com"
10
+ s.homepage = "http://github.com/rubiety/validates_lengths_from_database"
11
+ s.summary = "Automatic maximum-length validations."
12
+ s.description = "Introspects your database string field maximum lengths and automatically defines length validations."
13
+
14
+ s.files = Dir["{lib,spec,rails}/**/*", "[A-Z]*", "init.rb"]
15
+ s.require_path = "lib"
16
+
17
+ s.rubyforge_project = s.name
18
+ s.required_rubygems_version = ">= 1.3.4"
19
+ s.required_ruby_version = ">= 1.9.3"
20
+
21
+ s.add_dependency("activerecord", [">= 2.3.2"])
22
+ s.add_development_dependency("activesupport", [">= 2.3.2"])
23
+ s.add_development_dependency("rspec", ["~> 2.0"])
24
+ s.add_development_dependency("sqlite3", ["~> 1.3.4"])
25
+ s.add_development_dependency("appraisal", ["~> 1.0.2"])
26
+ s.add_development_dependency("pg", ["~> 0.17.1"])
27
+ s.add_development_dependency("rdoc", ["~> 3.12"])
28
+ s.add_development_dependency "rake"
29
+
30
+ # I'm not sure why this isn't installed along with activesupport,
31
+ # but for whatever reason running `bundle install` doesn't install
32
+ # i18n so I'm adding it here for now.
33
+ # https://github.com/rails/rails/blob/master/activesupport/activesupport.gemspec#L19 ?
34
+ s.add_development_dependency("i18n")
35
+ s.add_development_dependency("iconv", "~> 1.0.4")
36
+ end
37
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_lengths_from_database
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hughes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-04 00:00:00.000000000 Z
11
+ date: 2016-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -158,7 +158,7 @@ extensions: []
158
158
  extra_rdoc_files: []
159
159
  files:
160
160
  - Appraisals
161
- - CHANGELOG
161
+ - CHANGELOG.rdoc
162
162
  - Gemfile
163
163
  - Gemfile.lock
164
164
  - LICENSE
@@ -174,6 +174,8 @@ files:
174
174
  - spec/db/test.sqlite3
175
175
  - spec/spec_helper.rb
176
176
  - spec/validates_lengths_from_database_spec.rb
177
+ - validates_lengths_from_database-0.4.0.gem
178
+ - validates_lengths_from_database.gemspec
177
179
  homepage: http://github.com/rubiety/validates_lengths_from_database
178
180
  licenses: []
179
181
  metadata: {}
@@ -193,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
195
  version: 1.3.4
194
196
  requirements: []
195
197
  rubyforge_project: validates_lengths_from_database
196
- rubygems_version: 2.2.2
198
+ rubygems_version: 2.4.8
197
199
  signing_key:
198
200
  specification_version: 4
199
201
  summary: Automatic maximum-length validations.