-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlayer.rb
More file actions
63 lines (53 loc) · 1.68 KB
/
layer.rb
File metadata and controls
63 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Layer
include ActiveModel::Model
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :map, optional: true, touch: true
has_many :features, dependent: :destroy
scope :geojson, -> { where(type: "geojson") }
scope :overpass, -> { where(type: "overpass") }
field :type
field :name
field :query
field :heatmap, type: Boolean
field :cluster, type: Boolean
field :show, type: Boolean, default: true
field :features_count, type: Integer, default: 0
after_save :broadcast_update, if: -> { map.present? }
after_destroy :broadcast_destroy, if: -> { map.present? }
def to_summary_json
json = { id: id, type: type, name: name, heatmap: !!heatmap, cluster: !!cluster, show: show != false }
json[:query] = query if type == "overpass"
json
end
def to_json
json = to_summary_json
json[:geojson] = to_geojson
json
end
def to_geojson
{ type: "FeatureCollection",
features: features.map(&:geojson) }
end
def clone_with_features
clone = dup
clone.update(created_at: DateTime.now, map: nil)
features.each { |f| clone.features << f.dup }
clone
end
def broadcast_update
if (%w[name query heatmap cluster show] & previous_changes.keys).any?
# broadcast to private + public channel
[ map.private_id, map.public_id ].each do |map_id|
ActionCable.server.broadcast("map_channel_#{map_id}",
{ event: "update_layer", layer: to_summary_json.as_json })
end
end
end
def broadcast_destroy
[ map.private_id, map.public_id ].each do |map_id|
ActionCable.server.broadcast("map_channel_#{map_id}",
{ event: "delete_layer", layer: { id: id } })
end
end
end