Skip to content

Instantly share code, notes, and snippets.

@JasonSpatial
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JasonSpatial/b0b80445c9d3ea3b3f22 to your computer and use it in GitHub Desktop.
Save JasonSpatial/b0b80445c9d3ea3b3f22 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
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment