From 951505ce7d8f4ac7a46c31ef9abf468b7379c152 Mon Sep 17 00:00:00 2001
From: rgarcia
Date: Mon, 7 Mar 2016 18:09:49 +0100
Subject: [PATCH] admins can tag spending proposals
---
.../admin/spending_proposals_controller.rb | 20 +++++++++++
app/models/spending_proposal.rb | 1 +
.../admin/spending_proposals/edit.html.erb | 24 +++++++++++++
.../admin/spending_proposals/show.html.erb | 3 ++
app/views/shared/_tags.html.erb | 4 +--
config/locales/admin.en.yml | 2 ++
config/locales/admin.es.yml | 2 ++
config/locales/responders.en.yml | 3 +-
config/locales/responders.es.yml | 3 +-
config/routes.rb | 2 +-
.../features/admin/spending_proposals_spec.rb | 34 +++++++++++++++++++
11 files changed, 93 insertions(+), 5 deletions(-)
create mode 100644 app/views/admin/spending_proposals/edit.html.erb
diff --git a/app/controllers/admin/spending_proposals_controller.rb b/app/controllers/admin/spending_proposals_controller.rb
index 8956f7831..f3126b061 100644
--- a/app/controllers/admin/spending_proposals_controller.rb
+++ b/app/controllers/admin/spending_proposals_controller.rb
@@ -15,6 +15,20 @@ class Admin::SpendingProposalsController < Admin::BaseController
@valuators = Valuator.includes(:user).all.order("users.username ASC")
end
+ def edit
+ @spending_proposal = SpendingProposal.find(params[:id])
+ @tags = ActsAsTaggableOn::Tag.where('taggings.taggable_type' => 'SpendingProposal').includes(:taggings)
+ end
+
+ def update
+ @spending_proposal = SpendingProposal.find(params[:id])
+ if @spending_proposal.update(spending_proposal_params)
+ redirect_to admin_spending_proposal_path(@spending_proposal), notice: t("flash.actions.update.spending_proposal")
+ else
+ render :edit
+ end
+ end
+
def assign_admin
@spending_proposal.update(params.require(:spending_proposal).permit(:administrator_id))
render nothing: true
@@ -26,4 +40,10 @@ class Admin::SpendingProposalsController < Admin::BaseController
@spending_proposal.update(params.require(:spending_proposal).permit(valuator_ids: []))
end
+ private
+
+ def spending_proposal_params
+ params.require(:spending_proposal).permit(:tag_list)
+ end
+
end
diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb
index 48473640c..34873d6fb 100644
--- a/app/models/spending_proposal.rb
+++ b/app/models/spending_proposal.rb
@@ -1,6 +1,7 @@
class SpendingProposal < ActiveRecord::Base
include Measurable
include Sanitizable
+ include Taggable
apply_simple_captcha
diff --git a/app/views/admin/spending_proposals/edit.html.erb b/app/views/admin/spending_proposals/edit.html.erb
new file mode 100644
index 000000000..3812db0a7
--- /dev/null
+++ b/app/views/admin/spending_proposals/edit.html.erb
@@ -0,0 +1,24 @@
+<%= form_for @spending_proposal, url: admin_spending_proposal_path(@spending_proposal) do |f| %>
+
+ <%= f.label :tag_list, t("proposals.form.tags_label") %>
+
<%= t("proposals.form.tags_instructions") %>
+
+
+
+
+ <%= f.text_field :tag_list, value: @spending_proposal.tag_list.to_s,
+ label: false,
+ placeholder: t("proposals.form.tags_placeholder"),
+ class: 'js-tag-list' %>
+
+
+
+ <%= f.submit(class: "button", value: t("admin.spending_proposals.edit.submit_button")) %>
+
+
+<% end %>
\ No newline at end of file
diff --git a/app/views/admin/spending_proposals/show.html.erb b/app/views/admin/spending_proposals/show.html.erb
index cbdfbb2ad..65d2c6cd3 100644
--- a/app/views/admin/spending_proposals/show.html.erb
+++ b/app/views/admin/spending_proposals/show.html.erb
@@ -36,6 +36,9 @@
<% end %>
+<%= render 'shared/tags', taggable: @spending_proposal %>
+
+
<%= t("admin.spending_proposals.show.assigned_valuators") %>:
<%= render "assigned_valuators" %>
diff --git a/app/views/shared/_tags.html.erb b/app/views/shared/_tags.html.erb
index 8640a4d0c..a161a8228 100644
--- a/app/views/shared/_tags.html.erb
+++ b/app/views/shared/_tags.html.erb
@@ -3,12 +3,12 @@
<% if taggable.tags.any? %>
<% taggable.tag_list_with_limit(limit).each do |tag| %>
- <%= link_to sanitize(tag.name), send("#{taggable.class.to_s.downcase.pluralize}_path", tag: tag.name) %>
+ <%= link_to sanitize(tag.name), send("#{taggable.class.name.underscore.pluralize}_path", tag: tag.name) %>
<% end %>
<% if taggable.tags_count_out_of_limit(limit) > 0 %>
<%= link_to "#{taggable.tags_count_out_of_limit(limit)}+",
- send("#{taggable.class.to_s.downcase}_path", taggable) %>
+ send("#{taggable.class.name.underscore}_path", taggable) %>
<% end %>
<% end %>
diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml
index bd31ad1ad..ae9f7ad08 100755
--- a/config/locales/admin.en.yml
+++ b/config/locales/admin.en.yml
@@ -186,6 +186,8 @@ en:
assign_valuators: Assign valuators
no_valuators: There is not any valuator user
assign: Assign
+ edit:
+ submit_button: Update
stats:
show:
stats_title: Stats
diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml
index 53f615c1a..f97ac9067 100644
--- a/config/locales/admin.es.yml
+++ b/config/locales/admin.es.yml
@@ -186,6 +186,8 @@ es:
assign_valuators: Asignar evaluadores
no_valuators: No hay ningún usuario evaluador
assign: Asignar
+ edit:
+ submit_button: Actualizar
stats:
show:
stats_title: Estadísticas
diff --git a/config/locales/responders.en.yml b/config/locales/responders.en.yml
index b2640c6ac..03a8f0ec8 100755
--- a/config/locales/responders.en.yml
+++ b/config/locales/responders.en.yml
@@ -12,4 +12,5 @@ en:
update:
notice: "%{resource_name} updated successfully."
debate: "Debate updated successfully."
- proposal: "Proposal updated successfully."
\ No newline at end of file
+ proposal: "Proposal updated successfully."
+ spending_proposal: "Investment project updated succesfully."
\ No newline at end of file
diff --git a/config/locales/responders.es.yml b/config/locales/responders.es.yml
index 5c22856cc..ee6e011f8 100644
--- a/config/locales/responders.es.yml
+++ b/config/locales/responders.es.yml
@@ -12,4 +12,5 @@ es:
update:
notice: "%{resource_name} actualizado correctamente."
debate: "Debate actualizado correctamente."
- proposal: "Propuesta actualizada correctamente."
\ No newline at end of file
+ proposal: "Propuesta actualizada correctamente."
+ spending_proposal: "Propuesta de inversión actualizada correctamente."
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index e743e29d7..4a3c72427 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -130,7 +130,7 @@ Rails.application.routes.draw do
end
end
- resources :spending_proposals, only: [:index, :show] do
+ resources :spending_proposals, only: [:index, :show, :edit, :update] do
member do
patch :assign_admin
patch :assign_valuators
diff --git a/spec/features/admin/spending_proposals_spec.rb b/spec/features/admin/spending_proposals_spec.rb
index f4bd4582c..8d1877019 100644
--- a/spec/features/admin/spending_proposals_spec.rb
+++ b/spec/features/admin/spending_proposals_spec.rb
@@ -251,4 +251,38 @@ feature 'Admin spending proposals' do
end
end
+ scenario "Adds existing tags", :js do
+ create(:spending_proposal, tag_list: 'Education, Health')
+
+ spending_proposal = create(:spending_proposal)
+
+ visit edit_admin_spending_proposal_path(spending_proposal)
+
+ find('.js-add-tag-link', text: 'Education').click
+ click_button 'Update'
+
+ expect(page).to have_content 'Investment project updated succesfully.'
+
+ within "#tags" do
+ expect(page).to have_content 'Education'
+ expect(page).to_not have_content 'Health'
+ end
+ end
+
+ scenario "Adds non existent tags" do
+ spending_proposal = create(:spending_proposal)
+
+ visit edit_admin_spending_proposal_path(spending_proposal)
+
+ fill_in 'spending_proposal_tag_list', with: 'Refugees, Solidarity'
+ click_button 'Update'
+
+ expect(page).to have_content 'Investment project updated succesfully.'
+
+ within "#tags" do
+ expect(page).to have_content 'Refugees'
+ expect(page).to have_content 'Solidarity'
+ end
+ end
+
end