axle_attributes 1.13.2
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 +7 -0
- data/.gitignore +4 -0
- data/.travis.yml +16 -0
- data/Gemfile +15 -0
- data/README.md +10 -0
- data/Rakefile +18 -0
- data/app/models/attribute_customization.rb +7 -0
- data/app/models/attribute_customization/local_cache.rb +28 -0
- data/app/models/attribute_mapping.rb +15 -0
- data/app/models/attribute_mapping/local_cache.rb +40 -0
- data/axle_attributes.gemspec +22 -0
- data/bin/rails +8 -0
- data/db/migrate/20131118042651_create_attribute_customizations.rb +16 -0
- data/db/migrate/20131121013237_create_attribute_mappings.rb +17 -0
- data/db/migrate/20151120160522_add_editable_to_attribute_customizations.rb +7 -0
- data/lib/axle_attributes.rb +30 -0
- data/lib/axle_attributes/child_definition.rb +42 -0
- data/lib/axle_attributes/conversions.rb +36 -0
- data/lib/axle_attributes/definition.rb +118 -0
- data/lib/axle_attributes/definition/callbacks.rb +34 -0
- data/lib/axle_attributes/definition/customization.rb +51 -0
- data/lib/axle_attributes/definition/formatted.rb +57 -0
- data/lib/axle_attributes/definition/indexed.rb +41 -0
- data/lib/axle_attributes/definition/mappings.rb +48 -0
- data/lib/axle_attributes/dumper.rb +54 -0
- data/lib/axle_attributes/engine.rb +4 -0
- data/lib/axle_attributes/format.rb +74 -0
- data/lib/axle_attributes/has_attributes.rb +104 -0
- data/lib/axle_attributes/has_attributes/json_reader.rb +87 -0
- data/lib/axle_attributes/model.rb +34 -0
- data/lib/axle_attributes/null_definition.rb +11 -0
- data/lib/axle_attributes/parent_definition.rb +37 -0
- data/lib/axle_attributes/provided.rb +67 -0
- data/lib/axle_attributes/regex.rb +4 -0
- data/lib/axle_attributes/segmented.rb +30 -0
- data/lib/axle_attributes/serializations.rb +147 -0
- data/lib/axle_attributes/serializations/builder/serialization.rb +83 -0
- data/lib/axle_attributes/serializations/builder/serialize_many.rb +17 -0
- data/lib/axle_attributes/serializations/builder/serialize_one.rb +7 -0
- data/lib/axle_attributes/serializations/reflection.rb +40 -0
- data/lib/axle_attributes/serializations/serialization.rb +88 -0
- data/lib/axle_attributes/serializations/serialize_many.rb +88 -0
- data/lib/axle_attributes/serializations/serialize_one.rb +34 -0
- data/lib/axle_attributes/serialized_child.rb +58 -0
- data/lib/axle_attributes/validations.rb +29 -0
- data/lib/axle_attributes/versioned.rb +21 -0
- data/test/dummy/README.rdoc +6 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +12 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +13 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/production.rb +80 -0
- data/test/dummy/config/environments/test.rb +38 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +12 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/db/schema.rb +50 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/factories/all.rb +14 -0
- data/test/helper.rb +25 -0
- data/test/lib/child_definition_test.rb +43 -0
- data/test/lib/conversions_test.rb +39 -0
- data/test/lib/definition/customization_test.rb +36 -0
- data/test/lib/definition/formatted_test.rb +75 -0
- data/test/lib/definition/indexed_test.rb +57 -0
- data/test/lib/definition/mappings_test.rb +26 -0
- data/test/lib/definition_test.rb +84 -0
- data/test/lib/dumper_test.rb +38 -0
- data/test/lib/format_test.rb +64 -0
- data/test/lib/has_attributes/json_reader_test.rb +46 -0
- data/test/lib/has_attributes_test.rb +69 -0
- data/test/lib/model_test.rb +44 -0
- data/test/lib/null_definition_test.rb +27 -0
- data/test/lib/parent_definition_test.rb +75 -0
- data/test/lib/provided_test.rb +46 -0
- data/test/lib/segmented_test.rb +27 -0
- data/test/lib/serializations/reflection_test.rb +24 -0
- data/test/lib/serializations/serialize_many_test.rb +194 -0
- data/test/lib/serializations/serialize_one_test.rb +122 -0
- data/test/lib/serializations_test.rb +24 -0
- data/test/lib/serialized_child_test.rb +91 -0
- data/test/lib/timestamp_attributes_test.rb +14 -0
- data/test/lib/validations_test.rb +8 -0
- data/test/lib/versioned_test.rb +35 -0
- data/test/models/attribute_customization/local_cache_test.rb +16 -0
- data/test/models/attribute_customization_test.rb +8 -0
- data/test/models/attribute_mapping/local_cache_test.rb +31 -0
- data/test/models/attribute_mapping_test.rb +20 -0
- data/test/support/business.rb +22 -0
- data/test/support/pg.rb +9 -0
- data/test/support/vegetable.rb +8 -0
- metadata +215 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class AxleAttributes::ProvidedTest < ActiveSupport::TestCase
|
|
4
|
+
test 'provided on write' do
|
|
5
|
+
business = Business.new
|
|
6
|
+
assert_equal [], business.provided
|
|
7
|
+
|
|
8
|
+
business.name = "foo"
|
|
9
|
+
assert_equal ['name'].to_set, business.provided_set
|
|
10
|
+
assert_equal ['name'], business.provided
|
|
11
|
+
assert business.name_provided?
|
|
12
|
+
assert business.provided?('name')
|
|
13
|
+
refute business.street_provided?
|
|
14
|
+
refute business.provided?('street')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test 'without_provided_tracking' do
|
|
18
|
+
business = Business.new
|
|
19
|
+
|
|
20
|
+
Business.without_provided_tracking do
|
|
21
|
+
business.name = "foo"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
refute business.name_provided?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'provided cleared' do
|
|
28
|
+
Business.new(name: 'foo').tap do |business|
|
|
29
|
+
business.name = 'bar'
|
|
30
|
+
business.save
|
|
31
|
+
assert_equal [], business.provided
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
Business.new(name: 'foo').tap do |business|
|
|
35
|
+
business.name = 'bar'
|
|
36
|
+
business.save!
|
|
37
|
+
assert_equal [], business.provided
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
Business.create(name: 'foo').tap do |business|
|
|
41
|
+
business.name = 'bar'
|
|
42
|
+
business.reload
|
|
43
|
+
assert_equal [], business.provided
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class AxleAttributes::SegmentedTest < ActiveSupport::TestCase
|
|
4
|
+
class TestModel < Superstore::Base
|
|
5
|
+
include AxleAttributes::Model
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test 'segment_exists' do
|
|
9
|
+
record = TestModel.new
|
|
10
|
+
|
|
11
|
+
assert record.respond_to? :segment_id
|
|
12
|
+
assert record.respond_to? :segment_id=
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
test 'segment_range' do
|
|
16
|
+
assert_equal 0..1023, TestModel.segment_range
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
test 'segment_populated' do
|
|
20
|
+
record = TestModel.new(id: 'poop')
|
|
21
|
+
assert_nil record.segment_id
|
|
22
|
+
|
|
23
|
+
record.run_callbacks :create
|
|
24
|
+
|
|
25
|
+
assert_equal 260, record.segment_id
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class AxleAttributes::Serializations::ReflectionTest < ActiveSupport::TestCase
|
|
4
|
+
test 'collection' do
|
|
5
|
+
assert reflection_class.new(:serialize_many, :doggies, {}).collection?
|
|
6
|
+
refute reflection_class.new(:serialize_one, :doggy, {}).collection?
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
test 'counter_cache_column' do
|
|
10
|
+
assert_equal 'doggies_count', reflection_class.new(:serialize_many, :doggies, {}).counter_cache_column
|
|
11
|
+
assert_nil reflection_class.new(:serialize_many, :doggies, counter_cache: false).counter_cache_column
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'klass' do
|
|
15
|
+
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def reflection_class
|
|
21
|
+
AxleAttributes::Serializations::Reflection
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class AxleAttributes::Serializations::SerializeManyTest < ActiveSupport::TestCase
|
|
4
|
+
class Girlfriend < Superstore::Base
|
|
5
|
+
include AxleAttributes::SerializedChild
|
|
6
|
+
|
|
7
|
+
has_attribute :color, type: :string, index: true
|
|
8
|
+
has_attribute :height, type: :integer, index: false
|
|
9
|
+
|
|
10
|
+
validates :color, inclusion: {in: ['red', 'blue']}
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class TestModel < Business
|
|
14
|
+
serialize_many :girlfriends, class_name: 'AxleAttributes::Serializations::SerializeManyTest::Girlfriend'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test 'attributes' do
|
|
18
|
+
assert_nil TestModel.attributes['girlfriends_json']
|
|
19
|
+
refute_nil TestModel.attributes['girlfriends_count']
|
|
20
|
+
|
|
21
|
+
refute_nil TestModel.attributes['girlfriends.color']
|
|
22
|
+
refute_nil TestModel.attributes['girlfriends.height']
|
|
23
|
+
|
|
24
|
+
assert_equal 'girlfriends', TestModel.attributes['girlfriends.height'].nested_path
|
|
25
|
+
|
|
26
|
+
assert TestModel.new.respond_to?(:girlfriends)
|
|
27
|
+
assert TestModel.new.respond_to?(:girlfriends=)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test 'index mapping' do
|
|
31
|
+
contacts_mapping = TestModel.elastic_index.mapping[:properties]['girlfriends']
|
|
32
|
+
|
|
33
|
+
refute_nil contacts_mapping
|
|
34
|
+
assert_equal :nested, contacts_mapping[:type]
|
|
35
|
+
assert_equal Girlfriend.attributes['color'].index_mapping, contacts_mapping[:properties]['color']
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'serialization_reflections' do
|
|
39
|
+
refute_nil TestModel.serialization_reflections[:girlfriends]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
test 'writer on empty' do
|
|
43
|
+
girlfriend = Girlfriend.new(color: 'blue')
|
|
44
|
+
record = TestModel.new
|
|
45
|
+
|
|
46
|
+
record.girlfriends = [girlfriend]
|
|
47
|
+
|
|
48
|
+
assert_equal 1, record.girlfriends_count
|
|
49
|
+
assert_equal [girlfriend], record.girlfriends
|
|
50
|
+
assert_equal 'blue', record[:girlfriends].first['color']
|
|
51
|
+
assert_kind_of String, record[:girlfriends].first['created_at']
|
|
52
|
+
assert_equal record.child_serialization(:girlfriends), record.girlfriends.first.serialization_instance
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
test 'writer on existing record' do
|
|
56
|
+
original_girlfriend = Girlfriend.new(id: '5', color: 'blue')
|
|
57
|
+
updated_girlfriend = Girlfriend.new(id: '5', color: 'green')
|
|
58
|
+
record = TestModel.new girlfriends: [original_girlfriend]
|
|
59
|
+
|
|
60
|
+
record.girlfriends = [updated_girlfriend]
|
|
61
|
+
|
|
62
|
+
assert_equal 1, record.girlfriends_count
|
|
63
|
+
assert_equal 'green', record.girlfriends.first.color
|
|
64
|
+
assert_equal original_girlfriend.created_at, record.girlfriends.first.created_at
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test 'writer on record replacement' do
|
|
68
|
+
original_girlfriend = Girlfriend.new(id: '5', color: 'blue')
|
|
69
|
+
new_girlfriend = Girlfriend.new(id: '10', color: 'green')
|
|
70
|
+
record = TestModel.new girlfriends: [original_girlfriend]
|
|
71
|
+
|
|
72
|
+
record.girlfriends = [new_girlfriend]
|
|
73
|
+
|
|
74
|
+
assert_equal 1, record.girlfriends_count
|
|
75
|
+
assert_equal 'green', record.girlfriends.first.color
|
|
76
|
+
assert_equal new_girlfriend, record.girlfriends.first
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
test 'writer with nil' do
|
|
80
|
+
record = TestModel.new girlfriends: nil
|
|
81
|
+
assert_equal Set.new, record.provided_set
|
|
82
|
+
assert_equal Hash.new, record.changes
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
test 'attributes_writer updates existing' do
|
|
86
|
+
girlfriend = Girlfriend.new(id: 'abc', height: 60)
|
|
87
|
+
record = TestModel.new girlfriends: [girlfriend]
|
|
88
|
+
|
|
89
|
+
record.girlfriends_attributes = [
|
|
90
|
+
{'id' => 'abc', height: 68}
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
assert_equal [girlfriend.object_id], record.girlfriends.map(&:object_id)
|
|
94
|
+
assert_equal 68, girlfriend.height
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
test 'attributes_writer partial write' do
|
|
98
|
+
keeper_girlfriend = Girlfriend.new(id: 'abc', height: 60)
|
|
99
|
+
loser_girlfriend = Girlfriend.new(id: 'xyz', height: 48)
|
|
100
|
+
record = TestModel.new girlfriends: [keeper_girlfriend, loser_girlfriend]
|
|
101
|
+
|
|
102
|
+
record.girlfriends_attributes = [
|
|
103
|
+
{'id' => 'xyz', '_destroy' => '1'},
|
|
104
|
+
{'id' => 'mno', height: 68}
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
assert_equal ['abc', 'mno'].to_set, record.girlfriends.map(&:id).to_set
|
|
108
|
+
|
|
109
|
+
assert record.girlfriends.include?(keeper_girlfriend)
|
|
110
|
+
assert record.girlfriends.exclude?(loser_girlfriend)
|
|
111
|
+
new_girlfriend = record.girlfriends.detect { |gf| gf.id == 'mno' }
|
|
112
|
+
assert_equal 68, new_girlfriend.height
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
test 'attributes_writer with empty collection' do
|
|
116
|
+
record = TestModel.new girlfriends_attributes: []
|
|
117
|
+
assert_equal Set.new, record.provided_set
|
|
118
|
+
assert_equal Hash.new, record.changes
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
test 'read when nil' do
|
|
122
|
+
record = TestModel.new
|
|
123
|
+
|
|
124
|
+
assert_equal [], record.girlfriends
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
test 'reader' do
|
|
128
|
+
record = TestModel.new
|
|
129
|
+
record[:girlfriends] = [{'id' => '5', 'color' => 'red'}]
|
|
130
|
+
|
|
131
|
+
assert_equal [Girlfriend.new(id: '5')], record.girlfriends
|
|
132
|
+
assert_equal record.child_serialization(:girlfriends), record.girlfriends.first.serialization_instance
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
test 'reader_was' do
|
|
136
|
+
record = TestModel.new
|
|
137
|
+
record.girlfriends = [{'id' => '5', 'color' => 'red'}]
|
|
138
|
+
|
|
139
|
+
assert_equal [], record.girlfriends_was
|
|
140
|
+
|
|
141
|
+
record.changed_attributes.clear
|
|
142
|
+
|
|
143
|
+
record.girlfriends = [{'id' => '5', 'color' => 'blue'}]
|
|
144
|
+
assert_equal 1, record.girlfriends_was.size
|
|
145
|
+
assert_equal 'red', record.girlfriends_was.first.color
|
|
146
|
+
assert_equal '5', record.girlfriends_was.first.id
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
test 'validation' do
|
|
150
|
+
TestModel.new(girlfriends: [Girlfriend.new(color: 'red')]).tap do |record|
|
|
151
|
+
assert record.valid?
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
TestModel.new(girlfriends: [Girlfriend.new(color: 'green')]).tap do |record|
|
|
155
|
+
assert record.invalid?
|
|
156
|
+
assert_equal ["is not included in the list"], record.errors['girlfriends.color']
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
test 'dirty changed' do
|
|
161
|
+
girlfriends = [Girlfriend.new(color: 'red', id: '6')]
|
|
162
|
+
record = TestModel.create! girlfriends: girlfriends
|
|
163
|
+
|
|
164
|
+
record = TestModel.find record.id
|
|
165
|
+
record.girlfriends = girlfriends
|
|
166
|
+
|
|
167
|
+
refute record.changed?
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
test 'dirty restore_attributes' do
|
|
171
|
+
girlfriends = [Girlfriend.new(color: 'red', id: '6')]
|
|
172
|
+
record = TestModel.create! girlfriends: girlfriends
|
|
173
|
+
record = TestModel.find record.id
|
|
174
|
+
|
|
175
|
+
record.girlfriends = [{height: 6, id: '6'}]
|
|
176
|
+
assert_equal 6, record.girlfriends.first.height
|
|
177
|
+
assert_nil record.girlfriends.first.color
|
|
178
|
+
|
|
179
|
+
record.restore_attributes
|
|
180
|
+
|
|
181
|
+
assert_equal 'red', record.girlfriends.first.color
|
|
182
|
+
assert_nil record.girlfriends.first.height
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
test 'reload' do
|
|
186
|
+
record = TestModel.create! girlfriends: [Girlfriend.new(color: 'red')]
|
|
187
|
+
|
|
188
|
+
record = TestModel.find record.id
|
|
189
|
+
record.girlfriends = [Girlfriend.new(color: 'blue'), Girlfriend.new(color: 'red')]
|
|
190
|
+
|
|
191
|
+
record.reload
|
|
192
|
+
assert_equal 1, record.girlfriends.size
|
|
193
|
+
end
|
|
194
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class AxleAttributes::Serializations::SerializeOneTest < ActiveSupport::TestCase
|
|
4
|
+
class Mommy < Superstore::Base
|
|
5
|
+
include AxleAttributes::SerializedChild
|
|
6
|
+
|
|
7
|
+
has_attribute :eye_color, type: :string, index: true
|
|
8
|
+
has_attribute :age, type: :integer, index: false
|
|
9
|
+
|
|
10
|
+
validates :eye_color, inclusion: {in: ['red', 'blue']}
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class TestModel < Superstore::Base
|
|
14
|
+
include AxleAttributes::Model
|
|
15
|
+
|
|
16
|
+
serialize_one :mommy, class_name: 'AxleAttributes::Serializations::SerializeOneTest::Mommy'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
test 'attributes' do
|
|
20
|
+
refute_nil TestModel.attributes['mommy.eye_color']
|
|
21
|
+
refute_nil TestModel.attributes['mommy.age']
|
|
22
|
+
|
|
23
|
+
assert TestModel.new.respond_to?(:mommy)
|
|
24
|
+
assert TestModel.new.respond_to?(:mommy=)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'index mapping' do
|
|
28
|
+
mommy_mapping = TestModel.elastic_index.mapping[:properties]['mommy']
|
|
29
|
+
|
|
30
|
+
refute_nil mommy_mapping
|
|
31
|
+
assert_equal :object, mommy_mapping[:type]
|
|
32
|
+
assert_equal Mommy.attributes['eye_color'].index_mapping, mommy_mapping[:properties]['eye_color']
|
|
33
|
+
assert_nil mommy_mapping[:properties]['age']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
test 'serialization_reflections' do
|
|
37
|
+
refute_nil TestModel.serialization_reflections[:mommy]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test 'writer' do
|
|
41
|
+
mommy = Mommy.new(eye_color: 'blue')
|
|
42
|
+
record = TestModel.new
|
|
43
|
+
|
|
44
|
+
record.mommy = mommy
|
|
45
|
+
|
|
46
|
+
assert_equal mommy, record.mommy
|
|
47
|
+
assert_equal mommy.object_id, record.mommy.object_id
|
|
48
|
+
assert_equal 'blue', record[:mommy]['eye_color']
|
|
49
|
+
assert_kind_of String, record[:mommy]['created_at']
|
|
50
|
+
assert_equal record.child_serialization(:mommy), record.mommy.serialization_instance
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
test 'writer with hash' do
|
|
54
|
+
record = TestModel.new
|
|
55
|
+
|
|
56
|
+
record.mommy = {eye_color: 'blue'}
|
|
57
|
+
|
|
58
|
+
assert_equal 'blue', record.mommy.eye_color
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
test 'writer with hash and existing target' do
|
|
62
|
+
record = TestModel.new mommy: {id: 'abc', eye_color: 'red'}
|
|
63
|
+
|
|
64
|
+
record.mommy = {eye_color: 'blue'}
|
|
65
|
+
|
|
66
|
+
assert_equal 'blue', record.mommy.eye_color
|
|
67
|
+
assert_equal 'abc', record.mommy.id
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
test 'attributes_writer' do
|
|
71
|
+
record = TestModel.new mommy_attributes: {eye_color: 'blue'}
|
|
72
|
+
|
|
73
|
+
refute_nil record.mommy
|
|
74
|
+
assert_equal 'blue', record.mommy.eye_color
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
test 'attributes_writer delete' do
|
|
78
|
+
record = TestModel.new mommy: Mommy.new(eye_color: 'blue')
|
|
79
|
+
|
|
80
|
+
record.mommy_attributes = {'_destroy' => '1'}
|
|
81
|
+
|
|
82
|
+
assert_nil record.mommy
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
test 'reader' do
|
|
86
|
+
record = TestModel.new
|
|
87
|
+
record[:mommy] = {'id' => '5', 'eye_color' => 'red'}
|
|
88
|
+
|
|
89
|
+
refute_nil record.mommy
|
|
90
|
+
assert_equal 'red', record.mommy.eye_color
|
|
91
|
+
assert_equal record.child_serialization(:mommy), record.mommy.serialization_instance
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
test 'read when nil' do
|
|
95
|
+
record = TestModel.new
|
|
96
|
+
|
|
97
|
+
assert_nil record.mommy
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
test 'reader_was' do
|
|
101
|
+
record = TestModel.new
|
|
102
|
+
record.mommy = {'id' => '5', 'eye_color' => 'red'}
|
|
103
|
+
|
|
104
|
+
assert_nil record.mommy_was
|
|
105
|
+
|
|
106
|
+
record.changed_attributes.clear
|
|
107
|
+
|
|
108
|
+
record.mommy = {'id' => '5', 'eye_color' => 'blue'}
|
|
109
|
+
assert_equal 'red', record.mommy_was.eye_color
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
test 'validation' do
|
|
113
|
+
TestModel.new(mommy: Mommy.new(eye_color: 'red')).tap do |record|
|
|
114
|
+
assert record.valid?
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
TestModel.new(mommy: Mommy.new(color: 'green')).tap do |record|
|
|
118
|
+
assert record.invalid?
|
|
119
|
+
assert_equal ["is not included in the list"], record.errors['mommy.eye_color']
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class AxleAttributes::SerializationsTest < ActiveSupport::TestCase
|
|
4
|
+
class Pet < Superstore::Base
|
|
5
|
+
include AxleAttributes::SerializedChild
|
|
6
|
+
|
|
7
|
+
has_attribute :color, type: :string
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class Person < Business
|
|
11
|
+
serialize_many :pets, class_name: 'AxleAttributes::SerializationsTest::Pet'
|
|
12
|
+
serialize_one :favorite, class_name: 'AxleAttributes::SerializationsTest::Pet'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
test 'serialized_many' do
|
|
16
|
+
assert Person.serialized_many? 'pets'
|
|
17
|
+
assert Person.serialized_many? :pets
|
|
18
|
+
refute Person.serialized_many? :favorite
|
|
19
|
+
refute Person.serialized_many? :unknown
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test 'reload' do
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class AxleAttributes::SerializedChildTest < ActiveSupport::TestCase
|
|
4
|
+
class TestModel < Superstore::Base
|
|
5
|
+
include AxleAttributes::SerializedChild
|
|
6
|
+
|
|
7
|
+
has_attributes(
|
|
8
|
+
color: {type: :string, index: true, version: true},
|
|
9
|
+
price: {type: :integer, index: false, version: false}
|
|
10
|
+
)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_id
|
|
14
|
+
record = TestModel.new
|
|
15
|
+
|
|
16
|
+
refute_nil record.id
|
|
17
|
+
assert_equal :string, TestModel.attributes['id'].type
|
|
18
|
+
assert_equal true, TestModel.attributes['id'].indexed?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'equality' do
|
|
22
|
+
same_1 = TestModel.new id: '5'
|
|
23
|
+
same_2 = TestModel.new id: '5'
|
|
24
|
+
different = TestModel.new id: '6'
|
|
25
|
+
|
|
26
|
+
assert_equal same_1, same_2
|
|
27
|
+
refute_equal different, same_1
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test 'created_at' do
|
|
31
|
+
record = TestModel.new
|
|
32
|
+
|
|
33
|
+
assert_in_delta Time.now, record.created_at, 1
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
test 'attributes' do
|
|
37
|
+
refute_nil TestModel.new.attributes['id']
|
|
38
|
+
|
|
39
|
+
assert_equal({'color' => 'green'}, TestModel.new(color: 'green').attributes.except('id', 'created_at'))
|
|
40
|
+
assert_equal({'color' => 'green'}, TestModel.new(color: 'green', price: nil).attributes.except('id', 'created_at'))
|
|
41
|
+
assert_equal({'color' => 'green', 'price' => 25}, TestModel.new(color: 'green', price: 25).attributes.except('id', 'created_at'))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test 'attributes with unknown method' do
|
|
45
|
+
record = Class.new(TestModel) do
|
|
46
|
+
has_attribute :bad, type: false
|
|
47
|
+
end.new(bad: '1')
|
|
48
|
+
|
|
49
|
+
assert_equal({}, record.attributes.except('id', 'created_at'))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
test 'attributes with unknown attribute' do
|
|
53
|
+
record = Class.new(TestModel) do
|
|
54
|
+
def bad=(v)
|
|
55
|
+
@attributes['bad'] = v
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def bad
|
|
59
|
+
@attributes['bad']
|
|
60
|
+
end
|
|
61
|
+
end.new(bad: '1')
|
|
62
|
+
|
|
63
|
+
assert_equal({}, record.attributes.except('id', 'created_at'))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
test 'as_json' do
|
|
67
|
+
record = TestModel.new(color: 'green', price: 25)
|
|
68
|
+
|
|
69
|
+
assert_equal record.attributes, record.as_json
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
test 'versioned_attributes' do
|
|
73
|
+
record = TestModel.new(color: 'green', price: 25)
|
|
74
|
+
|
|
75
|
+
search = record.versioned_attributes
|
|
76
|
+
|
|
77
|
+
assert_equal 'green', search['color']
|
|
78
|
+
refute search.key?('created_at')
|
|
79
|
+
refute search.key?('price')
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
test 'as_search' do
|
|
83
|
+
record = TestModel.new(color: 'green', price: 25)
|
|
84
|
+
|
|
85
|
+
search = record.as_search
|
|
86
|
+
|
|
87
|
+
assert_equal 'green', search['color']
|
|
88
|
+
refute_nil search['created_at']
|
|
89
|
+
refute search.key?('price')
|
|
90
|
+
end
|
|
91
|
+
end
|