rails-marker 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.md +69 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/marker/map_google.js.coffee +78 -0
- data/app/views/marker/_google.html.erb +14 -0
- data/lib/marker/engine.rb +23 -0
- data/lib/marker/helpers/field_tag.rb +55 -0
- data/lib/marker/helpers/form_builder.rb +16 -0
- data/lib/marker/helpers/form_tag_helper.rb +15 -0
- data/lib/marker/hooks/formtastic.rb +12 -0
- data/lib/marker/hooks/simple_form.rb +7 -0
- data/lib/marker/version.rb +3 -0
- data/lib/marker.rb +10 -0
- data/lib/rails-marker.rb +1 -0
- metadata +71 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 Aimbulance http://www.aimbulance.com/
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Rails::Marker
|
2
|
+
|
3
|
+
Form helpers for edit fields such as zoom, longitude and latitude
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rails-marker'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rails-marker
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
For example you have model office:
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
|
25
|
+
# == Schema Information
|
26
|
+
#
|
27
|
+
# Table name: offices
|
28
|
+
#
|
29
|
+
# id :integer(4) not null, primary key
|
30
|
+
# title :string(255) not null
|
31
|
+
# address :string(255)
|
32
|
+
# phone :string(255)
|
33
|
+
# email :string(255)
|
34
|
+
# longitude :float
|
35
|
+
# latitude :float
|
36
|
+
# zoom :integer(1)
|
37
|
+
# created_at :datetime not null
|
38
|
+
# updated_at :datetime not null
|
39
|
+
#
|
40
|
+
|
41
|
+
class Office < ActiveRecord::Base
|
42
|
+
validates_presence_of :title, :address
|
43
|
+
validates_numericality_of :latitude, :longitude
|
44
|
+
|
45
|
+
attr_accessible :address, :email, :latitude, :longitude, :phone, :title, :is_visible, :zoom
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
And you want edit fields zoom, longitude and latitude:
|
50
|
+
|
51
|
+
``` erb
|
52
|
+
<%= form_for @office do |f| %>
|
53
|
+
<%= f.marker_field :map, {:lat => :latitude, :lng => :longitude}, {:style => "width:890px;height:400px;"} %>
|
54
|
+
<% end %>
|
55
|
+
```
|
56
|
+
|
57
|
+
It's all you need! Just move the marker, and zoom, latitude and longitude fields will be automatically filled:
|
58
|
+
|
59
|
+

|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
68
|
+
|
69
|
+
Copyright (c) 2012 Aimbulance, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
$ = jQuery
|
2
|
+
|
3
|
+
class MapGoogle
|
4
|
+
|
5
|
+
constructor: (@dom_id, options = {}) ->
|
6
|
+
defaults =
|
7
|
+
lat: 50.44067063154785
|
8
|
+
lng: 30.52654266357422
|
9
|
+
zoom: 6
|
10
|
+
field_lat: "#lat"
|
11
|
+
field_lng: "#lng"
|
12
|
+
field_zoom: "#zoom"
|
13
|
+
|
14
|
+
@options = $.extend defaults, options
|
15
|
+
this._setup()
|
16
|
+
|
17
|
+
_setup: ->
|
18
|
+
@location = this._build_location()
|
19
|
+
@map = this._build_map(@dom_id)
|
20
|
+
@marker = this._build_marker()
|
21
|
+
|
22
|
+
google.maps.event.addListener(@marker, 'dragend', (event) =>
|
23
|
+
pos = @marker.getPosition()
|
24
|
+
|
25
|
+
$(@options.field_lat).val pos.lat()
|
26
|
+
$(@options.field_lng).val pos.lng()
|
27
|
+
|
28
|
+
@map.setCenter(event.latLng)
|
29
|
+
)
|
30
|
+
|
31
|
+
google.maps.event.addListener(@map, 'zoom_changed', () =>
|
32
|
+
$(@options.field_zoom).val @map.getZoom()
|
33
|
+
)
|
34
|
+
|
35
|
+
placeMarker: (location) ->
|
36
|
+
marker = this._build_marker {position: location}
|
37
|
+
|
38
|
+
@map.setCenter(location);
|
39
|
+
|
40
|
+
return marker
|
41
|
+
|
42
|
+
_build_location: ->
|
43
|
+
lat = this._parse_value @options.field_lat, @options.lat
|
44
|
+
lng = this._parse_value @options.field_lng, @options.lng
|
45
|
+
|
46
|
+
new google.maps.LatLng(lat, lng)
|
47
|
+
|
48
|
+
_build_map: (dom_id, options = {}) ->
|
49
|
+
zoom = this._parse_value @options.field_zoom, @options.zoom
|
50
|
+
|
51
|
+
defaults =
|
52
|
+
zoom: zoom
|
53
|
+
center: @location
|
54
|
+
mapTypeId: google.maps.MapTypeId.ROADMAP
|
55
|
+
|
56
|
+
settings = $.extend defaults, options
|
57
|
+
|
58
|
+
new google.maps.Map(document.getElementById(dom_id), settings)
|
59
|
+
|
60
|
+
_build_marker: (options = {}) ->
|
61
|
+
defaults =
|
62
|
+
position: @location
|
63
|
+
map: @map
|
64
|
+
draggable: true
|
65
|
+
|
66
|
+
settings = $.extend defaults, options
|
67
|
+
|
68
|
+
new google.maps.Marker(settings)
|
69
|
+
|
70
|
+
_parse_value: (field, default_value) ->
|
71
|
+
value = parseFloat $(field).val()
|
72
|
+
|
73
|
+
if value
|
74
|
+
return value
|
75
|
+
else
|
76
|
+
return default_value
|
77
|
+
|
78
|
+
window.MapGoogle = MapGoogle
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<%= javascript_include_tag "http://maps.google.com/maps/api/js?sensor=false&language=#{I18n.locale}" %>
|
2
|
+
<%= javascript_include_tag "marker/map_google.js" %>
|
3
|
+
|
4
|
+
<%= hidden_field field.object_name, field.input_options[:lat] %>
|
5
|
+
<%= hidden_field field.object_name, field.input_options[:lng] %>
|
6
|
+
<%= hidden_field field.object_name, field.input_options[:zoom] %>
|
7
|
+
|
8
|
+
<%= content_tag :div, nil, field.html_options %>
|
9
|
+
|
10
|
+
<script type="text/javascript">
|
11
|
+
$(document).ready(function(){
|
12
|
+
new MapGoogle("<%= field.html_options[:id] %>", <%=raw field.map_options.to_json %>);
|
13
|
+
});
|
14
|
+
</script>
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'marker'
|
3
|
+
|
4
|
+
module Marker
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
|
7
|
+
initializer "marker.assets_precompile", :group => :assets do |app|
|
8
|
+
app.config.assets.precompile += ["marker/map_google.js"]
|
9
|
+
end
|
10
|
+
|
11
|
+
initializer "marker.helpers" do
|
12
|
+
ActiveSupport.on_load :action_view do
|
13
|
+
ActionView::Base.send(:include, Marker::Helpers::FormTagHelper)
|
14
|
+
ActionView::Helpers::FormBuilder.send(:include, Marker::Helpers::FormBuilder)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
initializer "marker.hooks" do
|
19
|
+
require "marker/hooks/simple_form" if Object.const_defined?("SimpleForm")
|
20
|
+
require "marker/hooks/formtastic" if Object.const_defined?("Formtastic")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Marker
|
2
|
+
module Helpers
|
3
|
+
class FieldTag
|
4
|
+
attr_reader :template, :object, :engine, :html_options
|
5
|
+
|
6
|
+
# Wrapper for render marker field
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# marker = FieldTag.new(object_name, method_name, template, options)
|
10
|
+
# marker.to_s
|
11
|
+
#
|
12
|
+
def initialize(object_name, method_name, template, options = {}, html_options = {}) #:nodoc:
|
13
|
+
options = { :object_name => object_name, :method_name => method_name }.merge(options)
|
14
|
+
|
15
|
+
@template, @options, @html_options = template, options.dup, html_options.dup
|
16
|
+
@engine = (@options.delete(:engine) || "google")
|
17
|
+
@object = (@options.delete(:object) || @template.instance_variable_get("@#{object_name}"))
|
18
|
+
@html_options[:id] ||= id
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s(locals = {}) #:nodoc:
|
22
|
+
locals = { :field => self }.merge(locals)
|
23
|
+
@template.render :partial => "marker/#{@engine}", :locals => @options.merge(locals)
|
24
|
+
end
|
25
|
+
|
26
|
+
def id
|
27
|
+
@id ||= @template.dom_id(@object, [method_name, 'marker'].join('_'))
|
28
|
+
end
|
29
|
+
|
30
|
+
def input_options
|
31
|
+
@input_options ||= {:zoom => :zoom, :lat => :latitude, :lng => :longitude}.merge(@options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def map_options
|
35
|
+
{
|
36
|
+
:field_lat => "##{sanitized_object_name}_#{input_options[:lat]}",
|
37
|
+
:field_lng => "##{sanitized_object_name}_#{input_options[:lng]}",
|
38
|
+
:field_zoom => "##{sanitized_object_name}_#{input_options[:zoom]}"
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_name
|
43
|
+
@options[:method_name]
|
44
|
+
end
|
45
|
+
|
46
|
+
def object_name
|
47
|
+
@options[:object_name]
|
48
|
+
end
|
49
|
+
|
50
|
+
def sanitized_object_name
|
51
|
+
@sanitized_object_name ||= object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Marker
|
2
|
+
module Helpers
|
3
|
+
module FormBuilder
|
4
|
+
# Render map field
|
5
|
+
# Usage:
|
6
|
+
#
|
7
|
+
# <%= form_for @office do |f| %>
|
8
|
+
# <%= f.marker_field :marker %>
|
9
|
+
# <%= end %>
|
10
|
+
#
|
11
|
+
def marker_field(method, options = {}, html_options = {})
|
12
|
+
@template.send("marker_field_tag", @object_name, method, objectify_options(options), html_options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Marker
|
2
|
+
module Helpers
|
3
|
+
module FormTagHelper
|
4
|
+
|
5
|
+
# A helper that renders file map container
|
6
|
+
#
|
7
|
+
# <%= marker_field_tag :office, :marker %>
|
8
|
+
#
|
9
|
+
def marker_field_tag(object_name, method_name, options = {}, html_options = {})
|
10
|
+
marker = FieldTag.new(object_name, method_name, self, options, html_options)
|
11
|
+
marker.to_s
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/marker.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "marker/version"
|
2
|
+
require "marker/engine"
|
3
|
+
|
4
|
+
module Marker
|
5
|
+
module Helpers
|
6
|
+
autoload :FormTagHelper, 'marker/helpers/form_tag_helper'
|
7
|
+
autoload :FormBuilder, 'marker/helpers/form_builder'
|
8
|
+
autoload :FieldTag, 'marker/helpers/field_tag'
|
9
|
+
end
|
10
|
+
end
|
data/lib/rails-marker.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "marker"
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-marker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Igor Galeta
|
9
|
+
- Pavlo Galeta
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-05-23 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sqlite3
|
17
|
+
requirement: &83065040 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *83065040
|
26
|
+
description: Form helpers for edit fields such as zoom, longitude and latitude
|
27
|
+
email:
|
28
|
+
- galeta.igor@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- app/views/marker/_google.html.erb
|
34
|
+
- app/assets/javascripts/marker/map_google.js.coffee
|
35
|
+
- lib/rails-marker.rb
|
36
|
+
- lib/marker.rb
|
37
|
+
- lib/marker/engine.rb
|
38
|
+
- lib/marker/helpers/form_tag_helper.rb
|
39
|
+
- lib/marker/helpers/field_tag.rb
|
40
|
+
- lib/marker/helpers/form_builder.rb
|
41
|
+
- lib/marker/hooks/simple_form.rb
|
42
|
+
- lib/marker/hooks/formtastic.rb
|
43
|
+
- lib/marker/version.rb
|
44
|
+
- MIT-LICENSE
|
45
|
+
- Rakefile
|
46
|
+
- README.md
|
47
|
+
homepage: https://github.com/galetahub/rails-marker
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Easy way to edit zoom, longitude and latitude
|
71
|
+
test_files: []
|