From d5295fb485b136a01da5b538d1e4f096ad0fd4d4 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 18 Sep 2011 18:54:39 +0100 Subject: [PATCH] Return GeoJSON for notes when JSON format is requested --- app/controllers/note_controller.rb | 6 +++--- app/models/note.rb | 13 ------------- app/views/note/_note.json.erb | 19 +++++++++++++++++++ app/views/note/_note_comment.json.erb | 8 ++++++++ app/views/note/_note_spacer.json.erb | 1 + app/views/note/list.json.erb | 6 ++++++ 6 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 app/views/note/_note.json.erb create mode 100644 app/views/note/_note_comment.json.erb create mode 100644 app/views/note/_note_spacer.json.erb create mode 100644 app/views/note/list.json.erb diff --git a/app/controllers/note_controller.rb b/app/controllers/note_controller.rb index c46145045..6d428d822 100644 --- a/app/controllers/note_controller.rb +++ b/app/controllers/note_controller.rb @@ -52,7 +52,7 @@ class NoteController < ApplicationController format.rss format.js format.xml - format.json { render :json => @notes.to_json } + format.json format.gpx end end @@ -200,7 +200,7 @@ class NoteController < ApplicationController respond_to do |format| format.xml format.rss - format.json { render :json => @note.to_json } + format.json format.gpx end end @@ -256,7 +256,7 @@ class NoteController < ApplicationController format.rss { render :action => :list } format.js format.xml { render :action => :list } - format.json { render :json => @notes.to_json } + format.json { render :action => :list } format.gpx { render :action => :list } end end diff --git a/app/models/note.rb b/app/models/note.rb index 892ada1aa..a2937074c 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -72,17 +72,4 @@ class Note < ActiveRecord::Base def author_name self.comments.first.author_name end - - # Custom JSON output routine for notes - def to_json(options = {}) - super options.reverse_merge( - :methods => [ :lat, :lon ], - :only => [ :id, :status, :created_at ], - :include => { - :comments => { - :only => [ :event, :author_name, :created_at, :body ] - } - } - ) - end end diff --git a/app/views/note/_note.json.erb b/app/views/note/_note.json.erb new file mode 100644 index 000000000..0d101f021 --- /dev/null +++ b/app/views/note/_note.json.erb @@ -0,0 +1,19 @@ +{ + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ <%= note.lon %>, <%= note.lat %> ] + }, + "properties": { + "id": <%= note.id %>, + "date_created": "<%= note.created_at %>", + "nearby": "<%= note.nearby_place %>", + "status": "<%= note.status %>", +<% if note.status == "closed" -%> + "closed_at": "<%= note.closed_at %>", +<% end -%> + "comments": [ + <%= render :partial => "note_comment", :collection => note.comments, :spacer_template => "note_spacer" -%> + ] + } +} diff --git a/app/views/note/_note_comment.json.erb b/app/views/note/_note_comment.json.erb new file mode 100644 index 000000000..405a87dcb --- /dev/null +++ b/app/views/note/_note_comment.json.erb @@ -0,0 +1,8 @@ +{ + "date": "<%= note_comment.created_at %>", +<% unless note_comment.author_id.nil? -%> + "uid": <%= note_comment.author_id %>, +<% end -%> + "user": "<%= note_comment.author_name %>", + "text": "<%= note_comment.body %>" +} diff --git a/app/views/note/_note_spacer.json.erb b/app/views/note/_note_spacer.json.erb new file mode 100644 index 000000000..7edb2fa5b --- /dev/null +++ b/app/views/note/_note_spacer.json.erb @@ -0,0 +1 @@ +, diff --git a/app/views/note/list.json.erb b/app/views/note/list.json.erb new file mode 100644 index 000000000..48844789d --- /dev/null +++ b/app/views/note/list.json.erb @@ -0,0 +1,6 @@ +{ + "type": "FeatureCollection", + "features": [ + <%= render :partial => "note", :collection => @notes, :spacer_template=> "note_spacer" -%> + ] +} -- 2.43.2