diff options
author | hellekin <hellekin@cepheide.org> | 2020-10-09 10:34:12 +0200 |
---|---|---|
committer | hellekin <hellekin@cepheide.org> | 2020-10-09 10:34:12 +0200 |
commit | d7030931a48c270c78181bee02613c1211e0d4fd (patch) | |
tree | a4176f791c8525c1da15ffd70933bd7d567884d0 /app | |
parent | 81b08d9b977837e2e714f9782ba90b664f36b98c (diff) | |
download | incommon-map-d7030931a48c270c78181bee02613c1211e0d4fd.tar.gz |
[WIP] Add Resource business logic
Currently there's a bug throwing:
TypeError (nil is not a symbol nor a string)
When creating a Resource...
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/resources_controller.rb | 110 |
1 files changed, 60 insertions, 50 deletions
diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index fdb69cc..1cccafa 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -12,13 +12,24 @@ class ResourcesController < ApplicationController # POST /resources def create - # Check list: - # 1. UUID is not set, or not known - # 2. Agent is correct - # 3. Name, Description, Phone, Email, etc. do not yield results - # 4. Background job to validate fields and save - # 5. Or Background job to list similar items - # 6. Return to user with new record or list of mergeable ones + # TODO Background job to list similar items + # TODO If there's a match, return to user with new record or list of mergeable ones + return 403 unless (current_user_editor? || current_user_observer?) + + classification = resource_params.delete(:classification) || { section_ids: [] } + + @resource = current_agent.resources.build(resource_params) + + respond_to do |format| + if @resource.save + classification[:section_ids].each { |id| @resource.classifications.find_or_create_by(section_id: id) } + format.html { redirect_to @resource, notice: 'Merci de votre contribution !' } + format.json { render :show, status: :created, location: @resource } + else + format.html { render :new } + format.json { render json: @resource.errors, status: :unprocessable_entity } + end + end end # GET /resources/:id @@ -27,6 +38,8 @@ class ResourcesController < ApplicationController # GET /resources/:id/edit def edit + # TODO Add a moderation queue for unauthorized but valid changes + flash.now[:notice] = 'Please ask an editor or a maintainer to edit this resource!' unless (current_user_editor? || current_user_maintainer?) end # PATCH /resources/:id @@ -36,70 +49,67 @@ class ResourcesController < ApplicationController # 2. Validate each change # 3. Moderate queue or save - updated_properties = @resource.feature['properties'] - updated_geometry = @resource.feature['geometry'] - - updated = false - - flash.now[:notice] = updated_geometry.inspect - - if @resource.uuid.present? && params[:resource][:uuid].present? && params[:resource][:uuid] != @resource.uuid - flash.now[:error] = "UUID cannot be changed." - render :edit, status: :unprocessable_entity and return - end - - params[:resource].each do |prop| - next if %w(longitude latitude).include?(prop.first.to_s) - if prop.last.present? && prop.last != @resource.send("#{prop.first.to_s}") - updated = true - updated_properties[prop.first.to_s] = prop.last - end - end + return 403 unless (current_user_editor? || current_user_maintainer?) - lon, lat = updated_geometry['coordinates'] - flash.now.notice = "lon #{lon} => #{params[:resource][:longitude]}, lat #{lat} =>#{params[:resource][:latitude]}" - if params[:resource][:longitude].present? && params[:resource][:longitude] != lon - updated = true - updated_geometry['coordinates'][0] = params[:resource][:longitude] - Rails.logger.info "UPDATED LON TO #{params[:resource][:longitude]} /// #{updated_geometry.inspect}" - end - if params[:resource][:latitude].present? && params[:resource][:latitude] != lat - updated = true - updated_geometry['coordinates'][1] = params[:resource][:latitude] - end - - if updated == true - Rails.logger.info "Got a Resource update..." - @resource.feature['properties'] = updated_properties - @resource.feature['geometry'] = updated_geometry - if @resource.save - flash.notice = "Good! It worked! #{@resource.feature['geometry']}" - redirect_to resource_url(@resource) and return + respond_to do |format| + if @resource.update(resource_params) + format.html { redirect_to @resource, notice: 'Merci de votre contribution !' } + format.json { render :show, status: :ok, location: @resource } else - flash.now[:notice] = @resource.errors.inspect + format.html { render :edit } + format.json { render json: @resource.errors, status: :unprocessable_entity } end end - flash.now[:alert] = "Update failed!" - render :edit end # GET /resources/:id/delete def delete + flash.now[:notice] = 'Please ask a maintainer to delete this resource!' unless current_user_maintainer? end # DELETE /resources/:id def destroy # Check list # 1. User belongs to Agent and is :maintainer? + if !current_user_maintainer? + msg = 'You must be a maintainer to delete resources!' + respond_to do |format| + format.html { redirect_to :show, notice: msg } + format.js { render json: '{}', status: :forbidden, message: msg } + end + else + @resource.destroy + respond_to do |format| + format.html { redirect_to :index, notice: 'OK, resource record successfully remove.' } + format.js { head :no_content } + end + end end private def resource_params - params.require(:resource).permit(:uuid, :name, :summary, :description, :email, :city, :postal_code, :phone_number, :website, :entry_number, :categories, :latitude, :longitude) + params + .require(:resource) + .permit(:agent_id, + :uuid, + :name, + :summary, + :description, + :email, + :website, + :phone_number, + :address, + :postal_code, + :city, + :entry_number, + :categories, + :latitude, + :longitude, + classification: [ :section_ids ]) end def set_resource - @resource = Resource.find_by(uuid: params[:id]) || Resource.new + @resource = Resource.find_by(uuid: params[:id]) || current_agent.resources.build end end |