├── .gitattributes ├── .github └── stale.yml ├── .gitignore ├── .travis.yml ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── leaflet-rails.gemspec ├── lib ├── leaflet-rails.rb └── leaflet-rails │ ├── version.rb │ └── view_helpers.rb ├── spec ├── spec_helper.rb └── view_helpers_spec.rb └── vendor └── assets ├── images ├── layers-2x.png ├── layers.png ├── marker-icon-2x.png ├── marker-icon.png └── marker-shadow.png ├── javascripts ├── leaflet-src.js.erb ├── leaflet-src.js.map └── leaflet.js.erb └── stylesheets └── leaflet.css.erb /.gitattributes: -------------------------------------------------------------------------------- 1 | *.map binary 2 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 60 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - pinned 8 | - security 9 | # Label to use when marking an issue as stale 10 | staleLabel: wontfix 11 | # Comment to post when marking an issue as stale. Set to `false` to disable 12 | markComment: > 13 | This issue has been automatically marked as stale because it has not had 14 | recent activity. It will be closed if no further activity occurs. Thank you 15 | for your contributions. 16 | # Comment to post when closing a stale issue. Set to `false` to disable 17 | closeComment: false 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | 6 | /.rspec 7 | /coverage/ 8 | /log/ 9 | .idea/* 10 | .ruby-gemset 11 | .ruby-version 12 | .DS_Store 13 | 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | cache: bundler 4 | matrix: 5 | include: 6 | - rvm: 2.3.0 7 | gemfile: Gemfile 8 | - rvm: 2.4.0 9 | gemfile: Gemfile 10 | - rvm: 2.5.0 11 | gemfile: Gemfile 12 | - rvm: 2.6.0 13 | gemfile: Gemfile 14 | - rvm: ruby-head 15 | gemfile: Gemfile 16 | - rvm: jruby-head 17 | gemfile: Gemfile 18 | allow_failures: 19 | - rvm: ruby-head 20 | - rvm: jruby-head 21 | before_install: 22 | - gem update --system 23 | - gem install bundler 24 | - gem --version 25 | notifications: 26 | email: false 27 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Specify your gem's dependencies in leaflet-rails.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010-2013, Vladimir Agafonkin 2 | Copyright (c) 2010-2011, CloudMade 3 | Copyright (c) 2012-2013, Akshay Joshi 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are 7 | permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this list of 10 | conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, this list 13 | of conditions and the following disclaimer in the documentation and/or other materials 14 | provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 17 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 | COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 23 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [](https://travis-ci.org/axyjo/leaflet-rails) 2 | [](http://badge.fury.io/rb/leaflet-rails) 3 | 4 | Note: Intent to Deprecate 5 | ================ 6 | As of 2024-07-14, Rails 5 is long past its EOL. Rails 6+ support alternative Javascript bundling solutions, which work a lot better than this approach does. As such, I intend on marking this project as deprecated on or after 2024-10-01. Any Leaflet upgrades prior to that date will still be honoured. 7 | 8 | 9 | Quickstart Guide 10 | ================ 11 | 12 | To start using the leaflet-rails gem, follow the steps below (assuming you use the default asset pipeline): 13 | 14 | First, add the following code to your `Gemfile`. 15 | 16 | ```ruby 17 | gem 'leaflet-rails' 18 | ``` 19 | 20 | Then, run `bundle install` from within your project to download the necessary files. Following that, open your application-wide CSS file (`app/assets/stylesheets/application.css`) and add the following line as a comment: 21 | 22 | ``` 23 | = require leaflet 24 | ``` 25 | 26 | After that, open your application-wide Javascript file (typically `app/assets/javascripts/application.js`) and add the following line before requiring files which depend on Leaflet: 27 | 28 | ``` 29 | = require leaflet 30 | ``` 31 | 32 | At this point, you may skip the first two steps of the [Leaflet Quick Start guide](http://leafletjs.com/examples/quick-start/) and start at the third step (adding the map `div` to a view). 33 | 34 | 35 | Version Parity 36 | ============== 37 | 38 | `leaflet-rails` keeps version parity with the upstream `leaflet.js` library. Before v0.7.7 the versions were not always in sync, as noted in the table below. 39 | 40 | | leaflet-rails | leaflet.js | Reason | 41 | | ------------- | ------------- | ------| 42 | | 0.7.4 | 0.7.3 | Requested in #33 because of large gap between master and rubygems.org.| 43 | | 0.7.5 | 0.7.5 | leaflet.js 0.7.4 was reverted. | 44 | | 0.7.6 | ---- | Skipped to sync with upstream. | 45 | | 0.7.7 | 0.7.7 | Sync version numbers with upstream. | 46 | | 1.9.5 | 1.9.4 | Adding intent to deprecate post-install message. | 47 | 48 | Helpers 49 | ======= 50 | 51 | To get you up and running quickly, you can also use the gem's helper. To get started, add the following lines to a file called `leaflet.rb` in `config/initializers`: 52 | 53 | ```ruby 54 | Leaflet.tile_layer = "http://{s}.tile.cloudmade.com/YOUR-CLOUDMADE-API-KEY/997/256/{z}/{x}/{y}.png" 55 | # You can also use any other tile layer here if you don't want to use Cloudmade - see http://leafletjs.com/reference.html#tilelayer for more 56 | Leaflet.attribution = "Your attribution statement" 57 | Leaflet.max_zoom = 18 58 | ``` 59 | 60 | If you are using a tile layer which requires non-default subdomains such as [MapQuest-OSM Tiles](http://developer.mapquest.com/web/products/open/map), you can set the subdomains like this: 61 | 62 | ```ruby 63 | Leaflet.tile_layer = "http://{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.png" 64 | Leaflet.subdomains = ['otile1', 'otile2', 'otile3', 'otile4'] 65 | ``` 66 | 67 | You will then be able to call the ```#map``` helper method in a view, and make sure that the helper method is inside an erb tag like so: 68 | ```ruby 69 | <%= map(:center => { 70 | :latlng => [51.52238797921441, -0.08366235665359283], 71 | :zoom => 18 72 | }) %> 73 | ``` 74 | 75 | You can also add any number of markers like so: 76 | ```ruby 77 | map(:center => { 78 | :latlng => [51.52238797921441, -0.08366235665359283], 79 | :zoom => 18 80 | }, 81 | :markers => [ 82 | { 83 | :latlng => [51.52238797921441, -0.08366235665359283], 84 | } 85 | ] 86 | ) 87 | ``` 88 | 89 | Adding a `:popup` element to a marker hash will also generate a popup for a maker: 90 | 91 | ```ruby 92 | map(:center => { 93 | :latlng => [51.52238797921441, -0.08366235665359283], 94 | :zoom => 18 95 | }, 96 | :markers => [ 97 | { 98 | :latlng => [51.52238797921441, -0.08366235665359283], 99 | :popup => "Hello!" 100 | } 101 | ] 102 | ) 103 | ``` 104 | 105 | If you want to override the map settings you have set in the initializer, you can also add them to the helper method: 106 | 107 | ```ruby 108 | map(:center => { 109 | :latlng => [51.52238797921441, -0.08366235665359283], 110 | :zoom => 18 111 | }, 112 | :tile_layer => "http://{s}.somedomain.com/somepath/{z}/{x}/{y}.png", 113 | :attribution => "Some other attribution text", 114 | :max_zoom => 4 115 | ) 116 | ``` 117 | 118 | If you want to have multiple maps on same page , you should add unique container_id in helper method for each map: 119 | 120 | ```ruby 121 | map(:container_id => "first_map", :center => { 122 | :latlng => [51.52238797921441, -0.08366235665359283], 123 | :zoom => 18 124 | }) 125 | 126 | map(:container_id => "second_map", :center => { 127 | :latlng => [51.52238797921441, -0.08366235665359283], 128 | :zoom => 18 129 | }) 130 | ``` 131 | 132 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | require 'rspec/core/rake_task' 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec -------------------------------------------------------------------------------- /leaflet-rails.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative 'lib/leaflet-rails/version' 4 | 5 | Gem::Specification.new do |s| 6 | s.name = 'leaflet-rails' 7 | s.version = Leaflet::Rails::VERSION 8 | s.authors = ['Akshay Joshi'] 9 | s.email = ['leaflet_rails@akshayjoshi.com'] 10 | s.license = 'BSD' 11 | s.homepage = '' 12 | s.summary = 'Use leaflet.js with Rails 4/5.' 13 | s.description = 'This gem provides the leaflet.js map display library for your Rails 4+ application.' 14 | 15 | s.files = Dir['lib/**/*', 'vendor/**/*'] 16 | s.test_files = Dir['spec/**/*'] 17 | s.executables = [] 18 | s.require_paths = ['lib'] 19 | 20 | s.post_install_message = "leaflet-rails: Intent to deprecate on or after 2024-10-01. See repo for details" 21 | 22 | s.add_dependency 'railties', '>= 4.2.0' 23 | s.add_dependency 'actionpack', '>= 4.2.0' 24 | s.add_development_dependency 'rspec', '<= 3.4.0' 25 | s.add_development_dependency 'simplecov-rcov' 26 | end 27 | -------------------------------------------------------------------------------- /lib/leaflet-rails.rb: -------------------------------------------------------------------------------- 1 | require "leaflet-rails/version" 2 | require "leaflet-rails/view_helpers" 3 | require "rails" 4 | 5 | module Leaflet 6 | mattr_accessor :tile_layer 7 | mattr_accessor :attribution 8 | mattr_accessor :max_zoom 9 | mattr_accessor :subdomains 10 | 11 | module Rails 12 | class Engine < ::Rails::Engine 13 | initializer 'leaflet-rails.precompile' do |app| 14 | if app.config.respond_to? (:assets) 15 | app.config.assets.precompile += %w(layers-2x.png layers.png marker-icon-2x.png marker-icon.png marker-shadow.png) 16 | end 17 | end 18 | 19 | initializer 'leaflet-rails.helpers' do 20 | ActionView::Base.send :include, Leaflet::ViewHelpers 21 | end 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/leaflet-rails/version.rb: -------------------------------------------------------------------------------- 1 | module Leaflet 2 | module Rails 3 | VERSION = "1.9.5" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/leaflet-rails/view_helpers.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/inflector' 2 | module Leaflet 3 | module ViewHelpers 4 | 5 | def map(options) 6 | 7 | options[:tile_layer] ||= Leaflet.tile_layer 8 | options[:attribution] ||= Leaflet.attribution 9 | options[:max_zoom] ||= Leaflet.max_zoom 10 | options[:subdomains] ||= Leaflet.subdomains 11 | options[:container_id] ||= 'map' 12 | 13 | tile_layer = options.delete(:tile_layer) || Leaflet.tile_layer 14 | attribution = options.delete(:attribution) || Leaflet.attribution 15 | max_zoom = options.delete(:max_zoom) || Leaflet.max_zoom 16 | container_id = options.delete(:container_id) || 'map' 17 | no_container = options.delete(:no_container) 18 | center = options.delete(:center) 19 | markers = options.delete(:markers) 20 | circles = options.delete(:circles) 21 | polylines = options.delete(:polylines) 22 | geojsons = options.delete(:geojsons) 23 | fitbounds = options.delete(:fitbounds) 24 | subdomains = options.delete(:subdomains) 25 | 26 | output = [] 27 | output << "
" unless no_container 28 | output << "" 103 | output.join("\n").html_safe 104 | end 105 | 106 | private 107 | 108 | def prep_icon_settings(settings) 109 | settings[:name] = 'icon' if settings[:name].nil? or settings[:name].blank? 110 | settings[:shadow_url] = '' if settings[:shadow_url].nil? 111 | settings[:icon_size] = [] if settings[:icon_size].nil? 112 | settings[:shadow_size] = [] if settings[:shadow_size].nil? 113 | settings[:icon_anchor] = [0, 0] if settings[:icon_anchor].nil? 114 | settings[:shadow_anchor] = [0, 0] if settings[:shadow_anchor].nil? 115 | settings[:popup_anchor] = [0, 0] if settings[:popup_anchor].nil? 116 | return settings 117 | end 118 | end 119 | 120 | end 121 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # Require this file using `require "spec_helper"` to ensure that it is only 4 | # loaded once. 5 | # 6 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 7 | 8 | require 'simplecov' 9 | require 'simplecov-rcov' 10 | 11 | SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter 12 | SimpleCov.start 13 | 14 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 15 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 16 | 17 | ENV["RAILS_ENV"] ||= 'test' 18 | 19 | require "action_controller/railtie" 20 | require "rails/test_unit/railtie" 21 | 22 | require "leaflet-rails" 23 | 24 | module Test 25 | class Application < ::Rails::Application 26 | # configuration here if needed 27 | config.active_support.deprecation = :stderr 28 | end 29 | end 30 | 31 | Test::Application.initialize! 32 | 33 | RSpec.configure do |config| 34 | config.run_all_when_everything_filtered = true 35 | config.filter_run :focus 36 | 37 | # Run specs in random order to surface order dependencies. If you find an 38 | # order dependency and want to debug it, you can fix the order by providing 39 | # the seed, which is printed after each run. 40 | # --seed 1234 41 | config.order = 'random' 42 | end 43 | -------------------------------------------------------------------------------- /spec/view_helpers_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Leaflet::ViewHelpers do 4 | 5 | class TestView < ActionView::Base 6 | end 7 | 8 | before :all do 9 | Leaflet.tile_layer = "http://{s}.somedomain.com/blabla/{z}/{x}/{y}.png" 10 | Leaflet.attribution = "Some attribution statement" 11 | Leaflet.max_zoom = 18 12 | 13 | @view = TestView.new 14 | end 15 | 16 | it 'should mix in view helpers on initialization' do 17 | expect(@view).to respond_to(:map) 18 | end 19 | 20 | it 'should set the method configuration options' do 21 | result = @view.map(:center => { 22 | :latlng => [51.52238797921441, -0.08366235665359283], 23 | :zoom => 18 24 | }) 25 | 26 | expect(result).to match(/L.tileLayer\('http:\/\/{s}.somedomain\.com\/blabla\/{z}\/{x}\/{y}\.png'/) 27 | expect(result).to match(/attribution: 'Some attribution statement'/) 28 | expect(result).to match(/maxZoom: 18/) 29 | end 30 | 31 | it 'should set subdomains if present' do 32 | result = @view.map(:center => { 33 | :latlng => [51.52238797921441, -0.08366235665359283], 34 | :zoom => 18 35 | }, :subdomains => ['otile1', 'otile2']) 36 | expect(result).to match(/subdomains: \["otile1", "otile2"\]/) 37 | end 38 | 39 | it 'should not set subdomains if nil' do 40 | result = @view.map(:center => { 41 | :latlng => [51.52238797921441, -0.08366235665359283], 42 | :zoom => 18 43 | }, :subdomains => nil) 44 | expect(result).not_to match(/subdomains:/) 45 | end 46 | 47 | it 'should generate a basic map with the correct latitude, longitude and zoom' do 48 | result = @view.map(:center => { 49 | :latlng => [51.52238797921441, -0.08366235665359283], 50 | :zoom => 18 51 | }) 52 | expect(result).to match(/map\.setView\(\[51.52238797921441, -0.08366235665359283\], 18\)/) 53 | end 54 | 55 | it 'should generate a marker' do 56 | result = @view.map(:center => { 57 | :latlng => [51.52238797921441, -0.08366235665359283], 58 | :zoom => 18 59 | }, 60 | :markers => [ 61 | { 62 | :latlng => [51.52238797921441, -0.08366235665359283], 63 | } 64 | ]) 65 | expect(result).to match(/marker = L\.marker\(\[51.52238797921441, -0.08366235665359283\]\).addTo\(map\)/) 66 | end 67 | 68 | it 'should generate a marker with a popup' do 69 | result = @view.map(:center => { 70 | :latlng => [51.52238797921441, -0.08366235665359283], 71 | :zoom => 18 72 | }, 73 | :markers => [ 74 | { 75 | :latlng => [51.52238797921441, -0.08366235665359283], 76 | :popup => "Hello!" 77 | } 78 | ]) 79 | expect(result).to match(/marker = L\.marker\(\[51.52238797921441, -0.08366235665359283\]\).addTo\(map\)/) 80 | expect(result).to match(/marker\.bindPopup\('Hello!'\)/) 81 | end 82 | 83 | it 'should generate a marker with a popup and a custom icon' do 84 | icon_options = { :name => 'house', 85 | :icon_url => 'images/house.png', 86 | :shadow_url => 'images/house_shadow.png', 87 | :icon_size => [38, 95], 88 | :shadow_size => [50, 64], 89 | :icon_anchor => [22, 94], 90 | :shadow_anchor => [4, 62], 91 | :popup_anchor => [-3, -76]} 92 | result = @view.map(:center => { 93 | :latlng => [51.52238797921441, -0.08366235665359283], 94 | :zoom => 18 95 | }, 96 | :markers => [ 97 | { 98 | :latlng => [51.52238797921441, -0.08366235665359283], 99 | :popup => "Hello!", 100 | :icon => icon_options 101 | } 102 | ]) 103 | expected_icon_def = "var #{icon_options[:name]}0 = L.icon({iconUrl: '#{icon_options[:icon_url]}', shadowUrl: '#{icon_options[:shadow_url]}', iconSize: #{icon_options[:icon_size]}, shadowSize: #{icon_options[:shadow_size]}, iconAnchor: #{icon_options[:icon_anchor]}, shadowAnchor: #{icon_options[:shadow_anchor]}, popupAnchor: #{icon_options[:popup_anchor]}})" 104 | expect(result).to include(expected_icon_def) 105 | expect(result).to match(/marker = L\.marker\(\[51.52238797921441, -0.08366235665359283\], \{icon: house\d+\}\).addTo\(map\)/) 106 | expect(result).to match(/marker\.bindPopup\('Hello!'\)/) 107 | end 108 | 109 | it 'should generate many markers with popups and custom icons' do 110 | icon_options = { :name => 'house', 111 | :icon_url => 'images/house.png', 112 | :shadow_url => 'images/house_shadow.png', 113 | :icon_size => [38, 95], 114 | :shadow_size => [50, 64], 115 | :icon_anchor => [22, 94], 116 | :shadow_anchor => [4, 62], 117 | :popup_anchor => [-3, -76]} 118 | result = @view.map(:center => { 119 | :latlng => [51.52238797921441, -0.08366235665359283], 120 | :zoom => 18 121 | }, 122 | :markers => [ 123 | { 124 | :latlng => [51.52238797921441, -0.08366235665359283], 125 | :popup => "Hello!", 126 | :icon => icon_options 127 | }, 128 | { 129 | :latlng => [51.54238797921441, -0.08566235665359283], 130 | :popup => "Farewell!", 131 | :icon => icon_options 132 | } 133 | ]) 134 | expected_icon_def_1 = "var #{icon_options[:name]}0 = L.icon({iconUrl: '#{icon_options[:icon_url]}', shadowUrl: '#{icon_options[:shadow_url]}', iconSize: #{icon_options[:icon_size]}, shadowSize: #{icon_options[:shadow_size]}, iconAnchor: #{icon_options[:icon_anchor]}, shadowAnchor: #{icon_options[:shadow_anchor]}, popupAnchor: #{icon_options[:popup_anchor]}})" 135 | expected_icon_def_2 = "var #{icon_options[:name]}1 = L.icon({iconUrl: '#{icon_options[:icon_url]}', shadowUrl: '#{icon_options[:shadow_url]}', iconSize: #{icon_options[:icon_size]}, shadowSize: #{icon_options[:shadow_size]}, iconAnchor: #{icon_options[:icon_anchor]}, shadowAnchor: #{icon_options[:shadow_anchor]}, popupAnchor: #{icon_options[:popup_anchor]}})" 136 | expect(result).to include(expected_icon_def_1) 137 | expect(result).to include(expected_icon_def_2) 138 | expect(result).to match(/marker = L\.marker\(\[51.52238797921441, -0.08366235665359283\], \{icon: house\d+\}\).addTo\(map\)/) 139 | expect(result).to match(/marker = L\.marker\(\[51.54238797921441, -0.08566235665359283\], \{icon: house\d+\}\).addTo\(map\)/) 140 | expect(result).to match(/marker\.bindPopup\('Hello!'\)/) 141 | expect(result).to match(/marker\.bindPopup\('Farewell!'\)/) 142 | end 143 | 144 | it 'should have defaults for icon options' do 145 | icon_options = { :icon_url => 'images/house.png'} 146 | result = @view.map(:center => { 147 | :latlng => [51.52238797921441, -0.08366235665359283], 148 | :zoom => 18 149 | }, 150 | :markers => [ 151 | { 152 | :latlng => [51.52238797921441, -0.08366235665359283], 153 | :popup => "Hello!", 154 | :icon => icon_options 155 | } 156 | ]) 157 | expected_icon_def = "var icon0 = L.icon({iconUrl: '#{icon_options[:icon_url]}', shadowUrl: '', iconSize: [], shadowSize: [], iconAnchor: [0, 0], shadowAnchor: [0, 0], popupAnchor: [0, 0]})" 158 | expect(result).to include(expected_icon_def) 159 | expect(result).to match(/marker = L\.marker\(\[51.52238797921441, -0.08366235665359283\], \{icon: icon\d+\}\).addTo\(map\)/) 160 | expect(result).to match(/marker\.bindPopup\('Hello!'\)/) 161 | end 162 | 163 | it 'should override the method configuration options if set' do 164 | result = @view.map(:center => { 165 | :latlng => [51.52238797921441, -0.08366235665359283], 166 | :zoom => 18 167 | }, 168 | :tile_layer => "http://{s}.someotherdomain.com/blabla/{z}/{x}/{y}.png", 169 | :attribution => "Some other attribution text", 170 | :max_zoom => 4 171 | ) 172 | 173 | expect(result).to match(/L.tileLayer\('http:\/\/{s}.someotherdomain\.com\/blabla\/{z}\/{x}\/{y}\.png'/) 174 | expect(result).to match(/attribution: 'Some other attribution text'/) 175 | expect(result).to match(/maxZoom: 4/) 176 | end 177 | 178 | it 'should pass any configuration options to L.tileLayer if set' do 179 | result = @view.map(:center => { 180 | :latlng => [51.52238797921441, -0.08366235665359283], 181 | :zoom => 18 182 | }, 183 | :tile_layer => "http://{s}.someotherdomain.com/blabla/{z}/{x}/{y}.png", 184 | :attribution => "Some other attribution text", 185 | :max_zoom => 4, 186 | :some_key => 'some value', 187 | :key2 => 42 188 | ) 189 | 190 | expect(result).to match(/L.tileLayer\('http:\/\/{s}.someotherdomain\.com\/blabla\/{z}\/{x}\/{y}\.png'/) 191 | expect(result).to match(/attribution: 'Some other attribution text'/) 192 | expect(result).to match(/maxZoom: 4/) 193 | expect(result).to match(/someKey: 'some value'/) 194 | expect(result).to match(/key2: '42'/) 195 | 196 | end 197 | 198 | it 'should have multiple map on single page' do 199 | result = @view.map(:container_id => "first_map", :center => { 200 | :latlng => [51.52238797921441, -0.08366235665359283], 201 | }) 202 | 203 | result1 = @view.map(:container_id => "second_map", :center => { 204 | :latlng => [51.62238797921441, -0.08366235665359284], 205 | }) 206 | 207 | expect(result).to match(/id=\'first_map'/) 208 | expect(result).to match(/L.map\('first_map'/) 209 | 210 | expect(result1).to match(/id=\'second_map'/) 211 | expect(result1).to match(/L.map\('second_map'/) 212 | 213 | end 214 | 215 | it 'should generate a map and add a circle' do 216 | result = @view.map( 217 | :container_id => "first_map", 218 | :center => { 219 | :latlng => [51.52238797921441, -0.08366235665359283] 220 | }, 221 | :circles => [ 222 | { 223 | :latlng => [51.52238797921441, -0.08366235665359283], 224 | :radius => 12, 225 | :color => 'red', 226 | :fillColor => '#f03', 227 | :fillOpacity => 0.5 228 | } 229 | ]) 230 | expect(result).to match(/L.circle\(\[\'51.52238797921441\', \'-0.08366235665359283\'\], 12, \{ 231 | color: \'red\', 232 | fillColor: \'#f03\', 233 | fillOpacity: 0.5 234 | \}\).addTo\(map\)/) 235 | end 236 | 237 | it 'should not create the container tag if no_container is set' do 238 | result = @view.map(:center => { 239 | :latlng => [51.52238797921441, -0.08366235665359283], 240 | :zoom => 18 241 | }, 242 | :no_container => true 243 | ) 244 | 245 | expect(result).not_to match(/