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
def set_filters
if params.include? :region
session[:region] = params[:region]
else
params[:region] = session[:region]
end
return unless params.include? :region
session[:region] = params[:region] == 'all' ? nil : params[:region].to_i
end
protected

View File

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

View File

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

View File

@ -63,7 +63,11 @@ class Event < ApplicationRecord
where '? <= end_time and start_time <= ?',
start_date, start_date.end_of_week.end_of_day
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 :tag, ->(tag) { tagged_with tag }
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 :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 :geo, -> { where 'latitude is not null and longitude is not null' }
scope :active, -> { where active: true }

View File

@ -39,11 +39,11 @@
= raw t '.calendar_in',
map: link_to(t('.map'), maps_path(tag: params[:tag])),
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],
protocol: 'webcal', format: :ics, region: params[:region])),
protocol: 'webcal', format: :ics, region: session[:region])),
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
xml.channel 'rdf:about' => root_url do
title = t 'layouts.application.title'
if params[:region].present? && params[:region] != 'all'
region = Region.find(params[:region]).name
title += " [#{region}]"
end
title += " [#{Region.find(session[:region]).name}]" if session[:region]
xml.title title
xml.description t 'layouts.application.subtitle'
xml.link root_url

View File

@ -21,7 +21,7 @@
= label_tag :region, t('.region')
= select_tag :region,
options_from_collection_for_select(Region.all, :id, :name,
params[:region]),
session[:region]),
include_blank: true
.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?
= link_to filter_region.url do
- if filter_region.code.present?
@ -8,7 +8,7 @@
%em.fa.fa-external-link
- else
= 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],
year: params[:year] do
- if filter_region.code.present?
@ -16,7 +16,7 @@
- else
%em.fa.fa-shield
= filter_region.name
- if params[:region].to_i == filter_region.id
- if session[:region] == filter_region.id
%em.fa.fa-close
- elsif filter_region.regions.present?
%small

View File

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

View File

@ -94,6 +94,11 @@ class EventTest < ActiveSupport::TestCase
assert_match(/start_time <=/, Event.year(2014).where_values[0])
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
# Setup geo data
@event.latitude = 3