Class: Decidim::Admin::NewslettersController

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

Overview

Controller that allows managing newsletters.

Instance Method Summary collapse

Methods included from NewslettersHelper

#newsletter_attention_callout_announcement, #newsletter_recipients_count_callout_announcement, #organization_participatory_space, #participatory_space_types_form_object, #participatory_spaces_for_select, #select_tag_participatory_spaces, #selective_newsletter_to, #sent_to_scopes, #sent_to_spaces, #sent_to_users, #space_as_option_for_select, #spaces_for_select, #spaces_user_can_admin

Methods inherited from ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

Instance Method Details

#createObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 45

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) 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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 116

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

  DeliverNewsletter.call(newsletter, @form) 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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 88

def destroy
  enforce_permission_to(:destroy, :newsletter, newsletter:)

  DestroyNewsletter.call(newsletter, current_user) do
    on(:invalid) 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



64
65
66
67
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 64

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

#indexObject



12
13
14
15
16
# File '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 '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



37
38
39
40
41
42
43
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 37

def preview
  enforce_permission_to(:read, :newsletter, newsletter:)

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

#recipients_countObject



110
111
112
113
114
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 110

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



104
105
106
107
108
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 104

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

#send_to_userObject



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

def send_to_user
  enforce_permission_to :read, :newsletter, newsletter:

  NewsletterMailer.newsletter(current_user, newsletter).deliver_later
  flash[:notice] = I18n.t("newsletters.send_to_user.sent_successfully", scope: "decidim.admin", email: current_user.email)

  redirect_back fallback_location: newsletters_path
end

#showObject



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

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

#updateObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/decidim/admin/newsletters_controller.rb', line 69

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

  UpdateNewsletter.call(newsletter, @form) 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