Skip to content

Instantly share code, notes, and snippets.

@JasonSpatial
Created April 6, 2015 23:49
Show Gist options
  • Save JasonSpatial/f095b9f784f7eac3227f to your computer and use it in GitHub Desktop.
Save JasonSpatial/f095b9f784f7eac3227f to your computer and use it in GitHub Desktop.
List#copy
def self.copy(lists, new_list_name, current_user)
new_list = current_user.lists.create(:name => new_list_name)
lists.each do |list|
list.categories.find(:all, :conditions=>["categories.deleted_at is null or categories.deleted_at > '#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}'"]).each do |category|
new_category = new_list.categories.create(:name => category.name,
:user_id => category.user_id,
:position => category.position)
category.items.find(:all, :conditions=>["items.deleted_at is null or items.deleted_at > '#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}'"]).each do |item|
new_category.items.create(:name => item.name,
:description => item.description,
:quantity => item.quantity,
:size => item.size,
:price => item.price,
:position => item.position,
:list_id => new_list.id,
:user_id => item.user_id)
end
end
end
new_list
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment