Permalink
Browse files

New method store_elements

  • Loading branch information...
ferblape committed Aug 16, 2009
1 parent 69eec18 commit 4af3666de2ed8a6c9176c90705a2d0b5dc30fcd6
Showing with 23 additions and 1 deletion.
  1. +3 −1 lib/active_record_collection.rb
  2. +20 −0 lib/collection.rb
@@ -8,7 +8,9 @@ def self.included(base) # :nodoc:
module ClassMethods
def has_collection( collection_name, options = {} )
- define_method collection_name.to_sym do
+ args = []
+ args << options[:args].to_sym if options[:args]
+ define_method collection_name.to_sym do |*args|
eval(collection_name.to_s.camelize.constantize.builder(options))
end
end
View
@@ -86,6 +86,26 @@ def store_element( element )
end
self.save
end
+
+ def store_elements( elements )
+ sort_required = false
+ elements.each do |element|
+ if position = self.index( element.id )
+ if self.elements[position].values.first != element.send(self.order_by_attribute)
+ self.elements[position][element.id] = element.send(self.order_by_attribute)
+ sort_required = true
+ end
+ else
+ self.elements << {element.id => element.send(self.order_by_attribute)}
+ self.total_elements += 1
+ sort_required = true
+ end
+ end
+ if sort_required
+ self.reorder!
+ end
+ self.save
+ end
# options sólo admite:
# - :limit

0 comments on commit 4af3666

Please sign in to comment.