From ec25b388c7ee14279c3dfbcc72868ce7ffab8cd5 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 1 Mar 2016 19:58:13 +0100 Subject: [PATCH] adds filtering of spending proposals by admin --- .../admin/spending_proposals_controller.rb | 14 +--------- app/helpers/admin_helper.rb | 4 +++ app/models/spending_proposal.rb | 26 +++++++++++++++++++ .../admin/spending_proposals/index.html.erb | 20 ++++++++++---- config/locales/admin.en.yml | 2 +- config/locales/admin.es.yml | 2 +- .../features/admin/spending_proposals_spec.rb | 22 ++++++++++++++++ 7 files changed, 70 insertions(+), 20 deletions(-) diff --git a/app/controllers/admin/spending_proposals_controller.rb b/app/controllers/admin/spending_proposals_controller.rb index d6457ea20..4a910f6d9 100644 --- a/app/controllers/admin/spending_proposals_controller.rb +++ b/app/controllers/admin/spending_proposals_controller.rb @@ -7,7 +7,7 @@ class Admin::SpendingProposalsController < Admin::BaseController load_and_authorize_resource def index - @spending_proposals = geozone_filter(params[:geozone_id].presence).includes(:geozone, administrator: :user, valuators: :user).send(@current_filter).order(created_at: :desc).page(params[:page]) + @spending_proposals = SpendingProposal.search(params, @current_filter).order(created_at: :desc).page(params[:page]) end def show @@ -26,17 +26,5 @@ class Admin::SpendingProposalsController < Admin::BaseController @spending_proposal.update(params.require(:spending_proposal).permit(valuator_ids: [])) end - private - - def geozone_filter(geozone) - case geozone - when nil - @spending_proposals - when 'all' - @spending_proposals.where(geozone_id: nil) - else - @spending_proposals.where(geozone_id: params[:geozone_id].presence) - end - end end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index f986eee8d..515a54deb 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -12,6 +12,10 @@ module AdminHelper options end + def admin_select_options + Administrator.all.order('users.username asc').includes(:user).collect { |v| [ v.name, v.id ] } + end + private def namespace diff --git a/app/models/spending_proposal.rb b/app/models/spending_proposal.rb index 221528582..9497dcae3 100644 --- a/app/models/spending_proposal.rb +++ b/app/models/spending_proposal.rb @@ -23,10 +23,36 @@ class SpendingProposal < ActiveRecord::Base scope :valuating, -> { where("valuation_assignments_count > 0 AND valuation_finished = ?", false) } scope :valuation_finished, -> { where(valuation_finished: true) } + scope :for_render, -> { includes(:geozone, administrator: :user, valuators: :user) } + def description super.try :html_safe end + def self.search(params, current_filter) + 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.send(current_filter) if current_filter.present? + results = results.for_render + results + end + + def self.by_geozone(geozone) + case geozone + when nil + self + when 'all' + where(geozone_id: nil) + else + where(geozone_id: geozone.presence) + end + end + + def self.by_administrator(administrator) + where(administrator_id: administrator.presence) + 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 8bd95ff74..18be63a1b 100644 --- a/app/views/admin/spending_proposals/index.html.erb +++ b/app/views/admin/spending_proposals/index.html.erb @@ -1,11 +1,21 @@

<%= t("admin.spending_proposals.index.title") %>

-
+
<%= form_tag admin_spending_proposals_path, method: :get, enforce_utf8: false do %> - <%= select_tag :geozone_id, options_for_select(geozone_select_options.unshift([t("geozones.none"), "all"]), params[:geozone_id]), - { prompt: t("admin.spending_proposals.index.geozone_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]), + { prompt: t("admin.spending_proposals.index.geozone_filter_all"), + label: false, + class: "js-submit-on-change" } %> +
+
+ <%= select_tag :administrator_id, + options_for_select(admin_select_options, params[:administrator_id]), + { prompt: t("admin.spending_proposals.index.administrator_filter_all"), + label: false, + class: "js-submit-on-change" } %> +
<% end %>
diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 6a5f01b8a..c44f216e4 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -151,7 +151,7 @@ en: spending_proposals: index: geozone_filter_all: All zones - valuator_filter_all: All valuators + administrator_filter_all: All administrators filters: all: All without_admin: Without assigned admin diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 22d0f62da..986e8671a 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -151,7 +151,7 @@ es: spending_proposals: index: geozone_filter_all: Todos los ámbitos de actuación - valuator_filter_all: Todos los evaluadores + administrator_filter_all: Todos los administradores filters: all: Todas without_admin: Sin administrador asignado diff --git a/spec/features/admin/spending_proposals_spec.rb b/spec/features/admin/spending_proposals_spec.rb index a357c4007..2f2ba8c4b 100644 --- a/spec/features/admin/spending_proposals_spec.rb +++ b/spec/features/admin/spending_proposals_spec.rb @@ -74,6 +74,28 @@ feature 'Admin spending proposals' do expect(page).to have_link("Destroy the city") end + scenario "Index filtering by admin", :js do + user = create(:user, username: 'Admin 1') + administrator = create(:administrator, user: user) + + create(:spending_proposal, title: "Realocate visitors", administrator: administrator) + create(:spending_proposal, title: "Destroy the city") + + visit admin_spending_proposals_path + expect(page).to have_link("Realocate visitors") + expect(page).to have_link("Destroy the city") + + select "Admin 1", from: "administrator_id" + + expect(page).to have_link("Realocate visitors") + expect(page).to_not have_link("Destroy the city") + + select "All administrators", from: "administrator_id" + + expect(page).to have_link("Destroy the city") + expect(page).to have_link("Realocate visitors") + end + scenario "Current filter is properly highlighted" do filters_links = {'all' => 'All', 'without_admin' => 'Without assigned admin',