Module: CopyMove::Model

Defined in:
lib/copy_move/model.rb

Instance Method Summary collapse

Instance Method Details

#copy_to(parent, status = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/copy_move/model.rb', line 23

def copy_to(parent, status = nil)
  parent.children.build(copiable_attributes.symbolize_keys.merge(new_slug_and_title_under(parent))).tap do |new_page|
    self.parts.each do |part|
      new_page.parts << part.clone
    end
    new_page.send :add_to_list_bottom if defined?(Reorder)
    new_page.status_id = status.blank? ? new_page.status_id : status
    new_page.save!
  end
end

#copy_tree_to(parent, status = nil) ⇒ Object



40
41
42
43
44
# File 'lib/copy_move/model.rb', line 40

def copy_tree_to(parent, status = nil)
  copy_to(parent, status).tap do |new_page|
    children.each {|child| child.copy_tree_to(new_page, status) }
  end
end

#copy_with_children_to(parent, status = nil) ⇒ Object



34
35
36
37
38
# File 'lib/copy_move/model.rb', line 34

def copy_with_children_to(parent, status = nil)
  copy_to(parent, status).tap do |new_page|
    children.each {|child| child.copy_to(new_page, status) }
  end
end

#move_under(parent, status = nil) ⇒ Object

Raises:



16
17
18
19
20
21
# File 'lib/copy_move/model.rb', line 16

def move_under(parent, status = nil)
  raise CircularHierarchy.new(self) if parent == self || parent.ancestors.include?(self)
  status_id = status.blank? ? self.status_id : status
  update_attributes!(:parent_id => parent.id, :status_id => status_id)
  assume_bottom_position if defined?(Reorder)
end

#new_slug_and_title_under(parent) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/copy_move/model.rb', line 3

def new_slug_and_title_under(parent)
  test_page = self.clone
  test_page.parent = parent
  until test_page.valid?
    index = (index || 0) + 1
    test_page.title = "#{title} Copy #{index}"
    test_page.slug = "#{slug}-copy-#{index}"
    test_page.breadcrumb = test_page.title
    test_page.errors.clear # Reset error status before revalidating
  end
  {:slug => test_page.slug, :title => test_page.title, :breadcrumb => test_page.breadcrumb}
end