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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 608512afb0bd466e2129b4ff11b4c6a03d6a69f5
4
- data.tar.gz: 5ec035ae1221a5519ce1acad4ac5855e402e909a
3
+ metadata.gz: c00506fa5c0018170d2c1b919b76f0a334cd1216
4
+ data.tar.gz: 6d8631cb529bbbeb52e53211f83dc27b9be94084
5
5
  SHA512:
6
- metadata.gz: 82727638c4003d042fd9466879f40ada27259903efb1c9e939a8398508f8f7e5e29038d667314f929e2af5bd547be68554ad94fe74f0b342ee84e00895b24071
7
- data.tar.gz: c98d2eea1a40164736f2f6830971b72f4d7b3710f7c0597d2890e53e9628c6746b679a964b2db9b51b0ea7acbe5324f83a7807746bf981f88a102859967c351f
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
 
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module DatabaseValidations
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
5
5
  end
@@ -5,35 +5,32 @@ module ActiveRecord
5
5
 
6
6
  module ClassMethods
7
7
  def truncate_string(field)
8
- column = self.columns_hash[field.to_s]
9
- case column.type
10
- when :string
11
- lambda do
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
- limit = StringTruncator.mysql_textual_column_limit(column)
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
- when :text
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-databasevalidations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen