Skip to content

Commit

Permalink
Merge pull request #1768 from AyuntamientoMadrid/crud_budget_and_usa
Browse files Browse the repository at this point in the history
[Upstream] New CRUD for budget groups and headings
  • Loading branch information
microweb10 authored Dec 28, 2018
2 parents 1b92dd3 + 6acbe41 commit ee99157
Show file tree
Hide file tree
Showing 71 changed files with 1,427 additions and 557 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/map.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ App.Map =
openMarkerPopup = (e) ->
marker = e.target

$.ajax 'investments/' + marker.options['id'] + '/json_data',
$.ajax '/investments/' + marker.options['id'] + '/json_data',
type: 'GET'
dataType: 'json'
success: (data) ->
Expand Down
7 changes: 7 additions & 0 deletions app/assets/stylesheets/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,13 @@ footer {
}
}

.sidebar-map {

.map {
z-index: 0;
}
}

.sidebar-title {
border-top: 2px solid $brand;
display: inline-block;
Expand Down
52 changes: 44 additions & 8 deletions app/controllers/admin/budget_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,61 @@ 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.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
@group = @budget.groups.by_slug(params[:id]).first
@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

private

def budget_group_params
params.require(:budget_group).permit(:name, :max_votable_headings, :max_supportable_headings)
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: params[:budget_id]) || Budget.find_by(id: params[:budget_id])
end

def load_group
@group = @budget.groups.find_by(slug: params[:id]) || @budget.groups.find_by(id: 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, :max_supportable_headings)
end

end
58 changes: 43 additions & 15 deletions app/controllers/admin/budget_headings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,69 @@ class Admin::BudgetHeadingsController < Admin::BaseController

before_action :load_budget
before_action :load_group
before_action :load_heading
before_action :load_heading, except: [:index, :new, :create]

def create
@budget_group.headings.create(budget_heading_params)
@headings = @budget_group.headings
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
@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.destroy
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 budget_heading_params
params.require(:budget_heading).permit(:name, :price, :population)
end

def load_budget
@budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id])
@budget = Budget.find_by(slug: params[:budget_id])
@budget ||= Budget.find_by(id: params[:budget_id])
end

def load_group
@budget_group = @budget.groups.find_by(slug: params[:budget_group_id]) || @budget.groups.find_by(id: params[:budget_group_id])
@group = @budget.groups.find_by(slug: params[:group_id])
@group ||= @budget.groups.find_by(id: params[:group_id])
end

def load_heading
@heading = @budget_group.headings.find_by(slug: params[:id]) || @budget_group.headings.find_by(id: params[:id])
@heading = @group.headings.find_by(slug: params[:id])
@heading ||= @group.headings.find_by(id: 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

end
6 changes: 4 additions & 2 deletions app/controllers/admin/budgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def index
def show
end

def new; end
def new
end

def edit; end
def edit
end

def calculate_winners
return unless @budget.balloting_process?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
class Admin::SiteCustomization::ContentBlocksController < Admin::SiteCustomization::BaseController
load_and_authorize_resource :content_block, class: "SiteCustomization::ContentBlock"
load_and_authorize_resource :content_block, class: "SiteCustomization::ContentBlock",
except: [
:delete_heading_content_block,
:edit_heading_content_block,
:update_heading_content_block
]

def index
@content_blocks = SiteCustomization::ContentBlock.order(:name, :locale)
@headings_content_blocks = Budget::ContentBlock.all
end

def create
if @content_block.save
if is_heading_content_block?(@content_block.name)
heading_content_block = new_heading_content_block
if heading_content_block.save
notice = t('admin.site_customization.content_blocks.create.notice')
redirect_to admin_site_customization_content_blocks_path, notice: notice
else
flash.now[:error] = t('admin.site_customization.content_blocks.create.error')
render :new
end
elsif @content_block.save
notice = t('admin.site_customization.content_blocks.create.notice')
redirect_to admin_site_customization_content_blocks_path, notice: notice
else
Expand All @@ -15,8 +30,26 @@ def create
end
end

def edit
if @content_block.is_a? SiteCustomization::ContentBlock
@selected_content_block = @content_block.name
else
@selected_content_block = "hcb_#{@content_block.heading_id}"
end
end

def update
if @content_block.update(content_block_params)
if is_heading_content_block?(params[:site_customization_content_block][:name])
heading_content_block = new_heading_content_block
if heading_content_block.save
@content_block.destroy
notice = t('admin.site_customization.content_blocks.create.notice')
redirect_to admin_site_customization_content_blocks_path, notice: notice
else
flash.now[:error] = t('admin.site_customization.content_blocks.create.error')
render :new
end
elsif @content_block.update(content_block_params)
notice = t('admin.site_customization.content_blocks.update.notice')
redirect_to admin_site_customization_content_blocks_path, notice: notice
else
Expand All @@ -31,6 +64,52 @@ def destroy
redirect_to admin_site_customization_content_blocks_path, notice: notice
end

def delete_heading_content_block
heading_content_block = Budget::ContentBlock.find(params[:id])
heading_content_block.destroy if heading_content_block
notice = t('admin.site_customization.content_blocks.destroy.notice')
redirect_to admin_site_customization_content_blocks_path, notice: notice
end

def edit_heading_content_block
@content_block = Budget::ContentBlock.find(params[:id])
if @content_block.is_a? Budget::ContentBlock
@selected_content_block = "hcb_#{@content_block.heading_id}"
else
@selected_content_block = @content_block.heading.name
end
@is_heading_content_block = true
render :edit
end

def update_heading_content_block
heading_content_block = Budget::ContentBlock.find(params[:id])
if is_heading_content_block?(params[:name])
heading_content_block.locale = params[:locale]
heading_content_block.body = params[:body]
if heading_content_block.save
notice = t('admin.site_customization.content_blocks.update.notice')
redirect_to admin_site_customization_content_blocks_path, notice: notice
else
flash.now[:error] = t('admin.site_customization.content_blocks.update.error')
render :edit
end
else
@content_block = SiteCustomization::ContentBlock.new
@content_block.name = params[:name]
@content_block.locale = params[:locale]
@content_block.body = params[:body]
if @content_block.save
heading_content_block.destroy
notice = t('admin.site_customization.content_blocks.update.notice')
redirect_to admin_site_customization_content_blocks_path, notice: notice
else
flash.now[:error] = t('admin.site_customization.content_blocks.update.error')
render :edit
end
end
end

private

def content_block_params
Expand All @@ -40,4 +119,17 @@ def content_block_params
:body
)
end

def is_heading_content_block?(name)
name.start_with?('hcb_')
end

def new_heading_content_block
heading_content_block = Budget::ContentBlock.new
heading_content_block.body = params[:site_customization_content_block][:body]
heading_content_block.locale = params[:site_customization_content_block][:locale]
block_heading_id = params[:site_customization_content_block][:name].sub('hcb_', '').to_i
heading_content_block.heading_id = block_heading_id
heading_content_block
end
end
10 changes: 10 additions & 0 deletions app/controllers/budgets/ballot/lines_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class LinesController < ApplicationController
def create
load_investment
load_heading
load_map

@ballot.add_investment(@investment)
end

def destroy
@investment = @line.investment
load_heading
load_map

@line.destroy
load_investments
Expand Down Expand Up @@ -69,6 +71,14 @@ def load_ballot_referer
@ballot_referer = session[:ballot_referer]
end

def load_map
@investments ||= []
@investments_map_coordinates = MapLocation.where(investment: @investments).map do |loc|
loc.json_data
end
@map_location = MapLocation.load_from_heading(@heading)
end

end
end
end
Loading

0 comments on commit ee99157

Please sign in to comment.