hydra_attribute 0.2.0 → 0.3.0.beta1
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/README.md +74 -68
- data/cucumber.yml +1 -0
- data/features/attributes/create.feature +22 -0
- data/features/attributes/destroy.feature +18 -0
- data/features/attributes/update.feature +19 -0
- data/features/create.feature +47 -0
- data/features/define.feature +33 -0
- data/features/query_methods/group.feature +13 -14
- data/features/query_methods/order.feature +63 -52
- data/features/query_methods/select.feature +36 -37
- data/features/query_methods/where.feature +36 -38
- data/features/step_definitions/model_steps.rb +23 -19
- data/features/step_definitions/query_methods.rb +6 -2
- data/features/step_definitions/record_steps.rb +28 -10
- data/features/support/env.rb +12 -6
- data/features/support/schema.rb +62 -35
- data/features/support/world.rb +14 -5
- data/features/update.feature +114 -0
- data/gemfiles/3.1.gemfile.lock +10 -10
- data/gemfiles/3.2.gemfile.lock +10 -10
- data/lib/hydra_attribute/active_record/association.rb +77 -0
- data/lib/hydra_attribute/active_record/association_preloader.rb +82 -0
- data/lib/hydra_attribute/active_record/attribute_methods.rb +145 -37
- data/lib/hydra_attribute/active_record/migration.rb +21 -0
- data/lib/hydra_attribute/active_record/reflection.rb +16 -0
- data/lib/hydra_attribute/active_record/relation/query_methods.rb +73 -71
- data/lib/hydra_attribute/active_record/relation.rb +1 -24
- data/lib/hydra_attribute/active_record.rb +16 -2
- data/lib/hydra_attribute/association_builder.rb +44 -20
- data/lib/hydra_attribute/builder.rb +15 -13
- data/lib/hydra_attribute/configuration.rb +9 -30
- data/lib/hydra_attribute/entity_callbacks.rb +46 -0
- data/lib/hydra_attribute/hydra_attribute.rb +27 -0
- data/lib/hydra_attribute/migrator.rb +106 -0
- data/lib/hydra_attribute/railtie.rb +2 -0
- data/lib/hydra_attribute/version.rb +2 -2
- data/lib/hydra_attribute.rb +8 -6
- data/lib/rails/generators/hydra_attribute/install/templates/hydra_attribute.txt +4 -0
- data/spec/spec_helper.rb +1 -2
- metadata +42 -60
- data/features/attribute_methods.feature +0 -146
- data/features/define_attributes.feature +0 -56
- data/features/load_associations.feature +0 -40
- data/features/step_definitions/class_steps.rb +0 -32
- data/features/typecast_attributes.feature +0 -24
- data/lib/generators/hydra_attribute/install/templates/hydra_attribute.txt +0 -11
- data/lib/hydra_attribute/active_record/attribute_methods/before_type_cast.rb +0 -16
- data/lib/hydra_attribute/active_record/attribute_methods/read.rb +0 -13
- data/lib/hydra_attribute/attribute_builder.rb +0 -57
- data/lib/hydra_attribute/attribute_proxy.rb +0 -16
- data/lib/hydra_attribute/migration.rb +0 -27
- data/spec/hydra_attribute/active_record/relation/query_methods_spec.rb +0 -334
- data/spec/hydra_attribute/active_record/relation_spec.rb +0 -67
- data/spec/hydra_attribute/active_record/scoping_spec.rb +0 -23
- data/spec/hydra_attribute/active_record_spec.rb +0 -18
- data/spec/hydra_attribute/association_builder_spec.rb +0 -95
- data/spec/hydra_attribute/attribute_builder_spec.rb +0 -70
- data/spec/hydra_attribute/attribute_helpers_spec.rb +0 -70
- data/spec/hydra_attribute/builder_spec.rb +0 -39
- data/spec/hydra_attribute/configuration_spec.rb +0 -65
- data/spec/hydra_attribute_spec.rb +0 -20
- /data/lib/{generators → rails/generators}/hydra_attribute/install/USAGE +0 -0
- /data/lib/{generators → rails/generators}/hydra_attribute/install/install_generator.rb +0 -0
@@ -1,334 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HydraAttribute::ActiveRecord::Relation::QueryMethods do
|
4
|
-
let(:relation_class) do
|
5
|
-
mock(
|
6
|
-
:hydra_attributes => {'code' => :string},
|
7
|
-
:hydra_attribute_names => ['code'],
|
8
|
-
:hydra_attribute_types => [:string]
|
9
|
-
)
|
10
|
-
end
|
11
|
-
|
12
|
-
let(:relation) { mock(klass: relation_class, where: nil) }
|
13
|
-
|
14
|
-
before do
|
15
|
-
relation.singleton_class.send :include, ::ActiveRecord::QueryMethods
|
16
|
-
relation.singleton_class.send :include, HydraAttribute::ActiveRecord::Relation::QueryMethods
|
17
|
-
end
|
18
|
-
|
19
|
-
describe '#where' do
|
20
|
-
let(:relation) do
|
21
|
-
mock_relation = mock(klass: relation_class)
|
22
|
-
mock_relation.instance_variable_set(:@where_values, [])
|
23
|
-
mock_relation.instance_variable_set(:@joins_values, [])
|
24
|
-
mock_relation.stub(:build_hydra_joins_values) { |name, value| ["join-#{name}-#{value}"] }
|
25
|
-
mock_relation.stub(:build_hydra_where_options) { |name, value| ["where-#{name}-#{value}"] }
|
26
|
-
mock_relation.stub(:build_where) { |value, *rest| ["#{value} #{rest}"] }
|
27
|
-
mock_relation
|
28
|
-
end
|
29
|
-
|
30
|
-
before do
|
31
|
-
module HydraAttribute
|
32
|
-
class StringAttribute
|
33
|
-
def self.table_name
|
34
|
-
'hydra_string_attributes'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
after do
|
41
|
-
HydraAttribute.send :remove_const, :StringAttribute
|
42
|
-
end
|
43
|
-
|
44
|
-
describe 'first param is Hash' do
|
45
|
-
let(:params) { {'title' => 1} }
|
46
|
-
|
47
|
-
describe 'hash has not hydra attribute' do
|
48
|
-
it 'should call rails native "where" method' do
|
49
|
-
object = relation.where(params)
|
50
|
-
object.where_values.should == ['{"title"=>1} [[]]']
|
51
|
-
object.joins_values.should == []
|
52
|
-
object.hydra_joins_aliases.should == []
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe 'hash has hydra attribute' do
|
57
|
-
let(:params) { {'title' => 1, 'code' => 2, 'name' => 3} }
|
58
|
-
|
59
|
-
it 'should call both native and overwritten "where" method' do
|
60
|
-
copy_relation = relation.where(params)
|
61
|
-
copy_relation.where_values.should == ['{"title"=>1} [[]]', 'where-code-2 []', '{"name"=>3} [[]]']
|
62
|
-
copy_relation.joins_values.should == ['join-code-2']
|
63
|
-
copy_relation.hydra_joins_aliases.should == ['hydra_string_attributes_inner_code']
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe 'first param is not Hash' do
|
69
|
-
let(:params) { 'name = 1' }
|
70
|
-
|
71
|
-
it 'should call rails native "where" method' do
|
72
|
-
copy_relation = relation.where(params)
|
73
|
-
copy_relation.where_values.should == ['name = 1 [[]]']
|
74
|
-
copy_relation.joins_values.should == []
|
75
|
-
copy_relation.hydra_joins_aliases.should == []
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe '#build_arel' do
|
81
|
-
let(:arel) { mock.as_null_object }
|
82
|
-
|
83
|
-
let(:relation) do
|
84
|
-
mock_relation = mock(klass: relation_class, where: nil, build_select: nil)
|
85
|
-
mock_relation.stub_chain(:table, :from).and_return(arel)
|
86
|
-
mock_relation.instance_variable_set(:@joins_values, [])
|
87
|
-
mock_relation.instance_variable_set(:@order_values, [])
|
88
|
-
mock_relation.instance_variable_set(:@where_values, [])
|
89
|
-
mock_relation.instance_variable_set(:@having_values, [])
|
90
|
-
mock_relation.instance_variable_set(:@group_values, [])
|
91
|
-
mock_relation.instance_variable_set(:@select_values, [])
|
92
|
-
mock_relation
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'should update @order_values before generate the arel object' do
|
96
|
-
relation.stub(build_hydra_values_for_arel: %w(build_order))
|
97
|
-
relation.build_arel.should == arel
|
98
|
-
relation.instance_variable_get(:@order_values).should == %w(build_order)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe '#build_hydra_values_for_arel' do
|
103
|
-
let(:connection) do
|
104
|
-
conn = mock
|
105
|
-
conn.stub(:quote_column_name) { |column| column.to_s }
|
106
|
-
conn.stub(:quote) { |value| %Q("#{value.to_s}") }
|
107
|
-
conn.stub(:quote_table_name) { |table| table.to_s }
|
108
|
-
conn
|
109
|
-
end
|
110
|
-
|
111
|
-
let(:relation_class) do
|
112
|
-
mock(connection: connection, quoted_table_name: 'product', hydra_attribute_names: %w(code title price))
|
113
|
-
end
|
114
|
-
|
115
|
-
let(:relation) do
|
116
|
-
mock_relation = mock(klass: relation_class, connection: connection)
|
117
|
-
mock_relation.stub(where: mock_relation)
|
118
|
-
mock_relation.stub(:hydra_ref_alias) { |name, value| "#{name}_#{value}" }
|
119
|
-
mock_relation.stub(:build_hydra_joins_values) { |name, value| ["#{name}_#{value}_join"] }
|
120
|
-
mock_relation.instance_variable_set(:@joins_values, [])
|
121
|
-
mock_relation.instance_variable_set(:@hydra_joins_aliases, %w(code_inner title_))
|
122
|
-
mock_relation
|
123
|
-
end
|
124
|
-
|
125
|
-
describe 'collection has not hydra attributes' do
|
126
|
-
it 'should return the same collection' do
|
127
|
-
relation.send(:build_hydra_values_for_arel, %w(name zone)).should == %w(product.name product.zone)
|
128
|
-
relation.joins_values.should == []
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe 'collection has hydra attributes' do
|
133
|
-
it 'should change hydra attributes and join hydra tables' do
|
134
|
-
relation.send(:build_hydra_values_for_arel, %w(name code title price)).should == %w(product.name code_inner.value title_.value price_.value)
|
135
|
-
relation.joins_values.should == %w(price__join)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
describe '#build_hydra_joins_values' do
|
141
|
-
let(:connection) do
|
142
|
-
conn = mock
|
143
|
-
conn.stub(:quote_column_name) { |column| column.to_s }
|
144
|
-
conn.stub(:quote) { |value| %Q("#{value.to_s}") }
|
145
|
-
conn.stub(:quote_table_name) { |table| table.to_s }
|
146
|
-
conn
|
147
|
-
end
|
148
|
-
|
149
|
-
let(:relation_class) do
|
150
|
-
mock(
|
151
|
-
:connection => connection,
|
152
|
-
:base_class => mock(name: 'BaseClass'),
|
153
|
-
:quoted_primary_key => 'id',
|
154
|
-
:quoted_table_name => 'hydra_string_attributes'
|
155
|
-
)
|
156
|
-
end
|
157
|
-
|
158
|
-
let(:relation) do
|
159
|
-
mock_relation = mock(klass: relation_class)
|
160
|
-
mock_relation.stub(where: mock_relation)
|
161
|
-
mock_relation.stub(:hydra_ref_alias) { |name, value| "#{name}_#{value}" }
|
162
|
-
mock_relation.stub(:hydra_ref_table) { |name| "table_#{name}" }
|
163
|
-
mock_relation
|
164
|
-
end
|
165
|
-
|
166
|
-
describe 'value is nil' do
|
167
|
-
let(:value) { nil }
|
168
|
-
let(:sql) { 'LEFT JOIN table_name AS name_ ON hydra_string_attributes.id = name_.entity_id AND name_.entity_type = "BaseClass" AND name_.name = "name"' }
|
169
|
-
|
170
|
-
it 'should return array with one SQL query element' do
|
171
|
-
relation.send(:build_hydra_joins_values, :name, value).should == [sql]
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
describe 'value is not nil' do
|
176
|
-
let(:value) { 'value' }
|
177
|
-
let(:sql) { 'INNER JOIN table_name AS name_value ON hydra_string_attributes.id = name_value.entity_id AND name_value.entity_type = "BaseClass" AND name_value.name = "name"' }
|
178
|
-
|
179
|
-
it 'should return array with one SQL query element' do
|
180
|
-
relation.send(:build_hydra_joins_values, :name, value).should == [sql]
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
describe '#build_hydra_where_options' do
|
186
|
-
before do
|
187
|
-
module HydraAttribute
|
188
|
-
class StringAttribute
|
189
|
-
def self.table_name
|
190
|
-
'hydra_string_attributes'
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
after { HydraAttribute.send :remove_const, :StringAttribute }
|
197
|
-
|
198
|
-
it 'should create where options with table namespace' do
|
199
|
-
relation.send(:build_hydra_where_options, 'code', 'abc').should == {'hydra_string_attributes_inner_code' => { value: 'abc' }}
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
describe '#hydra_ref_class' do
|
204
|
-
before do
|
205
|
-
module HydraAttribute
|
206
|
-
class StringAttribute
|
207
|
-
def self.table_name
|
208
|
-
'hydra_string_attributes'
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
after { HydraAttribute.send :remove_const, :StringAttribute }
|
215
|
-
|
216
|
-
it 'should return class by attribute name' do
|
217
|
-
relation.send(:hydra_ref_class, 'code').should == HydraAttribute::StringAttribute
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
describe '#hydra_ref_table' do
|
222
|
-
before do
|
223
|
-
module HydraAttribute
|
224
|
-
class StringAttribute
|
225
|
-
def self.table_name
|
226
|
-
'hydra_string_attributes'
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
after { HydraAttribute.send :remove_const, :StringAttribute }
|
233
|
-
|
234
|
-
it 'should return table name' do
|
235
|
-
relation.send(:hydra_ref_table, 'code').should == 'hydra_string_attributes'
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
describe '#hydra_ref_alias' do
|
240
|
-
before do
|
241
|
-
module HydraAttribute
|
242
|
-
class StringAttribute
|
243
|
-
def self.table_name
|
244
|
-
'hydra_string_attributes'
|
245
|
-
end
|
246
|
-
end
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
after { HydraAttribute.send :remove_const, :StringAttribute }
|
251
|
-
|
252
|
-
describe 'value is nil' do
|
253
|
-
let(:value) { nil }
|
254
|
-
|
255
|
-
it 'should return generated alias name' do
|
256
|
-
relation.send(:hydra_ref_alias, 'code', value).should == 'hydra_string_attributes_left_code'
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
describe 'value is not nil' do
|
261
|
-
let(:value) { '' }
|
262
|
-
|
263
|
-
it 'should return generated alias name' do
|
264
|
-
relation.send(:hydra_ref_alias, 'code', value).should == 'hydra_string_attributes_inner_code'
|
265
|
-
end
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
describe '#hydra_join_type' do
|
270
|
-
describe 'value is nil' do
|
271
|
-
let(:value) { nil }
|
272
|
-
|
273
|
-
it 'should return "LEFT"' do
|
274
|
-
relation.send(:hydra_join_type, value).should == 'LEFT'
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
describe 'value is not nil' do
|
279
|
-
let(:value) { '' }
|
280
|
-
|
281
|
-
it 'should return "INNER"' do
|
282
|
-
relation.send(:hydra_join_type, value).should == 'INNER'
|
283
|
-
end
|
284
|
-
end
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
describe HydraAttribute::ActiveRecord::Relation::QueryMethods::Helper do
|
289
|
-
let(:relation_class) do
|
290
|
-
mock(table_name: 'entities', quoted_table_name: '"entities"')
|
291
|
-
end
|
292
|
-
|
293
|
-
let(:connection) do
|
294
|
-
mock_connection = mock
|
295
|
-
mock_connection.stub(:quote_column_name) { |column| %Q("#{column}") }
|
296
|
-
mock_connection
|
297
|
-
end
|
298
|
-
|
299
|
-
let(:relation) do
|
300
|
-
mock(klass: relation_class, connection: connection)
|
301
|
-
end
|
302
|
-
|
303
|
-
let(:helper) do
|
304
|
-
HydraAttribute::ActiveRecord::Relation::QueryMethods::Helper.new(relation)
|
305
|
-
end
|
306
|
-
|
307
|
-
describe '#prepend_table_name' do
|
308
|
-
describe 'param is Symbol' do
|
309
|
-
it 'should prepend table name and quote param' do
|
310
|
-
helper.prepend_table_name(:column).should == '"entities"."column"'
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
describe 'param is String' do
|
315
|
-
describe 'params is a word character (letter, number, underscore)' do
|
316
|
-
it 'should prepend table name and quote param' do
|
317
|
-
helper.prepend_table_name('abc').should == '"entities"."abc"'
|
318
|
-
helper.prepend_table_name('a_c').should == '"entities"."a_c"'
|
319
|
-
helper.prepend_table_name('a1c').should == '"entities"."a1c"'
|
320
|
-
helper.prepend_table_name(' a ').should == '"entities"."a"'
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
describe 'params is not a word character (letter, number, underscore)' do
|
325
|
-
it 'should return current string' do
|
326
|
-
helper.prepend_table_name('a c').should == 'a c'
|
327
|
-
helper.prepend_table_name('a-c').should == 'a-c'
|
328
|
-
helper.prepend_table_name('(a)').should == '(a)'
|
329
|
-
helper.prepend_table_name('.').should == '.'
|
330
|
-
end
|
331
|
-
end
|
332
|
-
end
|
333
|
-
end
|
334
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HydraAttribute::ActiveRecord::Relation do
|
4
|
-
|
5
|
-
let(:record_class) do
|
6
|
-
mock(:record_class, hydra_attribute_types: [:string], hydra_attribute_names: %w(code), hydra_attributes: {'code' => :string})
|
7
|
-
end
|
8
|
-
|
9
|
-
let(:record) do
|
10
|
-
mock(:record, class: record_class, association: mock(loaded?: false))
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:loaded_record) do
|
14
|
-
mock(:loaded_record, class: record_class, association: mock(loaded?: true))
|
15
|
-
end
|
16
|
-
|
17
|
-
let(:exec_method) do
|
18
|
-
::ActiveRecord::VERSION::STRING.start_with?('3.1.') ? :to_a : :exec_queries
|
19
|
-
end
|
20
|
-
|
21
|
-
let(:relation) do
|
22
|
-
mock_relation = mock(:relation, loaded?: false, select_values: [], hydra_select_values: [])
|
23
|
-
|
24
|
-
mock_relation.stub(exec_method).and_return(records)
|
25
|
-
mock_relation.stub(:klass).and_return(records.first.class)
|
26
|
-
mock_relation.stub(:where).and_return(mock_relation)
|
27
|
-
mock_relation
|
28
|
-
end
|
29
|
-
|
30
|
-
let(:records) do
|
31
|
-
[record]
|
32
|
-
end
|
33
|
-
|
34
|
-
before do
|
35
|
-
relation.singleton_class.send(:include, HydraAttribute::ActiveRecord::Relation)
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "#exec_queries" do
|
39
|
-
describe 'parent method return one record' do
|
40
|
-
it 'should return one record' do
|
41
|
-
relation.send(exec_method).should have(1).record
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe 'parent method returns two records' do
|
46
|
-
describe 'association models are already loaded' do
|
47
|
-
let(:records) { [loaded_record, loaded_record] }
|
48
|
-
|
49
|
-
it 'should return two record' do
|
50
|
-
relation.send(exec_method).should have(2).records
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
describe 'association models are not yet loaded' do
|
55
|
-
let(:records) { [record, record] }
|
56
|
-
|
57
|
-
before do
|
58
|
-
::ActiveRecord::Associations::Preloader.should_receive(:new).with(records, :hydra_string_attributes).and_return(mock(run: records))
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'should return two record' do
|
62
|
-
relation.send(exec_method).should have(2).records
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HydraAttribute::ActiveRecord::Scoping do
|
4
|
-
describe '#scoped' do
|
5
|
-
let(:ancestor) do
|
6
|
-
method = ::ActiveRecord::VERSION::STRING.starts_with?('3.1.') ? :to_a : :exec_queries
|
7
|
-
instance = mock(:instance_relation, where: nil, method => nil)
|
8
|
-
|
9
|
-
Module.new do
|
10
|
-
define_method :scoped do |*|
|
11
|
-
instance
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
let(:klass) { Class.new.extend(ancestor) }
|
17
|
-
|
18
|
-
it 'should return ActiveRecord::Relation object with extended HydraAttribute::ActiveRecord::Relation module' do
|
19
|
-
klass.send :include, HydraAttribute::ActiveRecord::Scoping
|
20
|
-
klass.scoped.singleton_class.ancestors.should include(HydraAttribute::ActiveRecord::Relation)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HydraAttribute::ActiveRecord do
|
4
|
-
let(:klass) { Class.new.extend(HydraAttribute::ActiveRecord) }
|
5
|
-
|
6
|
-
describe '#define_hydra_attributes' do
|
7
|
-
it 'should yield block in builder scope' do
|
8
|
-
klass.define_hydra_attributes do
|
9
|
-
self.class.should == HydraAttribute::Builder
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should include HydraAttribute::ActiveRecord::Scoping to class' do
|
14
|
-
klass.define_hydra_attributes {}
|
15
|
-
klass.should include(HydraAttribute::ActiveRecord::Scoping)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HydraAttribute::AssociationBuilder do
|
4
|
-
def remove_association
|
5
|
-
[::HydraAttribute, ::Object].each do |klass|
|
6
|
-
klass.send(:remove_const, :StringAttribute) if klass.constants.include?(:StringAttribute)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
after(:each) { remove_association }
|
11
|
-
|
12
|
-
let(:klass) do
|
13
|
-
Class.new do
|
14
|
-
@reflection = {}
|
15
|
-
define_singleton_method :reflect_on_association do |assoc|
|
16
|
-
@reflection[assoc]
|
17
|
-
end
|
18
|
-
define_singleton_method :has_many do |assoc, options|
|
19
|
-
@reflection[assoc] = options
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
let(:type) { :string }
|
25
|
-
let(:association) { HydraAttribute::AssociationBuilder.new(klass, type) }
|
26
|
-
|
27
|
-
describe '#buld' do
|
28
|
-
it 'should call #build_associated_model and build_association' do
|
29
|
-
association.should_receive(:create_associated_model)
|
30
|
-
association.should_receive(:add_association_for_class)
|
31
|
-
association.build
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#create_associated_model' do
|
36
|
-
describe 'use namespace for associated models' do
|
37
|
-
it 'should create new model' do
|
38
|
-
association.send(:create_associated_model)
|
39
|
-
HydraAttribute.should be_const_defined(:StringAttribute)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe 'should not use namespace for associated models' do
|
44
|
-
before { HydraAttribute.config.use_module_for_associated_models = false }
|
45
|
-
after { HydraAttribute.config.use_module_for_associated_models = true }
|
46
|
-
|
47
|
-
it 'should create new model' do
|
48
|
-
association.send(:create_associated_model)
|
49
|
-
Object.should be_const_defined(:StringAttribute)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe 'do not try to create twice the same class' do
|
54
|
-
it 'should not warn "already initialized constant"' do
|
55
|
-
association.send(:create_associated_model)
|
56
|
-
HydraAttribute.should_not_receive(:const_set)
|
57
|
-
association.send(:create_associated_model)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe 'set correct table name' do
|
62
|
-
it 'should be :hydra_string_attributes' do
|
63
|
-
association.send(:create_associated_model)
|
64
|
-
HydraAttribute::StringAttribute.table_name.should == 'hydra_string_attributes'
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe 'set correct belongs_to' do
|
69
|
-
it 'should be polymorphic association' do
|
70
|
-
association.send(:create_associated_model)
|
71
|
-
reflection = HydraAttribute::StringAttribute.reflect_on_association(:entity)
|
72
|
-
reflection.macro.should == :belongs_to
|
73
|
-
reflection.options[:polymorphic].should be_true
|
74
|
-
reflection.options[:touch].should be_true
|
75
|
-
reflection.options[:autosave].should be_true
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe '#add_association_for_class' do
|
81
|
-
it 'should add has_many association for class' do
|
82
|
-
association.send(:add_association_for_class)
|
83
|
-
reflection = klass.reflect_on_association(:hydra_string_attributes)
|
84
|
-
reflection[:as].should == :entity
|
85
|
-
reflection[:class_name].should == 'HydraAttribute::StringAttribute'
|
86
|
-
reflection[:autosave].should be_true
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'should not add twice the same has_many association' do
|
90
|
-
association.send(:add_association_for_class)
|
91
|
-
klass.should_not_receive(:has_many)
|
92
|
-
association.send(:add_association_for_class)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe HydraAttribute::AttributeBuilder do
|
4
|
-
let(:klass) { Class.new }
|
5
|
-
let(:attribute) { HydraAttribute::AttributeBuilder.new(klass, :name, :string) }
|
6
|
-
|
7
|
-
describe '#build' do
|
8
|
-
it 'should call required methods' do
|
9
|
-
attribute.should_receive(:define_attribute_methods)
|
10
|
-
attribute.should_receive(:save_attribute)
|
11
|
-
attribute.build
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '#define_attribute_methods' do
|
16
|
-
let(:matcher_get) do
|
17
|
-
mock_matcher = mock
|
18
|
-
mock_matcher.stub(:method_name) { |value| value.to_s }
|
19
|
-
mock_matcher
|
20
|
-
end
|
21
|
-
|
22
|
-
let(:matcher_set) do
|
23
|
-
mock_matcher = mock
|
24
|
-
mock_matcher.stub(:method_name) { |value| "#{value}=" }
|
25
|
-
mock_matcher
|
26
|
-
end
|
27
|
-
|
28
|
-
let(:matcher_query) do
|
29
|
-
mock_matcher = mock
|
30
|
-
mock_matcher.stub(:method_name) { |value| "#{value}?" }
|
31
|
-
mock_matcher
|
32
|
-
end
|
33
|
-
|
34
|
-
before do
|
35
|
-
klass.stub(attribute_method_matchers: [matcher_get, matcher_set, matcher_query])
|
36
|
-
attribute.send(:define_attribute_methods)
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should respond to #name' do
|
40
|
-
klass.new.should respond_to(:name)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'should respond to #name=' do
|
44
|
-
klass.new.should respond_to(:name=)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should respond to #name?' do
|
48
|
-
klass.new.should respond_to(:name?)
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should define all methods in module' do
|
52
|
-
[:name, :name=, :name?].each do |method|
|
53
|
-
klass.new.method(method).owner.class.should == Module
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe '#store_attribute' do
|
59
|
-
before do
|
60
|
-
attribute.instance_variable_set(:@name, :name)
|
61
|
-
attribute.instance_variable_set(:@type, :type)
|
62
|
-
klass.instance_variable_set(:@hydra_attributes, {})
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should save attribute name and type in @hydra_attributes' do
|
66
|
-
attribute.send(:save_attribute)
|
67
|
-
klass.instance_variable_get(:@hydra_attributes)[:name].should == :type
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
__END__
|
3
|
-
describe HydraAttribute::AttributeHelpers do
|
4
|
-
let(:klass) { Class.new { include HydraAttribute::AttributeHelpers } }
|
5
|
-
|
6
|
-
describe '.inherited' do
|
7
|
-
it 'should clone @hydra_attributes to base class' do
|
8
|
-
klass.instance_variable_set(:@hydra_attributes, {a: 1, b: 2})
|
9
|
-
sub_class = Class.new(klass)
|
10
|
-
sub_class.instance_variable_get(:@hydra_attributes).should == {a: 1, b: 2}
|
11
|
-
sub_class.instance_variable_get(:@hydra_attributes).should_not equal(klass.instance_variable_get(:@hydra_attributes))
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '.hydra_attributes' do
|
16
|
-
it 'should return hash of attributes' do
|
17
|
-
klass.hydra_attributes.should == {}
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'should return cloned hash' do
|
21
|
-
klass.hydra_attributes.should_not equal(klass.hydra_attributes)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '.hydra_attribute_names' do
|
26
|
-
it 'should return hash keys' do
|
27
|
-
klass.instance_variable_set(:@hydra_attributes, {a: 1, b: 2})
|
28
|
-
klass.hydra_attribute_names.should == [:a, :b]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe '.hydra_attribute_types' do
|
33
|
-
it 'should return hash values' do
|
34
|
-
klass.instance_variable_set(:@hydra_attributes, {a: 1, b: 2})
|
35
|
-
klass.hydra_attribute_types.should == [1, 2]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe '#hydra_attribute_model' do
|
40
|
-
let(:code_model) { mock(name: :code) }
|
41
|
-
let(:title_model) { mock(name: :title) }
|
42
|
-
let(:collection) { [code_model, title_model] }
|
43
|
-
|
44
|
-
before do
|
45
|
-
HydraAttribute.config.should_receive(:association).with(:string).and_return(:string_association)
|
46
|
-
klass.any_instance.should_receive(:string_association).and_return(collection)
|
47
|
-
end
|
48
|
-
|
49
|
-
describe 'type collection has built model' do
|
50
|
-
let(:name) { :code }
|
51
|
-
|
52
|
-
it 'should return model' do
|
53
|
-
klass.new.hydra_attribute_model(name, :string).should == code_model
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe 'type collection has not built model' do
|
58
|
-
let(:name) { :price }
|
59
|
-
let(:built) { mock(:built) }
|
60
|
-
|
61
|
-
before do
|
62
|
-
collection.should_receive(:build).with(name: :price).and_return(built)
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should build model' do
|
66
|
-
klass.new.hydra_attribute_model(name, :string).should == built
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|