Class: Decidim::Pages::CopyPage

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ CopyPage

Returns a new instance of CopyPage.



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

def initialize(context)
  @context = context
end

Instance Method Details

#callObject



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

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