Class: LayersOfLondon::Booth::MapTool::SquaresController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#session

Instance Method Details

#coordinatesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/layers_of_london/booth/map_tool/squares_controller.rb', line 31

def coordinates
  squares = LayersOfLondon::Booth::MapTool::Square.all
  square_data = squares.collect do |square|
    {
      id: square.id,
      nw: square.north_west.to_a,
      se: square.south_east.to_a
    }
  end

  render json: square_data

end

#gridObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/layers_of_london/booth/map_tool/squares_controller.rb', line 15

def grid
  render json: {
    type: "FeatureCollection",
    features: Square.grid_coordinates.collect do |coords|
      {
        type: "Feature",
        geometry: {
          type: "LineString",
          coordinates: coords
        },
        properties: {}
      }
    end
  }
end

#indexObject



7
8
9
10
11
12
13
# File 'app/controllers/layers_of_london/booth/map_tool/squares_controller.rb', line 7

def index
  squares = LayersOfLondon::Booth::MapTool::Square.all
  render json: {
    type: "FeatureCollection",
    features: squares.collect(&:to_geojson)
  }
end

#polygonsObject



45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/layers_of_london/booth/map_tool/squares_controller.rb', line 45

def polygons
  features = LayersOfLondon::Booth::MapTool::PolygonPolicy::Scope.new(current_user, LayersOfLondon::Booth::MapTool::Polygon).resolve

  feature_collection = {
    type: "FeatureCollection",
    features: features
  }

  render json: feature_collection
end

#showObject



71
72
73
74
# File 'app/controllers/layers_of_london/booth/map_tool/squares_controller.rb', line 71

def show
  square = LayersOfLondon::Booth::MapTool::Square.find(params[:id]) rescue LayersOfLondon::Booth::MapTool::Square.create
  render json: square.to_json(padding: 20)
end

#updateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/layers_of_london/booth/map_tool/squares_controller.rb', line 56

def update
  square = LayersOfLondon::Booth::MapTool::Square.find(params[:id])
  proposed_state = params[:state]

  if square.send("may_mark_as_#{proposed_state}?")
    square.send("mark_as_#{proposed_state}")
    square.user = current_user if square.aasm_state === "done"
    square.save

    render json: square.to_json
  else
    render json: {}, status: :unprocessable_entity
  end
end