activerecord-databasevalidations 0.2.3 → 0.2.4

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: 337cf9c69d98f33651b5a008c20f0d24648151ad
4
- data.tar.gz: 3d604b4f812af63b98c0cc0de1820a0b61147eae
3
+ metadata.gz: 34dc53f04bf08ea073fef57188664b90956b8321
4
+ data.tar.gz: 1572b1ea15781c21c1ae70c46c3a5ff92732139a
5
5
  SHA512:
6
- metadata.gz: 6700a0056bb6dd20e32b6c584b21509b1f2531bec7c7c26eb03d4eaff7f118ad2485b62657c97ac1409d80c0ad080a0389b0dc60344a17ff5e9614612d619d35
7
- data.tar.gz: 09cc0d7b8c4dffd6f32700e2575e66d74c012f9215db3da75bfda9587f297c8578e5c3c9a38260bcf29123cc6b90726abe5c11c52213fc36a0432db5980ec37e
6
+ metadata.gz: 0eb07eaee9796217e129a10e1cd955fa48bfd97faa3eb4566331ae46a72c018376bec8b40a3756a42f9e2371d6c2c8e4806921aec74602e0e0992915a5b9b0dc
7
+ data.tar.gz: 00911b2ddf02fcb8acf37c42b9faa3e4fba6aa60f79d4086427f9ef2ed636f96e4d6039cf1ee71d2344a2225d086620934f7948f7315d3774f0237179e719bc9
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module DatabaseValidations
3
- VERSION = "0.2.3"
3
+ VERSION = "0.2.4"
4
4
  end
5
5
  end
@@ -3,24 +3,35 @@ module ActiveRecord
3
3
  module StringTruncator
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ def truncate_value_to_field_limit(field, value)
7
+ return if value.nil?
8
+
9
+ column = self.class.columns_hash[field.to_s]
10
+ maximum, type, encoding = ActiveRecord::DatabaseValidations::MySQL.column_size_limit(column)
11
+ value = ActiveRecord::DatabaseValidations::MySQL.value_for_column(value, encoding)
12
+
13
+ case type
14
+ when :characters
15
+ value = value.slice(0, maximum) if maximum && value.length > maximum
16
+ when :bytes
17
+ value = value.mb_chars.limit(maximum).to_s if maximum && value.bytesize > maximum
18
+ end
19
+
20
+ value
21
+ end
22
+
6
23
  module ClassMethods
24
+ def truncate_to_field_limit(field)
25
+ define_method(:"#{field}=") do |value|
26
+ super(truncate_value_to_field_limit(field, value))
27
+ end
28
+ end
29
+
7
30
  def truncate_string(field)
8
31
  method_name = :"truncate_#{field}_at_database_limit"
9
32
  define_method(method_name) do
10
33
  return unless self.changes.key?(field.to_s)
11
- return if self[field].nil?
12
-
13
- column = self.class.columns_hash[field.to_s]
14
- maximum, type, encoding = ActiveRecord::DatabaseValidations::MySQL.column_size_limit(column)
15
- value = ActiveRecord::DatabaseValidations::MySQL.value_for_column(self[field], encoding)
16
-
17
- case type
18
- when :characters
19
- self[field] = value.slice(0, maximum) if maximum && value.length > maximum
20
- when :bytes
21
- self[field] = value.mb_chars.limit(maximum).to_s if maximum && value.bytesize > maximum
22
- end
23
-
34
+ self[field] = truncate_value_to_field_limit(field, self[field])
24
35
  return # to make sure the callback chain doesn't halt
25
36
  end
26
37
  return method_name
@@ -6,6 +6,8 @@ ActiveRecord::Migration.suppress_messages do
6
6
  t.string :string, limit: 255
7
7
  t.text :tinytext, limit: 255
8
8
  t.text :text
9
+
10
+ t.string :another_string, limit: 255
9
11
  end
10
12
  end
11
13
 
@@ -16,7 +18,9 @@ class MagicalCreature < ActiveRecord::Base
16
18
  before_validation truncate_string(:tinytext)
17
19
  before_validation truncate_string(:text)
18
20
 
19
- validates :string, :tinytext, database_constraints: :size
21
+ validates :string, :tinytext, :text, :another_string, database_constraints: :size
22
+
23
+ truncate_to_field_limit :another_string
20
24
  end
21
25
 
22
26
  class StringTruncatorTest < Minitest::Test
@@ -63,4 +67,9 @@ class StringTruncatorTest < Minitest::Test
63
67
  assert u5.valid?
64
68
  assert_equal 'a' * 65535, u5.text
65
69
  end
70
+
71
+ def test_truncate_to_field_limit
72
+ u6 = MagicalCreature.new(another_string: 'a' * 256)
73
+ assert_equal 'a' * 255, u6.another_string
74
+ end
66
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-databasevalidations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-01 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord