adds filters: by geozone and without_admin

This commit is contained in:
Juanjo Bazán
2016-02-25 18:18:40 +01:00
parent e1aff00e5b
commit 0e1e0ce371
8 changed files with 81 additions and 10 deletions

View File

@@ -2,10 +2,12 @@ class Admin::SpendingProposalsController < Admin::BaseController
include FeatureFlags
feature_flag :spending_proposals
has_filters %w{all without_admin}, only: :index
load_and_authorize_resource
def index
@spending_proposals = @spending_proposals.includes(:geozone, administrator: :user, valuators: :user).order(created_at: :desc).page(params[:page])
@spending_proposals = geozone_filter(params[:geozone_id].presence).includes(:geozone, administrator: :user, valuators: :user).send(@current_filter).order(created_at: :desc).page(params[:page])
end
def show
@@ -24,4 +26,17 @@ 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

View File

@@ -39,7 +39,7 @@ class Proposal < ActiveRecord::Base
scope :sort_by_created_at, -> { reorder(created_at: :desc) }
scope :sort_by_most_commented, -> { reorder(comments_count: :desc) }
scope :sort_by_random, -> { reorder("RANDOM()") }
scope :sort_by_relevance , -> { all }
scope :sort_by_relevance, -> { all }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)}

View File

@@ -18,6 +18,8 @@ class SpendingProposal < ActiveRecord::Base
validates :description, length: { maximum: SpendingProposal.description_max_length }
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
scope :without_admin, -> { where(administrator_id: nil) }
def description
super.try :html_safe
end

View File

@@ -1,5 +1,16 @@
<h2><%= t("admin.spending_proposals.index.title") %></h2>
<div class="small-12 medium-4 column right">
<%= form_tag current_path_with_query_params(page: 1), 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"} %>
<% end %>
</div>
<%= render 'shared/filter_subnav', i18n_namespace: "admin.spending_proposals.index" %>
<h3><%= page_entries_info @spending_proposals %></h3>
<table>

View File

@@ -138,11 +138,10 @@ en:
placeholder: Search user by name or email'
spending_proposals:
index:
filter: Filter
geozone_filter_all: All zones
filters:
accepted: Accepted
rejected: Rejected
unresolved: Unresolved
all: All
without_admin: Without assigned admin
title: Investment projects for participatory budgeting
admin_assigned: Assigned administrator
no_admin_assigned: No admin assigned

View File

@@ -138,11 +138,10 @@ es:
placeholder: Buscar usuario por nombre o email
spending_proposals:
index:
filter: Filtro
geozone_filter_all: Todos los ámbitos de actuación
filters:
accepted: Aceptadas
rejected: Rechazadas
unresolved: Sin resolver
all: Todas
without_admin: Sin administrador asignado
title: Propuestas de inversión para presupuestos participativos
admin_assigned: Administrador asignado
no_admin_assigned: Sin admin asignado

View File

@@ -50,6 +50,36 @@ feature 'Admin spending proposals' do
end
end
scenario "Current filter is properly highlighted" do
visit admin_spending_proposals_path
expect(page).to_not have_link('All')
expect(page).to have_link('Without assigned admin')
visit admin_spending_proposals_path(filter: 'without_admin')
expect(page).to_not have_link('Without assigned admin')
expect(page).to have_link('All')
visit admin_spending_proposals_path(filter: 'all')
expect(page).to_not have_link('All')
expect(page).to have_link('Without assigned admin')
end
scenario "Filtering proposals" do
create(:spending_proposal, title: "New idea")
assigned = create(:spending_proposal, title: "Assigned idea", administrator: create(:administrator))
visit admin_spending_proposals_path(filter: 'all')
expect(page).to have_content("New idea")
expect(page).to have_content("Assigned idea")
visit admin_spending_proposals_path(filter: 'without_admin')
expect(page).to have_content("New idea")
expect(page).to_not have_content("Assigned idea")
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'))

View File

@@ -61,4 +61,19 @@ describe SpendingProposal do
end
end
describe "scopes" do
describe "without_admin" do
it "should return all spending proposals without assigned admin" do
spending_proposal1 = create(:spending_proposal)
spending_proposal2 = create(:spending_proposal, administrator: create(:administrator))
without_admin = SpendingProposal.without_admin
expect(without_admin.size).to eq(1)
expect(without_admin.first).to eq(spending_proposal1)
end
end
end
end