Skip to content

Instantly share code, notes, and snippets.

@jasononaquest
Last active August 29, 2015 14:18
Show Gist options
  • Save jasononaquest/c34f194674de5ad3f265 to your computer and use it in GitHub Desktop.
Save jasononaquest/c34f194674de5ad3f265 to your computer and use it in GitHub Desktop.
copy_list.rb
class CopyList
attr_reader :list, :new_name
def initialize(list, new_name)
@list = list
@new_name = new_name
end
def call
new_list = List.new(list.dup.attributes.merge(name: new_name))
copy_categories(list, new_list)
new_list.save
new_list
end
private
def copy_categories(list, new_list)
list.categories.each do |category|
new_category = Category.new(category.dup.attributes)
new_list.categories << new_category
copy_items(category, new_category)
end
end
def copy_items(category, new_category)
category.items.each do |item|
new_category.items << Item.new(item.dup.attributes)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment