refactors filterable concern

This commit is contained in:
rgarcia
2016-01-13 15:04:51 +01:00
parent 0cfddca432
commit 66b704b3b9
7 changed files with 97 additions and 87 deletions

View File

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

View File

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

View File

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

View File

@@ -1,8 +1,3 @@
<% # Params:
#
# search_path: for example proposals_path
%>
<div class="small-12 medium-12 columns">
<h4 class='advanced-search-title'><%= t("shared.advanced_search.title") %></h4>
</div>
@@ -12,30 +7,37 @@
<div class="small-12 medium-12 advanced-search columns">
<h5 class='search-option inline-block'><%= t("shared.advanced_search.general") %></h5>
<%= text_field_tag "advanced_search[search]", @search_terms, placeholder: t("shared.advanced_search.general_placeholder") %>
<%= text_field_tag "search", @search_terms,
placeholder: t("shared.advanced_search.general_placeholder") %>
</div>
<div class="small-12 medium-6 advanced-search columns">
<h5 class='search-option inline-block'><%= t("shared.advanced_search.author") %></h5>
<%= text_field_tag "advanced_search[author]", @params_author, placeholder: t("shared.advanced_search.author_placeholder") %>
<%= text_field_tag "advanced_search[author]", @params_author,
placeholder: t("shared.advanced_search.author_placeholder") %>
</div>
<div class="small-12 medium-6 advanced-search columns">
<h5 class='search-option'><%= t("shared.advanced_search.author_type") %></h5>
<%= select_tag('advanced_search[author_type]', 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_author_type), include_blank: t("shared.advanced_search.author_type_blank")) %>
<%= select_tag('advanced_search[official_level]', official_level_options,
include_blank: t("shared.advanced_search.author_type_blank")) %>
</div>
<div class="small-12 advanced-search columns">
<h5 class='search-option'><%= t("shared.advanced_search.date") %></h5>
<%= select_tag('advanced_search[date]', options_for_select([[t("shared.advanced_search.date_1"), 1], [t("shared.advanced_search.date_2"), 2], [t("shared.advanced_search.date_3"), 3],[t("shared.advanced_search.date_4"), 4], [t("shared.advanced_search.date_5"), 5]], @params_date), include_blank: 'Elige una fecha') %>
<%= select_tag('advanced_search[date_min]', date_range_options,
include_blank: 'Elige una fecha') %>
<div class='customized-date inline-block'>
&nbsp;&nbsp;<%= t("shared.advanced_search.from") %>:
&nbsp;&nbsp;
<%= t("shared.advanced_search.from") %>:
<div class='inline-block'>
<%= date_field 'advanced_search', 'date_min', max:Date.today, value: @params_date_min %>
<%= date_field 'advanced_search', 'date_min', max: Date.today, value: @params_date_min %>
</div>
&nbsp;&nbsp;<%= t("shared.advanced_search.to") %>:
&nbsp;&nbsp;
<%= t("shared.advanced_search.to") %>:
<div class='inline-block'>
<%= date_field 'advanced_search', 'date_max', max:Date.today, value: @params_date_max %>
<%= date_field 'advanced_search', 'date_max', max: Date.today, value: @params_date_max %>
</div>
</div>
</div>
@@ -73,14 +75,16 @@ $('.advanced-search-title').on({
//hide customized-date if it is not selected
if($('#advanced_search_date').val() != 5){
if($('#advanced_search_date_min').val() != 'custom'){
$('.customized-date').hide();
$('.customized-date input').prop('disabled', true);
}
$('#advanced_search_date').on('change',function(e){
$('#advanced_search_date_min').on('change',function(e){
var next = $(e.target).next();
if($(this).val() == 5){
if($(this).val() == 'custom'){
next.show();
$('.customized-date input').prop('disabled', false);
}
else{
next.hide();

View File

@@ -1,10 +1,4 @@
<% # Params:
#
# i18n_namespace: for example "debates.index.search_form"
# search_path: for example debates_path
%>
<div class="search-form">
<div id="search_form" class="search-form">
<div class="row">
<%= form_tag search_path, method: :get do %>

View File

@@ -462,8 +462,11 @@ feature 'Debates' do
debate3 = create(:debate, title: "Do not show me")
visit debates_path
fill_in "search", with: "Schwifty"
click_button "Search"
within "#search_form" do
fill_in "search", with: "Schwifty"
click_button "Search"
end
within("#debates") do
expect(page).to have_css('.debate', count: 2)
@@ -478,7 +481,7 @@ feature 'Debates' do
context "Advanced search" do
scenario "Search by text", :js do
scenario "Search by text", :js, :focus do
debate1 = create(:debate, title: "Get Schwifty")
debate2 = create(:debate, title: "Schwifty Hello")
debate3 = create(:debate, title: "Do not show me")
@@ -522,7 +525,7 @@ feature 'Debates' do
end
#NOTE: Test the different offical levels with unit tests.
scenario "Search by author category", :js do
scenario "Search by author type", :js do
ana = create :user, official_level: 1
john = create :user, official_level: 2
@@ -533,7 +536,7 @@ feature 'Debates' do
visit debates_path
find("h4.advanced-search-title").click
select "Public employee", from: "advanced_search_author_type"
select "Public employee", from: "advanced_search_official_level"
click_button "Filter"
within("#debates") do
@@ -556,7 +559,7 @@ feature 'Debates' do
visit debates_path
find("h4.advanced-search-title").click
select "Last 24 hours", from: "advanced_search_date"
select "Last 24 hours", from: "advanced_search_date_min"
click_button "Filter"
within("#debates") do
@@ -576,7 +579,7 @@ feature 'Debates' do
visit debates_path
find("h4.advanced-search-title").click
select "Customized", from: "advanced_search_date"
select "Customized", from: "advanced_search_date_min"
fill_in "advanced_search_date_min", with: 7.days.ago
fill_in "advanced_search_date_max", with: 1.days.ago
click_button "Filter"
@@ -636,8 +639,10 @@ feature 'Debates' do
debate = create(:debate, title: "Abcdefghi")
visit debates_path
fill_in "search", with: debate.title
click_button "Search"
within "#search_form" do
fill_in "search", with: debate.title
click_button "Search"
end
expect(page).to_not have_selector('#debates .debate-featured')
expect(page).to_not have_selector('#featured-debates')

View File

@@ -525,8 +525,11 @@ feature 'Proposals' do
proposal3 = create(:proposal, title: "Do not show me")
visit proposals_path
fill_in "search", with: "Schwifty"
click_button "Search"
within "#search_form" do
fill_in "search", with: "Schwifty"
click_button "Search"
end
within("#proposals") do
expect(page).to have_css('.proposal', count: 2)
@@ -541,7 +544,7 @@ feature 'Proposals' do
context "Advanced search" do
scenario "Search by text", :js do
scenario "Search by text", :js, :focus do
proposal1 = create(:proposal, title: "Get Schwifty")
proposal2 = create(:proposal, title: "Schwifty Hello")
proposal3 = create(:proposal, title: "Do not show me")
@@ -585,7 +588,7 @@ feature 'Proposals' do
end
#NOTE: Test the different offical levels with unit tests.
scenario "Search by author category", :js do
scenario "Search by author type", :js do
ana = create :user, official_level: 1
john = create :user, official_level: 2
@@ -596,7 +599,7 @@ feature 'Proposals' do
visit proposals_path
find("h4.advanced-search-title").click
select "Public employee", from: "advanced_search_author_type"
select "Public employee", from: "advanced_search_official_level"
click_button "Filter"
within("#proposals") do
@@ -619,7 +622,7 @@ feature 'Proposals' do
visit proposals_path
find("h4.advanced-search-title").click
select "Last 24 hours", from: "advanced_search_date"
select "Last 24 hours", from: "advanced_search_date_min"
click_button "Filter"
within("#proposals") do
@@ -639,7 +642,7 @@ feature 'Proposals' do
visit proposals_path
find("h4.advanced-search-title").click
select "Customized", from: "advanced_search_date"
select "Customized", from: "advanced_search_date_min"
fill_in "advanced_search_date_min", with: 7.days.ago
fill_in "advanced_search_date_max", with: 1.days.ago
click_button "Filter"
@@ -699,8 +702,10 @@ feature 'Proposals' do
proposal = create(:proposal, title: "Abcdefghi")
visit proposals_path
fill_in "search", with: proposal.title
click_button "Search"
within "#search_form" do
fill_in "search", with: proposal.title
click_button "Search"
end
expect(page).to_not have_selector('#proposals .proposal-featured')
expect(page).to_not have_selector('#featured-proposals')