From e0fba8e6d75ea1473234bfb8b62ee80ba6065a1e Mon Sep 17 00:00:00 2001 From: echarp Date: Thu, 5 Apr 2018 22:00:07 +0200 Subject: [PATCH] Events geojson generation is separated into its own jbuilder --- app/controllers/events_controller.rb | 8 ++++---- app/controllers/maps_controller.rb | 2 +- app/controllers/moderations_controller.rb | 4 ++-- app/controllers/orgas_controller.rb | 6 ++---- app/controllers/regions_controller.rb | 2 ++ app/models/event.rb | 10 ---------- app/views/maps/index.json.jbuilder | 20 ++++++++++++++++++++ test/models/event_test.rb | 7 ------- 8 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 app/views/maps/index.json.jbuilder diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 134262d7..b0167c41 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -40,8 +40,8 @@ class EventsController < ApplicationController format.json { render action: 'show', status: 201, location: @event } else format.html { render action: 'new' } - # 422 means :unprocessable_entity - format.json { render json: @event.errors, status: 422 } + status = :unprocessable_entity + format.json { render json: @event.errors, status: status } end end end @@ -64,8 +64,8 @@ class EventsController < ApplicationController format.json { head :no_content } else format.html { render action: 'edit' } - # 422 means :unprocessable_entity - format.json { render json: @event.errors, status: 422 } + status = :unprocessable_entity + format.json { render json: @event.errors, status: status } end end end diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 290d1bf6..f1a2fc75 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -10,7 +10,7 @@ class MapsController < ApplicationController def index respond_to do |format| format.html - format.json { render json: apply_scopes(Event.moderated.geo) } + format.json { @events = apply_scopes Event.moderated.geo } end end diff --git a/app/controllers/moderations_controller.rb b/app/controllers/moderations_controller.rb index 8b665921..5c0a5a56 100644 --- a/app/controllers/moderations_controller.rb +++ b/app/controllers/moderations_controller.rb @@ -29,8 +29,8 @@ class ModerationsController < ApplicationController format.json { head :no_content } else format.html { render action: 'edit' } - # 422 means :unprocessable_entity - format.json { render json: @moderation.errors, status: 422 } + status = :unprocessable_entity + format.json { render json: @moderation.errors, status: status } end end end diff --git a/app/controllers/orgas_controller.rb b/app/controllers/orgas_controller.rb index 3dfba96d..68d2813e 100644 --- a/app/controllers/orgas_controller.rb +++ b/app/controllers/orgas_controller.rb @@ -31,8 +31,7 @@ class OrgasController < ApplicationController format.json { render action: 'show', status: 201, location: @orga } else format.html { render action: 'new' } - # 422 means :unprocessable_entity - format.json { render json: @orga.errors, status: 422 } + format.json { render json: @orga.errors, status: :unprocessable_entity } end end end @@ -51,8 +50,7 @@ class OrgasController < ApplicationController format.json { head :no_content } else format.html { render action: 'edit' } - # 422 means :unprocessable_entity - format.json { render json: @orga.errors, status: 422 } + format.json { render json: @orga.errors, status: :unprocessable_entity } end end end diff --git a/app/controllers/regions_controller.rb b/app/controllers/regions_controller.rb index 9e22b52e..aacb2429 100644 --- a/app/controllers/regions_controller.rb +++ b/app/controllers/regions_controller.rb @@ -4,6 +4,8 @@ class RegionsController < ApplicationController before_action :set_regions, only: [:index] + def index; end + private def set_regions diff --git a/app/models/event.rb b/app/models/event.rb index 0497c599..522b65ee 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -85,16 +85,6 @@ class Event < ApplicationRecord self.longitude = nil if address_changed? end - def as_json(_options = {}) - { type: 'Feature', properties: { - id: id, name: title, start_time: start_time, end_time: end_time, - submission_time: submission_time, decision_time: decision_time, - place_name: place_name, address: address, city: city, - region: region.name, region_id: region_id, - tags: tag_list, popupContent: "#{self}" - }, geometry: { type: 'Point', coordinates: [longitude, latitude] } } - end - def full_address [address, city, region, region.try(:region)].compact.join ', ' end diff --git a/app/views/maps/index.json.jbuilder b/app/views/maps/index.json.jbuilder new file mode 100644 index 00000000..ee8f0292 --- /dev/null +++ b/app/views/maps/index.json.jbuilder @@ -0,0 +1,20 @@ +json.array!(@events) do |event| + json.merge!( + type: 'Feature', + properties: { + id: event.id, + name: event.title, + start_time: event.start_time, end_time: event.end_time, + submission_time: event.submission_time, + decision_time: event.decision_time, + place_name: event.place_name, address: event.address, city: event.city, + region: event.region.name, region_id: event.region_id, + tags: event.tag_list, + popupContent: "#{event}" + }, + geometry: { + type: 'Point', + coordinates: [event.longitude, event.latitude] + } + ) +end diff --git a/test/models/event_test.rb b/test/models/event_test.rb index ba8dc54b..120ae3cf 100644 --- a/test/models/event_test.rb +++ b/test/models/event_test.rb @@ -113,13 +113,6 @@ class EventTest < ActiveSupport::TestCase assert_equal(-74.0059731, @event.longitude) end - test 'json transform' do - assert_not_nil @event.as_json - - assert_equal 'Feature', @event.as_json[:type] - assert_equal 'Point', @event.as_json[:geometry][:type] - end - test 'full address' do @event.address = 'hello' @event.city = 'world'