Merge pull request #361 from Skud/crop-map

Crop map
This commit is contained in:
pozorvlak
2014-07-03 10:44:01 +01:00
7 changed files with 102 additions and 17 deletions

View File

@@ -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/

View File

@@ -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 &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors under <a href="http://www.openstreetmap.org/copyright">ODbL</a> | Map imagery &copy; <a href="http://mapbox.com">Mapbox</a>',
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 = "<a href='" + planting_url + "'>" + owner.login_name + "'s " + crop.name + "</a>";
where = "<p><i>" + owner.location + "</i></p>";
details = "<p>";
if (planting.quantity) {
details = details + "Quantity: " + planting.quantity + "<br/>";
}
if (planting.planted_from) {
details = details + "Planted from: " + planting.planted_from + "<br/>";
}
if (planting.sunniness) {
details = details + "Planted in: " + planting.sunniness+ "<br/>";
}
details = details + "</p>";
marker.bindPopup(planting_link + where + details).openPopup();
markers.addLayer(marker);
}
});
});
map.addLayer(markers);
}

View File

@@ -30,7 +30,7 @@ function showMap(map) {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors under <a href="http://www.openstreetmap.org/copyright">ODbL</a> | Map imagery &copy; <a href="http://mapbox.com">Mapbox</a>',
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) {

View File

@@ -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

View File

@@ -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 ] }

View File

@@ -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

View File

@@ -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)')