Region filtering should now work with countries and their sub regions

This commit is contained in:
echarp 2017-09-17 18:25:34 +02:00
parent 0c2c80fc32
commit 5a672589bc
11 changed files with 30 additions and 24 deletions

View File

@ -13,11 +13,8 @@ class ApplicationController < ActionController::Base
end end
def set_filters def set_filters
if params.include? :region return unless params.include? :region
session[:region] = params[:region] session[:region] = params[:region] == 'all' ? nil : params[:region].to_i
else
params[:region] = session[:region]
end
end end
protected protected

View File

@ -26,7 +26,7 @@ class EventsController < ApplicationController
time = Time.zone.now.change(min: 0) + 1.day time = Time.zone.now.change(min: 0) + 1.day
@event = Event.new start_time: time, @event = Event.new start_time: time,
end_time: time + 1.hour, end_time: time + 1.hour,
region_id: params[:region] region_id: session[:region]
end end
# POST /events/preview # POST /events/preview

View File

@ -17,7 +17,7 @@ class OrgasController < ApplicationController
# GET /orgas/new # GET /orgas/new
def new def new
@orga = Orga.new region_id: params[:region] @orga = Orga.new region_id: session[:region]
end end
# POST /orgas # POST /orgas

View File

@ -63,7 +63,11 @@ class Event < ApplicationRecord
where '? <= end_time and start_time <= ?', where '? <= end_time and start_time <= ?',
start_date, start_date.end_of_week.end_of_day start_date, start_date.end_of_week.end_of_day
end) end)
scope :region, ->(region) { where region: region unless region == 'all' } scope :region, (lambda do |region|
return if region.nil? || region == 'all'
temp = Region.find region
where region: [temp, temp.regions].flatten
end)
scope :locality, ->(locality) { where locality: locality } scope :locality, ->(locality) { where locality: locality }
scope :tag, ->(tag) { tagged_with tag } scope :tag, ->(tag) { tagged_with tag }
scope :geo, -> { where 'latitude is not null and longitude is not null' } scope :geo, -> { where 'latitude is not null and longitude is not null' }

View File

@ -32,7 +32,11 @@ class Orga < ApplicationRecord
scope :period, ->(_year, _week) {} scope :period, ->(_year, _week) {}
scope :kind, ->(kind) { where kind: kind } scope :kind, ->(kind) { where kind: kind }
scope :region, ->(region) { where region: region unless region == 'all' } scope :region, (lambda do |region|
return if region.nil? || region == 'all'
temp = Region.find region
where region: [temp, temp.regions].flatten
end)
scope :tag, ->(tag) { tagged_with tag } scope :tag, ->(tag) { tagged_with tag }
scope :geo, -> { where 'latitude is not null and longitude is not null' } scope :geo, -> { where 'latitude is not null and longitude is not null' }
scope :active, -> { where active: true } scope :active, -> { where active: true }

View File

@ -39,11 +39,11 @@
= raw t '.calendar_in', = raw t '.calendar_in',
map: link_to(t('.map'), maps_path(tag: params[:tag])), map: link_to(t('.map'), maps_path(tag: params[:tag])),
rss: link_to('RSS', events_path(:rss, rss: link_to('RSS', events_path(:rss,
tag: params[:tag], region: params[:region])), tag: params[:tag], region: session[:region])),
webcal: link_to('webcal', events_path(tag: params[:tag], webcal: link_to('webcal', events_path(tag: params[:tag],
protocol: 'webcal', format: :ics, region: params[:region])), protocol: 'webcal', format: :ics, region: session[:region])),
ical: link_to('iCal', events_path(:rss, ical: link_to('iCal', events_path(:rss,
tag: params[:tag], format: :ics, region: params[:region])) tag: params[:tag], format: :ics, region: session[:region]))
\- \-

View File

@ -26,10 +26,7 @@ xml.rdf :RDF,
'xmlns:georss' => 'http://www.georss.org/georss' do 'xmlns:georss' => 'http://www.georss.org/georss' do
xml.channel 'rdf:about' => root_url do xml.channel 'rdf:about' => root_url do
title = t 'layouts.application.title' title = t 'layouts.application.title'
if params[:region].present? && params[:region] != 'all' title += " [#{Region.find(session[:region]).name}]" if session[:region]
region = Region.find(params[:region]).name
title += " [#{region}]"
end
xml.title title xml.title title
xml.description t 'layouts.application.subtitle' xml.description t 'layouts.application.subtitle'
xml.link root_url xml.link root_url

View File

@ -21,7 +21,7 @@
= label_tag :region, t('.region') = label_tag :region, t('.region')
= select_tag :region, = select_tag :region,
options_from_collection_for_select(Region.all, :id, :name, options_from_collection_for_select(Region.all, :id, :name,
params[:region]), session[:region]),
include_blank: true include_blank: true
.helper .helper

View File

@ -1,4 +1,4 @@
%li{ class: params[:region].to_i == filter_region.id ? 'selected' : '' } %li{ class: session[:region] == filter_region.id ? 'selected' : '' }
- if filter_region.url.present? - if filter_region.url.present?
= link_to filter_region.url do = link_to filter_region.url do
- if filter_region.code.present? - if filter_region.code.present?
@ -8,7 +8,7 @@
%em.fa.fa-external-link %em.fa.fa-external-link
- else - else
= link_to tag: params[:tag], = link_to tag: params[:tag],
region: params[:region].to_i == filter_region.id ? :all : filter_region, region: session[:region] == filter_region.id ? :all : filter_region,
start_date: params[:start_date], start_date: params[:start_date],
year: params[:year] do year: params[:year] do
- if filter_region.code.present? - if filter_region.code.present?
@ -16,7 +16,7 @@
- else - else
%em.fa.fa-shield %em.fa.fa-shield
= filter_region.name = filter_region.name
- if params[:region].to_i == filter_region.id - if session[:region] == filter_region.id
%em.fa.fa-close %em.fa.fa-close
- elsif filter_region.regions.present? - elsif filter_region.regions.present?
%small %small

View File

@ -2,10 +2,9 @@ require 'differ/format/patch'
Differ.format = Differ::Format::Patch Differ.format = Differ::Format::Patch
def gen_title def gen_title
t 'layouts.application.title' + t('layouts.application.title') +
if params[:region].present? if session[:region].present?
region = Region.find(params[:region]).name " [#{Region.find session[:region]}]"
" [#{region}]"
else else
'' ''
end end

View File

@ -94,6 +94,11 @@ class EventTest < ActiveSupport::TestCase
assert_match(/start_time <=/, Event.year(2014).where_values[0]) assert_match(/start_time <=/, Event.year(2014).where_values[0])
end end
test 'named scope region' do
assert Event.respond_to? :region
assert_not_nil Event.region Region.first.id
end
test 'geo data reset' do test 'geo data reset' do
# Setup geo data # Setup geo data
@event.latitude = 3 @event.latitude = 3