diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index ebea082ea..a6f3fd817 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -85,7 +85,20 @@ module CommentableActions def parse_advanced_search_terms @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? + parse_search_date + end + + def parse_search_date + return unless search_by_date? + + start = params[:advanced_search][:date_min].to_time + finish = params[:advanced_search][:date_max].try(:to_time) || Time.now + + params[:advanced_search][:date_range] = start.beginning_of_day..finish.end_of_day + end + + def search_by_date? + params[:advanced_search] && params[:advanced_search][:date_min].present? end def set_search_order diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb new file mode 100644 index 000000000..8af4eaa52 --- /dev/null +++ b/app/helpers/search_helper.rb @@ -0,0 +1,23 @@ +module SearchHelper + + def official_level_options + options_for_select([ + [t("shared.advanced_search.author_type_1"), 1], + [t("shared.advanced_search.author_type_2"), 2], + [t("shared.advanced_search.author_type_3"), 3], + [t("shared.advanced_search.author_type_4"), 4], + [t("shared.advanced_search.author_type_5"), 5]], + params[:official_level]) + end + + def date_range_options + options_for_select([ + [t("shared.advanced_search.date_1"), 24.hours.ago], + [t("shared.advanced_search.date_2"), 7.days.ago], + [t("shared.advanced_search.date_3"), 30.days.ago], + [t("shared.advanced_search.date_4"), 365.days.ago], + [t("shared.advanced_search.date_5"), 'custom']], + params[:date_range]) + end + +end \ No newline at end of file diff --git a/app/models/concerns/filterable.rb b/app/models/concerns/filterable.rb index 40576f09c..274c19ac4 100644 --- a/app/models/concerns/filterable.rb +++ b/app/models/concerns/filterable.rb @@ -2,62 +2,28 @@ module Filterable extend ActiveSupport::Concern included do - scope :by_author, -> (author) { where(author: author) } - scope :by_official_level, -> (official_level) { where(users: { official_level: official_level }).joins(:author) } - scope :by_date_range, -> (start, finish) { where(created_at: start.beginning_of_day..finish.end_of_day) } + scope :by_author, -> (username) { where(users: { username: username }).includes(:author) } + scope :by_official_level, -> (official_level) { where(users: { official_level: official_level }).includes(:author) } + scope :by_date_range, -> (date_range) { where(created_at: date_range) } end class_methods do + def filter(params) resources = self.all - filters = parse_filters(params) - - if filters[:params_author] - author = User.where(username: filters[:params_author]).first - resources = author.present? ? self.by_author(author) : 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' - min_date_time = filters[:params_date_min].to_time + params.each do |filter, value| + if allowed_filter?(filter, value) + resources = resources.send("by_#{filter}", value) end - - max_date_time = filters[:params_date_max].present? ? filters[:params_date_max].to_time : DateTime.now - if min_date_time && max_date_time - resources = self.by_date_range(min_date_time, max_date_time) - end - end - - if filters[:params_author_type] - resources = self.by_official_level(filters[:params_author_type].to_i) 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 + def allowed_filter?(filter, value) + return if value.blank? + ['author', 'official_level', 'date_range'].include?(filter) end + end end \ No newline at end of file diff --git a/app/views/shared/_advanced_search.html.erb b/app/views/shared/_advanced_search.html.erb index a36ba7daf..ebe4b50e4 100644 --- a/app/views/shared/_advanced_search.html.erb +++ b/app/views/shared/_advanced_search.html.erb @@ -1,8 +1,3 @@ -<% # Params: - # - # search_path: for example proposals_path -%> -