validates_lengths_from_database 0.7.0 → 0.8.0
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 +5 -5
- data/Appraisals +6 -18
- data/CHANGELOG.rdoc +5 -0
- data/Gemfile.lock +6 -5
- data/README.rdoc +2 -4
- data/lib/validates_lengths_from_database/core.rb +59 -6
- data/lib/validates_lengths_from_database/version.rb +1 -1
- data/spec/validates_lengths_from_database_spec.rb +68 -1
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c8969981a23f54edfe7d597ad648c841c7beb9e7e296bb1bd050ca30b50ae18e
|
4
|
+
data.tar.gz: 55e92318175af1685a1665d9cb30c456a62b93701ee6e9e358a85f5b4159908c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4584e27e3758ceabeecf85f56263b0579be4ea190ecfe730cfe3aca2fac0e17208ad52fdbaf3afdd4dcd6aa0afa239bc496146f2c55784c003af83205f081eb
|
7
|
+
data.tar.gz: 6bb72ca778ab3aaae981f03bbf9e8bf9e73dacca4b96dd6dd30252d2bcd8a7a2fe53f080f0dc0d37732fde0eca80827355fe83aba4af67d941c39c0386a173cf
|
data/Appraisals
CHANGED
@@ -1,23 +1,11 @@
|
|
1
|
-
appraise "
|
2
|
-
gem "rails", "~>
|
3
|
-
end
|
4
|
-
|
5
|
-
appraise "3.1" do
|
6
|
-
gem "rails", "~> 3.1.4"
|
7
|
-
end
|
8
|
-
|
9
|
-
appraise "3.2" do
|
10
|
-
gem "rails", "~> 3.2.2"
|
11
|
-
end
|
12
|
-
|
13
|
-
appraise "4.0" do
|
14
|
-
gem "rails", "~> 4.0.12"
|
1
|
+
appraise "4.2" do
|
2
|
+
gem "rails", "~> 4.2"
|
15
3
|
end
|
16
4
|
|
17
|
-
appraise "
|
18
|
-
gem "rails", "~>
|
5
|
+
appraise "5.1" do
|
6
|
+
gem "rails", "~> 5.1"
|
19
7
|
end
|
20
8
|
|
21
|
-
appraise "
|
22
|
-
gem "rails", "~>
|
9
|
+
appraise "5.2" do
|
10
|
+
gem "rails", "~> 5.2"
|
23
11
|
end
|
data/CHANGELOG.rdoc
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
validates_lengths_from_database (0.
|
5
|
-
activerecord (>=
|
4
|
+
validates_lengths_from_database (0.7.0)
|
5
|
+
activerecord (>= 4)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
@@ -53,18 +53,19 @@ GEM
|
|
53
53
|
|
54
54
|
PLATFORMS
|
55
55
|
ruby
|
56
|
+
x86_64-darwin-18
|
56
57
|
|
57
58
|
DEPENDENCIES
|
58
|
-
activesupport (>=
|
59
|
+
activesupport (>= 4)
|
59
60
|
appraisal (~> 1.0.2)
|
60
61
|
i18n
|
61
62
|
iconv (~> 1.0.4)
|
62
63
|
pg (~> 0.17.1)
|
63
|
-
rake
|
64
|
+
rake (< 11)
|
64
65
|
rdoc (~> 3.12)
|
65
66
|
rspec (~> 2.0)
|
66
67
|
sqlite3 (~> 1.3.4)
|
67
68
|
validates_lengths_from_database!
|
68
69
|
|
69
70
|
BUNDLED WITH
|
70
|
-
1.
|
71
|
+
1.17.3
|
data/README.rdoc
CHANGED
@@ -9,10 +9,8 @@ This gem introspects your table schema for maximum lengths on string and text fi
|
|
9
9
|
|
10
10
|
== Requirements
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
Due to issues maintaining backwards compatibility, as of v0.4.0 validates_lengths_from_database requires ruby 1.9.3 or higher.
|
15
|
-
If you are still using 1.8.7 or 1.9.2, please use the v0.3.0 release.
|
12
|
+
Due to issues maintaining backwards compatibility, as of v0.8.0 validates_lengths_from_database requires ruby 2 or higher.
|
13
|
+
If you are still using 1.9, please use the v0.7.0 release.
|
16
14
|
|
17
15
|
== Installation
|
18
16
|
|
@@ -4,6 +4,14 @@ module ValidatesLengthsFromDatabase
|
|
4
4
|
base.send(:include, InstanceMethods)
|
5
5
|
end
|
6
6
|
|
7
|
+
class BytesizeValidator < ActiveModel::Validations::LengthValidator
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
value_for_validation = value.is_a?(String) ? value.bytes : value
|
10
|
+
|
11
|
+
super(record, attribute, value_for_validation)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
7
15
|
module ClassMethods
|
8
16
|
def validates_lengths_from_database(options = {})
|
9
17
|
options.symbolize_keys!
|
@@ -49,22 +57,67 @@ module ValidatesLengthsFromDatabase
|
|
49
57
|
|
50
58
|
columns_to_validate.each do |column|
|
51
59
|
column_schema = self.class.columns.find {|c| c.name == column }
|
60
|
+
|
52
61
|
next if column_schema.nil?
|
53
62
|
next if column_schema.respond_to?(:array) && column_schema.array
|
54
63
|
next unless [:string, :text, :decimal].include?(column_schema.type)
|
64
|
+
|
55
65
|
case column_schema.type
|
56
|
-
when :string
|
57
|
-
column_limit = options[:limit][column_schema.type] || column_schema.limit
|
58
|
-
|
59
|
-
|
66
|
+
when :string
|
67
|
+
if column_limit = options[:limit][column_schema.type] || column_schema.limit
|
68
|
+
validate_length_by_characters(column, column_limit)
|
69
|
+
end
|
70
|
+
when :text
|
71
|
+
if column_limit = options[:limit][column_schema.type] || column_schema.limit
|
72
|
+
validate_length_by_bytesize(column, column_limit)
|
60
73
|
end
|
61
74
|
when :decimal
|
62
75
|
if column_schema.precision && column_schema.scale
|
63
|
-
|
64
|
-
ActiveModel::Validations::NumericalityValidator.new(:less_than => max_val, :allow_blank => true, :attributes => [column]).validate(self)
|
76
|
+
validate_numericality_with_precision(column, column_schema)
|
65
77
|
end
|
66
78
|
end
|
67
79
|
end
|
68
80
|
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def validate_length_by_characters(column, column_limit)
|
85
|
+
ActiveModel::Validations::LengthValidator.new(
|
86
|
+
:allow_blank => true,
|
87
|
+
:attributes => [column],
|
88
|
+
:maximum => column_limit
|
89
|
+
).validate(self)
|
90
|
+
end
|
91
|
+
|
92
|
+
def validate_length_by_bytesize(column, column_limit)
|
93
|
+
validator =
|
94
|
+
if ActiveModel::Validations::LengthValidator::RESERVED_OPTIONS.include?(:tokenizer)
|
95
|
+
ActiveModel::Validations::LengthValidator.new(
|
96
|
+
:allow_blank => true,
|
97
|
+
:attributes => [column],
|
98
|
+
:maximum => column_limit,
|
99
|
+
:tokenizer => ->(string) { string.bytes }
|
100
|
+
)
|
101
|
+
else
|
102
|
+
BytesizeValidator.new(
|
103
|
+
:allow_blank => true,
|
104
|
+
:attributes => [column],
|
105
|
+
:maximum => column_limit,
|
106
|
+
:too_long => "is too long (maximum is %{count} bytes)"
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
validator.validate(self)
|
111
|
+
end
|
112
|
+
|
113
|
+
def validate_numericality_with_precision(column, column_schema)
|
114
|
+
max_val = (10 ** column_schema.precision)/(10 ** column_schema.scale)
|
115
|
+
|
116
|
+
ActiveModel::Validations::NumericalityValidator.new(
|
117
|
+
:allow_blank => true,
|
118
|
+
:attributes => [column],
|
119
|
+
:less_than => max_val
|
120
|
+
).validate(self)
|
121
|
+
end
|
69
122
|
end
|
70
123
|
end
|
@@ -222,7 +222,7 @@ describe ValidatesLengthsFromDatabase do
|
|
222
222
|
end
|
223
223
|
end
|
224
224
|
|
225
|
-
context "an article with
|
225
|
+
context "an article with string_1 that is too many characters" do
|
226
226
|
before { @article = ArticleValidateSpecificLimit.new(LONG_ATTRIBUTES); @article.valid? }
|
227
227
|
|
228
228
|
it "should not be valid" do
|
@@ -236,6 +236,73 @@ describe ValidatesLengthsFromDatabase do
|
|
236
236
|
@article.errors["decimal_1"].should_not be_present
|
237
237
|
end
|
238
238
|
end
|
239
|
+
|
240
|
+
context "an article with string_1 that is too many bytes, but few enough characters" do
|
241
|
+
before do
|
242
|
+
@article = ArticleValidateSpecificLimit.new(
|
243
|
+
LONG_ATTRIBUTES.merge(:string_1 => "😎😎😎😎", :string_2 => "👍👍👍👍")
|
244
|
+
)
|
245
|
+
|
246
|
+
@article.valid?
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should be valid" do
|
250
|
+
@article.should be_valid
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
context "an article with text_1 that is too many characters" do
|
255
|
+
before do
|
256
|
+
@article = ArticleValidateSpecificLimit.new(LONG_ATTRIBUTES.merge(:text_1 => "a"*101))
|
257
|
+
@article.valid?
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should not be valid" do
|
261
|
+
@article.should_not be_valid
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should have errors on all string/text attributes" do
|
265
|
+
@article.errors["string_1"].join.should =~ /too long/
|
266
|
+
@article.errors["string_2"].join.should =~ /too long/
|
267
|
+
@article.errors["text_1"].join.should =~ /too long/
|
268
|
+
@article.errors["decimal_1"].should_not be_present
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
context "an article with text_1 that is too many bytes, but few enough characters" do
|
273
|
+
before do
|
274
|
+
@article = ArticleValidateSpecificLimit.new(LONG_ATTRIBUTES.merge(:text_1 => "👍"*99))
|
275
|
+
@article.valid?
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should not be valid" do
|
279
|
+
@article.should_not be_valid
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should have errors on all string/text attributes" do
|
283
|
+
@article.errors["string_1"].join.should =~ /too long/
|
284
|
+
@article.errors["string_2"].join.should =~ /too long/
|
285
|
+
@article.errors["text_1"].join.should =~ /too long/
|
286
|
+
@article.errors["decimal_1"].should_not be_present
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
context "Model with validates_lengths_from_database :only => [:text_1], :limit => {:text => 10}" do
|
292
|
+
before do
|
293
|
+
class ArticleValidateOnlyText < ActiveRecord::Base
|
294
|
+
self.table_name = "articles_high_limit"
|
295
|
+
validates_lengths_from_database :only => [:text_1], :limit => {:text => 10}
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
context "an article with a length equal to max, but a bytesize greater than" do
|
300
|
+
before { @article = ArticleValidateOnlyText.new(:text_1 => "😎"*10); @article.valid? }
|
301
|
+
|
302
|
+
it "should not be valid" do
|
303
|
+
@article.should_not be_valid
|
304
|
+
end
|
305
|
+
end
|
239
306
|
end
|
240
307
|
|
241
308
|
context "Model with validates_lengths_from_database :only => [:string_1, :text_1]" do
|
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
|
+
version: 0.8.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:
|
11
|
+
date: 2019-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '4'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '4'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -112,16 +112,16 @@ dependencies:
|
|
112
112
|
name: rake
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - "
|
115
|
+
- - "<"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
117
|
+
version: '11'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - "
|
122
|
+
- - "<"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: '
|
124
|
+
version: '11'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: i18n
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -186,7 +186,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
186
186
|
requirements:
|
187
187
|
- - ">="
|
188
188
|
- !ruby/object:Gem::Version
|
189
|
-
version:
|
189
|
+
version: '2.4'
|
190
190
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
191
191
|
requirements:
|
192
192
|
- - ">="
|
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
194
194
|
version: 1.3.4
|
195
195
|
requirements: []
|
196
196
|
rubyforge_project: validates_lengths_from_database
|
197
|
-
rubygems_version: 2.6
|
197
|
+
rubygems_version: 2.7.6
|
198
198
|
signing_key:
|
199
199
|
specification_version: 4
|
200
200
|
summary: Automatic maximum-length validations.
|