adds search and filter for poll questions

This commit is contained in:
rgarcia
2016-11-29 11:44:51 +01:00
parent 66f4289e9c
commit 20cb044015
10 changed files with 93 additions and 6 deletions

View File

@@ -1,13 +1,18 @@
class Admin::Poll::QuestionsController < Admin::BaseController class Admin::Poll::QuestionsController < Admin::BaseController
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: 'Poll::Question' load_and_authorize_resource :question, class: 'Poll::Question'
before_action :load_geozones, only: [:new, :create, :edit, :update] before_action :load_geozones, only: [:new, :create, :edit, :update]
def index def index
@questions = @questions.page(params[:page]) @polls = Poll.all
@search = search_params[:search]
@questions = @questions.search(search_params).page(params[:page]).order("created_at DESC")
end end
def new def new
@polls = Poll.all
@question.valid_answers = I18n.t('poll_questions.default_valid_answers') @question.valid_answers = I18n.t('poll_questions.default_valid_answers')
proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present? proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present?
@question.copy_attributes_from_proposal(proposal) @question.copy_attributes_from_proposal(proposal)
@@ -53,7 +58,11 @@ class Admin::Poll::QuestionsController < Admin::BaseController
end end
def question_params def question_params
params.require(:poll_question).permit(:title, :question, :summary, :description, :proposal_id, :valid_answers, :geozone_ids => []) params.require(:poll_question).permit(:title, :question, :summary, :description, :proposal_id, :valid_answers, :poll_id, :geozone_ids => [])
end
def search_params
params.permit(:poll_id, :search)
end end
end end

View File

@@ -0,0 +1,15 @@
module PollsHelper
def poll_select_options(include_all=nil)
options = @polls.collect {|poll|
[poll.name, current_path_with_query_params(poll_id: poll.id)]
}
options << all_polls if include_all
options_for_select(options, request.fullpath)
end
def all_polls
["Todas", admin_questions_path]
end
end

View File

@@ -12,7 +12,7 @@ module Searchable
}, },
ignoring: :accents, ignoring: :accents,
ranked_by: '(:tsearch)', ranked_by: '(:tsearch)',
order_within_rank: "#{self.table_name}.cached_votes_up DESC" order_within_rank: (self.column_names.include?('cached_votes_up') ? "#{self.table_name}.cached_votes_up DESC" : nil)
} }
end end

View File

@@ -1,5 +1,6 @@
class Poll::Question < ActiveRecord::Base class Poll::Question < ActiveRecord::Base
include Measurable include Measurable
include Searchable
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases include ActsAsParanoidAliases
@@ -20,9 +21,29 @@ class Poll::Question < ActiveRecord::Base
validates :title, length: { in: 4..Poll::Question.title_max_length } validates :title, length: { in: 4..Poll::Question.title_max_length }
validates :description, length: { maximum: Poll::Question.description_max_length } validates :description, length: { maximum: Poll::Question.description_max_length }
scope :by_poll_id, -> (poll_id) { where(poll_id: poll_id) }
scope :by_geozone_id, -> (geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
scope :sort_for_list, -> { order('poll_questions.proposal_id IS NULL', :created_at)} scope :sort_for_list, -> { order('poll_questions.proposal_id IS NULL', :created_at)}
scope :for_render, -> { includes(:author, :proposal) } scope :for_render, -> { includes(:author, :proposal) }
scope :by_geozone, -> (geozone_id) { joins(:geozones).where(geozones: {id: geozone_id}) }
def self.search(params)
results = self.all
results = results.by_poll_id(params[:poll_id]) if params[:poll_id].present?
results = results.pg_search(params[:search]) if params[:search].present?
results
end
def searchable_values
{ title => 'A',
proposal.try(:title) => 'A',
summary => 'B',
description => 'C',
author.username => 'C',
author_visible_name => 'C',
geozones.pluck(:name).join(' ') => 'C'
}
end
def description def description
super.try :html_safe super.try :html_safe

View File

@@ -0,0 +1,8 @@
<div class="small-12 medium-6">
<%= form_tag '', method: :get do %>
<%= select_tag "poll_id",
poll_select_options(true),
prompt: t("admin.booths.index.select_poll"),
class: "js-location-changer" %>
<% end %>
</div>

View File

@@ -7,6 +7,12 @@
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">
<div class="small-12 medium-6">
<%= f.select :poll_id,
options_for_select(Poll.pluck(:name, :id)),
prompt: t("admin.booths.index.select_poll") %>
</div>
<%= f.text_field :title, maxlength: Poll::Question.title_max_length %> <%= f.text_field :title, maxlength: Poll::Question.title_max_length %>
<%= f.text_field :valid_answers %> <%= f.text_field :valid_answers %>

View File

@@ -0,0 +1,12 @@
<%= form_tag(admin_questions_path, method: :get) do |f| %>
<div class="row">
<div class="small-12 medium-6 column">
<%= text_field_tag :search,
@search,
placeholder: t("admin.shared.spending_proposal_search.placeholder") %>
</div>
<div class="form-inline small-12 medium-3 column end">
<%= submit_tag t("admin.shared.spending_proposal_search.button"), class: "button" %>
</div>
</div>
<% end %>

View File

@@ -3,6 +3,9 @@
<%= link_to t('admin.questions.index.create'), new_admin_question_path, <%= link_to t('admin.questions.index.create'), new_admin_question_path,
class: "button success float-right" %> class: "button success float-right" %>
<%= render 'filter' %>
<%= render 'search' %>
<% if @questions.count == 0 %> <% if @questions.count == 0 %>
<div class="callout primary"> <div class="callout primary">
<%= t('admin.questions.index.no_questions') %> <%= t('admin.questions.index.no_questions') %>

View File

@@ -0,0 +1,11 @@
class AddTsvToPollQuestions < ActiveRecord::Migration
def up
add_column :poll_questions, :tsv, :tsvector
add_index :poll_questions, :tsv, using: "gin"
end
def down
remove_index :poll_questions, :tsv
remove_column :poll_questions, :tsv
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161122101702) do ActiveRecord::Schema.define(version: 20161129011737) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -322,11 +322,13 @@ ActiveRecord::Schema.define(version: 20161122101702) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.boolean "all_geozones", default: false t.boolean "all_geozones", default: false
t.tsvector "tsv"
end end
add_index "poll_questions", ["author_id"], name: "index_poll_questions_on_author_id", using: :btree add_index "poll_questions", ["author_id"], name: "index_poll_questions_on_author_id", using: :btree
add_index "poll_questions", ["poll_id"], name: "index_poll_questions_on_poll_id", using: :btree add_index "poll_questions", ["poll_id"], name: "index_poll_questions_on_poll_id", using: :btree
add_index "poll_questions", ["proposal_id"], name: "index_poll_questions_on_proposal_id", using: :btree add_index "poll_questions", ["proposal_id"], name: "index_poll_questions_on_proposal_id", using: :btree
add_index "poll_questions", ["tsv"], name: "index_poll_questions_on_tsv", using: :gin
create_table "poll_voters", force: :cascade do |t| create_table "poll_voters", force: :cascade do |t|
t.integer "booth_id" t.integer "booth_id"