truncates 0.0.6 → 0.0.7
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/Gemfile +10 -0
- data/lib/truncates/version.rb +1 -1
- data/lib/truncates.rb +6 -5
- data/test/test_helper.rb +45 -0
- data/test/truncates_test.rb +64 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NGIxNDA5MWFkNWY4ZTUxNWQ4MTA4YTQ1MjJmMzUxYjNhNjRhM2Q4Nw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YTg1NTM5MzQ4NjZhNGJmNTk2MWViNTI3N2I4ZTI5YTE4ZmZlMDQ3Zg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NTEyOWY5ZmYzNTQwZTM0YTk0YjVjYmUyNWNiOTM2YmZlNjNhNjYzOTFhYmU0
|
10
|
+
YjZiYzE4YTQyODJhY2EzNDIyNTA1YjA5YzhiN2M3MDVkZjcwMWY5NjA0YjYy
|
11
|
+
N2RhMWU1NDE3MjAwYjkyYjJkOTRlNDgzOWNmMTk2NWZlNWZmZjY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
N2U1NGNjM2RjZmIyMjNjOGYyM2Q4MjBhYjM1NWNhMjc0ZWQ2NTQyYWMzZWQ5
|
14
|
+
MDk3NWNhYTI1MjRkMTAwZWNjYzQxNWU2YzQ2ODE1Njg0ZjBhMTlmMTE4MmUz
|
15
|
+
ZDFkYWRhODcwOGUxYjY3MWNjYTlmODA0MTk0YzFhYTQ1ZDMwYTU=
|
data/Gemfile
CHANGED
data/lib/truncates/version.rb
CHANGED
data/lib/truncates.rb
CHANGED
@@ -3,17 +3,17 @@ require "truncates/version"
|
|
3
3
|
module Truncates
|
4
4
|
DEFAULT_MAX_LENGTH = 255
|
5
5
|
DEFAULT_CHARACTER_TRAIL = ""
|
6
|
-
DEFAULT_ON = :
|
6
|
+
DEFAULT_ON = :validation
|
7
7
|
|
8
8
|
def truncates(field_name, options = {})
|
9
9
|
raise("Hash expected, got #{options.class.name}") if !options.is_a?(Hash) && !options.empty?
|
10
10
|
max_length = options[:max_length] || DEFAULT_MAX_LENGTH
|
11
11
|
character_trail = options[:character_trail] || DEFAULT_CHARACTER_TRAIL
|
12
12
|
on = options[:on] || DEFAULT_ON
|
13
|
-
|
13
|
+
puts "field_name: #{field_name}\non: #{on}"
|
14
14
|
case on.to_s
|
15
15
|
when "validation"
|
16
|
-
before_validation do
|
16
|
+
before_validation do
|
17
17
|
value = eval("self.#{field_name}")
|
18
18
|
|
19
19
|
if(value.present? && value.length > max_length)
|
@@ -28,8 +28,9 @@ module Truncates
|
|
28
28
|
new_value = value.slice(0, max_length - character_trail.length) + character_trail
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
eval("
|
31
|
+
super(new_value)
|
32
|
+
#eval("@#{field_name} = \"#{new_value}\"")
|
33
|
+
#eval("return @#{field_name}")
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require 'active_model'
|
6
|
+
require 'truncates'
|
7
|
+
|
8
|
+
#$:.unshift "#{File.dirname(__FILE__)}/../"
|
9
|
+
#$:.unshift "#{File.dirname(__FILE__)}/../lib/"
|
10
|
+
#$:.unshift "#{File.dirname(__FILE__)}/../lib/validations"
|
11
|
+
|
12
|
+
|
13
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
14
|
+
|
15
|
+
def setup_db
|
16
|
+
ActiveRecord::Schema.define(:version => 1) do
|
17
|
+
create_table :truncates_customers do |t|
|
18
|
+
t.string :first_name
|
19
|
+
t.string :middle_name
|
20
|
+
t.string :last_name
|
21
|
+
t.string :city
|
22
|
+
t.string :state
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def teardown_db
|
28
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
29
|
+
ActiveRecord::Base.connection.drop_table(table)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class TruncatesCustomer < ActiveRecord::Base
|
34
|
+
validates :first_name, length: {maximum: 255}
|
35
|
+
validates :middle_name, length: {maximum: 5}
|
36
|
+
validates :last_name, length: {maximum: 10}
|
37
|
+
validates :city, length: {maximum: 5}
|
38
|
+
validates :state, length: {maximum: 5}
|
39
|
+
|
40
|
+
truncates :first_name
|
41
|
+
truncates :middle_name, max_length: 5
|
42
|
+
truncates :last_name, max_length: 10, character_trail: "..."
|
43
|
+
truncates :city, max_length: 5, on: :set
|
44
|
+
truncates :state, max_length: 5, on: :validation
|
45
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TruncatesTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
setup_db
|
6
|
+
@customer = TruncatesCustomer.new
|
7
|
+
end
|
8
|
+
|
9
|
+
teardown do
|
10
|
+
teardown_db
|
11
|
+
end
|
12
|
+
|
13
|
+
test "should truncate first_name to 255 characters before validation" do
|
14
|
+
first_name = "m" * 270
|
15
|
+
expected_truncated_value= "m" * 255
|
16
|
+
|
17
|
+
@customer.first_name = first_name
|
18
|
+
|
19
|
+
assert_equal(first_name, @customer.first_name)
|
20
|
+
assert_equal(true, @customer.valid?)
|
21
|
+
assert_equal(expected_truncated_value, @customer.first_name)
|
22
|
+
end
|
23
|
+
|
24
|
+
test "should truncate middle_name to 5 characters before validation" do
|
25
|
+
middle_name = "johnny"
|
26
|
+
expected_truncated_value = "johnn"
|
27
|
+
|
28
|
+
@customer.middle_name = middle_name
|
29
|
+
|
30
|
+
assert_equal(middle_name, @customer.middle_name)
|
31
|
+
assert_equal(true, @customer.valid?)
|
32
|
+
assert_equal(expected_truncated_value, @customer.middle_name)
|
33
|
+
end
|
34
|
+
|
35
|
+
test "shold truncate last_name to 10 characters and replace last characters with ... before validation" do
|
36
|
+
last_name = "longlastname"
|
37
|
+
expected_truncated_value = "longlas..."
|
38
|
+
|
39
|
+
@customer.last_name = last_name
|
40
|
+
|
41
|
+
assert_equal(last_name, @customer.last_name)
|
42
|
+
assert_equal(true, @customer.valid?)
|
43
|
+
assert_equal(expected_truncated_value, @customer.last_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
test "should truncate city to 5 characters when set" do
|
47
|
+
city = "cityname"
|
48
|
+
expected_truncated_value = "cityn"
|
49
|
+
|
50
|
+
@customer.city = city
|
51
|
+
assert_equal(expected_truncated_value, @customer.city)
|
52
|
+
end
|
53
|
+
|
54
|
+
test "should truncate state to 5 characters before validation" do
|
55
|
+
state = "Virginia"
|
56
|
+
expected_truncated_value = "Virgi"
|
57
|
+
|
58
|
+
@customer.state = state
|
59
|
+
|
60
|
+
assert_equal(state, @customer.state)
|
61
|
+
assert_equal(true, @customer.valid?)
|
62
|
+
assert_equal(expected_truncated_value, @customer.state)
|
63
|
+
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: truncates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael DeSanty
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,6 +53,8 @@ files:
|
|
53
53
|
- Rakefile
|
54
54
|
- lib/truncates.rb
|
55
55
|
- lib/truncates/version.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
- test/truncates_test.rb
|
56
58
|
- truncates.gemspec
|
57
59
|
homepage: ''
|
58
60
|
licenses:
|
@@ -78,4 +80,6 @@ rubygems_version: 2.2.2
|
|
78
80
|
signing_key:
|
79
81
|
specification_version: 4
|
80
82
|
summary: Automatically truncate fields to specifies maximum lengths.
|
81
|
-
test_files:
|
83
|
+
test_files:
|
84
|
+
- test/test_helper.rb
|
85
|
+
- test/truncates_test.rb
|