From dd9e0691acae3eb63bdfbc8665eb3f8ce516536d Mon Sep 17 00:00:00 2001 From: hellekin Date: Fri, 9 Oct 2020 12:47:31 +0200 Subject: Fix Resource edition --- app/controllers/resources_controller.rb | 3 +++ app/models/resource.rb | 15 +++++++++++++-- app/serializers/hash_serializer.rb | 9 --------- app/views/resources/_edit.html.erb | 4 ++-- app/views/resources/_resource.html.erb | 10 +++++----- app/views/resources/edit.html.erb | 2 +- 6 files changed, 24 insertions(+), 19 deletions(-) delete mode 100644 app/serializers/hash_serializer.rb (limited to 'app') diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index 1cccafa..04d4e70 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -40,6 +40,9 @@ class ResourcesController < ApplicationController 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?) + + + Rails.logger.info "EDIT: #{@resource.uuid} #{@resource.name} // #{current_agent.id}" end # PATCH /resources/:id diff --git a/app/models/resource.rb b/app/models/resource.rb index 906933d..7d87e1a 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -8,8 +8,7 @@ class Resource < ApplicationRecord has_many :classifications has_many :sections, through: :classifications - serialize :feature, HashSerializer - store_accessor :feature, :name, :summary, :description, :email, :source, :address, :postal_code, :city, :phone_number, :website + store_accessor :feature, :geometry, :properties validates_associated :agent @@ -53,4 +52,16 @@ class Resource < ApplicationRecord def latitude=(value) feature['geometry']['coordinates'][1] = value end + + # Properties + + [:name, :summary, :description, :email, :source, :address, :postal_code, :city, :phone_number, :website].each do |prop| + # Define a reader + define_method prop do + properties[prop.to_s] + end + define_method :"#{prop}=" do |v| + feature['properties'][prop.to_s] = v + end + end end diff --git a/app/serializers/hash_serializer.rb b/app/serializers/hash_serializer.rb deleted file mode 100644 index 5db639f..0000000 --- a/app/serializers/hash_serializer.rb +++ /dev/null @@ -1,9 +0,0 @@ -class HashSerializer - def self.dump(hash) - hash.to_json - end - - def self.load(hash) - (hash || {}).with_indifferent_access - end -end diff --git a/app/views/resources/_edit.html.erb b/app/views/resources/_edit.html.erb index fdb2921..55c59f6 100644 --- a/app/views/resources/_edit.html.erb +++ b/app/views/resources/_edit.html.erb @@ -1,6 +1,6 @@ -

Edit <%= resource.presence&.name || 'new resource' %>

+

Edit <%= @resource.presence.feature['properties']['name'] || 'new resource' %>

Context: <%= current_agent %>

-<%= form_with model: [current_agent,@resource] do |f| %> +<%= form_with model: [current_agent,@resource], url: controller.action_name == 'new' ? agent_resources_path(agent: current_agent) : resource_path(@resource) do |f| %> <%= render partial: 'form', locals: { resource: resource, f: f } %> <% end %> diff --git a/app/views/resources/_resource.html.erb b/app/views/resources/_resource.html.erb index f013523..585bed8 100644 --- a/app/views/resources/_resource.html.erb +++ b/app/views/resources/_resource.html.erb @@ -1,9 +1,9 @@
-

<%= h resource.name %>

+

<%= resource.properties['name'] %>

<%= h resource.description %>

@@ -13,8 +13,8 @@
diff --git a/app/views/resources/edit.html.erb b/app/views/resources/edit.html.erb index 80d8206..b52c60d 100644 --- a/app/views/resources/edit.html.erb +++ b/app/views/resources/edit.html.erb @@ -1,2 +1,2 @@ -<%= render partial: 'edit', locals: { resource: @resource } %> +<%= render partial: 'edit', locals: { resource: @resource, foo: 'foo' } %> -- cgit v1.2.3