app/controllers/map_topic/geo_topics_controller.rb
# below is only used for updating geo_place - which is independent of the location record to which it is tied
# When I get round to creating a service object for geo_place I will do checks with location record each time
# - geo_places are created when a new location is created in a topic
below is only used for updating geo_place - which is independent of the location record to which it is tied
When I get round to creating a service object for geo_place I will do checks with location record each time
geo_places are created when a new location is created in a topic
below is only used for updating geo_place - which is independent of the location record to which it is tied When I get round to creating a service object for geo_place I will do checks with location record each time geo_places are created when a new location is created in a topic
def update_geo_places
      unless(params[:place] && params[:topic_id] && params[:location_id])
        render_error "incorrect params"
        return
      end
      @topic = Topic.find(params[:topic_id])
      if current_user.guardian.ensure_can_edit!(@topic)
        render status: :forbidden, json: false
        return
      end
      location_id = params[:location_id].to_s
      unless (@topic.geo.places.class == Hash) && (@topic.geo.places['sorted_ids'])
        @topic.geo.places = {
          'sorted_ids' => []
        }
        @topic.geo.save
      end
      unless @topic.geo.places['sorted_ids'].include? location_id
        @topic.geo.places['sorted_ids'].push location_id
      end
      place = JSON.parse params[:place]
      place['longitude'] = place['longitude'].to_f
      place['latitude'] = place['latitude'].to_f
      place['location_id'] = location_id
      @topic.geo.places[location_id] = place
      @topic.geo.save!
      return render_json_dump @topic.geo.as_json
    end
def update_geo_places unless ( params [ :place ] && params [ :topic_id ] && params [ :location_id ] ) render_error "incorrect params" return end @topic = Topic . find ( params [ :topic_id ] ) if current_user . guardian . ensure_can_edit! ( @topic ) render status : :forbidden , json : false return end location_id = params [ :location_id ] . to_s unless ( @topic . geo . places . class == Hash ) && ( @topic . geo . places [ 'sorted_ids' ] ) @topic . geo . places = { 'sorted_ids' => [ ] } @topic . geo . save end unless @topic . geo . places [ 'sorted_ids' ] . include? location_id @topic . geo . places [ 'sorted_ids' ] . push location_id end place = JSON . parse params [ :place ] place [ 'longitude' ] = place [ 'longitude' ] . to_f place [ 'latitude' ] = place [ 'latitude' ] . to_f place [ 'location_id' ] = location_id @topic . geo . places [ location_id ] = place @topic . geo . save! return render_json_dump @topic . geo . as_json end
below is only used for updating geo_place - which is independent of the location record to which it is tied
When I get round to creating a service object for geo_place I will do checks with location record each time
 geo_places are created when a new location is created in a topic
below is only used for updating geo_place - which is independent of the location record to which it is tied When I get round to creating a service object for geo_place I will do checks with location record each time geo_places are created when a new location is created in a topic
# passing in location_id might be redundant as with updates, place should already have location_id # below is a workaround check while I still have some places in db # that are arrays # binding.pry # if @topic.geo.places.class == Array # place param is now passed as a string ( json.stringyfy client side) # - allowing jquery to process the payload resulted in nested arrays getting converted to hashes # http://stackoverflow.com/questions/25856959/parsing-json-with-jquery-turns-array-into-hash # with json parse above, probably don't need below check anymore # should do some checks to prevent injection attacks # topic_places = @topic.geo.places || []