Browse Source

Géo coding ajouté aux evts

recurrent
echarp 8 years ago
parent
commit
55d2870b6d
  1. 10
      Gemfile
  2. 4
      Gemfile.lock
  3. 30
      app/assets/javascripts/maps.js.coffee
  4. 7
      app/assets/stylesheets/maps.css.sass
  5. 4
      app/controllers/events_controller.rb
  6. 4
      app/controllers/maps_controller.rb
  7. 4
      app/controllers/moderations_controller.rb
  8. 3
      app/helpers/events_helper.rb
  9. 9
      app/models/event.rb
  10. 4
      app/views/events/_form.html.haml
  11. 11
      app/views/events/show.html.haml
  12. 2
      app/views/events/show.ics.erb
  13. 9
      app/views/events/show.json.jbuilder
  14. 3
      app/views/events/show.text.haml
  15. 8
      db/migrate/20140823111115_add_address_latitude_and_longitude_to_event.rb
  16. 21
      db/schema.rb

10
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'

4
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

30
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 <a href="http://www.mapquest.com/">MapQuest</a> &mdash; Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
#subdomains: '1234'
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a>'
).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: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a>'
).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

7
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

4
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

4
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

4
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

3
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

9
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

4
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

11
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

2
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

9
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

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

8
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

21
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

Loading…
Cancel
Save