implement csv/xls export

This commit is contained in:
Devin Howard 2016-02-05 17:49:59 +08:00
parent e4d53162a7
commit 0ae8ea0ca5
6 changed files with 82 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]
@ -82,6 +82,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

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

View file

@ -88,6 +88,38 @@ class Topic < ActiveRecord::Base
result result
end end
# TODO move to a decorator?
def synapses_csv(output_format = 'array')
output = []
synapses.each do |synapse|
if synapse.category == 'from-to'
if synapse.node1_id == id
output << synapse.node1_id.to_s + '->' + synapse.node2_id.to_s
elsif synapse.node2_id == id
output << synapse.node2_id.to_s + '<-' + synapse.node1_id.to_s
else
fail 'invalid synapse on topic in synapse_csv'
end
elsif synapse.category == 'both'
if synapse.node1_id == id
output << synapse.node1_id.to_s + '<->' + synapse.node2_id.to_s
elsif synapse.node2_id == id
output << synapse.node2_id.to_s + '<->' + synapse.node1_id.to_s
else
fail 'invalid synapse on topic in synapse_csv'
end
end
end
if output_format == 'array'
return output
elsif output_format == 'text'
return output.join('; ')
else
fail 'invalid argument to synapses_csv'
end
output
end
##### PERMISSIONS ###### ##### PERMISSIONS ######
# returns false if user not allowed to 'show' Topic, Synapse, or Map # returns false if user not allowed to 'show' Topic, Synapse, or Map

View file

@ -0,0 +1,26 @@
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Metacode</th>
<th>Description</th>
<th>Link</th>
<th>Username</th>
<th>Permission</th>
<th>Synapses</th>
</tr>
<% @map.topics.each do |topic| %>
<tr>
<td><%= topic.id %></td>
<td><%= topic.name %></td>
<td><%= topic.metacode.name %></td>
<td><%= topic.desc %></td>
<td><%= topic.link %></td>
<td><%= topic.user.name %></td>
<td><%= topic.permission %></td>
<% topic.synapses_csv.each do |s_text| %>
<td><%= s_text %></td>
<% end %>
</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'
require 'dotenv' require 'dotenv'

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