rails_admin_yamap_field 0.1.2
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.
- 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_yamap_field.html.haml +45 -0
- data/app/views/rails_admin/main/_show_yamap_field.html.haml +22 -0
- data/app/views/rails_admin/main/_yamap_field.html.haml +3 -0
- data/lib/rails_admin_yamap_field.rb +109 -0
- data/lib/rails_admin_yamap_field/engine.rb +4 -0
- data/lib/rails_admin_yamap_field/version.rb +3 -0
- data/wiki/screenshot_1.png +0 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b0993ff3f9e0dc7202df5dbe87d40197345e9a89b8f850921d601efe76ea4440
|
4
|
+
data.tar.gz: fe67fdb59d52090f87ae17762cc9a8f7d20f84370719da24eb25845af8b2acd8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27ddcf593415c1a4b2ced964392689ce42ff2f8edc0900fdb43dbc07578a7f896e46bbe52c8650a9801d223e066103bd8b9b10c05f48b2a56d599f709a30876a
|
7
|
+
data.tar.gz: 5a30087b047f3121b5795e058fa4c54c0d3f43ad715ee3815dbe9bd64d03be934ed9311ea3cf7b16e124e53f3c3ae15d127ff0639abe11002f3d713c08e37070
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Artem
|
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
|
+
# RailsAdminYamapField
|
2
|
+
|
3
|
+
Simple implementation of Yandex maps in rails admin.
|
4
|
+
|
5
|
+
Inspired by these projects:
|
6
|
+
* [Rails Admin Map Field](https://github.com/beyondthestory/rails_admin_map_field)
|
7
|
+
* [RailsAdminGoogleMap](https://github.com/nicovak/rails_admin_google_map)
|
8
|
+
* [Rails Admin Place Field](https://github.com/thinkclay/rails_admin_place_field/)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'rails_admin_yamap_field'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
```bash
|
20
|
+
$ bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Add `attr_accessor ya_map` to you model for which you need to show the map or define
|
26
|
+
`ya_map` and `ya_map=` methods as you wish, for example:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Address < ApplicationRecord
|
30
|
+
# others methods
|
31
|
+
def ya_map=(lat_lon)
|
32
|
+
self[:lat], self[:lon] = lat_lon.split(",")
|
33
|
+
end
|
34
|
+
def ya_map
|
35
|
+
[lat, lon].join(",")
|
36
|
+
end
|
37
|
+
# others methods
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
### Available options for configure in rails_admin:
|
42
|
+
|
43
|
+
* center_lat - map center latitude
|
44
|
+
* center_long - map center longitude
|
45
|
+
* zoom_level - map zoom level
|
46
|
+
* map_html_width - width div map container: default '100%'
|
47
|
+
* map_html_height - height div map container: default '500px'
|
48
|
+
* map_lang - language map: default 'ru_RU', for other values see [this ref](https://tech.yandex.ru/maps/doc/jsapi/2.1/dg/concepts/load-docpage/#load__param)
|
49
|
+
* api_key - api key for enterprise version: default 'nil'
|
50
|
+
* latitude_field - latitude attribute name in you model: default 'latitude'
|
51
|
+
* longitude_field - longitude attribute name in you model: default 'longitude'
|
52
|
+
|
53
|
+
### Example:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class Address < ApplicationRecord
|
57
|
+
attr_accessor :ya_map
|
58
|
+
|
59
|
+
rails_admin do
|
60
|
+
field :lat, :hidden
|
61
|
+
field :lon, :hidden
|
62
|
+
field :ya_map, :yamap_field do
|
63
|
+
map_html_width "600px"
|
64
|
+
latitude_field :lat
|
65
|
+
longitude_field :lon
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Screenshot
|
72
|
+
|
73
|
+

|
74
|
+
|
75
|
+
## References
|
76
|
+
|
77
|
+
* [API Yandex map](https://tech.yandex.ru/maps/doc/jsapi/2.1/quick-start/index-docpage/)
|
78
|
+
* [Placemark docpage](https://tech.yandex.ru/maps/doc/jsapi/2.1/ref/reference/Placemark-docpage/)
|
79
|
+
* [API Yandex map. DragEnd event](https://yandex.ru/blog/mapsapi/24027)
|
80
|
+
* [Create custom field in rails_admin](https://github.com/sferik/rails_admin/wiki/Custom-field)
|
81
|
+
* [rails_admin base field class](https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/fields/base.rb)
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Feel free to send pull requests or write issue.
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](https://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 = 'RailsAdminYamapField'
|
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,45 @@
|
|
1
|
+
= render 'yamap_field', field: field
|
2
|
+
= form.send field.view_helper, field.method_name, field.html_attributes
|
3
|
+
|
4
|
+
:javascript
|
5
|
+
function errorMap(error) {
|
6
|
+
console.log("Error in loading Yandex map!");
|
7
|
+
console.log(error);
|
8
|
+
}
|
9
|
+
|
10
|
+
function initMap(ymaps){
|
11
|
+
var mapCenterLat = "#{field.center_lat}";
|
12
|
+
var mapCenterLong = "#{field.center_long}";
|
13
|
+
var zoomLevel = "#{field.zoom_level}";
|
14
|
+
var yaMap = new ymaps.Map("yamap_field", {
|
15
|
+
center: [mapCenterLat, mapCenterLong],
|
16
|
+
zoom: zoomLevel
|
17
|
+
});
|
18
|
+
|
19
|
+
var lat = "#{field.bindings[:object].send(field.latitude_field)}";
|
20
|
+
var long = "#{field.bindings[:object].send(field.longitude_field)}";
|
21
|
+
var myPlacemark = new ymaps.Placemark([lat, long], {}, {
|
22
|
+
preset: 'islands#redDotIcon',
|
23
|
+
draggable: true
|
24
|
+
});
|
25
|
+
yaMap.geoObjects.add(myPlacemark);
|
26
|
+
|
27
|
+
function setCoordinates(position) {
|
28
|
+
//console.log(position);
|
29
|
+
//console.log($("##{field.latitude_dom_name}"));
|
30
|
+
$("##{field.dom_name}").val(position.join(","));
|
31
|
+
$("##{field.latitude_dom_name}").val(position[0]);
|
32
|
+
$("##{field.longitude_dom_name}").val(position[1]);
|
33
|
+
}
|
34
|
+
|
35
|
+
yaMap.events.add('click', function(e) {
|
36
|
+
var position = e.get('coords');
|
37
|
+
myPlacemark.geometry.setCoordinates(position);
|
38
|
+
setCoordinates(position);
|
39
|
+
});
|
40
|
+
|
41
|
+
myPlacemark.events.add('dragend', function(e) {
|
42
|
+
var position = myPlacemark.geometry.getCoordinates();
|
43
|
+
setCoordinates(position);
|
44
|
+
});
|
45
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
= render 'yamap_field', field: field
|
2
|
+
|
3
|
+
:javascript
|
4
|
+
function errorMap(error) {
|
5
|
+
console.log("Error in loading Yandex map!");
|
6
|
+
console.log(error);
|
7
|
+
}
|
8
|
+
|
9
|
+
function initMap(ymaps){
|
10
|
+
var mapCenterLat = "#{field.center_lat}";
|
11
|
+
var mapCenterLong = "#{field.center_long}";
|
12
|
+
var zoomLevel = "#{field.zoom_level}";
|
13
|
+
var yaMap = new ymaps.Map("yamap_field", {
|
14
|
+
center: [mapCenterLat, mapCenterLong],
|
15
|
+
zoom: zoomLevel
|
16
|
+
});
|
17
|
+
|
18
|
+
var lat = "#{field.bindings[:object].send(field.latitude_field)}";
|
19
|
+
var long = "#{field.bindings[:object].send(field.longitude_field)}";
|
20
|
+
var myPlacemark = new ymaps.Placemark([lat, long]);
|
21
|
+
yaMap.geoObjects.add(myPlacemark);
|
22
|
+
}
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require "rails_admin_yamap_field/engine"
|
2
|
+
|
3
|
+
module RailsAdminYamapField
|
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 YamapField < RailsAdmin::Config::Fields::Base
|
14
|
+
RailsAdmin::Config::Fields::Types::register(self)
|
15
|
+
|
16
|
+
register_instance_option(:partial) do
|
17
|
+
:form_yamap_field
|
18
|
+
end
|
19
|
+
|
20
|
+
register_instance_option(:formatted_value) do
|
21
|
+
if bindings[:controller].action_name == "show"
|
22
|
+
bindings[:view].render "rails_admin/main/show_yamap_field", field: self
|
23
|
+
else
|
24
|
+
value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# If the point is presented, she is the center of the map
|
29
|
+
# else center of the map 0 km in Moscow
|
30
|
+
register_instance_option(:center_lat) do
|
31
|
+
bindings[:object].send(self.latitude_field) || 55.767461
|
32
|
+
end
|
33
|
+
|
34
|
+
register_instance_option(:center_long) do
|
35
|
+
bindings[:object].send(self.longitude_field) || 37.631422
|
36
|
+
end
|
37
|
+
|
38
|
+
register_instance_option(:zoom_level) do
|
39
|
+
7
|
40
|
+
end
|
41
|
+
|
42
|
+
register_instance_option(:map_html_width) do
|
43
|
+
'100%'
|
44
|
+
end
|
45
|
+
register_instance_option(:map_html_height) do
|
46
|
+
'500px'
|
47
|
+
end
|
48
|
+
register_instance_option(:map_lang) do
|
49
|
+
'ru_RU'
|
50
|
+
end
|
51
|
+
register_instance_option(:api_key) do
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
register_instance_option(:latitude_field) do
|
56
|
+
:latitude
|
57
|
+
end
|
58
|
+
|
59
|
+
register_instance_option(:longitude_field) do
|
60
|
+
:longitude
|
61
|
+
end
|
62
|
+
|
63
|
+
def dom_name
|
64
|
+
# @dom_name ||= "#{bindings[:form].try(:object_name)}_#{name}"
|
65
|
+
# .gsub /\[|\]\[|\]_|\]/, "_"
|
66
|
+
bindings[:form].instance_variable_get(:@dom_id)[:"#{name}"]
|
67
|
+
end
|
68
|
+
|
69
|
+
def latitude_dom_name
|
70
|
+
# @lat_dom_name ||= "#{bindings[:form].try(:object_name)}_#{latitude_field}"
|
71
|
+
# .gsub /\[|\]\[|\]_|\]/, "_"
|
72
|
+
bindings[:form].instance_variable_get(:@dom_id)[:"#{latitude_field}"]
|
73
|
+
end
|
74
|
+
|
75
|
+
def longitude_dom_name
|
76
|
+
# @lon_dom_name ||= "#{bindings[:form].try(:object_name)}_#{longitude_field}"
|
77
|
+
# .gsub /\[|\]\[|\]_|\]/, "_"
|
78
|
+
bindings[:form].instance_variable_get(:@dom_id)[:"#{longitude_field}"]
|
79
|
+
end
|
80
|
+
|
81
|
+
def javascript_url
|
82
|
+
return @javascript_url if @javascript_url.present?
|
83
|
+
api_key = api_key.present? ? "&apikey=#{field.api_key}" : ""
|
84
|
+
host = if api_key.present?
|
85
|
+
"enterprise.api-maps.yandex.ru"
|
86
|
+
else
|
87
|
+
"api-maps.yandex.ru"
|
88
|
+
end
|
89
|
+
@javascript_url = "https://#{host}/2.1/?lang=#{map_lang}" \
|
90
|
+
"&onload=initMap&onerror=errorMap#{api_key}"
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
|
100
|
+
# puts parent, properties, fields
|
101
|
+
# if properties[:name] == :yamap_field
|
102
|
+
# fields << RailsAdmin::Config::Fields::Types::YamapField.new(parent, properties[:name], properties)
|
103
|
+
# true
|
104
|
+
# else
|
105
|
+
# false
|
106
|
+
# end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_admin_yamap_field
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artem Yegorov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-12 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.2'
|
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.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
description: Simple implementation of Yandex maps in rails admin.
|
34
|
+
email:
|
35
|
+
- yegorov0725@yandex.ru
|
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_yamap_field.html.haml
|
44
|
+
- app/views/rails_admin/main/_show_yamap_field.html.haml
|
45
|
+
- app/views/rails_admin/main/_yamap_field.html.haml
|
46
|
+
- lib/rails_admin_yamap_field.rb
|
47
|
+
- lib/rails_admin_yamap_field/engine.rb
|
48
|
+
- lib/rails_admin_yamap_field/version.rb
|
49
|
+
- wiki/screenshot_1.png
|
50
|
+
homepage: https://github.com/yegorov/rails_admin_yamap_field
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.7.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Simple implementation of Yandex maps in rails admin.
|
74
|
+
test_files: []
|