validates_lengths_from_database 0.5.1 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f73da6eb753b718d07fa7eb50a117bbdd51e6d96
4
- data.tar.gz: d5a31be0048cea9e7960487582b75d3becc4fd65
3
+ metadata.gz: 7d17fe52e66ac67d1cfc3a81dd72fee22a6050b7
4
+ data.tar.gz: 636155e6624043e9981dd9961eb990aa7059febf
5
5
  SHA512:
6
- metadata.gz: 351f1fcf3f3e9f2c674050775f1ef6150f07a952aa341e4702c67b5411f8abdc33a38b373deb2ca65774a1ea3c8c6f6876f39a4939c5dec05df9a4aa71956731
7
- data.tar.gz: 0211d47b6bf46d5cfba5baea54d57ab872e963c8d9a66e1da7f023f423084f5cfc9e691981e7166106aa92bd937841500fec398377d5b4bb0efae258f44e2d7b
6
+ metadata.gz: d500d14353f1297f9dc3c35a5333e4cedd81fcc1172867264d77299550c5fd3de9e58ae56231c47b09da517ef6e0f3fe2382a75bfdb95d5f1f6981ff706d5219
7
+ data.tar.gz: 445d4c872b1add3772218b3811e112823981935d4cd2f477fccae00fb9cfa56c11dbe49ff5fd10b76b0e0acc09e6ec0ebc9c6683300e3761dd27fa03f9c3650f
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == v0.5.2 (2017-04-28)
2
+
3
+ * Fix handling with STI models - thanks @iNecas
4
+
1
5
  == v0.5.1 (2017-04-27)
2
6
 
3
7
  * Fix sharing options between models - thanks @iNecas
data/README.rdoc CHANGED
@@ -50,7 +50,27 @@ Or to set the limit differently for string (VARCHAR) and (TEXT) columns:
50
50
  validates_lengths_from_database :limit => {:string => 255, :text => 4092}
51
51
  end
52
52
 
53
- 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).
53
+ Since v0.5.2, it's possible to use the `validate_lengths_from_database`
54
+ on an abstract parent (without a table), such as +ApplicationRecord+ or <tt>ActiveRecord::Base</tt>.
55
+
56
+ class ApplicationRecord < ActiveRecord::Base
57
+ self.abstract_class = true
58
+ validates_lengths_from_database :limit => 255
59
+ end
60
+
61
+ # This will use the validations with default options (from parent)
62
+ class Post < ApplicationRecord
63
+ end
64
+
65
+ # This will use the validations, with overriding the default options (the limit will not be applied)
66
+ class User < ApplicationRecord
67
+ validates_lengths_from_database :only => [:name]
68
+ end
69
+
70
+ # This will use the validations, using the default options + adding some class specific customizations
71
+ class Comment < ApplicationRecord
72
+ validate_lengths_from_database_options[:only] << :name
73
+ end
54
74
 
55
75
  == Running Tests
56
76
 
@@ -26,7 +26,16 @@ module ValidatesLengthsFromDatabase
26
26
  end
27
27
 
28
28
  def validate_lengths_from_database_options
29
- @validate_lengths_from_database_options
29
+ if defined? @validate_lengths_from_database_options
30
+ @validate_lengths_from_database_options
31
+ else
32
+ # in case we inherited the validations, copy the options so that we can update it in child
33
+ # without affecting the parent
34
+ @validate_lengths_from_database_options = superclass.validate_lengths_from_database_options.inject({}) do |hash, (key, value)|
35
+ value = value.dup if value.respond_to?(:dup)
36
+ hash.update(key => value)
37
+ end
38
+ end
30
39
  end
31
40
  end
32
41
 
@@ -1,3 +1,3 @@
1
1
  module ValidatesLengthsFromDatabase
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -26,6 +26,11 @@ describe ValidatesLengthsFromDatabase do
26
26
  ActiveSupport::Deprecation.silenced = true
27
27
  end
28
28
 
29
+ class ApplicationRecord < ActiveRecord::Base
30
+ self.abstract_class = true
31
+ validates_lengths_from_database :limit => 255
32
+ end
33
+
29
34
  context "Model without associated table" do
30
35
  specify "defining validates_lengths_from_database should not raise an error" do
31
36
  lambda {
@@ -109,7 +114,7 @@ describe ValidatesLengthsFromDatabase do
109
114
 
110
115
  class ArticleValidateText < ActiveRecord::Base
111
116
  self.table_name = "articles"
112
- validates_lengths_from_database :only => [:text_1]
117
+ validates_lengths_from_database :only => [:string_1]
113
118
  end
114
119
  end
115
120
 
@@ -130,6 +135,36 @@ describe ValidatesLengthsFromDatabase do
130
135
  end
131
136
  end
132
137
 
138
+ context "inheritance" do
139
+ before do
140
+ class ArticleValidateChild < ApplicationRecord
141
+ self.table_name = "articles"
142
+ validates_lengths_from_database :only => [:string_1]
143
+ end
144
+ class ArticleValidateChildOverwrite < ArticleValidateChild
145
+ validate_lengths_from_database_options[:only] << :string_2
146
+ end
147
+ class ArticleWithoutValidations < ApplicationRecord
148
+ self.table_name = "articles"
149
+ validates_lengths_from_database :only => []
150
+ end
151
+ end
152
+
153
+ it "should inherit the validation options from the parent" do
154
+ @article = ArticleValidateChild.new(LONG_ATTRIBUTES)
155
+ @article.valid?
156
+ @article.errors["string_1"].join.should =~ /too long/
157
+ @article.errors["string_2"].join.should_not =~ /too long/
158
+ end
159
+
160
+ it "should allow changing the validation options of child" do
161
+ @article = ArticleValidateChildOverwrite.new(LONG_ATTRIBUTES)
162
+ @article.valid?
163
+ @article.errors["string_1"].join.should =~ /too long/
164
+ @article.errors["string_2"].join.should =~ /too long/
165
+ end
166
+ end
167
+
133
168
  context "an article with short attributes" do
134
169
  before { @article = ArticleValidateAll.new(SHORT_ATTRIBUTES); @article.valid? }
135
170
 
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.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hughes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-27 00:00:00.000000000 Z
11
+ date: 2017-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord