From 2f8e060b49a6bdbfcdce1d738e47d3459bdc8d21 Mon Sep 17 00:00:00 2001 From: Skud Date: Thu, 12 Jun 2014 19:44:09 -0700 Subject: [PATCH 1/6] added plantings, seeds, and harvests to the crops JSON --- app/controllers/crops_controller.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 94941c9ff..6fc04de7e 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -10,7 +10,7 @@ class CropsController < ApplicationController @crops = Crop.includes(:scientific_names, {:plantings => :photos}).paginate(:page => params[:page]) respond_to do |format| - format.html + format.html format.json { render :json => @crops } format.rss do @crops = Crop.recent.includes(:scientific_names, :creator) @@ -62,7 +62,9 @@ class CropsController < ApplicationController respond_to do |format| format.html # show.html.haml - format.json { render json: @crop } + format.json do + render :json => @crop.to_json(:include => [:plantings, :seeds, :harvests]) + end end end From 8eb644fc662f94f5f01e9e4ca94592cc91c74d94 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 30 Jun 2014 11:45:10 -0700 Subject: [PATCH 2/6] Add map, move info to sidebar --- app/views/crops/_varieties.html.haml | 2 +- app/views/crops/show.html.haml | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/views/crops/_varieties.html.haml b/app/views/crops/_varieties.html.haml index 73068bd6c..434ea3c1f 100644 --- a/app/views/crops/_varieties.html.haml +++ b/app/views/crops/_varieties.html.haml @@ -5,6 +5,6 @@ = succeed "." do = link_to crop.parent, crop.parent - if crop.varieties.count > 0 - %p + %h3 Varieties: = render :partial => 'hierarchy', :locals => { :display_crops => [ crop ] } diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index a8aeb5c93..0cee3b1a6 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -16,13 +16,6 @@ = render :partial => 'photos', :locals => { :crop => @crop } - = render :partial => 'varieties', :locals => { :crop => @crop } - - = render :partial => 'grown_for', :locals => { :crop => @crop } - - = render :partial => 'planting_advice', :locals => { :crop => @crop } - - %h2 Who's planted this crop? %p @@ -34,6 +27,7 @@ - else Nobody is growing this yet. You could be the first! + %div#map - if @crop.plantings.size > 0 - @crop.plantings.each do |p| @@ -65,6 +59,12 @@ - if can? :edit, @crop = link_to 'Add', new_scientific_name_path( :crop_id => @crop.id ), { :class => 'btn btn-mini' } + = render :partial => 'varieties', :locals => { :crop => @crop } + + = render :partial => 'grown_for', :locals => { :crop => @crop } + + = render :partial => 'planting_advice', :locals => { :crop => @crop } + %h4 More information %ul %li= link_to 'Wikipedia (English)', @crop.en_wikipedia_url From 3abaca3136859f236c06155f85ed7679db184345 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 30 Jun 2014 11:48:25 -0700 Subject: [PATCH 3/6] Tweak what's emitted as JSON (let's just emit plantings for now, as that's all we're using) --- app/controllers/crops_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 6fc04de7e..3bb020dda 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -63,7 +63,9 @@ class CropsController < ApplicationController respond_to do |format| format.html # show.html.haml format.json do - render :json => @crop.to_json(:include => [:plantings, :seeds, :harvests]) + render :json => @crop.to_json(:include => { + :plantings => { :include => { :owner => { :only => [:id, :login_name, :location, :latitude, :longitude] }}} + }) end end end From 497212795777b9f04512024010ad4f8358d654dc Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 30 Jun 2014 11:50:49 -0700 Subject: [PATCH 4/6] Display crop plantings on a map --- app/assets/javascripts/crops.js.coffee | 3 -- app/assets/javascripts/crops.js.erb | 48 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) delete mode 100644 app/assets/javascripts/crops.js.coffee create mode 100644 app/assets/javascripts/crops.js.erb diff --git a/app/assets/javascripts/crops.js.coffee b/app/assets/javascripts/crops.js.coffee deleted file mode 100644 index 761567942..000000000 --- a/app/assets/javascripts/crops.js.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/javascripts/crops.js.erb b/app/assets/javascripts/crops.js.erb new file mode 100644 index 000000000..5bc08c286 --- /dev/null +++ b/app/assets/javascripts/crops.js.erb @@ -0,0 +1,48 @@ +things_to_map = location.pathname + '.json'; +mapbox_map_id = "<%= Growstuff::Application.config.mapbox_map_id %>"; +base_url = "https://c.tiles.mapbox.com/v3/" + mapbox_map_id + "/{z}/{x}/{y}.png"; +nominatim_base_url = 'http://nominatim.openstreetmap.org/search/'; +nominatim_user_agent_email = "<%= Growstuff::Application.config.user_agent_email %>"; + +L.Icon.Default.imagePath = '/assets' + +map = L.map('map').setView([0.0, -0.0], 2); +showMap(map); + +function showMap(map) { + L.tileLayer(base_url, { + attribution: 'Map data © OpenStreetMap contributors under ODbL | Map imagery © Mapbox', + maxZoom: 18 + }).addTo(map); + markers = new L.MarkerClusterGroup({showCoverageOnHover: false}); + + $.getJSON(things_to_map, function(crop) { + $.each(crop.plantings, function(i, planting) { + owner = planting.owner; + if (owner.latitude && owner.longitude) { + marker = new L.Marker(new L.LatLng(owner.latitude, owner.longitude)); + + planting_url = "/plantings/" + planting.id; + planting_link = "" + owner.login_name + "'s " + crop.name + ""; + + where = "

" + owner.location + "

"; + + details = "

"; + if (planting.quantity) { + details = details + "Quantity: " + planting.quantity + "
"; + } + if (planting.planted_from) { + details = details + "Planted from: " + planting.planted_from + "
"; + } + if (planting.sunniness) { + details = details + "Planted in: " + planting.sunniness+ "
"; + } + details = details + "

"; + marker.bindPopup(planting_link + where + details).openPopup(); + markers.addLayer(marker); + } + }); + }); + + map.addLayer(markers); +} From f8cb7248b75d8ec80e4579c0e07c6ddae56333d9 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 30 Jun 2014 11:56:46 -0700 Subject: [PATCH 5/6] Reduced cluster radius for maps on both crops and places pages Did this because the clusters were annoyingly over-general, eg. showing 164 people in the eastern part of North America. Cluster radius of 20px is about the radius of the cluster icon itself. Seems to work better eg. for separating Melbourne from Sydney on the world map. --- app/assets/javascripts/crops.js.erb | 2 +- app/assets/javascripts/places.js.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/crops.js.erb b/app/assets/javascripts/crops.js.erb index 5bc08c286..83600f74d 100644 --- a/app/assets/javascripts/crops.js.erb +++ b/app/assets/javascripts/crops.js.erb @@ -14,7 +14,7 @@ function showMap(map) { attribution: 'Map data © OpenStreetMap contributors under ODbL | Map imagery © Mapbox', maxZoom: 18 }).addTo(map); - markers = new L.MarkerClusterGroup({showCoverageOnHover: false}); + markers = new L.MarkerClusterGroup({showCoverageOnHover: false, maxClusterRadius: 20 }); $.getJSON(things_to_map, function(crop) { $.each(crop.plantings, function(i, planting) { diff --git a/app/assets/javascripts/places.js.erb b/app/assets/javascripts/places.js.erb index 988978f99..475ecf0f9 100644 --- a/app/assets/javascripts/places.js.erb +++ b/app/assets/javascripts/places.js.erb @@ -30,7 +30,7 @@ function showMap(map) { attribution: 'Map data © OpenStreetMap contributors under ODbL | Map imagery © Mapbox', maxZoom: 18 }).addTo(map); - markers = new L.MarkerClusterGroup({showCoverageOnHover: false}); + markers = new L.MarkerClusterGroup({showCoverageOnHover: false, maxClusterRadius: 20 }); $.getJSON(things_to_map, function(members) { $.each(members, function(i, m) { From fea7da3546f2fccfe6cd265b14afb8cc483a3441 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 30 Jun 2014 12:14:06 -0700 Subject: [PATCH 6/6] rearranged crop page headings and added tests --- app/views/crops/show.html.haml | 13 ++++++++--- spec/views/crops/show.html.haml_spec.rb | 29 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 0cee3b1a6..a19421ff1 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -16,9 +16,7 @@ = render :partial => 'photos', :locals => { :crop => @crop } - %h2 Who's planted this crop? - - %p + %h2 - if @crop.plantings.size > 0 = @crop.name.titleize has been planted @@ -27,9 +25,18 @@ - else Nobody is growing this yet. You could be the first! + %p + Only plantings by members who have set their locations are shown on this map. + - if current_member && current_member.location.blank? + = link_to "Set your location.", edit_member_registration_path + + %div#map - if @crop.plantings.size > 0 + + %h2 All plantings + - @crop.plantings.each do |p| = render :partial => "plantings/thumbnail", :locals => { :planting => p, :title => 'owner' } diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index 4b637cc1d..1805dc7e1 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -33,6 +33,35 @@ describe "crops/show" do end end + context "map" do + it "has a map" do + render + assert_select("div#map") + end + + it "explains what's shown on the map" do + render + rendered.should contain "Only plantings by members who have set their locations are shown on this map" + end + + it "shows a 'set your location' link to people who need to" do + @nowhere = FactoryGirl.create(:member) + sign_in @nowhere + controller.stub(:current_user) { @nowhere } + render + rendered.should contain "Set your location" + end + + it "doesn't show 'set your location' to people who have one" do + @somewhere = FactoryGirl.create(:london_member) + sign_in @somewhere + controller.stub(:current_user) { @somewhere } + render + rendered.should_not contain "Set your location" + end + + end + it "shows the wikipedia URL" do render assert_select("a[href=#{@crop.en_wikipedia_url}]", 'Wikipedia (English)')