Merge pull request #3106 from consul/2918-crud_budget_groups_headings

Change CRUD for budget groups and headings
This commit is contained in:
Julian Nicolas Herrero
2018-12-18 10:23:24 +01:00
committed by GitHub
50 changed files with 832 additions and 507 deletions

View File

@@ -2,20 +2,60 @@ class Admin::BudgetGroupsController < Admin::BaseController
include FeatureFlags
feature_flag :budgets
before_action :load_budget
before_action :load_group, except: [:index, :new, :create]
def index
@groups = @budget.groups.order(:id)
end
def new
@group = @budget.groups.new
end
def edit
end
def create
@budget = Budget.find(params[:budget_id])
@budget.groups.create(budget_group_params)
@groups = @budget.groups.includes(:headings)
@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
@budget = Budget.find(params[:budget_id])
@group = @budget.groups.find(params[:id])
@group.update(budget_group_params)
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.includes(:groups).find(params[:budget_id])
end
def load_group
@group = @budget.groups.find(params[:id])
end
def groups_index
admin_budget_groups_path(@budget)
end
def budget_group_params
params.require(:budget_group).permit(:name, :max_votable_headings)
end

View File

@@ -2,36 +2,65 @@ class Admin::BudgetHeadingsController < Admin::BaseController
include FeatureFlags
feature_flag :budgets
def create
@budget = Budget.find(params[:budget_id])
@budget_group = @budget.groups.find(params[:budget_group_id])
@budget_group.headings.create(budget_heading_params)
@headings = @budget_group.headings
before_action :load_budget
before_action :load_group
before_action :load_heading, except: [:index, :new, :create]
def index
@headings = @group.headings.order(:id)
end
def new
@heading = @group.headings.new
end
def edit
@budget = Budget.find(params[:budget_id])
@budget_group = @budget.groups.find(params[:budget_group_id])
@heading = Budget::Heading.find(params[:id])
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
@budget = Budget.find(params[:budget_id])
@budget_group = @budget.groups.find(params[:budget_group_id])
@heading = Budget::Heading.find(params[:id])
@heading.assign_attributes(budget_heading_params)
render :edit unless @heading.save
if @heading.update(budget_heading_params)
redirect_to headings_index, notice: t('admin.budget_headings.update.notice')
else
render :edit
end
end
def destroy
@heading = Budget::Heading.find(params[:id])
@heading.destroy
@budget = Budget.find(params[:budget_id])
redirect_to admin_budget_path(@budget)
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.includes(:groups).find(params[:budget_id])
end
def load_group
@group = @budget.groups.find(params[:group_id])
end
def load_heading
@heading = @group.headings.find(params[:id])
end
def headings_index
admin_budget_group_headings_path(@budget, @group)
end
def budget_heading_params
params.require(:budget_heading).permit(:name, :price, :population, :allow_custom_content, :latitude, :longitude)
end

View File

@@ -11,12 +11,13 @@ class Admin::BudgetsController < Admin::BaseController
end
def show
@budget = Budget.includes(groups: :headings).find(params[:id])
end
def new; end
def new
end
def edit; end
def edit
end
def calculate_winners
return unless @budget.balloting_process?