activerecord-databasevalidations 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/active_record/database_validations/version.rb +1 -1
- data/lib/active_record/validations/string_truncator.rb +13 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c00506fa5c0018170d2c1b919b76f0a334cd1216
|
4
|
+
data.tar.gz: 6d8631cb529bbbeb52e53211f83dc27b9be94084
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fddcc3dcfee81c8dd7c9d2c812de2567307e30176ffc52b503218b30a0779d8a5668bce442990335f9d60e1817b28eb2ddfba8a164adb7828019cc1afbcf919
|
7
|
+
data.tar.gz: 4a41f0963cc799e802518fc21a97b1583bc7978136e4ec28c49f6a7d5b1be642e6f4ea3e86cbfcdb9a6873bf04168b95111dfba4ce724ec1e5bf34c7fd6d5021
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ActiveRecord::DatabaseValidations
|
1
|
+
# ActiveRecord::DatabaseValidations [![Build Status](https://travis-ci.org/wvanbergen/activerecord-databasevalidations.svg?branch=master)](https://travis-ci.org/wvanbergen/activerecord-databasevalidations)
|
2
2
|
|
3
3
|
Add validations to your ActiveRecord models based on your database constraints.
|
4
4
|
|
@@ -5,35 +5,32 @@ module ActiveRecord
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def truncate_string(field)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
return unless self.changes.key?(field.to_s)
|
13
|
-
return if self[field].nil?
|
8
|
+
method_name = :"truncate_#{field}_at_database_limit"
|
9
|
+
define_method(method_name) do
|
10
|
+
return unless self.changes.key?(field.to_s)
|
11
|
+
return if self[field].nil?
|
14
12
|
|
15
|
-
|
13
|
+
column = self.class.columns_hash[field.to_s]
|
14
|
+
limit = StringTruncator.mysql_textual_column_limit(column)
|
15
|
+
|
16
|
+
case column.type
|
17
|
+
when :string
|
16
18
|
value = self[field].to_s
|
17
19
|
if limit && value.length > limit
|
18
20
|
self[field] = value.slice(0, limit)
|
19
21
|
end
|
20
|
-
return true # to make sure the callback chain doesn't halt
|
21
|
-
end
|
22
22
|
|
23
|
-
|
24
|
-
lambda do
|
25
|
-
return unless self.changes.key?(field.to_s)
|
26
|
-
return if self[field].nil?
|
27
|
-
|
28
|
-
limit = StringTruncator.mysql_textual_column_limit(column)
|
23
|
+
when :text
|
29
24
|
value = self[field].to_s
|
30
25
|
value.encode!('utf-8') if value.encoding != Encoding::UTF_8
|
31
26
|
if limit && value.bytesize > limit
|
32
27
|
self[field] = value.mb_chars.limit(limit).to_s
|
33
28
|
end
|
34
|
-
return true # to make sure the callback chain doesn't halt
|
35
29
|
end
|
30
|
+
|
31
|
+
return # to make sure the callback chain doesn't halt
|
36
32
|
end
|
33
|
+
return method_name
|
37
34
|
end
|
38
35
|
end
|
39
36
|
|