moves filters to a concern
This commit is contained in:
@@ -4,32 +4,7 @@ module CommentableActions
|
|||||||
|
|
||||||
def index
|
def index
|
||||||
@resources = @search_terms.present? ? resource_model.search(@search_terms) : resource_model.all
|
@resources = @search_terms.present? ? resource_model.search(@search_terms) : resource_model.all
|
||||||
|
@resources = @advanced_search_terms.present? ? @resources.filter(@advanced_search_terms) : @resources
|
||||||
#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 = @resources.tagged_with(@tag_filter) if @tag_filter
|
@resources = @resources.tagged_with(@tag_filter) if @tag_filter
|
||||||
@resources = @resources.page(params[:page]).for_render.send("sort_by_#{@current_order}")
|
@resources = @resources.page(params[:page]).for_render.send("sort_by_#{@current_order}")
|
||||||
@@ -109,16 +84,8 @@ module CommentableActions
|
|||||||
end
|
end
|
||||||
|
|
||||||
def parse_advanced_search_terms
|
def parse_advanced_search_terms
|
||||||
search = params[:advanced_search]
|
@advanced_search_present = @advanced_search_terms = params[:advanced_search] if params[:advanced_search].present?
|
||||||
if search
|
@search_terms = params[:advanced_search][:search] if params[:advanced_search] && params[:advanced_search][:search].present?
|
||||||
@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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_search_order
|
def set_search_order
|
||||||
|
|||||||
55
app/models/concerns/filterable.rb
Normal file
55
app/models/concerns/filterable.rb
Normal file
@@ -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
|
||||||
@@ -6,6 +6,7 @@ class Debate < ActiveRecord::Base
|
|||||||
include Measurable
|
include Measurable
|
||||||
include Sanitizable
|
include Sanitizable
|
||||||
include PgSearch
|
include PgSearch
|
||||||
|
include Filterable
|
||||||
|
|
||||||
apply_simple_captcha
|
apply_simple_captcha
|
||||||
acts_as_votable
|
acts_as_votable
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ class Proposal < ActiveRecord::Base
|
|||||||
include Sanitizable
|
include Sanitizable
|
||||||
include PgSearch
|
include PgSearch
|
||||||
include SearchCache
|
include SearchCache
|
||||||
|
include Filterable
|
||||||
|
|
||||||
apply_simple_captcha
|
apply_simple_captcha
|
||||||
acts_as_votable
|
acts_as_votable
|
||||||
@@ -70,6 +71,10 @@ class Proposal < ActiveRecord::Base
|
|||||||
values
|
values
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.search(terms)
|
||||||
|
self.pg_search(terms)
|
||||||
|
end
|
||||||
|
|
||||||
def description
|
def description
|
||||||
super.try :html_safe
|
super.try :html_safe
|
||||||
end
|
end
|
||||||
@@ -123,10 +128,6 @@ class Proposal < ActiveRecord::Base
|
|||||||
self.tags.each{ |t| t.increment_custom_counter_for('Proposal') }
|
self.tags.each{ |t| t.increment_custom_counter_for('Proposal') }
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search(terms)
|
|
||||||
self.pg_search(terms)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.votes_needed_for_success
|
def self.votes_needed_for_success
|
||||||
Setting.value_for('votes_for_proposal_success').to_i
|
Setting.value_for('votes_for_proposal_success').to_i
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user