rails_admin_google_map 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +89 -0
- data/Rakefile +19 -0
- data/app/views/rails_admin/main/_form_google_map.html.erb +76 -0
- data/lib/rails_admin_google_map.rb +67 -0
- data/lib/rails_admin_google_map/engine.rb +4 -0
- data/lib/rails_admin_google_map/version.rb +3 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2c619860f0f650a331c96e796180d01d9dc7b132
|
4
|
+
data.tar.gz: c925f44aeb56dbd27b92aabaa24a54a6447d28c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e2ca3c9f608b1cedf0827614da54457359087525bd1f15e65647e40b8d72a40938d86ec80f63855fc3139a43bd9ad949e5ba53e9a15be865c06ec36bf759565
|
7
|
+
data.tar.gz: 0f16794a66326bcac6577b3b1a8b5dc25f33c829f99efcc4c6fb1b61bbd232a095c724782ae12d19d43d9df090883475ab25413958dcba87313f3c4085eb3fc2
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 KOVACS Nicolas
|
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,89 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/rails_admin_google_map.svg)](https://badge.fury.io/rb/rails_admin_google_map)
|
2
|
+
|
3
|
+
# RailsAdminGoogleMap
|
4
|
+
Implementation of user friendly google map field in rails_admin.
|
5
|
+
Any suggestions are welcome.
|
6
|
+
|
7
|
+
![Example](http://i.imgur.com/ZlxMvZK.png)
|
8
|
+
|
9
|
+
## TODO
|
10
|
+
- [ ] Test with multiple google map field within the same model
|
11
|
+
- [ ] More Google map options
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'rails_admin_google_map'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
```bash
|
22
|
+
$ bundle
|
23
|
+
```
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
```bash
|
27
|
+
$ gem install rails_admin_google_map
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
Strategy: using a serialized field to store all google map data. We store all datas return
|
32
|
+
|
33
|
+
### Migration example:
|
34
|
+
```ruby
|
35
|
+
def change
|
36
|
+
add_column :users, :google_map, :text
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
#### In your model:
|
41
|
+
```ruby
|
42
|
+
class User < ActiveRecord::Base
|
43
|
+
serialize :google_map, JSON
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
#### RailsAdmin configuration
|
48
|
+
```ruby
|
49
|
+
RailsAdmin.config do |config|
|
50
|
+
config.model Point do
|
51
|
+
edit do
|
52
|
+
field :google_map, :google_map do
|
53
|
+
google_api_key 'APIKEY'
|
54
|
+
default_latitude -34.0
|
55
|
+
default_longitude 151.0
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
Example of JSON stored values, everything is build dynamically and depends on Google return.
|
63
|
+
|
64
|
+
```javascript
|
65
|
+
{
|
66
|
+
"location":{
|
67
|
+
"latitude":45.5016889,
|
68
|
+
"longitude":-73.56725599999999
|
69
|
+
},
|
70
|
+
"formatted_address":"Montréal, QC, Canada",
|
71
|
+
"locality":"Montréal",
|
72
|
+
"political":"Canada",
|
73
|
+
"administrative_area_level_3":"Pointe-Calumet",
|
74
|
+
"administrative_area_level_2":"Communauté-Urbaine-de-Montréal",
|
75
|
+
"administrative_area_level_1":"Québec",
|
76
|
+
"country":"Canada"
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
Example of usage in a view
|
81
|
+
|
82
|
+
```
|
83
|
+
<%= u.google_map['location']['latitude'] %>
|
84
|
+
<%= u.google_map['country'] %>
|
85
|
+
<%= u.google_map['formatted_address'] %>
|
86
|
+
```
|
87
|
+
|
88
|
+
## License
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsAdminGoogleMap'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
load 'rails/tasks/statistics.rake'
|
18
|
+
|
19
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,76 @@
|
|
1
|
+
<%= form.send :text_field, nil, name:'_search', id: field.dom_name + '_search', class: 'form-control', style: 'width: 100%; border-radius: 0;', value: '' %>
|
2
|
+
<script async defer
|
3
|
+
src="https://maps.googleapis.com/maps/api/js?key=<%= field.google_api_key %>&libraries=places&callback=initMap">
|
4
|
+
</script>
|
5
|
+
<div class="ra-google-map-container" id="<%= field.dom_name %>" style="width:100%; min-height:350px"></div>
|
6
|
+
<%= form.send :hidden_field, field.method_name, id: form.options[:as].to_s + '_' + field.name.to_s, value: field.form_value %>
|
7
|
+
|
8
|
+
<script type="text/javascript">
|
9
|
+
var map = {};
|
10
|
+
var markers;
|
11
|
+
function initMap() {
|
12
|
+
markers = [];
|
13
|
+
var myOptions = {
|
14
|
+
zoom: <%= field.default_zoom_level %>,
|
15
|
+
center: new google.maps.LatLng(<%= field.default_latitude %>, <%= field.default_longitude %>),
|
16
|
+
mapTypeId: google.maps.MapTypeId.ROADMAP,
|
17
|
+
scrollwheel: false
|
18
|
+
};
|
19
|
+
map = new google.maps.Map(document.getElementById("<%= field.dom_name %>"), myOptions);
|
20
|
+
document.getElementById("<%= field.dom_name + '_search' %>").value = '';
|
21
|
+
initializeAutocomplete('<%= field.dom_name %>_search');
|
22
|
+
function initializeAutocomplete(id) {
|
23
|
+
var element = document.getElementById(id);
|
24
|
+
if (element) {
|
25
|
+
var autocomplete = new google.maps.places.Autocomplete(element, {types: ['geocode']});
|
26
|
+
google.maps.event.addListener(autocomplete, 'place_changed', onPlaceChanged);
|
27
|
+
}
|
28
|
+
}
|
29
|
+
updateMarker();
|
30
|
+
}
|
31
|
+
|
32
|
+
function onPlaceChanged() {
|
33
|
+
cleanAllMarkers();
|
34
|
+
var final_datas = {}
|
35
|
+
var place = this.getPlace();
|
36
|
+
|
37
|
+
if (place.length == 0) {
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
|
41
|
+
var location = place.geometry.location;
|
42
|
+
final_datas.location = {
|
43
|
+
latitude: location.lat(),
|
44
|
+
longitude: location.lng()
|
45
|
+
};
|
46
|
+
final_datas.formatted_address = place.formatted_address;
|
47
|
+
for (var i in place.address_components) {
|
48
|
+
var component = place.address_components[i];
|
49
|
+
for (var j in component.types) {
|
50
|
+
final_datas[component.types[j]] = component.long_name;
|
51
|
+
}
|
52
|
+
}
|
53
|
+
document.getElementById("<%= form.options[:as].to_s + '_' + field.name.to_s %>").value = JSON.stringify(final_datas);
|
54
|
+
updateMarker();
|
55
|
+
}
|
56
|
+
|
57
|
+
function updateMarker() {
|
58
|
+
if (document.getElementById("<%= form.options[:as].to_s + '_' + field.name.to_s %>").value.length > 0) {
|
59
|
+
var json = JSON.parse(document.getElementById("<%= form.options[:as].to_s + '_' + field.name.to_s %>").value);
|
60
|
+
if (json.location) {
|
61
|
+
var marker = new google.maps.Marker({
|
62
|
+
position: new google.maps.LatLng(json.location.latitude, json.location.longitude),
|
63
|
+
map: map
|
64
|
+
});
|
65
|
+
markers.push(marker);
|
66
|
+
map.setCenter(new google.maps.LatLng(json.location.latitude, json.location.longitude));
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
function cleanAllMarkers() {
|
72
|
+
for (var i = 0; i < markers.length; i++) {
|
73
|
+
markers[i].setMap(null);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
</script>
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'rails_admin_google_map/engine'
|
2
|
+
|
3
|
+
module RailsAdminGoogleMap
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'rails_admin/config/fields'
|
7
|
+
require 'rails_admin/config/fields/base'
|
8
|
+
|
9
|
+
module RailsAdmin
|
10
|
+
module Config
|
11
|
+
module Fields
|
12
|
+
module Types
|
13
|
+
class GoogleMap < RailsAdmin::Config::Fields::Base
|
14
|
+
RailsAdmin::Config::Fields::Types.register(self)
|
15
|
+
|
16
|
+
def allowed_methods
|
17
|
+
[@name]
|
18
|
+
end
|
19
|
+
|
20
|
+
register_instance_option(:partial) do
|
21
|
+
:form_google_map
|
22
|
+
end
|
23
|
+
|
24
|
+
# Google Maps API Key - optional
|
25
|
+
register_instance_option(:google_api_key) do
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
|
29
|
+
# Latitude value to display in the map if the latitude attribute is nil
|
30
|
+
# (Otherwise the location defaults to (0,0) which is in the Gulf of Guinea
|
31
|
+
register_instance_option(:default_latitude) do
|
32
|
+
51.5 # Latitude of London, United Kingdom
|
33
|
+
end
|
34
|
+
|
35
|
+
# Longitude value to display if the longitude attribute is nil
|
36
|
+
register_instance_option(:default_longitude) do
|
37
|
+
-0.126
|
38
|
+
end
|
39
|
+
|
40
|
+
# Default zoom level of the map
|
41
|
+
register_instance_option(:default_zoom_level) do
|
42
|
+
8
|
43
|
+
end
|
44
|
+
|
45
|
+
register_instance_option :formatted_value do
|
46
|
+
value.present? ? JSON.pretty_generate(value) : nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def dom_name
|
50
|
+
@dom_name ||= "#{bindings[:form].object_name}_#{@name}_google_map_field"
|
51
|
+
end
|
52
|
+
|
53
|
+
def parse_value(value)
|
54
|
+
value.present? ? JSON.parse(value) : nil
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_input(params)
|
58
|
+
params[name] = parse_value(params[name]) if params[name].is_a?(::String)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_admin_google_map
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- KOVACS Nicolas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
description: Easy implentation of google map adress field in rails_admin
|
34
|
+
email:
|
35
|
+
- pro.nkovacs@gmail.com
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- MIT-LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- app/views/rails_admin/main/_form_google_map.html.erb
|
44
|
+
- lib/rails_admin_google_map.rb
|
45
|
+
- lib/rails_admin_google_map/engine.rb
|
46
|
+
- lib/rails_admin_google_map/version.rb
|
47
|
+
homepage: http://www.nicovak.github.io
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata: {}
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 2.5.2
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Easy implentation of google map adress field in rails_admin
|
71
|
+
test_files: []
|