From f7e96067b094375daf5aea533112ad42025231be Mon Sep 17 00:00:00 2001 From: iagirre Date: Mon, 25 Sep 2017 15:40:17 +0200 Subject: [PATCH 1/2] Actions for budgets' headings added. Edit headings, delete headings if not budget investments associated and add link in order to see budgets investments filtered by the heading. Cambios para hacer commit: modificado: app/controllers/admin/budget_headings_controller.rb modificado: app/models/budget/heading.rb nuevo archivo: app/views/admin/budget_headings/_errors.html.erb nuevo archivo: app/views/admin/budget_headings/edit.js.erb nuevo archivo: app/views/admin/budget_headings/update.js.erb modificado: app/views/admin/budgets/_group.html.erb nuevo archivo: app/views/admin/budgets/_heading.html.erb nuevo archivo: app/views/admin/budgets/_heading_form.html.erb modificado: config/locales/en/activerecord.yml modificado: config/locales/es/activerecord.yml modificado: spec/features/admin/budgets_spec.rb modificado: spec/models/budget/heading_spec.rb --- .../admin/budget_headings_controller.rb | 21 ++++++++ app/models/budget/heading.rb | 6 ++- .../admin/budget_headings/_errors.html.erb | 7 +++ app/views/admin/budget_headings/edit.js.erb | 1 + app/views/admin/budget_headings/update.js.erb | 5 ++ app/views/admin/budgets/_group.html.erb | 48 +++---------------- app/views/admin/budgets/_heading.html.erb | 27 +++++++++++ .../admin/budgets/_heading_form.html.erb | 30 ++++++++++++ config/locales/en/activerecord.yml | 4 ++ config/locales/es/activerecord.yml | 4 ++ spec/features/admin/budgets_spec.rb | 38 +++++++++++++++ spec/models/budget/heading_spec.rb | 13 +++++ 12 files changed, 161 insertions(+), 43 deletions(-) create mode 100644 app/views/admin/budget_headings/_errors.html.erb create mode 100644 app/views/admin/budget_headings/edit.js.erb create mode 100644 app/views/admin/budget_headings/update.js.erb create mode 100644 app/views/admin/budgets/_heading.html.erb create mode 100644 app/views/admin/budgets/_heading_form.html.erb diff --git a/app/controllers/admin/budget_headings_controller.rb b/app/controllers/admin/budget_headings_controller.rb index eea93716f..ee1878edf 100644 --- a/app/controllers/admin/budget_headings_controller.rb +++ b/app/controllers/admin/budget_headings_controller.rb @@ -9,6 +9,27 @@ class Admin::BudgetHeadingsController < Admin::BaseController @headings = @budget_group.headings 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) + @errors = @heading.errors.full_messages 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 def budget_heading_params diff --git a/app/models/budget/heading.rb b/app/models/budget/heading.rb index 1a232c75e..10feaa909 100644 --- a/app/models/budget/heading.rb +++ b/app/models/budget/heading.rb @@ -20,7 +20,11 @@ class Budget end 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 diff --git a/app/views/admin/budget_headings/_errors.html.erb b/app/views/admin/budget_headings/_errors.html.erb new file mode 100644 index 000000000..bb3e5482e --- /dev/null +++ b/app/views/admin/budget_headings/_errors.html.erb @@ -0,0 +1,7 @@ +
+ +
diff --git a/app/views/admin/budget_headings/edit.js.erb b/app/views/admin/budget_headings/edit.js.erb new file mode 100644 index 000000000..5f9ac48f8 --- /dev/null +++ b/app/views/admin/budget_headings/edit.js.erb @@ -0,0 +1 @@ +$("#heading-<%=@heading.id%>").html("<%= j render("admin/budgets/heading_form", group: @budget_group, budget: @budget, heading: @heading) %>"); diff --git a/app/views/admin/budget_headings/update.js.erb b/app/views/admin/budget_headings/update.js.erb new file mode 100644 index 000000000..d46e35386 --- /dev/null +++ b/app/views/admin/budget_headings/update.js.erb @@ -0,0 +1,5 @@ +<% if @errors %> + $("#heading-<%=@heading.id%> div#error_explanation").replaceWith("<%= j render("admin/budget_headings/errors", errors: @errors) %>"); +<% else %> + $("#heading-<%=@heading.id%>").html("<%= j render("admin/budgets/heading", group: @budget_group, budget: @budget, heading: @heading) %>"); +<% end %> diff --git a/app/views/admin/budgets/_group.html.erb b/app/views/admin/budgets/_group.html.erb index 8537fa477..d6a648f10 100644 --- a/app/views/admin/budgets/_group.html.erb +++ b/app/views/admin/budgets/_group.html.erb @@ -1,7 +1,7 @@ - @@ -11,7 +11,7 @@ - + @@ -29,51 +30,14 @@ - <% headings.each do |heading| %> - - - - - + <%= render "admin/budgets/heading", group: group, budget: @budget, heading: heading %> <% end %> diff --git a/app/views/admin/budgets/_heading.html.erb b/app/views/admin/budgets/_heading.html.erb new file mode 100644 index 000000000..7ffeca7a0 --- /dev/null +++ b/app/views/admin/budgets/_heading.html.erb @@ -0,0 +1,27 @@ + + + + + + \ No newline at end of file diff --git a/app/views/admin/budgets/_heading_form.html.erb b/app/views/admin/budgets/_heading_form.html.erb new file mode 100644 index 000000000..43a4797a8 --- /dev/null +++ b/app/views/admin/budgets/_heading_form.html.erb @@ -0,0 +1,30 @@ +<%= form_for [:admin, budget, group, heading], remote: true do |f| %> + + <%= f.text_field :name, + label: false, + maxlength: 50, + placeholder: t("admin.budgets.form.heading") %> + +
+
+ + <%= f.text_field :price, + label: false, + maxlength: 8, + placeholder: t("admin.budgets.form.amount") %> +
+
+
+
+ + <%= f.text_field :population, + label: false, + maxlength: 8, + placeholder: t("admin.budgets.form.population") %> +
+
+ + <%= f.submit t("admin.budgets.form.save_heading"), class: "button success" %> + +
+<% end %> \ No newline at end of file diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 79982150c..0008eced6 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -107,6 +107,10 @@ en: budget/investment/milestone: title: "Title" description: "Description" + budget/heading: + name: "Heading name" + price: "Price" + population: "Population" comment: body: "Comment" user: "User" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index d409d1c5e..2315fc65a 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -99,6 +99,10 @@ es: title: "Título" location: "Ubicación" organization_name: "Si estás proponiendo en nombre de una organización o colectivo, escribe su nombre" + budget/heading: + name: "Nombre de la partida" + price: "Cantidad" + population: "Población" comment: body: "Comentario" user: "Usuario" diff --git a/spec/features/admin/budgets_spec.rb b/spec/features/admin/budgets_spec.rb index d4ed2d2ac..e21db46df 100644 --- a/spec/features/admin/budgets_spec.rb +++ b/spec/features/admin/budgets_spec.rb @@ -199,5 +199,43 @@ feature 'Admin budgets' do 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 diff --git a/spec/models/budget/heading_spec.rb b/spec/models/budget/heading_spec.rb index ee2e4c142..76ac82692 100644 --- a/spec/models/budget/heading_spec.rb +++ b/spec/models/budget/heading_spec.rb @@ -25,4 +25,17 @@ describe Budget::Heading do end end + describe "heading" do + it "can be deleted if not 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 From f0724ce13a97d03c4114430eae1db2e691740dc7 Mon Sep 17 00:00:00 2001 From: iagirre Date: Wed, 10 Jan 2018 13:13:05 +0100 Subject: [PATCH 2/2] Changes suggested in the PR made --- .../admin/budget_headings_controller.rb | 22 +++++++++---------- app/views/admin/budget_headings/update.js.erb | 6 +---- app/views/admin/budgets/_heading.html.erb | 8 +++---- .../admin/budgets/_heading_form.html.erb | 5 ++--- spec/models/budget/heading_spec.rb | 3 +-- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/app/controllers/admin/budget_headings_controller.rb b/app/controllers/admin/budget_headings_controller.rb index ee1878edf..902f256b5 100644 --- a/app/controllers/admin/budget_headings_controller.rb +++ b/app/controllers/admin/budget_headings_controller.rb @@ -3,30 +3,30 @@ class Admin::BudgetHeadingsController < Admin::BaseController feature_flag :budgets def create - @budget = Budget.find params[:budget_id] - @budget_group = @budget.groups.find params[:budget_group_id] + @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 end def edit - @budget = Budget.find params[:budget_id] - @budget_group = @budget.groups.find params[:budget_group_id] - @heading = Budget::Heading.find params[:id] + @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] + @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) - @errors = @heading.errors.full_messages unless @heading.save + render :edit unless @heading.save end def destroy - @heading = Budget::Heading.find params[:id] + @heading = Budget::Heading.find(params[:id]) @heading.destroy - @budget = Budget.find params[:budget_id] + @budget = Budget.find(params[:budget_id]) redirect_to admin_budget_path(@budget) end diff --git a/app/views/admin/budget_headings/update.js.erb b/app/views/admin/budget_headings/update.js.erb index d46e35386..6466959cd 100644 --- a/app/views/admin/budget_headings/update.js.erb +++ b/app/views/admin/budget_headings/update.js.erb @@ -1,5 +1 @@ -<% if @errors %> - $("#heading-<%=@heading.id%> div#error_explanation").replaceWith("<%= j render("admin/budget_headings/errors", errors: @errors) %>"); -<% else %> - $("#heading-<%=@heading.id%>").html(""); -<% end %> +$("#<%= dom_id(@budget_group) %>").html('<%= j render("admin/budgets/group", group: @budget_group, headings: @budget_group.headings) %>'); diff --git a/app/views/admin/budgets/_heading.html.erb b/app/views/admin/budgets/_heading.html.erb index 7ffeca7a0..bc9d59c5f 100644 --- a/app/views/admin/budgets/_heading.html.erb +++ b/app/views/admin/budgets/_heading.html.erb @@ -12,16 +12,16 @@ <%= 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%> + 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"%> + 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"%> + class: "button hollow alert" %> <% end %> - \ No newline at end of file + diff --git a/app/views/admin/budgets/_heading_form.html.erb b/app/views/admin/budgets/_heading_form.html.erb index 43a4797a8..8dd2d563a 100644 --- a/app/views/admin/budgets/_heading_form.html.erb +++ b/app/views/admin/budgets/_heading_form.html.erb @@ -1,4 +1,5 @@ <%= form_for [:admin, budget, group, heading], remote: true do |f| %> + <%= render 'shared/errors', resource: heading %> <%= f.text_field :name, label: false, @@ -25,6 +26,4 @@ <%= f.submit t("admin.budgets.form.save_heading"), class: "button success" %> - -
-<% end %> \ No newline at end of file +<% end %> diff --git a/spec/models/budget/heading_spec.rb b/spec/models/budget/heading_spec.rb index 76ac82692..a556cf2ca 100644 --- a/spec/models/budget/heading_spec.rb +++ b/spec/models/budget/heading_spec.rb @@ -26,7 +26,7 @@ describe Budget::Heading do end describe "heading" do - it "can be deleted if not budget's investments associated" 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') @@ -34,7 +34,6 @@ describe Budget::Heading do expect(heading1.can_be_deleted?).to eq false expect(heading2.can_be_deleted?).to eq true - end end
+ <%= 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" } %>
+
<%= t("admin.budgets.form.no_heading") %>
@@ -22,6 +22,7 @@
<%= t("admin.budgets.form.table_heading") %> <%= t("admin.budgets.form.table_amount") %> <%= t("admin.budgets.form.table_population") %><%= t("admin.actions.actions") %>
- <%= heading.name %> - - <%= heading.price %> - - <%= heading.population %> -
+ <%= heading.name %> + + <%= heading.price %> + + <%= heading.population %> + + <%= 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 %> +
<%= j render("admin/budgets/heading", group: @budget_group, budget: @budget, heading: @heading) %>