implement CSV and XLS export for map topics

This commit is contained in:
Devin Howard 2015-02-28 19:55:45 -05:00
parent 253ceb4b48
commit b5b13841bc
5 changed files with 36 additions and 1 deletions

View file

@ -2,7 +2,7 @@ class MapsController < ApplicationController
before_filter :require_user, only: [:create, :update, :screenshot, :destroy] before_filter :require_user, only: [:create, :update, :screenshot, :destroy]
respond_to :html, :json respond_to :html, :json, :csv
autocomplete :map, :name, :full => true, :extra_data => [:user_id] autocomplete :map, :name, :full => true, :extra_data => [:user_id]
@ -86,6 +86,8 @@ class MapsController < ApplicationController
respond_with(@allmappers, @allmappings, @allsynapses, @alltopics, @map) respond_with(@allmappers, @allmappings, @allsynapses, @alltopics, @map)
} }
format.json { render json: @map } format.json { render json: @map }
format.csv { send_data @map.to_csv }
format.xls
end end
end end

View file

@ -82,6 +82,20 @@ class Map < ActiveRecord::Base
json json
end end
def to_csv(options = {})
CSV.generate(options) do |csv|
csv << ["id", "name", "permission", "user.name"]
self.topics.each do |topic|
csv << [
topic.id,
topic.name,
topic.permission,
topic.user.name
]
end
end
end
##### PERMISSIONS ###### ##### PERMISSIONS ######
def authorize_to_delete(user) def authorize_to_delete(user)

View file

@ -0,0 +1,16 @@
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Permission</th>
<th>Username</th>
</tr>
<% @map.topics.each do |topic| %>
<tr>
<td><%= topic.id %></td>
<td><%= topic.name %></td>
<td><%= topic.permission %></td>
<td><%= topic.user.name %></td>
</tr>
<% end %>
</table>

View file

@ -1,5 +1,6 @@
require File.expand_path('../boot', __FILE__) require File.expand_path('../boot', __FILE__)
require 'csv'
require 'rails/all' require 'rails/all'
if defined?(Bundler) if defined?(Bundler)

View file

@ -3,3 +3,5 @@
# Add new mime types for use in respond_to blocks: # Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf # Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone # Mime::Type.register_alias "text/html", :iphone
Mime::Type.register "application/xls", :xls