Merge pull request #4531 from consul/budget-steps

Split budget creation in steps
This commit is contained in:
Javi Martín
2021-06-09 13:48:28 +02:00
committed by GitHub
104 changed files with 1850 additions and 500 deletions

View File

@@ -1,64 +1,22 @@
class Admin::BudgetGroupsController < Admin::BaseController
include Translatable
include FeatureFlags
feature_flag :budgets
include Admin::BudgetGroupsActions
before_action :load_budget
before_action :load_group, except: [:index, :new, :create]
before_action :load_groups, only: :index
def index
@groups = @budget.groups.order(:id)
end
def new
@group = @budget.groups.new
end
def edit
end
def create
@group = @budget.groups.new(budget_group_params)
if @group.save
redirect_to groups_index, notice: t("admin.budget_groups.create.notice")
else
render :new
end
end
def update
if @group.update(budget_group_params)
redirect_to groups_index, notice: t("admin.budget_groups.update.notice")
else
render :edit
end
end
def destroy
if @group.headings.any?
redirect_to groups_index, alert: t("admin.budget_groups.destroy.unable_notice")
else
@group.destroy!
redirect_to groups_index, notice: t("admin.budget_groups.destroy.success_notice")
end
end
private
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_group
@group = @budget.groups.find_by_slug_or_id! params[:id]
end
def groups_index
admin_budget_groups_path(@budget)
end
def budget_group_params
valid_attributes = [:max_votable_headings]
params.require(:budget_group).permit(*valid_attributes, translation_params(Budget::Group))
def new_action
:new
end
end

View File

@@ -1,69 +1,20 @@
class Admin::BudgetHeadingsController < Admin::BaseController
include Translatable
include FeatureFlags
feature_flag :budgets
before_action :load_budget
before_action :load_group
before_action :load_heading, except: [:index, :new, :create]
include Admin::BudgetHeadingsActions
def index
@headings = @group.headings.order(:id)
end
def new
@heading = @group.headings.new
end
def edit
end
def create
@heading = @group.headings.new(budget_heading_params)
if @heading.save
redirect_to headings_index, notice: t("admin.budget_headings.create.notice")
else
render :new
end
end
def update
if @heading.update(budget_heading_params)
redirect_to headings_index, notice: t("admin.budget_headings.update.notice")
else
render :edit
end
end
def destroy
if @heading.can_be_deleted?
@heading.destroy!
redirect_to headings_index, notice: t("admin.budget_headings.destroy.success_notice")
else
redirect_to headings_index, alert: t("admin.budget_headings.destroy.unable_notice")
end
end
private
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_group
@group = @budget.groups.find_by_slug_or_id! params[:group_id]
end
def load_heading
@heading = @group.headings.find_by_slug_or_id! params[:id]
end
def headings_index
admin_budget_group_headings_path(@budget, @group)
end
def budget_heading_params
valid_attributes = [:price, :population, :allow_custom_content, :latitude, :longitude, :max_ballot_lines]
params.require(:budget_heading).permit(*valid_attributes, translation_params(Budget::Heading))
def new_action
:new
end
end

View File

@@ -1,28 +1,9 @@
class Admin::BudgetPhasesController < Admin::BaseController
include Translatable
before_action :load_phase, only: [:edit, :update]
def edit
end
def update
if @phase.update(budget_phase_params)
notice = t("flash.actions.save_changes.notice")
redirect_to edit_admin_budget_path(@phase.budget), notice: notice
else
render :edit
end
end
include Admin::BudgetPhasesActions
private
def load_phase
@phase = Budget::Phase.find(params[:id])
end
def budget_phase_params
valid_attributes = [:starts_at, :ends_at, :enabled]
params.require(:budget_phase).permit(*valid_attributes, translation_params(Budget::Phase))
def phases_index
edit_admin_budget_path(@phase.budget)
end
end

View File

@@ -6,8 +6,7 @@ class Admin::BudgetsController < Admin::BaseController
has_filters %w[all open finished], only: :index
before_action :load_budget, except: [:index, :new, :create]
before_action :load_staff, only: [:new, :create, :edit, :update, :show]
before_action :load_budget, except: [:index]
load_and_authorize_resource
def index
@@ -18,9 +17,6 @@ class Admin::BudgetsController < Admin::BaseController
render :edit
end
def new
end
def edit
end
@@ -47,15 +43,6 @@ class Admin::BudgetsController < Admin::BaseController
end
end
def create
@budget = Budget.new(budget_params.merge(published: false))
if @budget.save
redirect_to edit_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")
@@ -83,9 +70,4 @@ class Admin::BudgetsController < Admin::BaseController
def load_budget
@budget = Budget.find_by_slug_or_id! params[:id]
end
def load_staff
@admins = Administrator.includes(:user)
@valuators = Valuator.includes(:user).order(description: :asc).order("users.email ASC")
end
end

View File

@@ -0,0 +1,47 @@
class Admin::BudgetsWizard::BudgetsController < Admin::BaseController
include Translatable
include FeatureFlags
feature_flag :budgets
load_and_authorize_resource
def new
end
def edit
end
def create
@budget.published = false
if @budget.save
redirect_to groups_index, notice: t("admin.budgets.create.notice")
else
render :new
end
end
def update
if @budget.update(budget_params)
redirect_to groups_index, notice: t("admin.budgets.update.notice")
else
render :edit
end
end
private
def budget_params
params.require(:budget).permit(*allowed_params)
end
def allowed_params
valid_attributes = [:currency_symbol, :voting_style, administrator_ids: [], valuator_ids: []]
valid_attributes + [translation_params(Budget)]
end
def groups_index
admin_budgets_wizard_budget_groups_path(@budget)
end
end

View File

@@ -0,0 +1,19 @@
class Admin::BudgetsWizard::GroupsController < Admin::BaseController
include Admin::BudgetGroupsActions
before_action :load_groups, only: [:index, :create]
def index
@group = @budget.groups.new
end
private
def groups_index
admin_budgets_wizard_budget_groups_path(@budget)
end
def new_action
:index
end
end

View File

@@ -0,0 +1,19 @@
class Admin::BudgetsWizard::HeadingsController < Admin::BaseController
include Admin::BudgetHeadingsActions
before_action :load_headings, only: [:index, :create]
def index
@heading = @group.headings.new
end
private
def headings_index
admin_budgets_wizard_budget_group_headings_path(@budget, @group)
end
def new_action
:index
end
end

View File

@@ -0,0 +1,26 @@
class Admin::BudgetsWizard::PhasesController < Admin::BaseController
include Admin::BudgetPhasesActions
def index
end
def update_all
@budget.update!(phases_params)
redirect_to admin_budgets_path, notice: t("admin.budgets_wizard.phases.update_all.notice")
end
private
def phases_index
admin_budgets_wizard_budget_budget_phases_path(@phase.budget)
end
def phases_params
params.require(:budget).permit(allowed_phases_params)
end
def allowed_phases_params
{ phases_attributes: [:id, :enabled] }
end
end

View File

@@ -0,0 +1,60 @@
module Admin::BudgetGroupsActions
extend ActiveSupport::Concern
included do
include Translatable
include FeatureFlags
feature_flag :budgets
before_action :load_budget
before_action :load_group, only: [:edit, :update, :destroy]
end
def edit
end
def create
@group = @budget.groups.new(budget_group_params)
if @group.save
redirect_to groups_index, notice: t("admin.budget_groups.create.notice")
else
render new_action
end
end
def update
if @group.update(budget_group_params)
redirect_to groups_index, notice: t("admin.budget_groups.update.notice")
else
render :edit
end
end
def destroy
if @group.headings.any?
redirect_to groups_index, alert: t("admin.budget_groups.destroy.unable_notice")
else
@group.destroy!
redirect_to groups_index, notice: t("admin.budget_groups.destroy.success_notice")
end
end
private
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_groups
@groups = @budget.groups.order(:id)
end
def load_group
@group = @budget.groups.find_by_slug_or_id! params[:id]
end
def budget_group_params
valid_attributes = [:max_votable_headings]
params.require(:budget_group).permit(*valid_attributes, translation_params(Budget::Group))
end
end

View File

@@ -0,0 +1,66 @@
module Admin::BudgetHeadingsActions
extend ActiveSupport::Concern
included do
include Translatable
include FeatureFlags
feature_flag :budgets
before_action :load_budget
before_action :load_group
before_action :load_headings, only: :index
before_action :load_heading, only: [:edit, :update, :destroy]
end
def edit
end
def create
@heading = @group.headings.new(budget_heading_params)
if @heading.save
redirect_to headings_index, notice: t("admin.budget_headings.create.notice")
else
render new_action
end
end
def update
if @heading.update(budget_heading_params)
redirect_to headings_index, notice: t("admin.budget_headings.update.notice")
else
render :edit
end
end
def destroy
if @heading.can_be_deleted?
@heading.destroy!
redirect_to headings_index, notice: t("admin.budget_headings.destroy.success_notice")
else
redirect_to headings_index, alert: t("admin.budget_headings.destroy.unable_notice")
end
end
private
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_group
@group = @budget.groups.find_by_slug_or_id! params[:group_id]
end
def load_headings
@headings = @group.headings.order(:id)
end
def load_heading
@heading = @group.headings.find_by_slug_or_id! params[:id]
end
def budget_heading_params
valid_attributes = [:price, :population, :allow_custom_content, :latitude, :longitude, :max_ballot_lines]
params.require(:budget_heading).permit(*valid_attributes, translation_params(Budget::Heading))
end
end

View File

@@ -0,0 +1,36 @@
module Admin::BudgetPhasesActions
extend ActiveSupport::Concern
included do
include Translatable
before_action :load_budget
before_action :load_phase, only: [:edit, :update]
end
def edit
end
def update
if @phase.update(budget_phase_params)
redirect_to phases_index, notice: t("flash.actions.save_changes.notice")
else
render :edit
end
end
private
def load_budget
@budget = Budget.find_by_slug_or_id!(params[:budget_id])
end
def load_phase
@phase = @budget.phases.find(params[:id])
end
def budget_phase_params
valid_attributes = [:starts_at, :ends_at, :enabled]
params.require(:budget_phase).permit(*valid_attributes, translation_params(Budget::Phase))
end
end