attribute_extras 0.1.4 → 1.0.1

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.
data/test/test_classes.rb DELETED
@@ -1,86 +0,0 @@
1
- COLUMN_LIMIT = 255
2
- NUMBERS = %w[one two three four]
3
-
4
- ActiveRecord::Schema.define do
5
- create_table :people, force: true do |t|
6
- %w[nullified stripped truncated].each do |prefix|
7
- options = (prefix == 'truncated') ? { limit: COLUMN_LIMIT } : {}
8
- NUMBERS.each do |number|
9
- t.string "person_#{prefix}_#{number}", options
10
- end
11
- t.string "architect_#{prefix}", options
12
- end
13
- end
14
-
15
- create_table :addresses, force: true do |t|
16
- t.string :first_line, limit: COLUMN_LIMIT
17
- t.string :second_line, limit: COLUMN_LIMIT
18
- end
19
- end
20
-
21
- class Person < ActiveRecord::Base
22
-
23
- { nullified: :nullify_attributes, stripped: :strip_attributes, truncated: :truncate_attributes }.each do |prefix, macro|
24
- NUMBERS.each_with_index do |number, index|
25
- send(macro, "person_#{prefix}_#{number}".to_sym, validator: (index / 2).even?, writer: (index % 2))
26
- end
27
- end
28
-
29
- def set_blank_attributes
30
- NUMBERS.each do |number|
31
- write_attribute("person_nullified_#{number}", ' ')
32
- end
33
- save(validate: false)
34
- end
35
-
36
- def set_long_attributes
37
- NUMBERS.each do |number|
38
- write_attribute("person_truncated_#{number}", self.class.long_value)
39
- end
40
- save(validate: false)
41
- end
42
-
43
- def set_padded_attributes
44
- NUMBERS.each do |number|
45
- write_attribute("person_stripped_#{number}", ' test value ')
46
- end
47
- save(validate: false)
48
- end
49
-
50
- class << self
51
- def short_value
52
- @short_value ||= ('a' * COLUMN_LIMIT)
53
- end
54
-
55
- def stripped_value
56
- @stripped_value ||= 'test value'
57
- end
58
-
59
- def long_value
60
- @long_value ||= ('a' * 500)
61
- end
62
- end
63
- end
64
-
65
- class Developer < Person; end
66
-
67
- class Architect < Developer
68
- nullify_attributes :architect_nullified
69
- strip_attributes :architect_stripped
70
- truncate_attributes :architect_truncated
71
-
72
- def set_blank_attributes
73
- write_attribute(:architect_nullified, ' ')
74
- super
75
- end
76
-
77
- def set_long_attributes
78
- write_attribute(:architect_truncated, self.class.long_value)
79
- super
80
- end
81
-
82
- def set_padded_attributes
83
- write_attribute(:architect_stripped, ' test value ')
84
- super
85
- end
86
- end
data/test/test_helper.rb DELETED
@@ -1,10 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
-
3
- require 'active_record'
4
- require 'minitest/autorun'
5
- require 'attribute_extras'
6
-
7
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
8
- ActiveSupport.test_order = :sorted
9
-
10
- require 'test_classes'
@@ -1,57 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TruncateAttributesTest < ActiveSupport::TestCase
4
- def test_truncate_attributes
5
- person = Person.new
6
- person.set_long_attributes
7
-
8
- assert person.truncate_attributes
9
- person_attributes.each do |attribute|
10
- assert_equal Person.short_value, person.send(attribute)
11
- end
12
- end
13
-
14
- def test_truncate_attributes!
15
- person = Person.new
16
- person.set_long_attributes
17
-
18
- assert person.truncate_attributes!
19
- person_attributes.each do |attribute|
20
- assert_equal Person.short_value, person.send(attribute)
21
- end
22
- end
23
-
24
- def test_truncated_attributes_inheritance
25
- architect = Architect.new
26
- architect.set_long_attributes
27
-
28
- assert architect.truncate_attributes
29
- architect_attributes.each do |attribute|
30
- assert_equal Architect.short_value, architect.send(attribute)
31
- end
32
- end
33
-
34
- def test_truncated_attributes
35
- assert_equal person_attributes, Person.truncated_attributes.map(&:attribute)
36
- assert_empty Developer.truncated_attributes.map(&:attribute)
37
- assert_equal [:architect_truncated], Architect.truncated_attributes.map(&:attribute)
38
- end
39
-
40
- def test_inherited_truncated_attributes
41
- assert_equal person_attributes, Person.inherited_truncated_attributes.map(&:attribute)
42
- assert_equal person_attributes, Developer.inherited_truncated_attributes.map(&:attribute)
43
- assert_equal architect_attributes, Architect.inherited_truncated_attributes.map(&:attribute)
44
- end
45
-
46
- private
47
-
48
- # a list of the attributes that are truncated on the architect class
49
- def architect_attributes
50
- @architect_attributes ||= ([:architect_truncated] + person_attributes)
51
- end
52
-
53
- # a list of the attributes that are truncated on the person class
54
- def person_attributes
55
- @person_attributes ||= [:person_truncated_one, :person_truncated_two, :person_truncated_three, :person_truncated_four]
56
- end
57
- end