Class: Decidim::Plans::PlansController

Inherits:
ApplicationController show all
Includes:
AttachedProposalsHelper, Orderable, FilterResource, FormFactory, Paginable
Defined in:
app/controllers/decidim/plans/plans_controller.rb

Overview

Exposes the plan resource so users can view and create them.

Direct Known Subclasses

PlanCollaboratorRequestsController

Instance Method Summary collapse

Methods included from AttachedProposalsHelper

#attached_proposals_picker_field, #search_proposals

Methods inherited from ApplicationController

#plan_limit, #plan_limit_reached?, #plans

Instance Method Details

#closeObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/decidim/plans/plans_controller.rb', line 147

def close
  enforce_permission_to :close, :plan, plan: @plan

  ClosePlan.call(@plan, current_user) do
    on(:ok) do |plan|
      flash[:notice] = I18n.t("close.success", scope: "decidim.plans.plans.plan")
      redirect_to Decidim::ResourceLocatorPresenter.new(plan).path
    end

    on(:invalid) do
      flash.now[:alert] = t("close.error", scope: "decidim.plans.plans.plan")
      redirect_to Decidim::ResourceLocatorPresenter.new(@plan).path
    end
  end
end

#createObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/decidim/plans/plans_controller.rb', line 50

def create
  enforce_permission_to :create, :plan

  @form = form(PlanForm).from_params(params, component: current_component)

  CreatePlan.call(@form, current_user) do
    on(:ok) do |plan|
      flash[:notice] = I18n.t("plans.plans.create.success", scope: "decidim")
      redirect_to Decidim::ResourceLocatorPresenter.new(plan).path + "/preview"
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("plans.plans.create.error", scope: "decidim")
      render :new
    end
  end
end

#destroyObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/decidim/plans/plans_controller.rb', line 94

def destroy
  enforce_permission_to :edit, :plan, plan: @plan

  # Form needed in case destroing fails
  @form = form(PlanForm).from_model(@plan)

  DestroyPlan.call(@plan, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("plans.plans.destroy.success", scope: "decidim")
      redirect_to new_plan_path
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("plans.plans.destroy.error", scope: "decidim")
      render :edit
    end
  end
end

#editObject



68
69
70
71
72
# File 'app/controllers/decidim/plans/plans_controller.rb', line 68

def edit
  enforce_permission_to :edit, :plan, plan: @plan

  @form = form(PlanForm).from_model(@plan)
end

#indexObject



25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/decidim/plans/plans_controller.rb', line 25

def index
  @plans = search
           .results
           .published
           .not_hidden
           .includes(:category)
           .includes(:scope)

  @plans = paginate(@plans)
  @plans = reorder(@plans)
end

#newObject



44
45
46
47
48
# File 'app/controllers/decidim/plans/plans_controller.rb', line 44

def new
  enforce_permission_to :create, :plan

  @form = form(PlanForm).from_model(Plan.new(component: current_component))
end

#previewObject



129
# File 'app/controllers/decidim/plans/plans_controller.rb', line 129

def preview; end

#publishObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/decidim/plans/plans_controller.rb', line 131

def publish
  enforce_permission_to :publish, :plan, plan: @plan

  PublishPlan.call(@plan, current_user) do
    on(:ok) do |plan|
      flash[:notice] = I18n.t("publish.success", scope: "decidim.plans.plans.plan")
      redirect_to Decidim::ResourceLocatorPresenter.new(plan).path
    end

    on(:invalid) do
      flash.now[:alert] = t("publish.error", scope: "decidim.plans.plans.plan")
      redirect_to Decidim::ResourceLocatorPresenter.new(@plan).path
    end
  end
end

#showObject



37
38
39
40
41
42
# File 'app/controllers/decidim/plans/plans_controller.rb', line 37

def show
  @report_form = form(Decidim::ReportForm).from_params(reason: "spam")
  @request_access_form = form(RequestAccessToPlanForm).from_params({})
  @accept_request_form = form(AcceptAccessToPlanForm).from_params({})
  @reject_request_form = form(RejectAccessToPlanForm).from_params({})
end

#updateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/decidim/plans/plans_controller.rb', line 74

def update
  enforce_permission_to :edit, :plan, plan: @plan

  @form = form(PlanForm).from_params(params, compontent: current_component)
  UpdatePlan.call(@form, current_user, @plan) do
    on(:ok) do |plan|
      flash[:notice] = I18n.t("plans.plans.update.success", scope: "decidim")

      return redirect_to Decidim::ResourceLocatorPresenter.new(plan).path if plan.published?

      redirect_to Decidim::ResourceLocatorPresenter.new(plan).path + "/preview"
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("plans.plans.update.error", scope: "decidim")
      render :edit
    end
  end
end

#withdrawObject

Raises:

  • (ActionController::RoutingError)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/decidim/plans/plans_controller.rb', line 113

def withdraw
  raise ActionController::RoutingError, "Not Found" if @plan.withdrawn?
  enforce_permission_to :withdraw, :plan, plan: @plan

  WithdrawPlan.call(@plan, current_user) do
    on(:ok) do
      flash[:notice] = t("withdraw.success", scope: "decidim.plans.plans")
    end

    on(:invalid) do
      flash.now[:alert] = t("withdraw.error", scope: "decidim.plans.plans")
    end
  end
  redirect_to Decidim::ResourceLocatorPresenter.new(@plan).path
end