Class: Decidim::Pages::CopyPage

Inherits:
Command
  • Object
show all
Defined in:
decidim-pages/app/commands/decidim/pages/copy_page.rb

Overview

Command that gets called whenever a component’s page has to be duplicated. It is need a context with the old component that is going to be duplicated on the new one

Instance Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

Constructor Details

#initialize(context) ⇒ CopyPage

Returns a new instance of CopyPage.



9
10
11
# File 'decidim-pages/app/commands/decidim/pages/copy_page.rb', line 9

def initialize(context)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
# File 'decidim-pages/app/commands/decidim/pages/copy_page.rb', line 13

def call
  Decidim::Pages::Page.transaction do
    pages = Decidim::Pages::Page.where(component: @context[:old_component])
    pages.each do |page|
      Decidim::Pages::Page.create!(component: @context[:new_component], body: page.body)
    end
  end
  broadcast(:ok)
rescue ActiveRecord::RecordInvalid
  broadcast(:invalid)
end