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 @@ +