Permalink
Browse files

- added ActiveResoruce::Associations::AssociationCollection

  move all related methods to this new class
  • Loading branch information...
1 parent ccd217e commit b13fe4744bbd744aa5fe6627e950589d13d4083a @gramos committed Oct 4, 2010
Showing with 8 additions and 26 deletions.
  1. +8 −26 activeresource/lib/active_resource/associations.rb
@@ -1,3 +1,5 @@
+require 'active_resource/associations/association_collection'
+
module ActiveResource
module Associations
@@ -112,32 +114,12 @@ def has_many(resource, opts = {})
#----------------------------------------------------------------------#
define_method(resource) do
- collection = set_resource_instance_variable(resource) {
- o[:klass].constantize.find(:all,
- :params => { o[:association_col] => id }) || []
- }
-
- instance_eval "
- def collection.<<(member)
- member.#{o[:association_col]} = #{id}
- member.save
- super(member)
- end"
-
- instance_eval "
- def collection.delete(member)
- member.#{o[:association_col]} = nil
- member.save
- super(member)
- end"
-
- instance_eval "
- def collection.clear
- self.each{|member| delete(member)}
- super
- end"
-
- collection
+ result = o[:klass].constantize.find(:all,
+ :params => { o[:association_col] => id }) || []
+
+ set_resource_instance_variable(resource) {
+ AssociationCollection.new result, self, o[:association_col]
+ }
end
define_method("#{resource}=") do |new_collection|

0 comments on commit b13fe47

Please sign in to comment.