Region filtering should now work with countries and their sub regions

rails5
echarp 5 years ago
parent 0c2c80fc32
commit 5a672589bc
  1. 7
      app/controllers/application_controller.rb
  2. 2
      app/controllers/events_controller.rb
  3. 2
      app/controllers/orgas_controller.rb
  4. 6
      app/models/event.rb
  5. 6
      app/models/orga.rb
  6. 6
      app/views/events/index.html.haml
  7. 5
      app/views/events/index.rss.builder
  8. 2
      app/views/pages/filter.haml
  9. 6
      app/views/regions/_filter_region.haml
  10. 7
      app/views/versions/index.rss.builder
  11. 5
      test/models/event_test.rb

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

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

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

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

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

@ -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]))
\-

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

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

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

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

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

Loading…
Cancel
Save