Class: LayersOfLondon::Booth::MapTool::PolygonsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/layers_of_london/booth/map_tool/polygons_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#session

Instance Method Details

#createObject



27
28
29
30
31
32
33
# File 'app/controllers/layers_of_london/booth/map_tool/polygons_controller.rb', line 27

def create
  square = LayersOfLondon::Booth::MapTool::Square.find(params[:square_id]) rescue LayersOfLondon::Booth::MapTool::Square.create
  poly = square.polygons.create(user: current_user, feature: polygon_params)
  authorize poly

  render json: poly.to_json(user_can_edit: true)
end

#destroyObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/layers_of_london/booth/map_tool/polygons_controller.rb', line 50

def destroy
  poly = LayersOfLondon::Booth::MapTool::Polygon.find(params[:id])
  authorize poly

  return render json: {data: "Error"}, status: :unprocessable_entity unless poly

  if poly.destroy
    render json: :ok, status: :ok
  else
    render json: :error, status: :unprocessable_entity
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/layers_of_london/booth/map_tool/polygons_controller.rb', line 5

def index
  skip_authorization
  features = LayersOfLondon::Booth::MapTool::Polygon.all.collect do |poly|
    user_can_edit = LayersOfLondon::Booth::MapTool::PolygonPolicy.new(current_user, poly).update?
    poly.to_json(user_can_edit: user_can_edit)
  end

  feature = {
    type: "FeatureCollection",
    features: features
  }
  render json: feature
end

#showObject



19
20
21
22
23
24
25
# File 'app/controllers/layers_of_london/booth/map_tool/polygons_controller.rb', line 19

def show
  poly = LayersOfLondon::Booth::MapTool::Polygon.find(params[:id])
  authorize poly

  user_can_edit = LayersOfLondon::Booth::MapTool::PolygonPolicy.new(current_user, poly).update?
  render json: poly.to_json(user_can_edit: user_can_edit)
end

#updateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/layers_of_london/booth/map_tool/polygons_controller.rb', line 35

def update
  poly = LayersOfLondon::Booth::MapTool::Polygon.find(params[:id])
  authorize poly

  return render json: {data: "Error"}, status: :unprocessable_entity unless poly

  poly.assign_attributes(feature: polygon_params)

  if poly.save
    render json: poly.to_json(user_can_edit: true)
  else
    render json: {data: "Error"}, status: :unprocessable_entity
  end
end