From d14700c51d692335f001a93c2f6b13b135783206 Mon Sep 17 00:00:00 2001 From: IN COMMON Collective Date: Thu, 8 Apr 2021 16:34:15 +0200 Subject: [FIX] Use form model to create/edit resources (fixes #4, fixes #5, refs #3) Since we must associate other models (e.g., classifications) to a Resource, we use a composite model to save all changes inside a database transaction. This approach makes it simpler to handle resources and their associations. Work remains to fix the geolocation and reverse geolocation to ensure these are in sync. --- app/controllers/resources_controller.rb | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index 2fa392f..4a99f2a 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -3,7 +3,8 @@ # SPDX-License-Identifier: AGPL-3.0-or-later class ResourcesController < ApplicationController - before_action :set_resource, only: [:new, :show, :edit, :update, :delete, :destroy] + before_action :set_resource, only: [:show, :delete, :destroy] +# before_action :set_resource_form, only: [:new, :create, :edit, :update] # GET /resources def index @@ -12,6 +13,7 @@ class ResourcesController < ApplicationController # GET /resources/new def new + @resource = ResourceForm.new(current_agent) end # POST /resources @@ -19,18 +21,12 @@ class ResourcesController < ApplicationController # TODO Background job to list similar items # TODO If there's a match, return to user with new record or list of mergeable ones - classification = resource_params.delete(:classification) || { section_ids: [] } - - Rails.logger.info resource_params - - @resource = current_agent.resources.build(resource_params) + @resource = ResourceForm.new(current_agent, resource_form_params) respond_to do |format| - Rails.logger.info "format: #{format} - Res: #{@resource.inspect}" 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 } + format.html { redirect_to @resource.resource, notice: 'Merci de votre contribution !' } + format.json { render :show, status: :created, location: @resource.resource } else format.html { render :new } format.json { render json: @resource.errors, status: :unprocessable_entity } @@ -62,8 +58,8 @@ class ResourcesController < ApplicationController 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 } + format.html { redirect_to @resource.resource, notice: 'Merci de votre contribution !' } + format.json { render :show, status: :ok, location: @resource.resource } else format.html { render :edit } format.json { render json: @resource.errors, status: :unprocessable_entity } @@ -97,9 +93,9 @@ class ResourcesController < ApplicationController private - def resource_params + def resource_form_params params - .require(:resource) + .require(:resource_form) .permit(:agent_id, :uuid, :name, @@ -111,11 +107,12 @@ class ResourcesController < ApplicationController :address, :postal_code, :city, - :entry_number, - :categories, + :entry_number, # Unused on new resources + :categories, # :latitude, :longitude, - classification: [ :section_ids ]) + :source, + section_ids: []) end def set_resource -- cgit v1.2.3