Class: Decidim::Admin::NewslettersController

Inherits:
ApplicationController show all
Includes:
NewslettersHelper, Paginable, NewslettersHelper
Defined in:
decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb

Overview

Controller that allows managing newsletters.

Instance Method Summary collapse

Methods included from NewslettersHelper

#custom_url_for_mail_root, #parse_interpolations, #utm_codes

Methods inherited from ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

Methods included from HttpCachingDisabler

#disable_http_caching

Methods included from NeedsSnippets

#snippets

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 36

def create
  enforce_permission_to :create, :newsletter
  @form = form(NewsletterForm).from_params(params)
  @form.images = images_block_context unless has_images_block_context?

  CreateNewsletter.call(@form, content_block, current_user) do
    on(:ok) do |newsletter|
      flash.now[:notice] = I18n.t("newsletters.create.success", scope: "decidim.admin")
      redirect_to action: :show, id: newsletter.id
    end

    on(:invalid) do |newsletter|
      @newsletter = newsletter
      flash.now[:error] = I18n.t("newsletters.create.error", scope: "decidim.admin")
      render action: :new
    end
  end
end

#deliverObject


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 107

def deliver
  enforce_permission_to :update, :newsletter, newsletter: newsletter
  @form = form(SelectiveNewsletterForm).from_params(params)

  DeliverNewsletter.call(newsletter, @form, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("newsletters.deliver.success", scope: "decidim.admin")
      redirect_to action: :index
    end

    on(:invalid) do
      flash.now[:error] = I18n.t("newsletters.deliver.error", scope: "decidim.admin")
      render action: :select_recipients_to_deliver
    end

    on(:no_recipients) do
      flash.now[:error] = I18n.t("newsletters.send.no_recipients", scope: "decidim.admin")
      render action: :select_recipients_to_deliver
    end
  end
end

#destroyObject


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 79

def destroy
  enforce_permission_to :destroy, :newsletter, newsletter: newsletter

  DestroyNewsletter.call(newsletter, current_user) do
    on(:already_sent) do
      flash.now[:error] = I18n.t("newsletters.destroy.error_already_sent", scope: "decidim.admin")
      redirect_to :back
    end

    on(:ok) do
      flash[:notice] = I18n.t("newsletters.destroy.success", scope: "decidim.admin")
      redirect_to action: :index
    end
  end
end

#editObject


55
56
57
58
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 55

def edit
  enforce_permission_to :update, :newsletter, newsletter: newsletter
  @form = form(NewsletterForm).from_model(content_block)
end

#indexObject


12
13
14
15
16
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 12

def index
  enforce_permission_to :index, :newsletter
  @newsletters = collection.order(Newsletter.arel_table[:created_at].desc)
  @newsletters = paginate(@newsletters)
end

#newObject


18
19
20
21
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 18

def new
  enforce_permission_to :create, :newsletter
  @form = form(NewsletterForm).from_model(content_block)
end

#previewObject


28
29
30
31
32
33
34
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 28

def preview
  enforce_permission_to :read, :newsletter, newsletter: newsletter

  email = NewsletterMailer.newsletter(current_user, newsletter)
  Premailer::Rails::Hook.perform(email)
  render html: email.html_part.body.decoded.html_safe
end

#recipients_countObject


101
102
103
104
105
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 101

def recipients_count
  data = params.permit(data: {}).to_h[:data]
  @form = form(SelectiveNewsletterForm).from_params(data)
  render plain: recipients_count_query
end

#select_recipients_to_deliverObject


95
96
97
98
99
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 95

def select_recipients_to_deliver
  enforce_permission_to :update, :newsletter, newsletter: newsletter
  @form = form(SelectiveNewsletterForm).from_model(newsletter)
  @form.send_to_all_users = current_user.admin?
end

#showObject


23
24
25
26
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 23

def show
  enforce_permission_to :read, :newsletter, newsletter: newsletter
  @email = NewsletterMailer.newsletter(current_user, newsletter)
end

#updateObject


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'decidim-admin/app/controllers/decidim/admin/newsletters_controller.rb', line 60

def update
  enforce_permission_to :update, :newsletter, newsletter: newsletter
  @form = form(NewsletterForm).from_params(params)
  @form.images = images_block_context unless has_images_block_context?

  UpdateNewsletter.call(newsletter, @form, current_user) do
    on(:ok) do |newsletter|
      flash[:notice] = I18n.t("newsletters.update.success", scope: "decidim.admin")
      redirect_to action: :show, id: newsletter.id
    end

    on(:invalid) do |newsletter|
      @newsletter = newsletter
      flash.now[:error] = I18n.t("newsletters.update.error", scope: "decidim.admin")
      render action: :edit
    end
  end
end