Merge pull request #3106 from consul/2918-crud_budget_groups_headings
Change CRUD for budget groups and headings
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -17,6 +17,7 @@ module Budgets
|
||||
def create
|
||||
load_investment
|
||||
load_heading
|
||||
load_map
|
||||
|
||||
@ballot.add_investment(@investment)
|
||||
end
|
||||
@@ -24,6 +25,7 @@ module Budgets
|
||||
def destroy
|
||||
@investment = @line.investment
|
||||
load_heading
|
||||
load_map
|
||||
|
||||
@line.destroy
|
||||
load_investments
|
||||
@@ -74,6 +76,12 @@ module Budgets
|
||||
@ballot_referer = session[:ballot_referer]
|
||||
end
|
||||
|
||||
def load_map
|
||||
@investments ||= []
|
||||
@investments_map_coordinates = MapLocation.where(investment: @investments).map(&:json_data)
|
||||
@map_location = MapLocation.load_from_heading(@heading)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
module Budgets
|
||||
class InvestmentsController < ApplicationController
|
||||
OSM_DISTRICT_LEVEL_ZOOM = 12
|
||||
|
||||
include FeatureFlags
|
||||
include CommentableActions
|
||||
@@ -180,10 +179,7 @@ module Budgets
|
||||
end
|
||||
|
||||
def load_map
|
||||
@map_location = MapLocation.new
|
||||
@map_location.zoom = OSM_DISTRICT_LEVEL_ZOOM
|
||||
@map_location.latitude = @heading.latitude.to_f
|
||||
@map_location.longitude = @heading.longitude.to_f
|
||||
@map_location = MapLocation.load_from_heading(@heading)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user