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,95 +0,0 @@
1
- module HydraAttribute
2
- # This error is raised when called method for attribute which doesn't exist in current hydra set
3
- #
4
- # @example
5
- # Product.hydra_attributes.create(name: 'price', backend_type: 'float')
6
- # Product.hydra_attributes.create(name: 'title', backend_type: 'string')
7
- #
8
- # hydra_set = Product.hydra_sets.create(name: 'Default')
9
- # hydra_set.hydra_attributes = [Product.hydra_attribute('title')]
10
- #
11
- # product = Product.new(hydra_set_id: hydra_set.id)
12
- # product.title = 'Toy' # ok
13
- # product.price = 2.50 # raise HydraAttribute::MissingAttributeInHydraSetError
14
- class MissingAttributeInHydraSetError < NoMethodError
15
- end
16
-
17
- # @see HydraAttribute::HydraSetMethods::ClassMethods ClassMethods for documentation
18
- module HydraSetMethods
19
- extend ActiveSupport::Concern
20
-
21
- module ClassMethods
22
- extend Memoizable
23
-
24
- # Returns attribute sets for current entity.
25
- #
26
- # @note This method is cacheable, therefore just one request to database is used
27
- # @example
28
- # Product.hydra_sets # [<HydraAttribute::HydraSet>, ...]
29
- # Product.hydra_sets.create(name: 'Default') # create attribute set
30
- # Product.hydra_sets.each(&:destroy) # remove all attribute sets
31
- # @return [ActiveRecord::Relation] contains preloaded attribute sets
32
- def hydra_sets
33
- HydraSet.where(entity_type: base_class.model_name)
34
- end
35
- hydra_memoize :hydra_sets
36
-
37
- # Finds attribute set by name or id
38
- #
39
- # @note This method is cacheable, therefore it's better to use it instead of manually searching.
40
- # @example
41
- # Product.hydra_set(10) # or
42
- # Product.hydra_set('Default')
43
- # @param identifier [Fixnum, String] id or name of attribute set
44
- # @return [HydraAttribute::HydraSet, NilClass] attribute set model or +nil+
45
- def hydra_set(identifier)
46
- hydra_sets.find do |hydra_set|
47
- hydra_set.id == identifier || hydra_set.name == identifier
48
- end
49
- end
50
- hydra_memoize :hydra_set
51
-
52
- # @!method hydra_set_ids
53
- # Returns all attribute set ids
54
- #
55
- # @note This method is cacheable.
56
- # @example
57
- # Product.hydra_sets.create(name: 'First') # 1
58
- # Product.hydra_sets.create(name: 'Second') # 2
59
- # Product.hydra_set_ids # [1, 2]
60
- # @return [Array<Fixnum>] contains attribute set ids
61
-
62
- # @!method hydra_set_names
63
- # Returns all attribute set names
64
- #
65
- # @note This method is cacheable.
66
- # @example
67
- # Product.hydra_sets.create(name: 'First') # 1
68
- # Product.hydra_sets.create(name: 'Second') # 2
69
- # Product.hydra_set_names # ["First", "Second"]
70
- # @return [Array<Fixnum>] contains attribute set names
71
- %w(id name).each do |prefix|
72
- module_eval <<-EOS, __FILE__, __LINE__ + 1
73
- def hydra_set_#{prefix}s
74
- hydra_sets.map(&:#{prefix})
75
- end
76
- hydra_memoize :hydra_set_#{prefix}s
77
- EOS
78
- end
79
-
80
- # Clear cache for the following methods:
81
- # * hydra_sets
82
- # * hydra_set(identifier)
83
- # * hydra_set_ids
84
- # * hydra_set_names
85
- #
86
- # @note This method should not be called manually. It used for hydra_attribute gem engine.
87
- # @return [NilClass]
88
- def clear_hydra_set_cache!
89
- [:@hydra_sets, :@hydra_set, :@hydra_set_ids, :@hydra_set_names].each do |variable|
90
- remove_instance_variable(variable) if instance_variable_defined?(variable)
91
- end
92
- end
93
- end
94
- end
95
- end
@@ -1,21 +0,0 @@
1
- module HydraAttribute
2
- module HydraValueMethods
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- # Clear cache
7
- #
8
- # @return [NilClass]
9
- def clear_hydra_value_cache!
10
- end
11
- end
12
-
13
- # Returns the association instance for the given backend type attribute name
14
- #
15
- # @param backend_type [String] one of the HydraAttribute::SUPPORTED_BACKEND_TYPES values
16
- # @return [ActiveRecord::Associations::Association]
17
- def hydra_value_association(backend_type)
18
- association(::HydraAttribute::AssociationBuilder.association_name(backend_type))
19
- end
20
- end
21
- end
@@ -1,37 +0,0 @@
1
- module HydraAttribute
2
- module Memoizable
3
- def hydra_memoize(*methods)
4
- methods.each do |method_name|
5
- bound_method = instance_method(method_name)
6
- alias_method "unmemoized_#{method_name}", method_name
7
-
8
- if bound_method.arity.abs == 0
9
- module_eval <<-EOS, __FILE__, __LINE__ + 1
10
- def #{method_name}
11
- @#{method_name} = unmemoized_#{method_name} unless instance_variable_defined?(:@#{method_name})
12
- @#{method_name}
13
- end
14
- EOS
15
- else
16
- args = 1.upto(bound_method.arity.abs).map { |i| "a#{i}" }
17
- keys = 1.upto(bound_method.arity.abs).map { |i| "k#{i}" }
18
-
19
- call = args.inject("@#{method_name}") do |hash_call, arg|
20
- "#{hash_call}[#{arg}]"
21
- end
22
-
23
- hash = keys.reverse.inject("unmemoized_#{method_name}(#{keys.join(', ')})") do |code, key|
24
- "Hash.new { |hash, #{key}| hash[#{key}] = #{code} }"
25
- end
26
-
27
- module_eval <<-EOS, __FILE__, __LINE__ + 1
28
- def #{method_name}(#{args.join(', ')}) # def method(a1)
29
- @#{method_name} ||= #{hash} # @method ||= Hash.new { |hash, key1| hash[key1] = unmemoized_method(key1) }
30
- #{call} # @method[a1]
31
- end # end
32
- EOS
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe HydraAttribute::ActiveRecord::Relation::QueryMethods do
4
- describe '#build_arel' do
5
- describe 'necessary columns for hydra attribute' do
6
- let(:id_column) { Product.quoted_table_name + '.' + Product.quoted_primary_key }
7
- let(:hydra_set_id_column) { Product.quoted_table_name + '.' + Product.quoted_primary_key }
8
-
9
- it 'should add "id" and "hydra_set_id" columns to query if they are omitted' do
10
- arel = Product.select(:name).build_arel
11
- arel.to_sql.should match(id_column)
12
- arel.to_sql.should match(hydra_set_id_column)
13
- end
14
-
15
- it 'should not add "id" and "hydra_set_id" columns to default query' do
16
- arel = Product.scoped.build_arel
17
- arel.to_sql.should_not match(id_column)
18
- arel.to_sql.should_not match(hydra_set_id_column)
19
- end
20
-
21
- it 'should not add "id" and "hydra_set_id" columns to query which performs calculation' do
22
- scope = Product.select(:name)
23
- scope.hydra_attribute_performs_calculation = true
24
-
25
- arel = scope.build_arel
26
- arel.to_sql.should_not match(id_column)
27
- arel.to_sql.should_not match(hydra_set_id_column)
28
- end
29
- end
30
- end
31
- end
@@ -1,458 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe HydraAttribute::HydraAttributeMethods do
4
- describe '.hydra_attributes' do
5
- it 'should return blank array if there are not any hydra attributes for entity' do
6
- Product.hydra_attributes.should be_blank
7
- end
8
-
9
- it 'should return all hydra attributes for entity' do
10
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
11
- two = Product.hydra_attributes.create(name: 'two', backend_type: 'string')
12
-
13
- Product.should have(2).hydra_attributes
14
- Product.hydra_attributes.first.should == one
15
- Product.hydra_attributes.last.should == two
16
- end
17
-
18
- it 'should cache result' do
19
- # hydra_attributes method is called for generation hydra methods
20
- # so this method is already cached and this cache should be removed manually
21
- Product.send(:remove_instance_variable, :@hydra_attributes) if Product.instance_variable_defined?(:@hydra_attributes)
22
-
23
- Product.should_receive(:unmemoized_hydra_attributes).once
24
- 2.times { Product.hydra_attributes }
25
- end
26
-
27
- it 'should reset method cache after creating hydra attribute' do
28
- hydra_attributes = Product.hydra_attributes
29
- Product.should_receive(:unmemoized_hydra_attributes).once
30
-
31
- hydra_attributes.create(name: 'one', backend_type: 'string')
32
- 2.times { Product.hydra_attributes }
33
- end
34
-
35
- it 'should reset method cache after updating hydra attribute' do
36
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
37
- Product.should_receive(:unmemoized_hydra_attributes).once
38
-
39
- hydra_attribute.update_attributes(name: 'two')
40
- 2.times { Product.hydra_attributes }
41
- end
42
-
43
- it 'should reset method cache after removing hydra attribute' do
44
- hydra_attribute = Product.hydra_attributes.create(name: 'once', backend_type: 'string')
45
- Product.should_receive(:unmemoized_hydra_attributes).once
46
-
47
- hydra_attribute.destroy
48
- 2.times { Product.hydra_attributes }
49
- end
50
- end
51
-
52
- describe '.hydra_attribute' do
53
- it 'should return hydra attribute by id' do
54
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
55
- two = Product.hydra_attributes.create(name: 'two', backend_type: 'string')
56
-
57
- Product.hydra_attribute(one.id).should == one
58
- Product.hydra_attribute(two.id).should == two
59
- end
60
-
61
- it 'should return hydra attribute by name' do
62
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
63
- two = Product.hydra_attributes.create(name: 'two', backend_type: 'string')
64
-
65
- Product.hydra_attribute(one.name).should == one
66
- Product.hydra_attribute(two.name).should == two
67
- end
68
-
69
- it 'should cache result' do
70
- Product.should_receive(:unmemoized_hydra_attribute).once
71
- 2.times { Product.hydra_attribute('name') }
72
- end
73
-
74
- it 'should reset method cache after creating hydra attribute' do
75
- hydra_attributes = Product.hydra_attributes
76
- Product.should_receive(:unmemoized_hydra_attribute).twice
77
-
78
- Product.hydra_attribute('one')
79
- hydra_attributes.create(name: 'one', backend_type: 'string')
80
- Product.hydra_attribute('one')
81
- end
82
-
83
- it 'should reset method cache after updating hydra attribute' do
84
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
85
- Product.should_receive(:unmemoized_hydra_attribute).twice
86
-
87
- Product.hydra_attribute('one')
88
- hydra_attribute.update_attributes(name: 'two')
89
- Product.hydra_attribute('one')
90
- end
91
-
92
- it 'should reset method cache after removing hydra attribute' do
93
- hydra_attribute = Product.hydra_attributes.create(name: 'once', backend_type: 'string')
94
- Product.should_receive(:unmemoized_hydra_attribute).twice
95
-
96
- Product.hydra_attribute('one')
97
- hydra_attribute.destroy
98
- Product.hydra_attribute('one')
99
- end
100
- end
101
-
102
- describe '.hydra_attribute_backend_types' do
103
- it 'should return all backend types' do
104
- Product.hydra_attributes.create(name: 'one', backend_type: 'string')
105
- Product.hydra_attributes.create(name: 'two', backend_type: 'string')
106
- Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
107
-
108
- Product.hydra_attribute_backend_types.should =~ %w(string integer)
109
- end
110
-
111
- it 'should cache result' do
112
- Product.should_receive(:unmemoized_hydra_attribute_backend_types).once
113
- 2.times { Product.hydra_attribute_backend_types }
114
- end
115
-
116
- it 'should reset method cache after creating hydra attribute' do
117
- hydra_attributes = Product.hydra_attributes
118
- Product.should_receive(:unmemoized_hydra_attribute_backend_types).twice
119
-
120
- Product.hydra_attribute_backend_types
121
- hydra_attributes.create(name: 'one', backend_type: 'string')
122
- Product.hydra_attribute_backend_types
123
- end
124
-
125
- it 'should reset method cache after updating hydra attribute' do
126
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
127
- Product.should_receive(:unmemoized_hydra_attribute_backend_types).twice
128
-
129
- Product.hydra_attribute_backend_types
130
- hydra_attribute.update_attributes(name: 'two')
131
- Product.hydra_attribute_backend_types
132
- end
133
-
134
- it 'should reset method cache after removing hydra attribute' do
135
- hydra_attribute = Product.hydra_attributes.create(name: 'once', backend_type: 'string')
136
- Product.should_receive(:unmemoized_hydra_attribute_backend_types).twice
137
-
138
- Product.hydra_attribute_backend_types
139
- hydra_attribute.destroy
140
- Product.hydra_attribute_backend_types
141
- end
142
- end
143
-
144
- describe '.hydra_attribute_ids' do
145
- it 'should return all ids' do
146
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
147
- two = Product.hydra_attributes.create(name: 'two', backend_type: 'string')
148
- three = Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
149
-
150
- Product.hydra_attribute_ids.should =~ [one.id, two.id, three.id]
151
- end
152
-
153
- it 'should cache result' do
154
- Product.should_receive(:unmemoized_hydra_attribute_ids).once
155
- 2.times { Product.hydra_attribute_ids }
156
- end
157
-
158
- it 'should reset method cache after creating hydra attribute' do
159
- hydra_attributes = Product.hydra_attributes
160
- Product.should_receive(:unmemoized_hydra_attribute_ids).twice
161
-
162
- Product.hydra_attribute_ids
163
- hydra_attributes.create(name: 'one', backend_type: 'string')
164
- Product.hydra_attribute_ids
165
- end
166
-
167
- it 'should reset method cache after updating hydra attribute' do
168
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
169
- Product.should_receive(:unmemoized_hydra_attribute_ids).twice
170
-
171
- Product.hydra_attribute_ids
172
- hydra_attribute.update_attributes(name: 'two')
173
- Product.hydra_attribute_ids
174
- end
175
-
176
- it 'should reset method cache after removing hydra attribute' do
177
- hydra_attribute = Product.hydra_attributes.create(name: 'once', backend_type: 'string')
178
- Product.should_receive(:unmemoized_hydra_attribute_ids).twice
179
-
180
- Product.hydra_attribute_ids
181
- hydra_attribute.destroy
182
- Product.hydra_attribute_ids
183
- end
184
- end
185
-
186
- describe '.hydra_attribute_names' do
187
- it 'should return names' do
188
- Product.hydra_attributes.create(name: 'one', backend_type: 'string')
189
- Product.hydra_attributes.create(name: 'two', backend_type: 'string')
190
- Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
191
-
192
- Product.hydra_attribute_names.should =~ %w(one two three)
193
- end
194
-
195
- it 'should cache result' do
196
- Product.should_receive(:unmemoized_hydra_attribute_names).once
197
- 2.times { Product.hydra_attribute_names }
198
- end
199
-
200
- it 'should reset method cache after creating hydra attribute' do
201
- hydra_attributes = Product.hydra_attributes
202
- Product.should_receive(:unmemoized_hydra_attribute_names).twice
203
-
204
- Product.hydra_attribute_names
205
- hydra_attributes.create(name: 'one', backend_type: 'string')
206
- Product.hydra_attribute_names
207
- end
208
-
209
- it 'should reset method cache after updating hydra attribute' do
210
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
211
- Product.should_receive(:unmemoized_hydra_attribute_names).twice
212
-
213
- Product.hydra_attribute_names
214
- hydra_attribute.update_attributes(name: 'two')
215
- Product.hydra_attribute_names
216
- end
217
-
218
- it 'should reset method cache after removing hydra attribute' do
219
- hydra_attribute = Product.hydra_attributes.create(name: 'once', backend_type: 'string')
220
- Product.should_receive(:unmemoized_hydra_attribute_names).twice
221
-
222
- Product.hydra_attribute_names
223
- hydra_attribute.destroy
224
- Product.hydra_attribute_names
225
- end
226
- end
227
-
228
- describe '.hydra_attributes_by_backend_type' do
229
- it 'should group attributes by backend type' do
230
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
231
- two = Product.hydra_attributes.create(name: 'two', backend_type: 'string')
232
- three = Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
233
- four = Product.hydra_attributes.create(name: 'four', backend_type: 'float')
234
-
235
- attributes = Product.hydra_attributes_by_backend_type
236
- attributes['string'].should =~ [one, two]
237
- attributes['integer'].should == [three]
238
- attributes['float'].should == [four]
239
- end
240
-
241
- it 'should return blank hash if there are not any attributes' do
242
- Product.hydra_attributes_by_backend_type.should == {}
243
- end
244
-
245
- it 'should cache result' do
246
- Product.should_receive(:unmemoized_hydra_attributes_by_backend_type).once
247
- 2.times { Product.hydra_attributes_by_backend_type }
248
- end
249
-
250
- it 'should reset method cache after creating hydra attribute' do
251
- Product.should_receive(:unmemoized_hydra_attributes_by_backend_type).twice
252
-
253
- Product.hydra_attributes_by_backend_type
254
- Product.hydra_attributes.create(name: 'one', backend_type: 'string')
255
- Product.hydra_attributes_by_backend_type
256
- end
257
-
258
- it 'should reset method cache after updating hydra attribute' do
259
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
260
- Product.should_receive(:unmemoized_hydra_attributes_by_backend_type).twice
261
-
262
- Product.hydra_attributes_by_backend_type
263
- one.update_attributes(name: 'two')
264
- Product.hydra_attributes_by_backend_type
265
- end
266
-
267
- it 'should reset method cache after removing hydra attribute' do
268
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
269
- Product.should_receive(:unmemoized_hydra_attributes_by_backend_type).twice
270
-
271
- Product.hydra_attributes_by_backend_type
272
- one.destroy
273
- Product.hydra_attributes_by_backend_type
274
- end
275
- end
276
-
277
- describe '.hydra_attribute_ids_by_backend_type' do
278
- it 'should group attribute ids by backend type' do
279
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
280
- two = Product.hydra_attributes.create(name: 'two', backend_type: 'string')
281
- three = Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
282
- four = Product.hydra_attributes.create(name: 'four', backend_type: 'float')
283
-
284
- attributes = Product.hydra_attribute_ids_by_backend_type
285
- attributes['string'].should =~ [one.id, two.id]
286
- attributes['integer'].should == [three.id]
287
- attributes['float'].should == [four.id]
288
- end
289
-
290
- it 'should return blank hash if there are not any attributes' do
291
- Product.hydra_attribute_ids_by_backend_type.should == {}
292
- end
293
-
294
- it 'should cache result' do
295
- Product.should_receive(:unmemoized_hydra_attribute_ids_by_backend_type).once
296
- 2.times { Product.hydra_attribute_ids_by_backend_type }
297
- end
298
-
299
- it 'should reset method cache after creating hydra attribute' do
300
- Product.should_receive(:unmemoized_hydra_attribute_ids_by_backend_type).twice
301
-
302
- Product.hydra_attribute_ids_by_backend_type
303
- Product.hydra_attributes.create(name: 'one', backend_type: 'string')
304
- Product.hydra_attribute_ids_by_backend_type
305
- end
306
-
307
- it 'should reset method cache after updating hydra attribute' do
308
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
309
- Product.should_receive(:unmemoized_hydra_attribute_ids_by_backend_type).twice
310
-
311
- Product.hydra_attribute_ids_by_backend_type
312
- hydra_attribute.update_attributes(name: 'two')
313
- Product.hydra_attribute_ids_by_backend_type
314
- end
315
-
316
- it 'should reset method cache after removing hydra attribute' do
317
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
318
- Product.should_receive(:unmemoized_hydra_attribute_ids_by_backend_type).twice
319
-
320
- Product.hydra_attribute_ids_by_backend_type
321
- hydra_attribute.destroy
322
- Product.hydra_attribute_ids_by_backend_type
323
- end
324
- end
325
-
326
- describe '.hydra_attribute_names_by_backend_type' do
327
- it 'should group attribute names by backend type' do
328
- one = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
329
- two = Product.hydra_attributes.create(name: 'two', backend_type: 'string')
330
- three = Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
331
- four = Product.hydra_attributes.create(name: 'four', backend_type: 'float')
332
-
333
- attributes = Product.hydra_attribute_names_by_backend_type
334
- attributes['string'].should =~ [one.name, two.name]
335
- attributes['integer'].should == [three.name]
336
- attributes['float'].should == [four.name]
337
- end
338
-
339
- it 'should return blank hash if there are not any attributes' do
340
- Product.hydra_attribute_names_by_backend_type.should == {}
341
- end
342
-
343
- it 'should cache result' do
344
- Product.should_receive(:unmemoized_hydra_attribute_names_by_backend_type).once
345
- 2.times { Product.hydra_attribute_names_by_backend_type }
346
- end
347
-
348
- it 'should reset method cache after creating hydra attribute' do
349
- Product.should_receive(:unmemoized_hydra_attribute_names_by_backend_type).twice
350
-
351
- Product.hydra_attribute_names_by_backend_type
352
- Product.hydra_attributes.create(name: 'one', backend_type: 'string')
353
- Product.hydra_attribute_names_by_backend_type
354
- end
355
-
356
- it 'should reset method cache after updating hydra attribute' do
357
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
358
- Product.should_receive(:unmemoized_hydra_attribute_names_by_backend_type).twice
359
-
360
- Product.hydra_attribute_names_by_backend_type
361
- hydra_attribute.update_attributes(name: 'two')
362
- Product.hydra_attribute_names_by_backend_type
363
- end
364
-
365
- it 'should reset method cache after removing hydra attribute' do
366
- hydra_attribute = Product.hydra_attributes.create(name: 'one', backend_type: 'string')
367
- Product.should_receive(:unmemoized_hydra_attribute_names_by_backend_type).twice
368
-
369
- Product.hydra_attribute_names_by_backend_type
370
- hydra_attribute.destroy
371
- Product.hydra_attribute_names_by_backend_type
372
- end
373
- end
374
-
375
- describe '.hydra_attributes_for_backend_type' do
376
- it 'should return hydra attributes for specific backend type' do
377
- a1 = Product.hydra_attributes.create(name: 'a1', backend_type: 'string')
378
- a2 = Product.hydra_attributes.create(name: 'a2', backend_type: 'string')
379
- a3 = Product.hydra_attributes.create(name: 'a3', backend_type: 'integer')
380
- a4 = Product.hydra_attributes.create(name: 'a4', backend_type: 'integer')
381
-
382
- Product.hydra_attributes_for_backend_type('string').should =~ [a1, a2]
383
- Product.hydra_attributes_for_backend_type('integer').should =~ [a3, a4]
384
- end
385
-
386
- it 'should return blank array if there are not any hydra attributes' do
387
- Product.hydra_attributes_for_backend_type('string').should == []
388
- end
389
- end
390
-
391
- describe '.hydra_attribute_ids_for_backend_type' do
392
- it 'should return hydra attribute ids for specific backend type' do
393
- a1 = Product.hydra_attributes.create(name: 'a1', backend_type: 'string')
394
- a2 = Product.hydra_attributes.create(name: 'a2', backend_type: 'string')
395
- a3 = Product.hydra_attributes.create(name: 'a3', backend_type: 'integer')
396
- a4 = Product.hydra_attributes.create(name: 'a4', backend_type: 'integer')
397
-
398
- Product.hydra_attribute_ids_for_backend_type('string').should =~ [a1.id, a2.id]
399
- Product.hydra_attribute_ids_for_backend_type('integer').should =~ [a3.id, a4.id]
400
- end
401
-
402
- it 'should return blank array if there are not any hydra attributes' do
403
- Product.hydra_attribute_ids_for_backend_type('string').should == []
404
- end
405
- end
406
-
407
- describe '.hydra_attribute_names_for_backend_type' do
408
- it 'should return hydra attribute names for specific backend type' do
409
- a1 = Product.hydra_attributes.create(name: 'a1', backend_type: 'string')
410
- a2 = Product.hydra_attributes.create(name: 'a2', backend_type: 'string')
411
- a3 = Product.hydra_attributes.create(name: 'a3', backend_type: 'integer')
412
- a4 = Product.hydra_attributes.create(name: 'a4', backend_type: 'integer')
413
-
414
- Product.hydra_attribute_names_for_backend_type('string').should =~ [a1.name, a2.name]
415
- Product.hydra_attribute_names_for_backend_type('integer').should =~ [a3.name, a4.name]
416
- end
417
-
418
- it 'should return blank array if there are not any hydra attributes' do
419
- Product.hydra_attribute_names_for_backend_type('string').should == []
420
- end
421
- end
422
-
423
- describe '.clear_hydra_attribute_cache!' do
424
- it 'should reset cache for all methods' do
425
- # Force clear cache because some methods
426
- # can be already cached during initialization hydra_attribute gem
427
- Product.clear_hydra_attribute_cache!
428
-
429
- [
430
- :unmemoized_hydra_attributes,
431
- :unmemoized_hydra_attribute,
432
- :unmemoized_hydra_attribute_backend_types,
433
- :unmemoized_hydra_attribute_ids,
434
- :unmemoized_hydra_attribute_names,
435
- :unmemoized_hydra_attributes_by_backend_type,
436
- :unmemoized_hydra_attribute_ids_by_backend_type,
437
- :unmemoized_hydra_attribute_names_by_backend_type
438
- ].each do |symbol|
439
- Product.should_receive(symbol).twice.and_return([])
440
- end
441
-
442
- block = lambda do
443
- Product.hydra_attributes
444
- Product.hydra_attribute(1)
445
- Product.hydra_attribute_backend_types
446
- Product.hydra_attribute_ids
447
- Product.hydra_attribute_names
448
- Product.hydra_attributes_by_backend_type
449
- Product.hydra_attribute_ids_by_backend_type
450
- Product.hydra_attribute_names_by_backend_type
451
- end
452
-
453
- block.call
454
- Product.clear_hydra_attribute_cache!
455
- block.call
456
- end
457
- end
458
- end