Merge pull request #1917 from wairbut-m2c/iagirre-admin-budget-heading-action
Actions for budgets' headings added. Edit headings, delete headings i…
This commit is contained in:
@@ -3,12 +3,33 @@ class Admin::BudgetHeadingsController < Admin::BaseController
|
|||||||
feature_flag :budgets
|
feature_flag :budgets
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@budget = Budget.find params[:budget_id]
|
@budget = Budget.find(params[:budget_id])
|
||||||
@budget_group = @budget.groups.find params[:budget_group_id]
|
@budget_group = @budget.groups.find(params[:budget_group_id])
|
||||||
@budget_group.headings.create(budget_heading_params)
|
@budget_group.headings.create(budget_heading_params)
|
||||||
@headings = @budget_group.headings
|
@headings = @budget_group.headings
|
||||||
end
|
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 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
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@heading = Budget::Heading.find(params[:id])
|
||||||
|
@heading.destroy
|
||||||
|
@budget = Budget.find(params[:budget_id])
|
||||||
|
redirect_to admin_budget_path(@budget)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def budget_heading_params
|
def budget_heading_params
|
||||||
|
|||||||
@@ -21,7 +21,11 @@ class Budget
|
|||||||
end
|
end
|
||||||
|
|
||||||
def name_exists_in_budget_headings
|
def name_exists_in_budget_headings
|
||||||
group.budget.headings.where(name: name).any?
|
group.budget.headings.where(name: name).where.not(id: id).any?
|
||||||
|
end
|
||||||
|
|
||||||
|
def can_be_deleted?
|
||||||
|
investments.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
7
app/views/admin/budget_headings/_errors.html.erb
Normal file
7
app/views/admin/budget_headings/_errors.html.erb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<div id='error_explanation' class='callout alert'>
|
||||||
|
<ul>
|
||||||
|
<% errors.each do |error| %>
|
||||||
|
<li><%= error %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
1
app/views/admin/budget_headings/edit.js.erb
Normal file
1
app/views/admin/budget_headings/edit.js.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$("#heading-<%=@heading.id%>").html("<td colspan='4'><%= j render("admin/budgets/heading_form", group: @budget_group, budget: @budget, heading: @heading) %></td>");
|
||||||
1
app/views/admin/budget_headings/update.js.erb
Normal file
1
app/views/admin/budget_headings/update.js.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$("#<%= dom_id(@budget_group) %>").html('<%= j render("admin/budgets/group", group: @budget_group, headings: @budget_group.headings) %>');
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th colspan="3" class="with-button">
|
<th colspan="4" class="with-button">
|
||||||
<%= group.name %>
|
<%= group.name %>
|
||||||
<%= link_to t("admin.budgets.form.add_heading"), "#", class: "button float-right js-toggle-link", data: { "toggle-selector" => "#group-#{group.id}-new-heading-form" } %>
|
<%= link_to t("admin.budgets.form.add_heading"), "#", class: "button float-right js-toggle-link", data: { "toggle-selector" => "#group-#{group.id}-new-heading-form" } %>
|
||||||
</th>
|
</th>
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="3">
|
<td colspan="4">
|
||||||
<div class="callout primary">
|
<div class="callout primary">
|
||||||
<%= t("admin.budgets.form.no_heading") %>
|
<%= t("admin.budgets.form.no_heading") %>
|
||||||
</div>
|
</div>
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
<th><%= t("admin.budgets.form.table_heading") %></th>
|
<th><%= t("admin.budgets.form.table_heading") %></th>
|
||||||
<th><%= t("admin.budgets.form.table_amount") %></th>
|
<th><%= t("admin.budgets.form.table_amount") %></th>
|
||||||
<th><%= t("admin.budgets.form.table_population") %></th>
|
<th><%= t("admin.budgets.form.table_population") %></th>
|
||||||
|
<th><%= t("admin.actions.actions") %></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -29,58 +30,14 @@
|
|||||||
|
|
||||||
<!-- new heading form -->
|
<!-- new heading form -->
|
||||||
<tr id="group-<%= group.id %>-new-heading-form" style="display:none">
|
<tr id="group-<%= group.id %>-new-heading-form" style="display:none">
|
||||||
<td colspan="3">
|
<td colspan="4">
|
||||||
<%= form_for [:admin, @budget, group, Budget::Heading.new], remote: true do |f| %>
|
<%= render "admin/budgets/heading_form", group: group, budget: @budget, heading: Budget::Heading.new %>
|
||||||
<label><%= t("admin.budgets.form.heading") %></label>
|
|
||||||
<%= f.text_field :name,
|
|
||||||
label: false,
|
|
||||||
maxlength: 50,
|
|
||||||
placeholder: t("admin.budgets.form.heading") %>
|
|
||||||
|
|
||||||
<div class="row">
|
|
||||||
<div class="small-12 medium-6 column">
|
|
||||||
<label><%= t("admin.budgets.form.amount") %></label>
|
|
||||||
<%= f.text_field :price,
|
|
||||||
label: false,
|
|
||||||
maxlength: 8,
|
|
||||||
placeholder: t("admin.budgets.form.amount") %>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<div class="small-12 medium-6 column">
|
|
||||||
<label><%= t("admin.budgets.form.population") %></label>
|
|
||||||
<%= f.number_field :population,
|
|
||||||
label: false,
|
|
||||||
maxlength: 8,
|
|
||||||
min: 1,
|
|
||||||
placeholder: t("admin.budgets.form.population"),
|
|
||||||
data: {toggle_focus: "population-info"} %>
|
|
||||||
</div>
|
|
||||||
<div class="small-12 medium-6 column " >
|
|
||||||
<div id="population-info" class="is-hidden" data-toggler="is-hidden">
|
|
||||||
<%= t("admin.budgets.form.population_info") %>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<%= f.submit t("admin.budgets.form.save_heading"), class: "button success" %>
|
|
||||||
<% end %>
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<!-- /. new heading form -->
|
<!-- /. new heading form -->
|
||||||
<!-- headings list -->
|
<!-- headings list -->
|
||||||
<% headings.each do |heading| %>
|
<% headings.each do |heading| %>
|
||||||
<tr>
|
<%= render "admin/budgets/heading", group: group, budget: @budget, heading: heading %>
|
||||||
<td>
|
|
||||||
<%= heading.name %>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<%= heading.price %>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<%= heading.population %>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
<!-- /. headings list -->
|
<!-- /. headings list -->
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
27
app/views/admin/budgets/_heading.html.erb
Normal file
27
app/views/admin/budgets/_heading.html.erb
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<tr id="heading-<%=heading.id%>">
|
||||||
|
<td>
|
||||||
|
<%= heading.name %>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<%= heading.price %>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<%= heading.population %>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<%= link_to t("admin.actions.edit"),
|
||||||
|
edit_admin_budget_budget_group_budget_heading_path(budget_id: group.budget.id, budget_group_id: group.id, id: heading.id),
|
||||||
|
class: "button hollow",
|
||||||
|
remote: true %>
|
||||||
|
<%= link_to t("admin.budget_investments.index.title"),
|
||||||
|
admin_budget_budget_investments_path(budget_id: group.budget.id, heading_id: heading.id),
|
||||||
|
class: "button hollow" %>
|
||||||
|
<% if heading.can_be_deleted? %>
|
||||||
|
<%= link_to t('admin.administrators.administrator.delete'),
|
||||||
|
#admin_budget_budget_group_budget_headings_path(group.budget.id, group.id),
|
||||||
|
[:admin, group.budget, group, heading],
|
||||||
|
method: :delete,
|
||||||
|
class: "button hollow alert" %>
|
||||||
|
<% end %>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
35
app/views/admin/budgets/_heading_form.html.erb
Normal file
35
app/views/admin/budgets/_heading_form.html.erb
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<%= form_for [:admin, budget, group, heading], remote: true do |f| %>
|
||||||
|
<%= render 'shared/errors', resource: heading %>
|
||||||
|
<label><%= t("admin.budgets.form.heading") %></label>
|
||||||
|
<%= f.text_field :name,
|
||||||
|
label: false,
|
||||||
|
maxlength: 50,
|
||||||
|
placeholder: t("admin.budgets.form.heading") %>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="small-12 medium-6 column">
|
||||||
|
<label><%= t("admin.budgets.form.amount") %></label>
|
||||||
|
<%= f.text_field :price,
|
||||||
|
label: false,
|
||||||
|
maxlength: 8,
|
||||||
|
placeholder: t("admin.budgets.form.amount") %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="small-12 medium-6 column">
|
||||||
|
<label><%= t("admin.budgets.form.population") %></label>
|
||||||
|
<%= f.text_field :population,
|
||||||
|
label: false,
|
||||||
|
maxlength: 8,
|
||||||
|
placeholder: t("admin.budgets.form.population"),
|
||||||
|
data: {toggle_focus: "population-info"} %>
|
||||||
|
</div>
|
||||||
|
<div class="small-12 medium-6 column " >
|
||||||
|
<div id="population-info" class="is-hidden" data-toggler="is-hidden">
|
||||||
|
<%= t("admin.budgets.form.population_info") %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<%= f.submit t("admin.budgets.form.save_heading"), class: "button success" %>
|
||||||
|
<% end %>
|
||||||
@@ -125,6 +125,10 @@ en:
|
|||||||
title: "Title"
|
title: "Title"
|
||||||
description: "Description"
|
description: "Description"
|
||||||
publication_date: "Publication date"
|
publication_date: "Publication date"
|
||||||
|
budget/heading:
|
||||||
|
name: "Heading name"
|
||||||
|
price: "Price"
|
||||||
|
population: "Population"
|
||||||
comment:
|
comment:
|
||||||
body: "Comment"
|
body: "Comment"
|
||||||
user: "User"
|
user: "User"
|
||||||
|
|||||||
@@ -177,6 +177,7 @@ en:
|
|||||||
proposal_notification: "Notification"
|
proposal_notification: "Notification"
|
||||||
spending_proposal: Spending proposal
|
spending_proposal: Spending proposal
|
||||||
budget/investment: Investment
|
budget/investment: Investment
|
||||||
|
budget/heading: Budget Heading
|
||||||
poll/shift: Shift
|
poll/shift: Shift
|
||||||
poll/question/answer: Answer
|
poll/question/answer: Answer
|
||||||
user: Account
|
user: Account
|
||||||
|
|||||||
@@ -116,6 +116,10 @@ es:
|
|||||||
organization_name: "Si estás proponiendo en nombre de una organización o colectivo, escribe su nombre"
|
organization_name: "Si estás proponiendo en nombre de una organización o colectivo, escribe su nombre"
|
||||||
image: "Imagen descriptiva de la propuesta de inversión"
|
image: "Imagen descriptiva de la propuesta de inversión"
|
||||||
image_title: "Título de la imagen"
|
image_title: "Título de la imagen"
|
||||||
|
budget/heading:
|
||||||
|
name: "Nombre de la partida"
|
||||||
|
price: "Cantidad"
|
||||||
|
population: "Población"
|
||||||
budget/investment/milestone:
|
budget/investment/milestone:
|
||||||
title: "Título"
|
title: "Título"
|
||||||
description: "Descripción"
|
description: "Descripción"
|
||||||
|
|||||||
@@ -177,6 +177,7 @@ es:
|
|||||||
proposal_notification: "la notificación"
|
proposal_notification: "la notificación"
|
||||||
spending_proposal: la propuesta de gasto
|
spending_proposal: la propuesta de gasto
|
||||||
budget/investment: la propuesta de inversión
|
budget/investment: la propuesta de inversión
|
||||||
|
budget/heading: la partida presupuestaria
|
||||||
poll/shift: el turno
|
poll/shift: el turno
|
||||||
poll/question/answer: la respuesta
|
poll/question/answer: la respuesta
|
||||||
user: la cuenta
|
user: la cuenta
|
||||||
|
|||||||
@@ -244,5 +244,43 @@ feature 'Admin budgets' do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario 'Update heading', :js do
|
||||||
|
budget = create(:budget, name: 'Yearly participatory budget')
|
||||||
|
group = create(:budget_group, budget: budget, name: 'Districts improvments')
|
||||||
|
heading = create(:budget_heading, group: group, name: "District 1")
|
||||||
|
heading = create(:budget_heading, group: group, name: "District 3")
|
||||||
|
|
||||||
|
visit admin_budget_path(budget)
|
||||||
|
|
||||||
|
within("#heading-#{heading.id}") do
|
||||||
|
click_link 'Edit'
|
||||||
|
|
||||||
|
fill_in 'budget_heading_name', with: 'District 2'
|
||||||
|
fill_in 'budget_heading_price', with: '10000'
|
||||||
|
fill_in 'budget_heading_population', with: '6000'
|
||||||
|
click_button 'Save heading'
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_content 'District 2'
|
||||||
|
expect(page).to have_content '10000'
|
||||||
|
expect(page).to have_content '6000'
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'Delete heading', :js do
|
||||||
|
budget = create(:budget, name: 'Yearly participatory budget')
|
||||||
|
group = create(:budget_group, budget: budget, name: 'Districts improvments')
|
||||||
|
heading = create(:budget_heading, group: group, name: "District 1")
|
||||||
|
|
||||||
|
visit admin_budget_path(budget)
|
||||||
|
|
||||||
|
expect(page).to have_content 'District 1'
|
||||||
|
|
||||||
|
within("#heading-#{heading.id}") do
|
||||||
|
click_link 'Delete'
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to_not have_content 'District 1'
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -44,4 +44,16 @@ describe Budget::Heading do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "heading" do
|
||||||
|
it "can be deleted if no budget's investments associated" do
|
||||||
|
heading1 = create(:budget_heading, group: group, name: 'name')
|
||||||
|
heading2 = create(:budget_heading, group: group, name: 'name 2')
|
||||||
|
|
||||||
|
create(:budget_investment, heading: heading1)
|
||||||
|
|
||||||
|
expect(heading1.can_be_deleted?).to eq false
|
||||||
|
expect(heading2.can_be_deleted?).to eq true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user