@@ -180,15 +180,15 @@ def create_collection_of_getter(attrib, options)
180
180
def #{ attrib } (reload = false)
181
181
return @#{ attrib } unless @#{ attrib } .nil? or reload
182
182
ary = self.#{ options [ :foreign_key ] } .collect{|i| #{ options [ :proxy ] } .get(i)}
183
- @#{ attrib } = ::CouchRest::Model::CollectionOfProxy.new(ary, find_property('#{ options [ :foreign_key ] } '), self)
183
+ @#{ attrib } = ::CouchRest::Model::Associations:: CollectionOfProxy.new(ary, find_property('#{ options [ :foreign_key ] } '), self)
184
184
end
185
185
EOS
186
186
end
187
187
188
188
def create_collection_of_setter ( attrib , options )
189
189
class_eval <<-EOS , __FILE__ , __LINE__ + 1
190
190
def #{ attrib } =(value)
191
- @#{ attrib } = ::CouchRest::Model::CollectionOfProxy.new(value, find_property('#{ options [ :foreign_key ] } '), self)
191
+ @#{ attrib } = ::CouchRest::Model::Associations:: CollectionOfProxy.new(value, find_property('#{ options [ :foreign_key ] } '), self)
192
192
end
193
193
EOS
194
194
end
@@ -232,82 +232,6 @@ def save_dirty_association
232
232
233
233
end
234
234
235
- # Special proxy for a collection of items so that adding and removing
236
- # to the list automatically updates the associated property.
237
- class CollectionOfProxy < CastedArray
238
-
239
- def initialize ( array , property , parent )
240
- ( array ||= [ ] ) . compact!
241
- super ( array , property , parent )
242
- casted_by [ casted_by_property . to_s ] = [ ] # replace the original array!
243
- array . compact . each do |obj |
244
- check_obj ( obj )
245
- casted_by [ casted_by_property . to_s ] << obj . id
246
- end
247
- end
248
-
249
- def << obj
250
- add_to_collection_with ( :<< , obj )
251
- super ( obj )
252
- end
253
-
254
- def push ( obj )
255
- add_to_collection_with ( :push , obj )
256
- super ( obj )
257
- end
258
-
259
- def unshift ( obj )
260
- add_to_collection_with ( :unshift , obj )
261
- super ( obj )
262
- end
263
-
264
- def []= index , obj
265
- add_to_collection_with ( :[]= , obj , index )
266
- super ( index , obj )
267
- end
268
-
269
- def pop
270
- obj = casted_by . send ( casted_by_property . options [ :proxy_name ] ) . last
271
- casted_by [ casted_by_property . to_s ] . pop
272
- obj . set_back_association ( nil , casted_by . class . name , casted_by_property . options [ :reverse_association ] )
273
- casted_by . register_dirty_association ( obj )
274
- super
275
- end
276
-
277
- def shift
278
- obj = casted_by . send ( casted_by_property . options [ :proxy_name ] ) . first
279
- casted_by [ casted_by_property . to_s ] . shift
280
- obj . set_back_association ( nil , casted_by . class . name , casted_by_property . options [ :reverse_association ] )
281
- casted_by . register_dirty_association ( obj )
282
- super
283
- end
284
-
285
- protected
286
-
287
- def check_obj ( obj )
288
- raise "Object cannot be added to #{ casted_by . class . to_s } ##{ casted_by_property . to_s } collection unless saved" if obj . new?
289
- end
290
-
291
- def add_to_collection_with ( method , obj , index = nil )
292
- check_obj ( obj )
293
- args = [ obj . id ]
294
- args = args . insert ( 0 , index ) if index
295
- casted_by [ casted_by_property . to_s ] . send ( method , *args )
296
- obj . set_back_association ( casted_by , casted_by . class . name , casted_by_property . options [ :reverse_association ] )
297
- casted_by . register_dirty_association ( obj )
298
- end
299
-
300
- # Override CastedArray instantiation_and_cast method for a simpler
301
- # version that will not try to cast the model.
302
- def instantiate_and_cast ( obj , change = true )
303
- couchrest_parent_will_change! if change && use_dirty?
304
- obj . casted_by = casted_by if obj . respond_to? ( :casted_by )
305
- obj . casted_by_property = casted_by_property if obj . respond_to? ( :casted_by_property )
306
- obj
307
- end
308
-
309
- end
310
-
311
235
end
312
236
313
237
end
0 commit comments