From cd8cbef389dd3e4e73e3ff7419b0df1251b2cf3e Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 8 Mar 2016 13:57:58 +0100 Subject: [PATCH] adds filtering of tags for spending proposals --- .../admin/spending_proposals_controller.rb | 2 +- app/helpers/spending_proposals_helper.rb | 6 ++++++ app/models/spending_proposal.rb | 5 +++++ .../admin/spending_proposals/index.html.erb | 9 ++++++++- config/initializers/acts_as_taggable_on.rb | 4 ++++ config/locales/admin.en.yml | 1 + config/locales/admin.es.yml | 1 + .../features/admin/spending_proposals_spec.rb | 20 +++++++++++++++++++ 8 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 app/helpers/spending_proposals_helper.rb diff --git a/app/controllers/admin/spending_proposals_controller.rb b/app/controllers/admin/spending_proposals_controller.rb index c013d5a2d..2ef48f320 100644 --- a/app/controllers/admin/spending_proposals_controller.rb +++ b/app/controllers/admin/spending_proposals_controller.rb @@ -16,7 +16,7 @@ class Admin::SpendingProposalsController < Admin::BaseController def edit @admins = Administrator.includes(:user).all @valuators = Valuator.includes(:user).all.order("users.username ASC") - @tags = ActsAsTaggableOn::Tag.where('taggings.taggable_type' => 'SpendingProposal').includes(:taggings) + @tags = ActsAsTaggableOn::Tag.spending_proposal_tags end def update diff --git a/app/helpers/spending_proposals_helper.rb b/app/helpers/spending_proposals_helper.rb new file mode 100644 index 000000000..43ef7311a --- /dev/null +++ b/app/helpers/spending_proposals_helper.rb @@ -0,0 +1,6 @@ +module SpendingProposalsHelper + + def spending_proposal_tags_select_options + ActsAsTaggableOn::Tag.spending_proposal_tags.pluck(:name) + end +end \ No newline at end of file diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 34873d6fb..3716c4086 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -35,6 +35,7 @@ class SpendingProposal < ActiveRecord::Base results = self results = results.by_geozone(params[:geozone_id]) if params[:geozone_id].present? results = results.by_administrator(params[:administrator_id]) if params[:administrator_id].present? + results = results.by_tag(params[:tag_name]) if params[:tag_name].present? results = results.send(current_filter) if current_filter.present? results.for_render end @@ -51,6 +52,10 @@ class SpendingProposal < ActiveRecord::Base where(administrator_id: administrator.presence) end + def self.by_tag(tag_name) + tagged_with(tag_name) + end + def feasibility case feasible when true diff --git a/app/views/admin/spending_proposals/index.html.erb b/app/views/admin/spending_proposals/index.html.erb index 7c8bca37f..b40c63ab4 100644 --- a/app/views/admin/spending_proposals/index.html.erb +++ b/app/views/admin/spending_proposals/index.html.erb @@ -2,6 +2,13 @@
<%= form_tag admin_spending_proposals_path, method: :get, enforce_utf8: false do %> +
+ <%= select_tag :tag_name, + options_for_select(spending_proposal_tags_select_options, params[:tag_name]), + { prompt: t("admin.spending_proposals.index.tags_filter_all"), + label: false, + class: "js-submit-on-change" } %> +
<%= select_tag :geozone_id, options_for_select(geozone_select_options.unshift([t("geozones.none"), "all"]), params[:geozone_id]), @@ -25,7 +32,7 @@ <% @spending_proposals.each do |spending_proposal| %> - + diff --git a/config/initializers/acts_as_taggable_on.rb b/config/initializers/acts_as_taggable_on.rb index 40a0a5949..d9882dff8 100644 --- a/config/initializers/acts_as_taggable_on.rb +++ b/config/initializers/acts_as_taggable_on.rb @@ -38,6 +38,10 @@ module ActsAsTaggableOn Tag.where("kind = 'category'").pluck(:name) end + def self.spending_proposal_tags + ActsAsTaggableOn::Tag.where('taggings.taggable_type' => 'SpendingProposal').includes(:taggings).uniq + end + private def custom_counter_field_name_for(taggable_type) "#{taggable_type.underscore.pluralize}_count" diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index f625d45e5..f46fd806c 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -150,6 +150,7 @@ en: index: geozone_filter_all: All zones administrator_filter_all: All administrators + tags_filter_all: All tags filters: valuation_open: Open without_admin: Without assigned admin diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 05ec213f0..a247eb6a6 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -150,6 +150,7 @@ es: index: geozone_filter_all: Todos los ámbitos de actuación administrator_filter_all: Todos los administradores + tags_filter_all: Todas las etiquetas filters: valuation_open: Abiertas without_admin: Sin administrador asignado diff --git a/spec/features/admin/spending_proposals_spec.rb b/spec/features/admin/spending_proposals_spec.rb index 186d411cb..22e16bb93 100644 --- a/spec/features/admin/spending_proposals_spec.rb +++ b/spec/features/admin/spending_proposals_spec.rb @@ -162,6 +162,26 @@ feature 'Admin spending proposals' do expect(page).to have_content("Old idea") end + scenario "Index filtering by tag" do + create(:spending_proposal, title: 'Educate the children', tag_list: 'Education') + create(:spending_proposal, title: 'More schools', tag_list: 'Education') + create(:spending_proposal, title: 'More hospitals', tag_list: 'Health') + + visit admin_spending_proposals_path + + expect(page).to have_css(".spending_proposal", count: 3) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).to have_content("More hospitals") + + visit admin_spending_proposals_path(tag_name: 'Education') + + expect(page).to have_css(".spending_proposal", count: 2) + expect(page).to have_content("Educate the children") + expect(page).to have_content("More schools") + expect(page).to_not have_content("More hospitals") + end + scenario 'Show' do administrator = create(:administrator, user: create(:user, username: 'Ana', email: 'ana@admins.org')) valuator = create(:valuator, user: create(:user, username: 'Rachel', email: 'rachel@valuators.org'))
<%= spending_proposal.id %>