From f202c23116adf0ac05ea10162e024818c9b04192 Mon Sep 17 00:00:00 2001 From: echarp Date: Sat, 10 May 2014 11:38:39 +0200 Subject: [PATCH] =?UTF-8?q?Les=20relations=20allant=20d'event=20=C3=A0=20c?= =?UTF-8?q?ity=20et=20de=20lug=20=C3=A0=20city=20sont=20maintenant=20des?= =?UTF-8?q?=20relations=20appropri=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/maps.js.coffee | 2 +- app/assets/stylesheets/maps.css.sass | 2 +- app/controllers/events_controller.rb | 2 +- app/controllers/maps_controller.rb | 16 ++-------------- app/models/city.rb | 6 ++++++ app/models/event.rb | 7 +------ app/models/lug.rb | 7 +------ app/views/events/index.rss.builder | 3 +-- app/views/events/show.html.haml | 3 +-- app/views/maps/index.html.haml | 6 ++---- test/fixtures/cities.yml | 8 ++++---- test/fixtures/events.yml | 4 ++-- 12 files changed, 23 insertions(+), 43 deletions(-) diff --git a/app/assets/javascripts/maps.js.coffee b/app/assets/javascripts/maps.js.coffee index 0f05d418..398db3d4 100644 --- a/app/assets/javascripts/maps.js.coffee +++ b/app/assets/javascripts/maps.js.coffee @@ -11,7 +11,7 @@ $(document).ready -> layers: 'basic' } map.addLayer layer - map.setCenter new OpenLayers.LonLat(2.5, 47), 6 + map.setCenter new OpenLayers.LonLat(2.5, 46.4), 7 map.addControl new OpenLayers.Control.LayerSwitcher() map.addLayer eventsLayer diff --git a/app/assets/stylesheets/maps.css.sass b/app/assets/stylesheets/maps.css.sass index 61c0e077..635fbe38 100644 --- a/app/assets/stylesheets/maps.css.sass +++ b/app/assets/stylesheets/maps.css.sass @@ -1,3 +1,3 @@ #map width: 100% - height: 40em + height: 60em diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 85bf7cd6..925fc9be 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -19,7 +19,7 @@ class EventsController < InheritedResources::Base } format.rss { - @events = @events.future_30 + @events = @events.future_30.includes(:related_city) @events = @events.limit params[:daylimit] if params[:daylimit] } diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index aa7c4765..2f0ce646 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -1,19 +1,7 @@ class MapsController < ApplicationController def index - @events = Event - if (params[:region] && params[:region].present? && params[:region] != 'all') - @events = @events.region(params[:region]) - end - @events = @events.tag(params[:tag]) if (params[:tag]) + @cities_event = City.joins(:events).where('start_time > ?', 360.days.ago) - @events = @events.where('start_time > ?', 360.days.ago).order :id - - @cities_event = @events.collect { |event| - event.related_city - }.uniq.keep_if { |city| city } - - @cities_lug = Lug.all.collect { |lug| - lug.related_city - }.uniq.keep_if { |city| city } + @cities_lug = City.joins :lugs end end diff --git a/app/models/city.rb b/app/models/city.rb index ceba1d0c..fb0e229f 100644 --- a/app/models/city.rb +++ b/app/models/city.rb @@ -1,2 +1,8 @@ +# This class is linked to events and lugs +# +# It is mainly used to manage coordinates +# class City < ActiveRecord::Base + has_many :events, foreign_key: :city, primary_key: :name + has_many :lugs, foreign_key: :city, primary_key: :name end diff --git a/app/models/event.rb b/app/models/event.rb index 2997a892..1fb9d86e 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,6 +1,7 @@ class Event < ActiveRecord::Base belongs_to :region, foreign_key: 'region' has_many :notes + has_one :related_city, foreign_key: :name, primary_key: :city, class_name: City validates_presence_of :region @@ -32,10 +33,4 @@ class Event < ActiveRecord::Base def same_day? start_time.to_date == end_time.to_date end - - def related_city - if city.present? - City.find_by_majname city.gsub('-', ' ').upcase - end - end end diff --git a/app/models/lug.rb b/app/models/lug.rb index 4a5e6230..b264edcd 100644 --- a/app/models/lug.rb +++ b/app/models/lug.rb @@ -1,9 +1,4 @@ class Lug < ActiveRecord::Base belongs_to :region, foreign_key: 'region' - - def related_city - if city.present? - City.find_by_majname city.gsub('-', ' ').upcase - end - end + has_one :related_city, foreign_key: :name, primary_key: :city, class_name: City end diff --git a/app/views/events/index.rss.builder b/app/views/events/index.rss.builder index 45dd0396..ef694da1 100644 --- a/app/views/events/index.rss.builder +++ b/app/views/events/index.rss.builder @@ -28,8 +28,7 @@ xml.tag! 'rdf:RDF', 'xmlns:rdf' => "http://www.w3.org/1999/02/22-rdf-syntax-ns#" xml.link event_url event xml.tag! 'dc:identifier', "#{event.id}@agendadulibre.org" xml.tag! 'dc:date', event.start_time - city = event.related_city - xml.tag! 'georss:point', "#{city.latitude} #{city.longitude}" rescue nil + xml.tag! 'georss:point', "#{event.related_city.latitude} #{event.related_city.longitude}" rescue nil @event = event content = render file: '/events/show.html', locals: { rss: true } diff --git a/app/views/events/show.html.haml b/app/views/events/show.html.haml index fb5fc030..c47b2751 100644 --- a/app/views/events/show.html.haml +++ b/app/views/events/show.html.haml @@ -14,8 +14,7 @@ = link_to t('.add_to_calendar'), root_url %h2 - %em= @event.city - \: + %em= @event.city + ':' = @event.title %h3 Date et lieu diff --git a/app/views/maps/index.html.haml b/app/views/maps/index.html.haml index 0f906d8f..c4383a24 100644 --- a/app/views/maps/index.html.haml +++ b/app/views/maps/index.html.haml @@ -10,10 +10,9 @@ - for city in @cities_event :coffee - marker = new OpenLayers.Marker( + eventsLayer.addMarker new OpenLayers.Marker( new OpenLayers.LonLat(#{city.longitude}, #{city.latitude}) ) - eventsLayer.addMarker marker :javascript var lugsLayer = new OpenLayers.Layer.Markers("#{t '.lugs'}"); @@ -23,8 +22,7 @@ - for city in @cities_lug :coffee - marker = new OpenLayers.Marker( + lugsLayer.addMarker new OpenLayers.Marker( new OpenLayers.LonLat(#{city.longitude}, #{city.latitude}) new OpenLayers.Icon("/assets/team.png", iconSize, iconOffset) ) - lugsLayer.addMarker marker diff --git a/test/fixtures/cities.yml b/test/fixtures/cities.yml index d83063e4..8782b241 100644 --- a/test/fixtures/cities.yml +++ b/test/fixtures/cities.yml @@ -1,8 +1,8 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: - name: MyString - majname: MyString + name: Jolie ville + majname: JOLIE VILLE postalcode: 1 inseecode: 1 regioncode: 1 @@ -10,8 +10,8 @@ one: longitude: 1.5 two: - name: MyString - majname: MyString + name: Une autre ville + majname: UNE AUTRE VILLE postalcode: 1 inseecode: 1 regioncode: 1 diff --git a/test/fixtures/events.yml b/test/fixtures/events.yml index 0d74fd09..854c0c88 100644 --- a/test/fixtures/events.yml +++ b/test/fixtures/events.yml @@ -5,7 +5,7 @@ one: description: MyText start_time: 2013-12-28 16:04:56 end_time: 2013-12-28 16:04:56 - city: MyString + city: Jolie ville region: region_one locality: 1 url: MyString @@ -24,7 +24,7 @@ two: description: MyText start_time: 2013-12-28 16:04:56 end_time: 2013-12-28 16:04:56 - city: MyString + city: Une autre ville region: region_one locality: 1 url: MyString