Last active
August 29, 2015 14:18
-
-
Save jasononaquest/c34f194674de5ad3f265 to your computer and use it in GitHub Desktop.
copy_list.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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