From a75f7c5314e125c2a80dca56b23884c52fd84bad Mon Sep 17 00:00:00 2001 From: echarp Date: Wed, 17 Oct 2018 15:47:43 +0200 Subject: [PATCH] Permanent filters: "near" and "tag" --- Gemfile | 1 + Gemfile.lock | 5 ++++- app/assets/javascripts/filter.js.coffee | 6 ++---- app/assets/stylesheets/all.sass | 10 ++++++---- app/assets/stylesheets/form.sass | 2 +- app/assets/stylesheets/mobile.sass | 6 +----- app/assets/stylesheets/orgas.sass | 3 ++- app/assets/stylesheets/pages.sass | 7 +++++++ app/assets/stylesheets/region.sass | 1 + app/controllers/application_controller.rb | 14 +++++++------- app/controllers/stats_controller.rb | 19 ++++++++++--------- app/models/event.rb | 2 -- app/models/orga.rb | 2 -- app/views/layouts/application.html.haml | 4 +++- app/views/maps/index.html.haml | 3 --- app/views/orgas/_types.haml | 4 ++-- app/views/pages/_search.haml | 18 ++++++++++++++++++ app/views/pages/filter.haml | 10 ++++++---- app/views/regions/_filter_region.haml | 10 ++++------ 19 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 app/views/pages/_search.haml diff --git a/Gemfile b/Gemfile index cf81456d..38fd5bdf 100644 --- a/Gemfile +++ b/Gemfile @@ -46,6 +46,7 @@ gem 'differ' # A generic library to administrate the tool gem 'activeadmin' gem 'has_scope' +gem 'preserve' # Markdown display gem 'kramdown' diff --git a/Gemfile.lock b/Gemfile.lock index 7534194f..66c0f030 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -114,7 +114,7 @@ GEM ffi (1.9.25) flag-icons-rails (3.1.0) sass (~> 3.2) - font-awesome-sass (5.3.1) + font-awesome-sass (5.4.1) sassc (>= 1.11) formatador (0.2.5) formtastic (3.1.5) @@ -272,6 +272,8 @@ GEM activesupport rails (>= 3.0.0) powerpack (0.1.2) + preserve (0.1.0) + rails (>= 3.0) pry (0.11.3) coderay (~> 1.1.0) method_source (~> 0.9.0) @@ -476,6 +478,7 @@ DEPENDENCIES mysql2 paper_trail piwik_analytics + preserve puma (~> 3.7) rack-livereload rails (>= 5.1.4) diff --git a/app/assets/javascripts/filter.js.coffee b/app/assets/javascripts/filter.js.coffee index 5e1ea10f..d5257387 100644 --- a/app/assets/javascripts/filter.js.coffee +++ b/app/assets/javascripts/filter.js.coffee @@ -2,8 +2,6 @@ $(document).on 'turbolinks:load', -> $('body.pages form :input').prop 'disabled', false - $('body.pages form').submit -> + $('form').submit -> $('input[name=utf8]').prop 'disabled', true - $(':input', this).filter -> - this.value.length == 0 && this.name != 'region' - .prop 'disabled', true + $('button').prop 'disabled', true diff --git a/app/assets/stylesheets/all.sass b/app/assets/stylesheets/all.sass index c17dabd5..41f1010e 100644 --- a/app/assets/stylesheets/all.sass +++ b/app/assets/stylesheets/all.sass @@ -74,18 +74,19 @@ header.top text-align: left img.logo float: left - margin-right: 2% + margin-right: 1% h1 - margin-top: 0 - margin-bottom: 0.2em + margin: 0 + padding: 0 h2 font: size: inherit style: italic weight: normal - margin-top: 0 + margin-top: 0.2em main, body.mce-content-body + clear: both position: relative h3 text-align: left @@ -109,6 +110,7 @@ aside z-index: 10 padding: 5px position: relative + max-width: 20em box-shadow: 4px 4px 1em gray margin-right: -4em border-radius: 1em diff --git a/app/assets/stylesheets/form.sass b/app/assets/stylesheets/form.sass index dd0c4370..817457c4 100644 --- a/app/assets/stylesheets/form.sass +++ b/app/assets/stylesheets/form.sass @@ -180,7 +180,7 @@ input, textarea, select, a.button, .actions > button, .mce-tinymce, div.tagsinpu &.period_week label:before content: fa-content($fa-var-step-backward) &.near_location label:before - content: fa-content($fa-var-dot-circle) + content: fa-content($fa-var-map-pin) &.near_distance label:before content: fa-content($fa-var-arrows-alt-h) &.iframe label:before diff --git a/app/assets/stylesheets/mobile.sass b/app/assets/stylesheets/mobile.sass index 4bf8ade2..4da70c41 100644 --- a/app/assets/stylesheets/mobile.sass +++ b/app/assets/stylesheets/mobile.sass @@ -17,7 +17,7 @@ main ul margin-left: 0 - form#orga_search + form#orga_search, button.search, .digest font-size: smaller !important table.list.dates @@ -35,14 +35,10 @@ padding-right: 0.4em header.top - text-align: center nav display: none img.logo margin-right: 0 - h1 - padding-top: 0 - letter-spacing: initial form#orga_search display: none diff --git a/app/assets/stylesheets/orgas.sass b/app/assets/stylesheets/orgas.sass index e9d9421f..d42c1abb 100644 --- a/app/assets/stylesheets/orgas.sass +++ b/app/assets/stylesheets/orgas.sass @@ -7,7 +7,8 @@ form#orga_search clear: right float: right font-size: larger - margin-top: 1em + margin-top: 0.9em + margin-left: 1em label display: none em.fa diff --git a/app/assets/stylesheets/pages.sass b/app/assets/stylesheets/pages.sass index 598058fa..44abdef7 100644 --- a/app/assets/stylesheets/pages.sass +++ b/app/assets/stylesheets/pages.sass @@ -1,3 +1,10 @@ body.pages main h3, h4 text-align: left + +.digest + float: right + font-size: larger + margin-top: 1em + span + margin-left: 1em diff --git a/app/assets/stylesheets/region.sass b/app/assets/stylesheets/region.sass index c8ef5120..db992afd 100644 --- a/app/assets/stylesheets/region.sass +++ b/app/assets/stylesheets/region.sass @@ -1,5 +1,6 @@ header nav + max-width: calc(99% - 91px) margin-top: -3.1em &:hover nav margin-top: 0 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2647aac8..3fb06961 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,12 @@ # The top level controller, where can be centralised almost everything class ApplicationController < ActionController::Base - before_action :set_paper_trail_whodunnit, :set_filters + before_action :set_paper_trail_whodunnit, :discard # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery prepend: true, with: :exception + preserve :region, :tag, :near + layout :handle_xhr_layout private @@ -14,12 +16,10 @@ class ApplicationController < ActionController::Base http_accept_language.compatible_language_from I18n.available_locales end - # Mechanism to manage the region filter - def set_filters - if params.include? :region - session[:region] = params[:region] == 'all' ? nil : params[:region].to_i - end - params[:region] ||= session[:region] + # Mechanism to manage filter resets + def discard + params.keys.keep_if { |key| params[key].empty? } + .each { |key| session.delete ['application', key].join '_' } end protected diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 58175508..dd8deed6 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -8,28 +8,29 @@ class StatsController < ApplicationController private def counts - @events_count = @events.count - @events_um_count = apply_scopes(Event.unmoderated).count - @orgas_count = apply_scopes(Orga).moderated.count - @orgas_um_count = apply_scopes(Orga.unmoderated).count + @events_count = @events.count :all + @events_um_count = apply_scopes(Event.unmoderated).count :all + @orgas_count = apply_scopes(Orga).moderated.count :all + @orgas_um_count = apply_scopes(Orga.unmoderated).count :all end def temporal - @years = @events.group(year_grouping).count - @months = @events.group(year_grouping, month_grouping).count + @years = @events.group(year_grouping).count :all + @months = @events.group(year_grouping, month_grouping).count :all end def local - @region_events = @events.group(:region_id, year_grouping).count + @region_events = @events.group(:region_id, year_grouping).count :all @regions = Region.all.find_all do |region| @years.sum { |year| @region_events[[region.id, year[0]]] || 0 }.positive? end @city_events = @events.group(:city).having('count(city) > 3') - .order('count(city) desc').count + .order('count(city) desc').count :all end def set_events - @events = apply_scopes Event.moderated + # Remove the ordering which can occur after geocoding + @events = apply_scopes(Event.moderated).reorder nil end def year_grouping diff --git a/app/models/event.rb b/app/models/event.rb index 0cf84c78..bd3f9c52 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -63,8 +63,6 @@ class Event < ApplicationRecord start_date, start_date.end_of_week.end_of_day end) scope :region, (lambda do |region| - return if region.nil? || region == 'all' || region.to_i.zero? - temp = Region.find region where region: [temp, temp.regions].flatten end) diff --git a/app/models/orga.rb b/app/models/orga.rb index 2632ef97..f89911e3 100644 --- a/app/models/orga.rb +++ b/app/models/orga.rb @@ -33,8 +33,6 @@ class Orga < ApplicationRecord scope :kind, ->(kind) { where kind: kind } scope :region, (lambda do |region| - return if region.nil? || region == 'all' || region.to_i.zero? - temp = Region.find region where region: [temp, temp.regions].flatten end) diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 09826571..f02c7d48 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -20,9 +20,11 @@ - sub = request.domain ? request.domain.split('.')[0] : '' %body{ class: [sub, controller_name, action_name] } %header.top - = render '/regions/filter' = image_tag 'baby_gnu_adl.png', alt: '', class: :logo + + = render '/regions/filter' = render '/orgas/search' + = render '/pages/search' %h1= link_to t('.title'), root_path %h2= t '.subtitle' diff --git a/app/views/maps/index.html.haml b/app/views/maps/index.html.haml index 3d62391b..9580445a 100644 --- a/app/views/maps/index.html.haml +++ b/app/views/maps/index.html.haml @@ -1,9 +1,6 @@ %h2 %em.fa.fa-map = title t '.title' - - if params[:tag] - %em.fa.fa-tag - %em= params[:tag] %ul.list#map %li diff --git a/app/views/orgas/_types.haml b/app/views/orgas/_types.haml index b90ed9ed..ef08c014 100644 --- a/app/views/orgas/_types.haml +++ b/app/views/orgas/_types.haml @@ -3,7 +3,7 @@ %ul.counters -# The reorder and specific count are required for the "near" scope - - @unfiltered_orgas.reorder('').group(:kind).count.each do |kind, count| + - @unfiltered_orgas.reorder('').group(:kind).count(:all).each do |kind, count| - p[:kind_id_eq] = kind.id %li{ title: Kind.human_attribute_name("name_#{kind.name}") } = link_to orgas_url(q: p) do @@ -16,4 +16,4 @@ | = link_to orgas_url(q: p) do %em.fa.fa-heartbeat - = @unfiltered_orgas.active.count + = @unfiltered_orgas.active.count :all diff --git a/app/views/pages/_search.haml b/app/views/pages/_search.haml new file mode 100644 index 00000000..bdf3c7d2 --- /dev/null +++ b/app/views/pages/_search.haml @@ -0,0 +1,18 @@ +- if action_name == 'index' && controller_name != 'tags' + .digest + - if params[:tag].present? + %span.tag + %em.fa.fa-tag + = params[:tag] + %a(href="?tag=") + %em.fa.fa-times + + - if params[:near].present? + %span.near + %em.fa.fa-map-pin + = params[:near][:location] + %em.fa.fa-arrows-alt-h + = params[:near][:distance] + km + %a(href="?near=") + %em.fa.fa-times diff --git a/app/views/pages/filter.haml b/app/views/pages/filter.haml index 86e76949..7a875b37 100644 --- a/app/views/pages/filter.haml +++ b/app/views/pages/filter.haml @@ -9,12 +9,14 @@ = form_tag events_url, method: :get do .field.near_location = label_tag 'near[location]', t('.near_location') - = text_field_tag 'near[location]', params['near[location]'], + = text_field_tag 'near[location]', + params[:near].present? ? params[:near][:location] : '', placeholder: t('.near_location_helper') .field.near_distance = label_tag 'near[distance]', t('.near_distance') - = number_field_tag 'near[distance]', params['near[distance]'], + = number_field_tag 'near[distance]', + params[:near].present? ? params[:near][:distance] : '', placeholder: t('.near_distance_helper') %span.helper km @@ -22,12 +24,12 @@ = label_tag :region, t('.region') = select_tag :region, options_from_collection_for_select(Region.all, :id, :name, - session[:region]), + params[:region]), include_blank: true .field.tags = label_tag :tag, t('.tag'), for: 'tag_tag' - = text_field_tag :tag + = text_field_tag :tag, params[:tag] .field.future = label_tag :future, t('.past') diff --git a/app/views/regions/_filter_region.haml b/app/views/regions/_filter_region.haml index 01a51096..3e31a43f 100644 --- a/app/views/regions/_filter_region.haml +++ b/app/views/regions/_filter_region.haml @@ -1,4 +1,4 @@ -%li{ class: session[:region] == filter_region.id ? 'selected' : '' } +%li{ class: params[:region] == filter_region.id.to_s ? 'selected' : '' } - if filter_region.url.present? = link_to filter_region.url do - if filter_region.code.present? @@ -7,16 +7,14 @@ %small %em.fa.fa-external-link-alt - else - = link_to tag: params[:tag], - region: session[:region] == filter_region.id ? :all : filter_region, - start_date: params[:start_date], - year: params[:year] do + = link_to start_date: params[:start_date], year: params[:year], + region: params[:region] == filter_region.id.to_s ? '' : filter_region do - if filter_region.code.present? = flag_icon filter_region.code.downcase - else %em.fa.fa-shield-alt = filter_region.name - - if session[:region] == filter_region.id + - if params[:region] == filter_region.id.to_s %em.fa.fa-times - elsif !filter_region.region && filter_region.regions.present? %small