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..83600f74d --- /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, maxClusterRadius: 20 }); + + $.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); +} diff --git a/app/assets/javascripts/places.js.erb b/app/assets/javascripts/places.js.erb index bde724b19..7f321a33d 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) { diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 94941c9ff..3bb020dda 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,11 @@ 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 => { :include => { :owner => { :only => [:id, :login_name, :location, :latitude, :longitude] }}} + }) + end end end 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..a19421ff1 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -16,16 +16,7 @@ = 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 + %h2 - if @crop.plantings.size > 0 = @crop.name.titleize has been planted @@ -34,8 +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' } @@ -65,6 +66,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 diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index b530f16c8..475816923 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -32,6 +32,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)')