Events geojson generation is separated into its own jbuilder

This commit is contained in:
echarp 2018-04-05 22:00:07 +02:00
parent 8327a0db70
commit e0fba8e6d7
8 changed files with 31 additions and 28 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -4,6 +4,8 @@ class RegionsController < ApplicationController
before_action :set_regions, only: [:index]
def index; end
private
def set_regions

View File

@ -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: "<a href=\"/events/#{id}\">#{self}</a>"
}, geometry: { type: 'Point', coordinates: [longitude, latitude] } }
end
def full_address
[address, city, region, region.try(:region)].compact.join ', '
end

View File

@ -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: "<a href=\"/events/#{event.id}\">#{event}</a>"
},
geometry: {
type: 'Point',
coordinates: [event.longitude, event.latitude]
}
)
end

View File

@ -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'