Class: Decidim::EnhancedTextwork::Admin::ParticipatoryTextsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb

Overview

This controller manages the participatory texts area.

Instance Method Summary collapse

Instance Method Details

#discardObject

Removes all the unpublished paragraphs (drafts).



115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb', line 115

def discard
  enforce_permission_to :manage, :participatory_texts

  DiscardParticipatoryText.call(current_component) do
    on(:ok) do
      flash[:notice] = I18n.t("participatory_texts.discard.success", scope: "decidim.enhanced_textwork.admin")
      redirect_to EngineRouter.admin_proxy(current_component).participatory_texts_path
    end
  end
end

#importObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb', line 23

def import
  enforce_permission_to :manage, :participatory_texts
  @import = form(Admin::ImportParticipatoryTextForm).from_params(params)

  Admin::ImportParticipatoryText.call(@import) do
    on(:ok) do
      flash[:notice] = I18n.t("participatory_texts.import.success", scope: "decidim.enhanced_textwork.admin")
      redirect_to EngineRouter.admin_proxy(current_component).participatory_texts_path
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("participatory_texts.import.invalid", scope: "decidim.enhanced_textwork.admin")
      render action: "new_import"
    end

    on(:invalid_file) do
      flash.now[:alert] = I18n.t("participatory_texts.import.invalid_file", scope: "decidim.enhanced_textwork.admin")
      render action: "new_import"
    end
  end
end

#import_from_editorObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb', line 51

def import_from_editor
  enforce_permission_to :manage, :participatory_texts
  @import = form(Admin::ImportEditorParticipatoryTextForm).from_params(params)

  Admin::ImportFromEditorParticipatoryText.call(@import) do
    on(:ok) do
      flash[:notice] = I18n.t("participatory_texts.import.success", scope: "decidim.enhanced_textwork.admin")
      redirect_to EngineRouter.admin_proxy(current_component).participatory_texts_path
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("participatory_texts.import.invalid", scope: "decidim.enhanced_textwork.admin")
      render action: "new_editor"
    end

    on(:invalid_file) do
      flash.now[:alert] = I18n.t("participatory_texts.import.invalid_file", scope: "decidim.enhanced_textwork.admin")
      render action: "new_editor"
    end
  end
end

#indexObject



11
12
13
14
15
# File 'app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb', line 11

def index
  @drafts = Paragraph.where(component: current_component).drafts.order(:position)
  @preview_form = form(Admin::PreviewParticipatoryTextForm).instance
  @preview_form.from_models(@drafts)
end

#new_editorObject



45
46
47
48
49
# File 'app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb', line 45

def new_editor
  enforce_permission_to :manage, :participatory_texts
  participatory_text = Decidim::EnhancedTextwork::ParticipatoryText.find_by(component: current_component)
  @import = form(Admin::ImportEditorParticipatoryTextForm).from_model(participatory_text)
end

#new_importObject



17
18
19
20
21
# File 'app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb', line 17

def new_import
  enforce_permission_to :manage, :participatory_texts
  participatory_text = Decidim::EnhancedTextwork::ParticipatoryText.find_by(component: current_component)
  @import = form(Admin::ImportParticipatoryTextForm).from_model(participatory_text)
end

#updateObject

When `save_draft` param exists, paragraphs are only saved. When no `save_draft` param is set, paragraphs are saved and published.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/decidim/enhanced_textwork/admin/participatory_texts_controller.rb', line 75

def update
  enforce_permission_to :manage, :participatory_texts

  form_params = params.require(:preview_participatory_text).permit!
  @preview_form = form(Admin::PreviewParticipatoryTextForm).from_params(paragraphs: form_params[:paragraphs_attributes]&.values)

  if params.has_key?("save_draft")
    UpdateParticipatoryText.call(@preview_form) do
      on(:ok) do
        flash[:notice] = I18n.t("participatory_texts.update.success", scope: "decidim.enhanced_textwork.admin")
        redirect_to EngineRouter.admin_proxy(current_component).participatory_texts_path
      end

      on(:invalid) do |failures|
        alert_msg = [I18n.t("participatory_texts.publish.invalid", scope: "decidim.enhanced_textwork.admin")]
        failures.each_pair { |id, msg| alert_msg << "ID:[#{id}] #{msg}" }
        flash.now[:alert] = alert_msg.join("<br/>").html_safe
        index
        render action: "index"
      end
    end
  else
    PublishParticipatoryText.call(@preview_form) do
      on(:ok) do
        flash[:notice] = I18n.t("participatory_texts.publish.success", scope: "decidim.enhanced_textwork.admin")
        redirect_to paragraphs_path
      end

      on(:invalid) do |failures|
        alert_msg = [I18n.t("participatory_texts.publish.invalid", scope: "decidim.enhanced_textwork.admin")]
        failures.each_pair { |id, msg| alert_msg << "ID:[#{id}] #{msg}" }
        flash.now[:alert] = alert_msg.join("<br/>").html_safe
        index
        render action: "index"
      end
    end
  end
end