hydra_attribute 0.4.2 → 0.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/.gitignore +2 -1
  2. data/.travis.yml +6 -5
  3. data/CHANGELOG.md +6 -0
  4. data/Gemfile +1 -1
  5. data/README.md +3 -3
  6. data/Rakefile +2 -7
  7. data/gemfiles/activerecord-3.2.gemfile +5 -0
  8. data/hydra_attribute.gemspec +6 -7
  9. data/lib/hydra_attribute.rb +17 -18
  10. data/lib/hydra_attribute/active_record.rb +34 -13
  11. data/lib/hydra_attribute/active_record/association_preloader.rb +47 -28
  12. data/lib/hydra_attribute/active_record/attribute_methods.rb +29 -140
  13. data/lib/hydra_attribute/active_record/mass_assignment_security.rb +39 -0
  14. data/lib/hydra_attribute/active_record/migration.rb +4 -4
  15. data/lib/hydra_attribute/active_record/relation.rb +6 -7
  16. data/lib/hydra_attribute/active_record/relation/query_methods.rb +28 -18
  17. data/lib/hydra_attribute/hydra_attribute.rb +12 -49
  18. data/lib/hydra_attribute/hydra_attribute_set.rb +67 -0
  19. data/lib/hydra_attribute/hydra_entity.rb +110 -0
  20. data/lib/hydra_attribute/hydra_entity_attribute_association.rb +155 -0
  21. data/lib/hydra_attribute/hydra_set.rb +24 -26
  22. data/lib/hydra_attribute/hydra_value.rb +210 -0
  23. data/lib/hydra_attribute/identity_map.rb +18 -0
  24. data/lib/hydra_attribute/middleware/identity_map.rb +15 -0
  25. data/lib/hydra_attribute/migrator.rb +24 -21
  26. data/lib/hydra_attribute/model.rb +47 -0
  27. data/lib/hydra_attribute/model/cacheable.rb +207 -0
  28. data/lib/hydra_attribute/model/dirty.rb +39 -0
  29. data/lib/hydra_attribute/model/has_many_through.rb +168 -0
  30. data/lib/hydra_attribute/model/identity_map.rb +59 -0
  31. data/lib/hydra_attribute/model/mediator.rb +89 -0
  32. data/lib/hydra_attribute/model/notifiable.rb +23 -0
  33. data/lib/hydra_attribute/model/persistence.rb +424 -0
  34. data/lib/hydra_attribute/model/validations.rb +40 -0
  35. data/lib/hydra_attribute/version.rb +1 -1
  36. data/spec/environments/mysql.rb +23 -0
  37. data/spec/environments/postgresql.rb +23 -0
  38. data/spec/environments/sqlite.rb +12 -0
  39. data/spec/fixtures/category.rb +8 -0
  40. data/spec/fixtures/product.rb +8 -0
  41. data/spec/fixtures/product_black_list.rb +13 -0
  42. data/spec/fixtures/product_white_list.rb +13 -0
  43. data/spec/hydra_attribute/active_record/attribute_methods_spec.rb +23 -28
  44. data/spec/hydra_attribute/active_record/mass_assignment_security_spec.rb +41 -0
  45. data/spec/hydra_attribute/active_record_spec.rb +577 -0
  46. data/spec/hydra_attribute/hydra_attribute_set_spec.rb +651 -0
  47. data/spec/hydra_attribute/hydra_attribute_spec.rb +208 -10
  48. data/spec/hydra_attribute/hydra_entity_attribute_association_spec.rb +216 -0
  49. data/spec/hydra_attribute/hydra_entity_spec.rb +71 -0
  50. data/spec/hydra_attribute/hydra_set_spec.rb +51 -10
  51. data/spec/hydra_attribute/hydra_value_spec.rb +286 -0
  52. data/spec/hydra_attribute/identity_map_spec.rb +47 -0
  53. data/spec/hydra_attribute/migrator_spec.rb +411 -0
  54. data/spec/hydra_attribute/model/cacheable_spec.rb +106 -0
  55. data/spec/hydra_attribute/model/has_many_through_spec.rb +132 -0
  56. data/spec/hydra_attribute/model/identity_map_spec.rb +39 -0
  57. data/spec/hydra_attribute/model/mediator_spec.rb +62 -0
  58. data/spec/hydra_attribute/model/persistence_spec.rb +550 -0
  59. data/spec/hydra_attribute/model_spec.rb +39 -0
  60. data/spec/hydra_attribute_spec.rb +36 -0
  61. data/spec/spec_helper.rb +10 -42
  62. metadata +76 -100
  63. data/Appraisals +0 -7
  64. data/cucumber.yml +0 -1
  65. data/features/entity/create.feature +0 -145
  66. data/features/entity/destroy.feature +0 -111
  67. data/features/entity/new.feature +0 -121
  68. data/features/entity/update.feature +0 -147
  69. data/features/hydra_attribute/create.feature +0 -30
  70. data/features/hydra_attribute/destroy.feature +0 -26
  71. data/features/hydra_attribute/update.feature +0 -36
  72. data/features/hydra_set/destroy.feature +0 -31
  73. data/features/migrations/create_and_drop.feature +0 -165
  74. data/features/migrations/migrate_and_rollback.feature +0 -211
  75. data/features/relation/query_methods/group.feature +0 -42
  76. data/features/relation/query_methods/order.feature +0 -67
  77. data/features/relation/query_methods/reorder.feature +0 -29
  78. data/features/relation/query_methods/reverse_order.feature +0 -29
  79. data/features/relation/query_methods/select.feature +0 -50
  80. data/features/relation/query_methods/where.feature +0 -115
  81. data/features/step_definitions/connections.rb +0 -65
  82. data/features/step_definitions/model_steps.rb +0 -136
  83. data/features/step_definitions/query_methods.rb +0 -48
  84. data/features/step_definitions/record_steps.rb +0 -93
  85. data/features/support/env.rb +0 -38
  86. data/features/support/world.rb +0 -61
  87. data/lib/hydra_attribute/active_record/association.rb +0 -113
  88. data/lib/hydra_attribute/active_record/reflection.rb +0 -16
  89. data/lib/hydra_attribute/association_builder.rb +0 -69
  90. data/lib/hydra_attribute/builder.rb +0 -37
  91. data/lib/hydra_attribute/entity_callbacks.rb +0 -26
  92. data/lib/hydra_attribute/hydra_attribute_methods.rb +0 -226
  93. data/lib/hydra_attribute/hydra_methods.rb +0 -528
  94. data/lib/hydra_attribute/hydra_set_methods.rb +0 -95
  95. data/lib/hydra_attribute/hydra_value_methods.rb +0 -21
  96. data/lib/hydra_attribute/memoizable.rb +0 -37
  97. data/spec/hydra_attribute/active_record/relation/query_methods_spec.rb +0 -31
  98. data/spec/hydra_attribute/hydra_attribute_methods_spec.rb +0 -458
  99. data/spec/hydra_attribute/hydra_methods_spec.rb +0 -456
  100. data/spec/hydra_attribute/hydra_set_methods_spec.rb +0 -203
  101. data/spec/hydra_attribute/memoizable_spec.rb +0 -95
@@ -1,19 +1,217 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe HydraAttribute::HydraAttribute do
4
- describe '#hydra_sets=' do
5
- let!(:hydra_attribute) { Product.hydra_attributes.create(name: 'one', backend_type: 'string') }
4
+ describe '.all_by_entity_type' do
5
+ describe 'hydra_attributes table has several records' do
6
+ let!(:attr_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr1', 'string', '2012-12-12', '2012-12-12')]) }
7
+ let!(:attr_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr2', 'integer', '2012-12-12', '2012-12-12')]) }
8
+ let!(:attr_id3) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Category', 'attr3', 'string', '2012-12-12', '2012-12-12')]) }
6
9
 
7
- it 'should clear entity cache if assign hydra sets collection' do
8
- hydra_set = Product.hydra_sets.create(name: 'Default')
9
- Product.should_receive(:clear_hydra_method_cache!)
10
- hydra_attribute.hydra_sets = [hydra_set]
10
+ it 'should return hydra_attributes which have the following entity_type' do
11
+ HydraAttribute::HydraAttribute.all_by_entity_type('Product').map(&:name).should =~ %w[attr1 attr2]
12
+ HydraAttribute::HydraAttribute.all_by_entity_type('Category').map(&:name).should =~ %w[attr3]
13
+ end
14
+
15
+ it 'should not return hydra_attribute which was removed in runtime' do
16
+ HydraAttribute::HydraAttribute.find(attr_id1).destroy
17
+ HydraAttribute::HydraAttribute.all_by_entity_type('Product').map(&:name).should =~ %w[attr2]
18
+ HydraAttribute::HydraAttribute.all_by_entity_type('Category').map(&:name).should =~ %w[attr3]
19
+ end
20
+
21
+ it 'should not return hydra_attribute which entity_type was changed in runtime' do
22
+ hydra_attribute = HydraAttribute::HydraAttribute.find(attr_id1)
23
+ hydra_attribute.entity_type = 'Category'
24
+ hydra_attribute.save
25
+
26
+ HydraAttribute::HydraAttribute.all_by_entity_type('Product').map(&:name).should =~ %w[attr2]
27
+ HydraAttribute::HydraAttribute.all_by_entity_type('Category').map(&:name).should =~ %w[attr1 attr3]
28
+ end
29
+ end
30
+
31
+ describe 'hydra_attributes table is blank' do
32
+ it 'should return blank collection' do
33
+ HydraAttribute::HydraAttribute.all_by_entity_type('Product').should == []
34
+ end
35
+
36
+ it 'should return hydra_attribute which was created in runtime' do
37
+ HydraAttribute::HydraAttribute.all_by_entity_type('Product').should == []
38
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr1', backend_type: 'string')
39
+ HydraAttribute::HydraAttribute.all_by_entity_type('Product').should == [hydra_attribute]
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '.ids_by_entity_type' do
45
+ describe 'hydra_attributes table has several records' do
46
+ let!(:attr_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr1', 'string', '2012-12-12', '2012-12-12')]).to_i }
47
+ let!(:attr_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr2', 'integer', '2012-12-12', '2012-12-12')]).to_i }
48
+ let!(:attr_id3) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Category', 'attr3', 'string', '2012-12-12', '2012-12-12')]).to_i }
49
+
50
+ it 'should return IDs by entity_type' do
51
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Product').should =~ [attr_id1, attr_id2]
52
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Category').should =~ [attr_id3]
53
+ end
54
+
55
+ it 'should not return ID if model was removed in runtime' do
56
+ HydraAttribute::HydraAttribute.find(attr_id1).destroy
57
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Product').should =~ [attr_id2]
58
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Category').should =~ [attr_id3]
59
+ end
60
+
61
+ it 'should not return ID if entity_type was updated in runtime' do
62
+ hydra_attribute = HydraAttribute::HydraAttribute.find(attr_id1)
63
+ hydra_attribute.entity_type = 'Category'
64
+ hydra_attribute.save
65
+
66
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Product').should =~ [attr_id2]
67
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Category').should =~ [attr_id1, attr_id3]
68
+ end
69
+ end
70
+
71
+ describe 'hydra_attributes table is blank' do
72
+ it 'should return blank collection' do
73
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Product').should == []
74
+ end
75
+
76
+ it 'should return IDs which were created in runtime' do
77
+ a1 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr', backend_type: 'string')
78
+ a2 = HydraAttribute::HydraAttribute.create(entity_type: 'Category', name: 'attr', backend_type: 'string')
79
+
80
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Product').should == [a1.id]
81
+ HydraAttribute::HydraAttribute.ids_by_entity_type('Category').should == [a2.id]
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '.names_by_entity_type' do
87
+ describe 'hydra_attributes table has several records' do
88
+ let!(:attr_id1) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr1', 'string', '2012-12-12', '2012-12-12')]) }
89
+ let!(:attr_id2) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Product', 'attr2', 'integer', '2012-12-12', '2012-12-12')]) }
90
+ let!(:attr_id3) { ::ActiveRecord::Base.connection.insert(%q[INSERT INTO hydra_attributes(entity_type, name, backend_type, created_at, updated_at) VALUES('Category', 'attr3', 'string', '2012-12-12', '2012-12-12')]) }
91
+
92
+ it 'should return names by entity_type' do
93
+ HydraAttribute::HydraAttribute.names_by_entity_type('Product').should =~ %w[attr1 attr2]
94
+ HydraAttribute::HydraAttribute.names_by_entity_type('Category').should =~ %w[attr3]
95
+ end
96
+
97
+ it 'should not return name for model which was removed in runtime' do
98
+ HydraAttribute::HydraAttribute.find(attr_id1).destroy
99
+ HydraAttribute::HydraAttribute.names_by_entity_type('Product').should =~ %w[attr2]
100
+ HydraAttribute::HydraAttribute.names_by_entity_type('Category').should =~ %w[attr3]
101
+ end
102
+
103
+ it 'should not return name for model which entity_type was changed in runtime' do
104
+ hydra_attribute = HydraAttribute::HydraAttribute.find(attr_id1)
105
+ hydra_attribute.entity_type = 'Category'
106
+ hydra_attribute.save
107
+
108
+ HydraAttribute::HydraAttribute.names_by_entity_type('Product').should =~ %w[attr2]
109
+ HydraAttribute::HydraAttribute.names_by_entity_type('Category').should =~ %w[attr1 attr3]
110
+ end
111
+
112
+ it 'should return updated attribute name' do
113
+ hydra_attribute = HydraAttribute::HydraAttribute.find(attr_id1)
114
+ hydra_attribute.name = 'new_attr'
115
+ hydra_attribute.save
116
+
117
+ HydraAttribute::HydraAttribute.names_by_entity_type('Product').should =~ %w[new_attr attr2]
118
+ HydraAttribute::HydraAttribute.names_by_entity_type('Category').should =~ %w[attr3]
119
+ end
11
120
  end
12
121
 
13
- it 'should clear entity cache if assign blank collection' do
14
- hydra_attribute.hydra_sets.create(name: 'Default')
15
- Product.should_receive(:clear_hydra_method_cache!)
16
- hydra_attribute.hydra_sets = []
122
+ describe 'hydra_attributes table is blank' do
123
+ it 'should return blank collection' do
124
+ HydraAttribute::HydraAttribute.names_by_entity_type('Product').should == []
125
+ end
126
+
127
+ it 'should return names which where created in runtime' do
128
+ HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr1', backend_type: 'string')
129
+ HydraAttribute::HydraAttribute.create(entity_type: 'Category', name: 'attr2', backend_type: 'string')
130
+
131
+ HydraAttribute::HydraAttribute.names_by_entity_type('Product').should == %w[attr1]
132
+ HydraAttribute::HydraAttribute.names_by_entity_type('Category').should == %w[attr2]
133
+ end
17
134
  end
18
135
  end
136
+
137
+ describe '#hydra_sets' do
138
+ it 'should return blank array if model has not ID' do
139
+ HydraAttribute::HydraAttribute.new.should have(0).hydra_sets
140
+ end
141
+
142
+ it 'should return blank array if model has not any hydra_sets' do
143
+ hydra_attribute = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string')
144
+ hydra_attribute.should have(0).hydra_sets
145
+ end
146
+
147
+ it 'should return array of hydra_sets for the current hydra_attribute' do
148
+ hydra_attribute1 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr1', backend_type: 'string')
149
+ hydra_attribute2 = HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'attr2', backend_type: 'string')
150
+ hydra_set1 = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'default1')
151
+ hydra_set2 = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'default2')
152
+ hydra_set3 = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'default3')
153
+
154
+
155
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set1.id, hydra_attribute_id: hydra_attribute1.id)
156
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set2.id, hydra_attribute_id: hydra_attribute1.id)
157
+ HydraAttribute::HydraAttributeSet.create(hydra_set_id: hydra_set3.id, hydra_attribute_id: hydra_attribute2.id)
158
+
159
+ hydra_attribute1.should have(2).hydra_sets
160
+ hydra_attribute2.should have(1).hydra_sets
161
+
162
+ hydra_attribute1.hydra_sets.should include(hydra_set1)
163
+ hydra_attribute1.hydra_sets.should include(hydra_set2)
164
+ hydra_attribute2.hydra_sets.should include(hydra_set3)
165
+ end
166
+ end
167
+
168
+ describe 'validations' do
169
+ it 'should require entity_type' do
170
+ hydra_attribute = HydraAttribute::HydraAttribute.new
171
+ hydra_attribute.valid?
172
+ hydra_attribute.errors.should include(:entity_type)
173
+
174
+ hydra_attribute.entity_type = 'Product'
175
+ hydra_attribute.valid?
176
+ hydra_attribute.errors.should_not include(:entity_type)
177
+ end
178
+
179
+ it 'should require name' do
180
+ hydra_attribute = HydraAttribute::HydraAttribute.new
181
+ hydra_attribute.valid?
182
+ hydra_attribute.errors.should include(:name)
183
+
184
+ hydra_attribute.name = 'price'
185
+ hydra_attribute.valid?
186
+ hydra_attribute.errors.should_not include(:price)
187
+ end
188
+
189
+ it 'should have a unique entity_type and name' do
190
+ HydraAttribute::HydraAttribute.create(name: 'price', entity_type: 'Product', backend_type: 'float').should be_persisted
191
+ HydraAttribute::HydraAttribute.create(name: 'price', entity_type: 'Product', backend_type: 'float').should_not be_persisted
192
+ end
193
+
194
+ it 'should require backend_type' do
195
+ hydra_attribute = HydraAttribute::HydraAttribute.new
196
+ hydra_attribute.valid?
197
+ hydra_attribute.errors.should include(:backend_type)
198
+
199
+ hydra_attribute.backend_type = 'integer'
200
+ hydra_attribute.valid?
201
+ hydra_attribute.errors.should_not include(:backend_type)
202
+ end
203
+
204
+ it 'should have a valid backend_type' do
205
+ hydra_attribute = HydraAttribute::HydraAttribute.new(backend_type: 'fake')
206
+ hydra_attribute.valid?
207
+ hydra_attribute.errors.should include(:backend_type)
208
+
209
+ HydraAttribute::SUPPORTED_BACKEND_TYPES.each do |backend_type|
210
+ hydra_attribute.backend_type = backend_type
211
+ hydra_attribute.valid?
212
+ hydra_attribute.errors.should_not include(:backend_type)
213
+ end
214
+ end
215
+ end
216
+
19
217
  end
@@ -0,0 +1,216 @@
1
+ require 'spec_helper'
2
+
3
+ describe HydraAttribute::HydraEntityAttributeAssociation do
4
+ describe '#has_proxy_method?' do
5
+ let(:association) { HydraAttribute::HydraEntityAttributeAssociation.new(Product.new) }
6
+ let!(:hydra_attribute) { HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'code', backend_type: 'string') }
7
+
8
+ it 'should return true if hydra_attribute has appropriate attribute for entity type' do
9
+ [:code, :code=, :code_was, :code_before_type_cast].each do |method|
10
+ association.should have_proxy_method(method)
11
+ end
12
+ end
13
+
14
+ it 'should return false if hydra_attribute has not appropriate attribute for entity type' do
15
+ HydraAttribute::HydraAttribute.create(entity_type: 'Category', name: 'title', backend_type: 'string')
16
+ [:title, :title=, :title_was, :title_before_type_cast].each do |method|
17
+ association.should_not have_proxy_method(method)
18
+ end
19
+ end
20
+
21
+ it 'should correct resolve if new attribute was created in runtime' do
22
+ [:title, :title=, :title_was, :title_before_type_cast].each do |method|
23
+ association.should_not have_proxy_method(method)
24
+ end
25
+
26
+ HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string')
27
+
28
+ [:title, :title=, :title_was, :title_before_type_cast].each do |method|
29
+ association.should have_proxy_method(method)
30
+ end
31
+ end
32
+
33
+ it 'should correct resolve if attribute name was updated' do
34
+ [:code, :code=, :code_was, :code_before_type_cast].each do |method|
35
+ association.should have_proxy_method(method)
36
+ end
37
+
38
+ hydra_attribute.name = 'title'
39
+ hydra_attribute.save
40
+
41
+ [:code, :code=, :code_was, :code_before_type_cast].each do |method|
42
+ association.should_not have_proxy_method(method)
43
+ end
44
+
45
+ [:title, :title=, :title_was, :title_before_type_cast].each do |method|
46
+ association.should have_proxy_method(method)
47
+ end
48
+ end
49
+
50
+ it 'should correct resolve if attribute entity type was updated' do
51
+ hydra_attribute.entity_type = 'Category'
52
+ hydra_attribute.save
53
+
54
+ category_assoc = HydraAttribute::HydraEntityAttributeAssociation.new(Category.new)
55
+ [:code, :code=, :code_was, :code_before_type_cast].each do |method|
56
+ association.should_not have_proxy_method(method)
57
+ category_assoc.should have_proxy_method(method)
58
+ end
59
+ end
60
+
61
+ it 'should correct resolve if hydra attribute was deleted' do
62
+ hydra_attribute.destroy
63
+ [:code, :code=, :code_was, :code_before_type_cast].each do |method|
64
+ association.should_not have_proxy_method(method)
65
+ end
66
+ end
67
+ end
68
+
69
+ describe '#delegate' do
70
+ let!(:hydra_attribute1) { HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'color', backend_type: 'string', default_value: 'green') }
71
+ let!(:hydra_attribute2) { HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string') }
72
+ let(:association) { HydraAttribute::HydraEntityAttributeAssociation.new(Product.new) }
73
+
74
+ it 'should correct proxy method to hydra value' do
75
+ association.delegate(:color).should == 'green'
76
+ association.delegate(:title).should be_nil
77
+
78
+ association.delegate(:color?).should be_true
79
+ association.delegate(:title?).should be_false
80
+
81
+ association.delegate(:color=, 'black')
82
+ association.delegate(:title=, 'qwerty')
83
+
84
+ association.delegate(:color).should == 'black'
85
+ association.delegate(:title).should == 'qwerty'
86
+
87
+ association.delegate(:color?).should be_true
88
+ association.delegate(:title?).should be_true
89
+ end
90
+
91
+ it 'should raise an error if attribute name is unknown' do
92
+ lambda do
93
+ association.delegate(:price)
94
+ end.should raise_error(HydraAttribute::HydraEntityAttributeAssociation::WrongProxyMethodError, 'Unknown :price method')
95
+ end
96
+
97
+ it 'should raise an error if hydra set does not have attribute' do
98
+ hydra_set = HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'default')
99
+ association.entity.hydra_set_id = hydra_set.id
100
+
101
+ lambda do
102
+ association.delegate(:color)
103
+ end.should raise_error(HydraAttribute::HydraSet::MissingAttributeInHydraSetError, "Attribute ID #{hydra_attribute1.id} is missed in Set ID #{hydra_set.id}")
104
+ end
105
+
106
+ it 'should delegate to method if it was created in runtime' do
107
+ HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'one', backend_type: 'string', default_value: 'one')
108
+ association.delegate(:one).should == 'one'
109
+ end
110
+
111
+ it 'should delegate to method if attribute name was updated in runtime' do
112
+ hydra_attribute1.name = 'color1'
113
+ hydra_attribute1.save
114
+
115
+ lambda { association.delegate(:color) }.should raise_error(HydraAttribute::HydraEntityAttributeAssociation::WrongProxyMethodError)
116
+ lambda { association.delegate(:color1) }.should_not raise_error
117
+ end
118
+
119
+ it 'should delegate to method if attribute entity type was updated in runtime' do
120
+ hydra_attribute1.entity_type = 'Category'
121
+ hydra_attribute1.save
122
+
123
+ lambda { association.delegate(:color) }.should raise_error(HydraAttribute::HydraEntityAttributeAssociation::WrongProxyMethodError)
124
+ lambda { HydraAttribute::HydraEntityAttributeAssociation.new(Category.new) }.should_not raise_error
125
+ end
126
+
127
+ it 'should not delegate to method if attribute was deleted in runtime' do
128
+ hydra_attribute1.destroy
129
+ lambda { association.delegate(:color) }.should raise_error(HydraAttribute::HydraEntityAttributeAssociation::WrongProxyMethodError)
130
+ end
131
+ end
132
+
133
+ describe '#hydra_attributes' do
134
+ let(:entity) { Product.new }
135
+ let!(:attr1) { HydraAttribute::HydraAttribute.create(entity_type: entity.class.name, name: 'color', backend_type: 'string', default_value: 'red') }
136
+ let!(:attr2) { HydraAttribute::HydraAttribute.create(entity_type: entity.class.name, name: 'title', backend_type: 'string') }
137
+ let!(:attr3) { HydraAttribute::HydraAttribute.create(entity_type: entity.class.name, name: 'total', backend_type: 'decimal') }
138
+
139
+ let(:hydra_attribute_association) { entity.hydra_attribute_association }
140
+
141
+ describe 'entity does not have a hydra set' do
142
+ describe 'entity is persisted' do
143
+ before do
144
+ entity.color = 'green'
145
+ entity.total = 5
146
+ entity.save
147
+ end
148
+
149
+ it 'should return all attributes with their values' do
150
+ hydra_attribute_association.hydra_attributes.should == {'color' => 'green', 'title' => nil, 'total' => 5}
151
+ end
152
+ end
153
+
154
+ describe 'entity is not persisted' do
155
+ it 'should return all attributes with their values' do
156
+ hydra_attribute_association.hydra_attributes.should == {'color' => 'red', 'title' => nil, 'total' => nil}
157
+ end
158
+ end
159
+ end
160
+
161
+ describe 'entity has a hydra set' do
162
+ let(:hydra_set) { HydraAttribute::HydraSet.create(entity_type: entity.class.name, name: 'default') }
163
+
164
+ before do
165
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: attr1.id, hydra_set_id: hydra_set.id)
166
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: attr3.id, hydra_set_id: hydra_set.id)
167
+ entity.hydra_set_id = hydra_set.id
168
+ end
169
+
170
+ it 'should return only attributes which are in the hydra set' do
171
+ hydra_attribute_association.hydra_attributes.should == {'color' => 'red', 'total' => nil}
172
+ end
173
+ end
174
+ end
175
+
176
+ describe '#hydra_attributes_before_type_cast' do
177
+ let(:entity) { Product.new }
178
+ let!(:attr1) { HydraAttribute::HydraAttribute.create(entity_type: entity.class.name, name: 'purchase_at', backend_type: 'datetime', default_value: '2013-12-04 00:00') }
179
+ let!(:attr2) { HydraAttribute::HydraAttribute.create(entity_type: entity.class.name, name: 'grand_total', backend_type: 'decimal') }
180
+
181
+ let(:hydra_attribute_association) { entity.hydra_attribute_association }
182
+
183
+ describe 'entity does not have a hydra set' do
184
+ describe 'entity is persisted' do
185
+ before do
186
+ entity.grand_total = '123456.1234'
187
+ entity.save
188
+ end
189
+
190
+ it 'should return all attributes with their values' do
191
+ hydra_attribute_association.hydra_attributes_before_type_cast.should == {'purchase_at' => '2013-12-04 00:00', 'grand_total' => '123456.1234'}
192
+ end
193
+ end
194
+
195
+ describe 'entity is not persisted' do
196
+ it 'should return all attributes with their values' do
197
+ hydra_attribute_association.hydra_attributes_before_type_cast.should == {'purchase_at' => '2013-12-04 00:00', 'grand_total' => nil}
198
+ end
199
+ end
200
+ end
201
+
202
+ describe 'entity has a hydra set' do
203
+ let(:hydra_set) { HydraAttribute::HydraSet.create(entity_type: entity.class.name, name: 'default') }
204
+
205
+ before do
206
+ HydraAttribute::HydraAttributeSet.create(hydra_attribute_id: attr1.id, hydra_set_id: hydra_set.id)
207
+ entity.hydra_set_id = hydra_set.id
208
+ entity.save
209
+ end
210
+
211
+ it 'should return only attributes which are in the hydra set' do
212
+ entity.hydra_attribute_association.hydra_attributes_before_type_cast.should == {'purchase_at' => '2013-12-04 00:00'}
213
+ end
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe HydraAttribute::HydraEntity do
4
+ describe '.hydra_attributes' do
5
+ it 'should return all hydra attributes for the model' do
6
+ Product.should have(0).hydra_attributes
7
+ Category.should have(0).hydra_attributes
8
+
9
+ HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'color', backend_type: 'string')
10
+ HydraAttribute::HydraAttribute.create(entity_type: 'Product', name: 'title', backend_type: 'string')
11
+ HydraAttribute::HydraAttribute.create(entity_type: 'Category', name: 'price', backend_type: 'decimal')
12
+
13
+ Product.should have(2).hydra_attributes
14
+ Category.should have(1).hydra_attributes
15
+
16
+ Product.hydra_attributes.map(&:name).should =~ %w[color title]
17
+ Category.hydra_attributes.map(&:name).should =~ %w[price]
18
+ end
19
+
20
+ it 'should have a helper method for creating a hydra attribute for this model' do
21
+ Product.hydra_attributes.create(name: 'height', backend_type: 'string')
22
+ Product.hydra_attributes.create(name: 'weight', backend_type: 'string')
23
+ Category.hydra_attributes.create(name: 'total', backend_type: 'decimal')
24
+
25
+ Product.hydra_attributes.map(&:name).should =~ %w[height weight]
26
+ Category.hydra_attributes.map(&:name).should =~ %w[total]
27
+ end
28
+ end
29
+
30
+ describe '.hydra_sets' do
31
+ it 'should return all hydra sets for the model' do
32
+ Product.should have(0).hydra_sets
33
+ Category.should have(0).hydra_sets
34
+
35
+ HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'one')
36
+ HydraAttribute::HydraSet.create(entity_type: 'Product', name: 'two')
37
+ HydraAttribute::HydraSet.create(entity_type: 'Category', name: 'three')
38
+
39
+ Product.should have(2).hydra_sets
40
+ Category.should have(1).hydra_sets
41
+
42
+ Product.hydra_sets.map(&:name).should =~ %w[one two]
43
+ Category.hydra_sets.map(&:name).should =~ %w[three]
44
+ end
45
+
46
+ it 'should have a helper method for creating a hydra set for this model' do
47
+ Product.hydra_sets.create(name: 'default1')
48
+ Product.hydra_sets.create(name: 'default2')
49
+ Category.hydra_sets.create(name: 'default3')
50
+
51
+ Product.hydra_sets.map(&:name).should =~ %w[default1 default2]
52
+ Category.hydra_sets.map(&:name).should =~ %w[default3]
53
+ end
54
+ end
55
+
56
+ describe '#hydra_attributes' do
57
+ it 'should delegate the method to the hydra_attribute_association object' do
58
+ product = Product.new
59
+ product.hydra_attribute_association.should_receive(:hydra_attributes).once.and_return(name: :value)
60
+ product.hydra_attributes.should == {name: :value}
61
+ end
62
+ end
63
+
64
+ describe '#hydra_attributes_before_type_cast' do
65
+ it 'should delegate the method to the hydra_attribute_association object' do
66
+ product = Product.new
67
+ product.hydra_attribute_association.should_receive(:hydra_attributes_before_type_cast).once.and_return(name: :value)
68
+ product.hydra_attributes_before_type_cast.should == {name: :value}
69
+ end
70
+ end
71
+ end