Skip to content

Commit

Permalink
some changes in partial + view tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apneadiving committed Aug 31, 2011
1 parent 7bc8625 commit 5334105
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 16 deletions.
10 changes: 4 additions & 6 deletions app/views/gmaps4rails/_gmaps4rails.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<% #thanks to enable_css, user can avoid this css to be loaded
if enable_css == true && options[:scripts].nil? %>
if enable_css == true and options[:scripts].nil? %>
<% content_for :head do %>
<%= stylesheet_link_tag 'gmaps4rails' %>
<% end %>
<% end %>
<% content_for :scripts do %>
<% if enable_js == true && options[:scripts].try(:to_sym) != :none %>
<% if enable_js == true and options[:scripts].try(:to_sym) != :none %>
<% case options[:map_options].try(:[], :provider) %>
<% when "openlayers" %>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
Expand Down Expand Up @@ -50,11 +50,9 @@ if enable_css == true && options[:scripts].nil? %>
<!-- TODO: change this style dynamically -->
</div>
<% when "bing" %>
<div id="<%= gmaps_container_id(options) %>" >
<div id="<%= gmaps_map_id(options) %>" class="bing_map"></div>
</div>
<div id="<%= gmaps_map_id(options) %>" class="<%= gmaps_map_class(options) %>"></div>
<% else %>
<div class="<%= gmaps_container_id(options) %>">
<div class="<%= gmaps_container_class(options) %>">
<div id="<%= gmaps_map_id(options) %>" class="<%= gmaps_map_class(options) %>"></div>
</div>
<% end %>
7 changes: 4 additions & 3 deletions lib/gmaps4rails/helper/gmaps4rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ def gmaps_map_id(options)
options[:map_options].try(:[], :id) || Gmaps4rails::DEFAULT_MAP_ID
end

def gmaps_container_id(options)
options[:map_options].try(:[], :container_id) || "map_container"
def gmaps_container_class(options)
options[:map_options].try(:[], :container_class) || "map_container"
end

def gmaps_map_class(options)
options[:map_options].try(:[], :class) || "gmaps4rails_map"
default_class = options[:map_options].try(:[], :provider) == "bing" ? "bing_map" : "gmaps4rails_map"
options[:map_options].try(:[], :class) || default_class
end
end
3 changes: 2 additions & 1 deletion public/javascripts/gmaps4rails/gmaps4rails.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ function Gmaps4Rails() {
////////////////////////////////////////////////////

//to make the map fit the different LatLng points
this.adjustMapToBounds = function(latlng) {
this.adjustMapToBounds = function() {

//FIRST_STEP: retrieve all bounds
//create the bounds object only if necessary
Expand Down Expand Up @@ -464,6 +464,7 @@ function Gmaps4Rails() {
//in every case, I've to take into account the bounds set up by the user
for (var i = 0; i < this.map_options.bounds.length; ++i) {
//create points from bounds provided
//TODO:only works with google maps
var bound = this.createLatLng(this.map_options.bounds[i].lat, this.map_options.bounds[i].lng);
this.boundsObject.extend(bound);
}
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GIT
PATH
remote: ../../
specs:
gmaps4rails (1.0.0)
gmaps4rails (1.0.1)
crack

GEM
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_list
@json = User.all.to_gmaps4rails
end


def test_partial
@json = User.all.to_gmaps4rails
end

def ajax_map
end

Expand Down
6 changes: 3 additions & 3 deletions test/dummy/app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<%= stylesheet_link_tag 'test' %>
<% content_for :scripts do -%>
<script type="text/javascript" charset="utf-8">
var url = "http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0";
Expand Down Expand Up @@ -116,8 +115,9 @@ var builder3 = [{
:last_map => false ) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "joe3", :provider => "mapquest", :provider_key => MAPQUEST_KEY },
:scripts => "api" ) %>
<%#= gmaps(:markers => {"data" => @json, :options => {:do_clustering => false, :draggable => true } },
:scripts => "api",
:last_map => false ) %>
<%= gmaps(:markers => {"data" => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "joe2", :provider => "bing", :provider_key => BING_KEY },
:scripts => "api" ) %>
<%#= gmaps({
Expand Down
21 changes: 21 additions & 0 deletions test/dummy/app/views/users/test_partial.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => true, :draggable => true } },
:last_map => false) %>
<%= gmaps(:markers => {:data => '[{ "lng": "5.9311119", "lat": "43.1251606"} ,{ "lng": "2.3509871", "lat": "48.8566667" },{ "lng": "0", "lat": "0" } ]',
"options" => {"do_clustering" => true, "draggable" => true } },
:map_options => { :id => "custom_id", :class => "custom_class", :container_class => "custom_container" },
:scripts => "none",
:last_map => false ) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :provider => "openlayers", :id => "joe" },
:scripts => "api",
:last_map => false ) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "joe3", :provider => "mapquest", :provider_key => MAPQUEST_KEY },
:scripts => "api",
:last_map => false ) %>
<div id="bing">
<%= gmaps(:markers => {"data" => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "foo", :provider => "bing", :provider_key => BING_KEY },
:scripts => "api" ) %>
</div>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Dummy::Application.routes.draw do
match "users/test_list" => "users#test_list", :as => "test_list"
match "users/test_partial" => "users#test_partial", :as => "test_partial"
match "users/ajax_map" => "users#ajax_map", :as => "ajax_map"
match "users/renderer" => "users#renderer", :as => "renderer"
resources :users
Expand Down
3 changes: 1 addition & 2 deletions test/dummy/spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ def gmaps4rails_sidebar
Factory(:user, :name => "User1")
Factory(:user, :name => "User2")

visit test_list_path
page.should have_content("User1")
page.should have_content("User2")
end

end
end
69 changes: 69 additions & 0 deletions test/dummy/spec/views/users/test_partial.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe "users/test_partial.html.erb" do
before(:each) do
Factory :user
Factory :user_paris
assigns[:json] = User.all.to_gmaps4rails
render
end

it "should contain proper default div properties" do
rendered.should have_selector('div.map_container') do
with_tag('div#map')
with_tag('div.gmaps4rails')
end
end

it "should render proper customized ids and class" do
rendered.should have_selector('div.custom_container') do
with_tag('div#custom_id')
with_tag('div.custom_class')
end
end

it "should display proper bing's map default class" do
rendered.should have_selector('div#bing') do
with_tag('div#foo')
with_tag('div.bing_map')
end
end

context "content_for content" do

before(:each) do
#hack to access attr_readers
self.instance_eval do
def get_partials; @partials; end
end
view.instance_eval do
def get_content_for; @_content_for; end
end
end

it "partial should have been inserted 5 times" do
get_partials["_gmaps4rails"].should eq 5
end

it "all scripts should have only be inserted once" do
[
/http:\/\/maps.google.com\/maps\/api\/js/,
/http:\/\/www.openlayers.org\/api/,
/http:\/\/mapquestapi.com\/sdk\/js/,
/http:\/\/ecn.dev.virtualearth.net/,
/gmaps4rails.base.js/,
/gmaps4rails.googlemaps.js/,
/gmaps4rails.mapquest.js/,
/gmaps4rails.bing.js/,
/gmaps4rails.openlayers.js/
].each do |regexp|
view.get_content_for[:scripts].scan(regexp).size.should eq 1
end
end

it "css should only be inserted once" do
view.get_content_for[:head].scan(/stylesheets\/gmaps4rails.css/).size.should eq 1
end
end

end

0 comments on commit 5334105

Please sign in to comment.