diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index 741ecd989..3b23ec21d 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -1,13 +1,18 @@ class Admin::Poll::QuestionsController < Admin::BaseController + load_and_authorize_resource :poll load_and_authorize_resource :question, class: 'Poll::Question' before_action :load_geozones, only: [:new, :create, :edit, :update] 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 def new + @polls = Poll.all @question.valid_answers = I18n.t('poll_questions.default_valid_answers') proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present? @question.copy_attributes_from_proposal(proposal) @@ -53,7 +58,11 @@ class Admin::Poll::QuestionsController < Admin::BaseController end 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 \ No newline at end of file diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb new file mode 100644 index 000000000..09303b68f --- /dev/null +++ b/app/helpers/polls_helper.rb @@ -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 \ No newline at end of file diff --git a/app/models/concerns/searchable.rb b/app/models/concerns/searchable.rb index 4d717959e..147a37fbc 100644 --- a/app/models/concerns/searchable.rb +++ b/app/models/concerns/searchable.rb @@ -12,7 +12,7 @@ module Searchable }, ignoring: :accents, 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 diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 2dacddb70..b6e2c5ce0 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -1,5 +1,6 @@ class Poll::Question < ActiveRecord::Base include Measurable + include Searchable acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases @@ -20,9 +21,29 @@ class Poll::Question < ActiveRecord::Base validates :title, length: { in: 4..Poll::Question.title_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 :for_render, -> { includes(:author, :proposal) } - scope :by_geozone, -> (geozone_id) { joins(:geozones).where(geozones: {id: geozone_id}) } + scope :for_render, -> { includes(:author, :proposal) } + + 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 super.try :html_safe diff --git a/app/views/admin/poll/questions/_filter.html.erb b/app/views/admin/poll/questions/_filter.html.erb new file mode 100644 index 000000000..0c9e17695 --- /dev/null +++ b/app/views/admin/poll/questions/_filter.html.erb @@ -0,0 +1,8 @@ +
+ <%= 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 %> +
\ No newline at end of file diff --git a/app/views/admin/poll/questions/_form.html.erb b/app/views/admin/poll/questions/_form.html.erb index fdd9c72d9..b9986f98c 100644 --- a/app/views/admin/poll/questions/_form.html.erb +++ b/app/views/admin/poll/questions/_form.html.erb @@ -7,6 +7,12 @@
+
+ <%= f.select :poll_id, + options_for_select(Poll.pluck(:name, :id)), + prompt: t("admin.booths.index.select_poll") %> +
+ <%= f.text_field :title, maxlength: Poll::Question.title_max_length %> <%= f.text_field :valid_answers %> diff --git a/app/views/admin/poll/questions/_search.html.erb b/app/views/admin/poll/questions/_search.html.erb new file mode 100644 index 000000000..30cf0d6e5 --- /dev/null +++ b/app/views/admin/poll/questions/_search.html.erb @@ -0,0 +1,12 @@ +<%= form_tag(admin_questions_path, method: :get) do |f| %> +
+
+ <%= text_field_tag :search, + @search, + placeholder: t("admin.shared.spending_proposal_search.placeholder") %> +
+
+ <%= submit_tag t("admin.shared.spending_proposal_search.button"), class: "button" %> +
+
+<% end %> \ No newline at end of file diff --git a/app/views/admin/poll/questions/index.html.erb b/app/views/admin/poll/questions/index.html.erb index c09e7fe82..2aef2b6e5 100644 --- a/app/views/admin/poll/questions/index.html.erb +++ b/app/views/admin/poll/questions/index.html.erb @@ -3,6 +3,9 @@ <%= link_to t('admin.questions.index.create'), new_admin_question_path, class: "button success float-right" %> +<%= render 'filter' %> +<%= render 'search' %> + <% if @questions.count == 0 %>
<%= t('admin.questions.index.no_questions') %> diff --git a/db/migrate/20161129011737_add_tsv_to_poll_questions.rb b/db/migrate/20161129011737_add_tsv_to_poll_questions.rb new file mode 100644 index 000000000..a6a44f13d --- /dev/null +++ b/db/migrate/20161129011737_add_tsv_to_poll_questions.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index acd0e5344..4f7c23f63 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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 enable_extension "plpgsql" @@ -322,11 +322,13 @@ ActiveRecord::Schema.define(version: 20161122101702) do t.datetime "created_at" t.datetime "updated_at" t.boolean "all_geozones", default: false + t.tsvector "tsv" end 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", ["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| t.integer "booth_id"