Class: GeoController

Inherits:
ApplicationController show all
Defined in:
app/controllers/geo_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/geo_controller.rb', line 28

def create
  @features = []
  feature_collection = geo_model.geojson_decode(request.raw_post)
  if feature_collection.nil? || !geo_model.can_edit?(current_ability)
    head :bad_request
    return
  end

  feature_collection.each do |feature|
    if feature.feature_id.is_a? Integer
      new_feature = geo_model.find(feature.feature_id)
    end
    if new_feature.nil?
      new_feature = geo_model.new
    end

    logger.info "#{geo_model.table_name}.update_attributes_from_feature: #{feature.inspect}"
    if new_feature.update_attributes_from_geojson_feature(feature, current_user)
      @features << new_feature
    else
      head :unprocessable_entity
      return
    end
  end

  render :json => @features.to_geojson, :status => :created
end

#destroyObject



76
77
78
79
80
# File 'app/controllers/geo_controller.rb', line 76

def destroy
  @feature = geo_model.user_filter(current_ability).find(params[:id])
  @feature.destroy
  head :no_content
end

#geo_modelObject



5
6
7
# File 'app/controllers/geo_controller.rb', line 5

def geo_model
  throw NotImplementedError
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/geo_controller.rb', line 9

def index
  @features = geo_model.bbox_filter(params)
  @features = @features.user_filter(current_ability)
  respond_to do |format|
    # NOTE: return GeoJSON by default (OpenLayers.Protocol.HTTP PUT does not work with '.json' format selection)
    format.html {
      render :json => @features.json_filter.to_geojson
    }
    format.csv {
      send_csv_excel(@features.csv_filter)
    }
  end
end

#showObject



23
24
25
26
# File 'app/controllers/geo_controller.rb', line 23

def show
  @feature = geo_model.user_filter(current_ability).json_filter.find(params[:id])
  render :json => [@feature].to_geojson
end

#updateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/geo_controller.rb', line 56

def update
  # NOTE: user_filter checks if model is editable
  feature = geo_model.user_filter(current_ability).geojson_decode(request.raw_post)
  if feature.nil?
    head :bad_request
    return
  end

  if feature.feature_id.is_a? Integer
    # NOTE: user_filter may limit editable features; find raises RecordNotFound if feature cannot be found
    @feature = geo_model.user_filter(current_ability).find(feature.feature_id)
  end

  if @feature.update_attributes_from_geojson_feature(feature, current_user)
    render :json => @feature.to_geojson, :status => :created
  else
    head :unprocessable_entity
  end
end