aboutsummaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authorIN COMMON Collective <collective@incommon.cc>2021-04-08 16:34:15 +0200
committerIN COMMON Collective <collective@incommon.cc>2021-04-08 16:34:15 +0200
commitd14700c51d692335f001a93c2f6b13b135783206 (patch)
tree1204bc1ae744098eba6604a961765187984a90d8 /app/controllers
parentc738e96b2b99bfd92b70d4cec26d6874a7f609e4 (diff)
downloadincommon-map-d14700c51d692335f001a93c2f6b13b135783206.tar.gz
[FIX] Use form model to create/edit resources (fixes #4, fixes #5, refs #3)v0.1.10
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.
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/resources_controller.rb31
1 files changed, 14 insertions, 17 deletions
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