activerecord-databasevalidations 0.2.0 → 0.2.1
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 608512afb0bd466e2129b4ff11b4c6a03d6a69f5
|
4
|
+
data.tar.gz: 5ec035ae1221a5519ce1acad4ac5855e402e909a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82727638c4003d042fd9466879f40ada27259903efb1c9e939a8398508f8f7e5e29038d667314f929e2af5bd547be68554ad94fe74f0b342ee84e00895b24071
|
7
|
+
data.tar.gz: c98d2eea1a40164736f2f6830971b72f4d7b3710f7c0597d2890e53e9628c6746b679a964b2db9b51b0ea7acbe5324f83a7807746bf981f88a102859967c351f
|
@@ -14,7 +14,7 @@ module ActiveRecord
|
|
14
14
|
|
15
15
|
limit = StringTruncator.mysql_textual_column_limit(column)
|
16
16
|
value = self[field].to_s
|
17
|
-
if value.length > limit
|
17
|
+
if limit && value.length > limit
|
18
18
|
self[field] = value.slice(0, limit)
|
19
19
|
end
|
20
20
|
return true # to make sure the callback chain doesn't halt
|
@@ -28,7 +28,7 @@ module ActiveRecord
|
|
28
28
|
limit = StringTruncator.mysql_textual_column_limit(column)
|
29
29
|
value = self[field].to_s
|
30
30
|
value.encode!('utf-8') if value.encoding != Encoding::UTF_8
|
31
|
-
if value.bytesize > limit
|
31
|
+
if limit && value.bytesize > limit
|
32
32
|
self[field] = value.mb_chars.limit(limit).to_s
|
33
33
|
end
|
34
34
|
return true # to make sure the callback chain doesn't halt
|
@@ -41,7 +41,10 @@ module ActiveRecord
|
|
41
41
|
@mysql_textual_column_limits ||= {}
|
42
42
|
@mysql_textual_column_limits[column] ||= begin
|
43
43
|
raise ArgumentError, "Only UTF-8 textual columns are supported." unless column.text? && column.collation =~ /\Autf8_/
|
44
|
-
|
44
|
+
|
45
|
+
column_type = column.sql_type.sub(/\(.*\z/, '').gsub(/\s/, '_').to_sym
|
46
|
+
type_limit = ActiveRecord::Validations::DatabaseConstraintsValidator::TYPE_LIMITS.fetch(column_type, {})
|
47
|
+
column.limit || type_limit[:default_maximum]
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end
|
@@ -5,6 +5,7 @@ ActiveRecord::Migration.suppress_messages do
|
|
5
5
|
ActiveRecord::Migration.create_table("magical_creatures", force: true, options: "CHARACTER SET utf8mb3") do |t|
|
6
6
|
t.string :string, limit: 255
|
7
7
|
t.text :tinytext, limit: 255
|
8
|
+
t.text :text
|
8
9
|
end
|
9
10
|
end
|
10
11
|
|
@@ -13,6 +14,7 @@ class MagicalCreature < ActiveRecord::Base
|
|
13
14
|
|
14
15
|
before_validation truncate_string(:string)
|
15
16
|
before_validation truncate_string(:tinytext)
|
17
|
+
before_validation truncate_string(:text)
|
16
18
|
|
17
19
|
validates :string, :tinytext, database_constraints: :size
|
18
20
|
end
|
@@ -55,4 +57,10 @@ class StringTruncatorTest < Minitest::Test
|
|
55
57
|
assert_equal 'ü' * 127, u4.tinytext
|
56
58
|
assert_equal Encoding::UTF_8, u4.tinytext.encoding
|
57
59
|
end
|
60
|
+
|
61
|
+
def test_knows_limits_of_standard_types
|
62
|
+
u5 = MagicalCreature.new(text: 'a' * 65536)
|
63
|
+
assert u5.valid?
|
64
|
+
assert_equal 'a' * 65535, u5.text
|
65
|
+
end
|
58
66
|
end
|