truncates 0.0.7 → 0.0.8
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 +8 -8
- data/lib/truncates.rb +5 -3
- data/lib/truncates/version.rb +1 -1
- data/test/test_helper.rb +23 -0
- data/test/truncates_test.rb +49 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MWM5YjY5ZDg4NGUzNjA1MTNkM2JhYmFhNzA3ZTgyOGZiNzA5YjkxNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjE2NDY5ZTJhZGYzNjE2OTI4NzY0ZDVhYjkwZjVjODJhMzIzZTkxYw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MTkwZWVjMzQwMDdjNWM5YWYyMTI1Y2VhOGNkZDMxYTE0ZGM1MWM0MDdkNTRk
|
10
|
+
OWJmOTRkM2Q4ZDUxNjNlMjM2MWQ2ZTFjZDkzZmNjMTc2NjBkZmY1NWUzMTUy
|
11
|
+
NjJmYjgyY2VlYmJlYjc5NzU3NDY1NDgxZDBmZDAyNzRiMmJmN2Q=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTJjOWFkNzE3MGVhZTc4ZWIzMjE0Nzk5NjE0ZTQwMmRmZjNkZDY2MTllMzky
|
14
|
+
ZTdkZDAyMGRhYzdmM2VkN2QxYTEyOTc3NDNiMGIwYzE1OTNiNDEwZTJhOTZi
|
15
|
+
MzkxMTUzZTc0ODdlMzFlNWVjYTg3YTc5NzRlYWYzZWY0YWFjY2Q=
|
data/lib/truncates.rb
CHANGED
@@ -28,9 +28,11 @@ module Truncates
|
|
28
28
|
new_value = value.slice(0, max_length - character_trail.length) + character_trail
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
begin
|
32
|
+
super(new_value)
|
33
|
+
rescue NoMethodError
|
34
|
+
eval("@#{field_name} = \"#{new_value}\"")
|
35
|
+
end
|
34
36
|
end
|
35
37
|
end
|
36
38
|
end
|
data/lib/truncates/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -42,4 +42,27 @@ class TruncatesCustomer < ActiveRecord::Base
|
|
42
42
|
truncates :last_name, max_length: 10, character_trail: "..."
|
43
43
|
truncates :city, max_length: 5, on: :set
|
44
44
|
truncates :state, max_length: 5, on: :validation
|
45
|
+
end
|
46
|
+
|
47
|
+
class TruncatesUser
|
48
|
+
include ActiveModel::Validations
|
49
|
+
extend Truncates
|
50
|
+
|
51
|
+
attr_accessor :first_name,
|
52
|
+
:middle_name,
|
53
|
+
:last_name,
|
54
|
+
:city,
|
55
|
+
:state
|
56
|
+
|
57
|
+
validates :first_name, length: {maximum: 255}
|
58
|
+
validates :middle_name, length: {maximum: 5}
|
59
|
+
validates :last_name, length: {maximum: 10}
|
60
|
+
validates :city, length: {maximum: 5}
|
61
|
+
validates :state, length: {maximum: 5}
|
62
|
+
|
63
|
+
truncates :first_name, on: :set
|
64
|
+
truncates :middle_name, max_length: 5, on: :set
|
65
|
+
truncates :last_name, max_length: 10, character_trail: "...", on: :set
|
66
|
+
truncates :city, max_length: 5, on: :set
|
67
|
+
truncates :state, max_length: 5, on: :set
|
45
68
|
end
|
data/test/truncates_test.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TruncatesTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
|
5
|
+
setup do
|
6
|
+
setup_db
|
6
7
|
@customer = TruncatesCustomer.new
|
8
|
+
@user = TruncatesUser.new
|
7
9
|
end
|
8
10
|
|
9
11
|
teardown do
|
10
12
|
teardown_db
|
11
13
|
end
|
12
14
|
|
13
|
-
test "should truncate first_name to 255 characters before validation" do
|
15
|
+
test "should truncate customer first_name to 255 characters before validation" do
|
14
16
|
first_name = "m" * 270
|
15
17
|
expected_truncated_value= "m" * 255
|
16
18
|
|
@@ -21,7 +23,15 @@ class TruncatesTest < ActiveSupport::TestCase
|
|
21
23
|
assert_equal(expected_truncated_value, @customer.first_name)
|
22
24
|
end
|
23
25
|
|
24
|
-
test "should truncate
|
26
|
+
test "should truncate user first_name to 255 characters when set" do
|
27
|
+
first_name = "m" * 270
|
28
|
+
expected_truncated_value = "m" * 255
|
29
|
+
|
30
|
+
@user.first_name = first_name
|
31
|
+
assert_equal(expected_truncated_value, @user.first_name)
|
32
|
+
end
|
33
|
+
|
34
|
+
test "should truncate customer middle_name to 5 characters before validation" do
|
25
35
|
middle_name = "johnny"
|
26
36
|
expected_truncated_value = "johnn"
|
27
37
|
|
@@ -32,7 +42,15 @@ class TruncatesTest < ActiveSupport::TestCase
|
|
32
42
|
assert_equal(expected_truncated_value, @customer.middle_name)
|
33
43
|
end
|
34
44
|
|
35
|
-
test "
|
45
|
+
test "should truncate user middle_name to 5 characters when set" do
|
46
|
+
middle_name = "johnny"
|
47
|
+
expected_truncated_value = "johnn"
|
48
|
+
|
49
|
+
@user.middle_name = middle_name
|
50
|
+
assert_equal(expected_truncated_value, @user.middle_name)
|
51
|
+
end
|
52
|
+
|
53
|
+
test "shold truncate customer last_name to 10 characters and replace last characters with ... before validation" do
|
36
54
|
last_name = "longlastname"
|
37
55
|
expected_truncated_value = "longlas..."
|
38
56
|
|
@@ -43,7 +61,15 @@ class TruncatesTest < ActiveSupport::TestCase
|
|
43
61
|
assert_equal(expected_truncated_value, @customer.last_name)
|
44
62
|
end
|
45
63
|
|
46
|
-
test "should truncate
|
64
|
+
test "should truncate user last_name to 10 characters and replace last characters with ... when set" do
|
65
|
+
last_name = "longlastname"
|
66
|
+
expected_truncated_value = "longlas..."
|
67
|
+
|
68
|
+
@user.last_name = last_name
|
69
|
+
assert_equal(expected_truncated_value, @user.last_name)
|
70
|
+
end
|
71
|
+
|
72
|
+
test "should truncate customer city to 5 characters when set" do
|
47
73
|
city = "cityname"
|
48
74
|
expected_truncated_value = "cityn"
|
49
75
|
|
@@ -51,7 +77,15 @@ class TruncatesTest < ActiveSupport::TestCase
|
|
51
77
|
assert_equal(expected_truncated_value, @customer.city)
|
52
78
|
end
|
53
79
|
|
54
|
-
test "should truncate
|
80
|
+
test "should truncate user city to 5 characters when set" do
|
81
|
+
city = "cityname"
|
82
|
+
expected_truncated_value = "cityn"
|
83
|
+
|
84
|
+
@user.city = city
|
85
|
+
assert_equal(expected_truncated_value, @user.city)
|
86
|
+
end
|
87
|
+
|
88
|
+
test "should truncate customer state to 5 characters before validation" do
|
55
89
|
state = "Virginia"
|
56
90
|
expected_truncated_value = "Virgi"
|
57
91
|
|
@@ -61,4 +95,12 @@ class TruncatesTest < ActiveSupport::TestCase
|
|
61
95
|
assert_equal(true, @customer.valid?)
|
62
96
|
assert_equal(expected_truncated_value, @customer.state)
|
63
97
|
end
|
98
|
+
|
99
|
+
test "should truncate user state to 5 characters when set" do
|
100
|
+
state "Virginia"
|
101
|
+
expected_truncated_value = "Virgi"
|
102
|
+
|
103
|
+
@user.state = state
|
104
|
+
assert_equal(expected_truncated_value, @user.state)
|
105
|
+
end
|
64
106
|
end
|