-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
budgets_controller.rb
74 lines (60 loc) · 1.95 KB
/
budgets_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Admin::BudgetsController < Admin::BaseController
include Translatable
include ReportAttributes
include FeatureFlags
feature_flag :budgets
has_filters %w{open finished}, only: :index
before_action :load_budget, except: [:index, :new, :create]
load_and_authorize_resource
def index
@budgets = Budget.send(@current_filter).order(created_at: :desc).page(params[:page])
end
def show
end
def new
end
def edit
end
def calculate_winners
return unless @budget.balloting_process?
@budget.headings.each { |heading| Budget::Result.new(@budget, heading).delay.calculate_winners }
redirect_to admin_budget_budget_investments_path(
budget_id: @budget.id,
advanced_filters: ["winners"]),
notice: I18n.t("admin.budgets.winners.calculated")
end
def update
if @budget.update(budget_params)
redirect_to admin_budgets_path, notice: t("admin.budgets.update.notice")
else
render :edit
end
end
def create
@budget = Budget.new(budget_params)
if @budget.save
redirect_to admin_budget_path(@budget), notice: t("admin.budgets.create.notice")
else
render :new
end
end
def destroy
if @budget.investments.any?
redirect_to admin_budgets_path, alert: t("admin.budgets.destroy.unable_notice")
elsif @budget.poll.present?
redirect_to admin_budgets_path, alert: t("admin.budgets.destroy.unable_notice_polls")
else
@budget.destroy
redirect_to admin_budgets_path, notice: t("admin.budgets.destroy.success_notice")
end
end
private
def budget_params
descriptions = Budget::Phase::PHASE_KINDS.map{|p| "description_#{p}"}.map(&:to_sym)
valid_attributes = [:phase, :currency_symbol] + descriptions
params.require(:budget).permit(*valid_attributes, *report_attributes, translation_params(Budget))
end
def load_budget
@budget = Budget.find_by_slug_or_id! params[:id]
end
end