From d147cec9293d3e778a7f2584af96dc8842f2945d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 12 Jan 2016 15:18:42 +0100 Subject: [PATCH] moves filters to a concern --- .../concerns/commentable_actions.rb | 39 +------------ app/models/concerns/filterable.rb | 55 +++++++++++++++++++ app/models/debate.rb | 1 + app/models/proposal.rb | 9 +-- 4 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 app/models/concerns/filterable.rb diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index d95caaa8e..ebea082ea 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -4,32 +4,7 @@ module CommentableActions def index @resources = @search_terms.present? ? resource_model.search(@search_terms) : resource_model.all - - #advanced search filters - if @params_author - author = User.where(username: @params_author) - @resources = author.count > 0 ? @resources.where(author_id: author.first.id) : resource_model.none - end - if @params_date - case @params_date - when '1' - min_date_time = DateTime.now - 24.hour - when '2' - min_date_time = DateTime.now - 7.day - when '3' - min_date_time = DateTime.now - 30.day - when '4' - min_date_time = DateTime.now - 365.day - when '5' - @resources = @resources.where('created_at <= ?', @params_date_max) if @params_date_max - min_date_time = @params_date_min - end - @resources = @resources.where('created_at >= ?', min_date_time) if min_date_time - end - if @params_author_type - authors = User.where(official_level: @params_author_type.to_i) - @resources = @resources.where('author_id IN (?)', authors.map(&:id)) - end + @resources = @advanced_search_terms.present? ? @resources.filter(@advanced_search_terms) : @resources @resources = @resources.tagged_with(@tag_filter) if @tag_filter @resources = @resources.page(params[:page]).for_render.send("sort_by_#{@current_order}") @@ -109,16 +84,8 @@ module CommentableActions end def parse_advanced_search_terms - search = params[:advanced_search] - if search - @params_author = search[:author] if search[:author].present? - @params_author_type = search[:author_type] if search[:author_type].present? - @params_date = search[:date] if search[:date].present? - @params_date_min = search[:date_min] if (@params_date == '5') && search[:date_min].present? - @params_date_max = search[:date_max] if (@params_date == '5') && search[:date_max].present? - @search_terms = search[:search] if search[:search].present? - @advanced_search_present = true if params[:commit] || @params_author || @params_author_type || @params_date - end + @advanced_search_present = @advanced_search_terms = params[:advanced_search] if params[:advanced_search].present? + @search_terms = params[:advanced_search][:search] if params[:advanced_search] && params[:advanced_search][:search].present? end def set_search_order diff --git a/app/models/concerns/filterable.rb b/app/models/concerns/filterable.rb new file mode 100644 index 000000000..2b84e4651 --- /dev/null +++ b/app/models/concerns/filterable.rb @@ -0,0 +1,55 @@ +module Filterable + extend ActiveSupport::Concern + + class_methods do + + def filter(params) + resources = self.all + filters = parse_filters(params) + + if filters[:params_author] + author = User.where(username: filters[:params_author]) + resources = author.count > 0 ? self.where(author_id: author.first.id) : self.none + end + if filters[:params_date] + case filters[:params_date] + when '1' + min_date_time = DateTime.now - 24.hour + when '2' + min_date_time = DateTime.now - 7.day + when '3' + min_date_time = DateTime.now - 30.day + when '4' + min_date_time = DateTime.now - 365.day + when '5' + resources = self.where('created_at <= ?', filters[:params_date_max]) if filters[:params_date_max] + min_date_time = filters[:params_date_min] + end + resources = self.where('created_at >= ?', min_date_time) if min_date_time + end + if filters[:params_author_type] + authors = User.where(official_level: filters[:params_author_type].to_i) + resources = self.where('author_id IN (?)', authors.map(&:id)) + end + resources + end + + def parse_filters(params) + search = params + filters = {} + if search + filters[:params_author] = search[:author] if search[:author].present? + filters[:params_author_type] = search[:author_type] if search[:author_type].present? + + filters[:params_date] = search[:date] if search[:date].present? + filters[:params_date_min] = search[:date_min] if (filters[:params_date] == '5') && search[:date_min].present? + filters[:params_date_max] = search[:date_max] if (filters[:params_date]== '5') && search[:date_max].present? + + filters[:advanced_search_present] = true if params[:commit] || filters[:params_author] || filters[:params_author_type] || filters[:params_date] + end + filters + end + + end + +end \ No newline at end of file diff --git a/app/models/debate.rb b/app/models/debate.rb index 086bd5ae4..db11e4b7c 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -6,6 +6,7 @@ class Debate < ActiveRecord::Base include Measurable include Sanitizable include PgSearch + include Filterable apply_simple_captcha acts_as_votable diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 22fb55cbd..ed2bf71a2 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -6,6 +6,7 @@ class Proposal < ActiveRecord::Base include Sanitizable include PgSearch include SearchCache + include Filterable apply_simple_captcha acts_as_votable @@ -70,6 +71,10 @@ class Proposal < ActiveRecord::Base values end + def self.search(terms) + self.pg_search(terms) + end + def description super.try :html_safe end @@ -123,10 +128,6 @@ class Proposal < ActiveRecord::Base self.tags.each{ |t| t.increment_custom_counter_for('Proposal') } end - def self.search(terms) - self.pg_search(terms) - end - def self.votes_needed_for_success Setting.value_for('votes_for_proposal_success').to_i end