From 55d2870b6de7f5ddbbe2a2645a5f6ff03975f809 Mon Sep 17 00:00:00 2001 From: echarp Date: Sat, 23 Aug 2014 16:59:42 +0200 Subject: [PATCH] =?UTF-8?q?G=C3=A9o=20coding=20ajout=C3=A9=20aux=20evts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 10 ++++--- Gemfile.lock | 4 ++- app/assets/javascripts/maps.js.coffee | 30 +++++++++++++++---- app/assets/stylesheets/maps.css.sass | 7 ++++- app/controllers/events_controller.rb | 4 +-- app/controllers/maps_controller.rb | 4 +-- app/controllers/moderations_controller.rb | 4 +-- app/helpers/events_helper.rb | 3 +- app/models/event.rb | 9 +++++- app/views/events/_form.html.haml | 4 +++ app/views/events/show.html.haml | 11 +++++-- app/views/events/show.ics.erb | 2 +- app/views/events/show.json.jbuilder | 9 +++--- app/views/events/show.text.haml | 3 +- ...address_latitude_and_longitude_to_event.rb | 8 +++++ db/schema.rb | 21 ++++++++----- 16 files changed, 95 insertions(+), 38 deletions(-) create mode 100644 db/migrate/20140823111115_add_address_latitude_and_longitude_to_event.rb diff --git a/Gemfile b/Gemfile index bf26bf44..2843b36f 100644 --- a/Gemfile +++ b/Gemfile @@ -29,10 +29,6 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc -# Spring speeds up development by keeping your application running in the -# background. Read more: https://github.com/rails/spring -gem 'spring', group: :development - # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.1.2' @@ -83,6 +79,8 @@ gem 'redcarpet' # Carte openstreetmap gem 'leaflet-rails' +gem 'geocoder' + # Tiny MCE integration gem 'tinymce-rails' gem 'tinymce-rails-langs' @@ -91,6 +89,10 @@ gem 'tinymce-rails-langs' gem 'meta-tags' group :development do + # Spring speeds up development by keeping your application running in the + # background. Read more: https://github.com/rails/spring + gem 'spring' + gem 'sqlite3' gem 'guard-livereload' gem 'guard-bundler' diff --git a/Gemfile.lock b/Gemfile.lock index 66eed619..a987fb1a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -113,9 +113,10 @@ GEM font-awesome-rails (4.1.0.0) railties (>= 3.2, < 5.0) formatador (0.2.5) - formtastic (2.3.0.rc4) + formtastic (2.3.0) actionpack (>= 3.0) fssm (0.2.10) + geocoder (1.2.4) guard (2.6.1) formatador (>= 0.2.4) listen (~> 2.7) @@ -324,6 +325,7 @@ DEPENDENCIES differ email_validator font-awesome-rails + geocoder guard-brakeman guard-bundler guard-livereload diff --git a/app/assets/javascripts/maps.js.coffee b/app/assets/javascripts/maps.js.coffee index 5544393f..fa566f0e 100644 --- a/app/assets/javascripts/maps.js.coffee +++ b/app/assets/javascripts/maps.js.coffee @@ -1,14 +1,30 @@ $(document).ready -> - if $('#map').size() > 0 + $('.maps #map').each -> map = L.map('map').setView [46.5, 4], 6 L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', - #L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', - #L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpeg', - #attribution: 'Tiles Courtesy of MapQuest — Map data © OpenStreetMap contributors, CC-BY-SA', - attribution: '© OpenStreetMap contributors', - #subdomains: '1234' + attribution: '© OpenStreetMap' + ).addTo map + $.getJSON '/maps.json', (json) -> + points = [] + L.geoJson(json, + onEachFeature: (feature, layer) -> + points += [feature.geometry.coordinates[0], feature.geometry.coordinates[1]] + # Does this feature have a property named popupContent? + if (feature.properties && feature.properties.popupContent) + layer.bindPopup(feature.properties.popupContent) + ).addTo map + console.log L.bounds(points[0], points[1]) + + + $('.events #map').each -> + coord = [$(this).attr('latitude'), $(this).attr('longitude')] + + map = L.map('map').setView [coord[0], coord[1]], 16 + + L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', + attribution: '© OpenStreetMap' ).addTo map $.getJSON '/maps.json', (json) -> @@ -18,3 +34,5 @@ $(document).ready -> if (feature.properties && feature.properties.popupContent) layer.bindPopup(feature.properties.popupContent) ).addTo map + + marker = L.marker([coord[0], coord[1]]).addTo map diff --git a/app/assets/stylesheets/maps.css.sass b/app/assets/stylesheets/maps.css.sass index 58202b90..d48e2516 100644 --- a/app/assets/stylesheets/maps.css.sass +++ b/app/assets/stylesheets/maps.css.sass @@ -2,6 +2,11 @@ @import leaflet #map - height: 60em +box-shadow(0 0 1em SteelBlue) +transition(none) + +.maps #map + height: 60em + +.events #map + height: 20em diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index a7af4455..cdfcae38 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -107,8 +107,8 @@ class EventsController < ApplicationController # through. def event_params params.require(:event) - .permit :title, :start_time, :end_time, :description, :city, :region, - :locality, :url, :contact, :submitter, :tags + .permit :title, :start_time, :end_time, :description, :address, :city, + :region, :locality, :url, :contact, :submitter, :tags end # Check that you can only edit an existing event if you know its secret diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 72ad23dd..818a9613 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -6,9 +6,7 @@ class MapsController < ApplicationController respond_to do |format| format.html format.json do - render json: Event.moderated.future - .joins(:related_city).includes(:related_city) - .map { |event| event.to_json } + render json: Event.moderated.future.map { |event| event.to_json } end end end diff --git a/app/controllers/moderations_controller.rb b/app/controllers/moderations_controller.rb index 0e824c25..b0473b9f 100644 --- a/app/controllers/moderations_controller.rb +++ b/app/controllers/moderations_controller.rb @@ -72,8 +72,8 @@ class ModerationsController < ApplicationController # through. def moderation_params params.require(:event) - .permit :title, :start_time, :end_time, :description, :city, :region, - :locality, :url, :contact, :submitter, :tags + .permit :title, :start_time, :end_time, :description, :address, :city, + :region, :locality, :url, :contact, :submitter, :tags end # Useful to manage absolute url in mails diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb index 448a6e5b..cc926a3e 100644 --- a/app/helpers/events_helper.rb +++ b/app/helpers/events_helper.rb @@ -19,8 +19,7 @@ module EventsHelper geo: { region: @event.related_region, placename: @event.city, - position: "#{@event.related_city.try :latitude};" \ - + "#{@event.related_city.try :longitude}" + position: "#{@event.latitude};#{@event.longitude}" } end diff --git a/app/models/event.rb b/app/models/event.rb index 1cbd1255..f2a8f452 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -16,6 +16,9 @@ class Event < ActiveRecord::Base validates :contact, email: true validates :submitter, email: true + geocoded_by :full_address, lookup: :nominatim + after_validation :geocode, if: -> (obj) { obj.address_changed? } + scope :moderated, -> { where moderated: true } scope :unmoderated, -> { where moderated: false } scope :last_year, -> { where 'start_time >= ?', 360.days.ago } @@ -76,8 +79,12 @@ class Event < ActiveRecord::Base }, geometry: { type: 'Point', - coordinates: [related_city.longitude, related_city.latitude] + coordinates: [longitude, latitude] } } end + + def full_address + [address, city, related_region.name].compact.join(', ') + end end diff --git a/app/views/events/_form.html.haml b/app/views/events/_form.html.haml index 64dfd1d7..c990ea0f 100644 --- a/app/views/events/_form.html.haml +++ b/app/views/events/_form.html.haml @@ -28,6 +28,9 @@ = f.label :description = f.text_area :description, rows: 25, cols: 90 + .field + = f.label :address + = f.text_field :address, size: 70 .field = f.label :city = f.text_field :city, required: true, size: 70, list: :cities @@ -42,6 +45,7 @@ = f.label :locality = f.select :locality, options_for_select([[t('attributes.locality_0'), 0], [t('attributes.locality_1'), 1]], @event.locality) + .field .helper :markdown diff --git a/app/views/events/show.html.haml b/app/views/events/show.html.haml index f0a10026..e3895999 100644 --- a/app/views/events/show.html.haml +++ b/app/views/events/show.html.haml @@ -31,6 +31,7 @@ =t '.cancel' %h3=t '.dateAndPlace' + %p - if @event.same_day? Le #{l @event.start_time.to_date, format: :long}, @@ -42,9 +43,13 @@ #{l @event.end_time, format: :at}. %p - =t '.at' - %em= link_to(@event.city, "http://fr.wikipedia.org/wiki/#{url_encode @event.city}") + ',' - = link_to @event.related_region.name, "http://fr.wikipedia.org/wiki/#{url_encode @event.related_region.name}" rescue nil + = raw [@event.address.present? ? @event.address : nil, + link_to(@event.city, "http://fr.wikipedia.org/wiki/#{url_encode @event.city}"), + link_to(@event.related_region.try(:name), + "http://fr.wikipedia.org/wiki/#{url_encode @event.related_region.name}")].compact.join(', ') + +- if @event.latitude && @event.longitude + #map(latitude="#{@event.latitude}" longitude="#{@event.longitude}") %h3=t '.description' .description diff --git a/app/views/events/show.ics.erb b/app/views/events/show.ics.erb index 584831a4..4b6adcb9 100644 --- a/app/views/events/show.ics.erb +++ b/app/views/events/show.ics.erb @@ -13,6 +13,6 @@ UID:<%= event.id %>@agendadulibre.org SUMMARY:<%= event.title %> URL:<%= event_url event %> DESCRIPTION:Un événement de l'Agenda du Libre -LOCATION:<%= event.city.gsub('-', ' ') %> +LOCATION:<%= event.address %> <%= event.city %> END:VEVENT END:VCALENDAR diff --git a/app/views/events/show.json.jbuilder b/app/views/events/show.json.jbuilder index 6dfd9c05..98d51ef2 100644 --- a/app/views/events/show.json.jbuilder +++ b/app/views/events/show.json.jbuilder @@ -1,4 +1,5 @@ -json.extract! @event, :id, :title, :description, :start_time, :end_time, :city, - :region_id, :locality, :url, :contact, :contact, :submitter, - :moderated, :tags, :secret, :decision_time, :submission_time, - :moderator_mail_id, :submitter_mail_id, :created_at, :updated_at +json.extract! @event, :id, :title, :description, :start_time, :end_time, + :address, :city, :region_id, :locality, :url, :contact, :contact, + :submitter, :moderated, :tags, :secret, :decision_time, + :submission_time, :moderator_mail_id, :submitter_mail_id, + :created_at, :updated_at diff --git a/app/views/events/show.text.haml b/app/views/events/show.text.haml index 3bdb4930..503ec5f2 100644 --- a/app/views/events/show.text.haml +++ b/app/views/events/show.text.haml @@ -2,8 +2,9 @@ #{Event.human_attribute_name(:title).concat(':').ljust 12 } #{@event.title} #{Event.human_attribute_name(:start_time).concat(':').ljust 12 } #{l @event.start_time, format: :at} #{Event.human_attribute_name(:end_time).concat(':').ljust 12 } #{l @event.end_time, format: :at} -#{Event.human_attribute_name(:region).concat(':').ljust 12 } #{@event.related_region} +#{Event.human_attribute_name(:address).concat(':').ljust 12 } #{@event.address} #{Event.human_attribute_name(:city).concat(':').ljust 12 } #{@event.city} +#{Event.human_attribute_name(:region).concat(':').ljust 12 } #{@event.related_region} #{Event.human_attribute_name(:url).concat(':').ljust 12 } #{@event.url} #{Event.human_attribute_name(:contact).concat(':').ljust 12 } #{@event.contact} #{Event.human_attribute_name(:submitter).concat(':').ljust 12 } #{@event.submitter} diff --git a/db/migrate/20140823111115_add_address_latitude_and_longitude_to_event.rb b/db/migrate/20140823111115_add_address_latitude_and_longitude_to_event.rb new file mode 100644 index 00000000..197b93d2 --- /dev/null +++ b/db/migrate/20140823111115_add_address_latitude_and_longitude_to_event.rb @@ -0,0 +1,8 @@ +# Add geocoding to events +class AddAddressLatitudeAndLongitudeToEvent < ActiveRecord::Migration + def change + add_column :events, :address, :text + add_column :events, :latitude, :float + add_column :events, :longitude, :float + end +end diff --git a/db/schema.rb b/db/schema.rb index 43ab93fe..99c7d3d5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140403204748) do +ActiveRecord::Schema.define(version: 20140823111115) do create_table "active_admin_comments", force: true do |t| t.string "namespace" @@ -25,9 +25,9 @@ ActiveRecord::Schema.define(version: 20140403204748) do t.datetime "updated_at" end - add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id", using: :btree - add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace", using: :btree - add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree + add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id" + add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace" + add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id" create_table "admin_users", force: true do |t| t.string "email", default: "", null: false @@ -44,8 +44,8 @@ ActiveRecord::Schema.define(version: 20140403204748) do t.datetime "updated_at" end - add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree - add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree + add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true + add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true create_table "cities", force: true do |t| t.string "name", default: "", null: false @@ -57,6 +57,8 @@ ActiveRecord::Schema.define(version: 20140403204748) do t.float "longitude", limit: 24 end + add_index "cities", ["name"], name: "cities_name" + create_table "events", force: true do |t| t.string "title", default: "", null: false t.text "description", null: false @@ -75,8 +77,13 @@ ActiveRecord::Schema.define(version: 20140403204748) do t.datetime "submission_time", null: false t.string "moderator_mail_id", limit: 32 t.string "submitter_mail_id", limit: 32 + t.text "address" + t.float "latitude" + t.float "longitude" end + add_index "events", ["start_time", "end_time"], name: "events_date" + create_table "lugs", force: true do |t| t.integer "region", default: 0, null: false t.integer "department", default: 0, null: false @@ -108,8 +115,8 @@ ActiveRecord::Schema.define(version: 20140403204748) do t.string "login", default: "", null: false t.string "password", default: "", null: false t.string "email", default: "", null: false - t.string "firstname", default: "", null: false t.string "lastname", default: "", null: false + t.string "firstname", default: "", null: false end end