implement csv/xls export
This commit is contained in:
parent
40a833b069
commit
b36722da78
6 changed files with 82 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
||||||
class MapsController < ApplicationController
|
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]
|
||||||
|
|
||||||
|
@ -67,6 +67,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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -87,6 +87,38 @@ class Topic < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
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 ######
|
||||||
|
|
||||||
|
|
26
app/views/maps/show.xls.erb
Normal file
26
app/views/maps/show.xls.erb
Normal 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>
|
|
@ -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'
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue