DRY up csv/xls rendering, put it into model

This commit is contained in:
Devin Howard 2016-02-13 12:53:07 +08:00
parent 987d68198c
commit 4e90e4808d
3 changed files with 44 additions and 80 deletions

View file

@ -46,7 +46,6 @@ class MapsController < ApplicationController
# GET maps/:id # GET maps/:id
def show def show
@current = current_user @current = current_user
@map = Map.find(params[:id]).authorize_to_show(@current) @map = Map.find(params[:id]).authorize_to_show(@current)

View file

@ -79,31 +79,30 @@ class Map < ActiveRecord::Base
json json
end end
def to_csv(options = {}) def to_spreadsheet
CSV.generate(options) do |csv| spreadsheet = []
csv << ["topics"] spreadsheet << ["Topics"]
csv << ["id", "name", "metacode", "x", "y", "desc", "link", "user.name", "permission"] spreadsheet << ["Id", "Name", "Metacode", "X", "Y", "Description", "Link", "User", "Permission"]
self.topicmappings.each do |mapping| self.topicmappings.each do |mapping|
topic = mapping.mappable topic = mapping.mappable
next if topic.nil? next if topic.nil?
csv << [ spreadsheet << [
topic.id, topic.id,
topic.name, topic.name,
topic.metacode.name, topic.metacode.name,
mapping.x, mapping.xloc,
mapping.y, mapping.yloc,
topic.desc, topic.desc,
topic.link, topic.link,
topic.user.name, topic.user.name,
topic.permission, topic.permission
topic.synapses_csv("text")
] ]
end end
csv << [] spreadsheet << []
csv << ["synapses"] spreadsheet << ["Synapses"]
csv << ["id", "description", "category", "topic1", "topic2", "username", "permission"] spreadsheet << ["Id", "Description", "Category", "Topic1", "Topic2", "User", "Permission"]
self.synapses.each do |synapse| self.synapses.each do |synapse|
csv << [ spreadsheet << [
synapse.id, synapse.id,
synapse.desc, synapse.desc,
synapse.category, synapse.category,
@ -113,6 +112,14 @@ class Map < ActiveRecord::Base
synapse.permission synapse.permission
] ]
end end
spreadsheet
end
def to_csv(options = {})
CSV.generate(options) do |csv|
to_spreadsheet.each do |line|
csv << line
end
end end
end end

View file

@ -1,51 +1,9 @@
<table> <table>
<tr><th>Topics</th></tr> <% @map.to_spreadsheet.each do |line| %>
<tr> <tr>
<th>ID</th> <% line.each do |field| %>
<th>Name</th> <td><%= field %></td>
<th>Metacode</th>
<th>X</th>
<th>Y</th>
<th>Description</th>
<th>Link</th>
<th>Username</th>
<th>Permission</th>
</tr>
<% @map.topicmappings.each do |mapping| %>
<% topic = mapping.mappable %>
<% next if topic.nil? %>
<tr>
<td><%= topic.id %></td>
<td><%= topic.name %></td>
<td><%= topic.metacode.name %></td>
<td><%= mapping.xloc %></td>
<td><%= mapping.yloc %></td>
<td><%= topic.desc %></td>
<td><%= topic.link %></td>
<td><%= topic.user.name %></td>
<td><%= topic.permission %></td>
</tr>
<% end %> <% end %>
<tr></tr>
<tr><th>Synapses</th></tr>
<tr>
<th>ID</th>
<th>Description</th>
<th>Category</th>
<th>Topic1</th>
<th>Topic2</th>
<th>Username</th>
<th>Permission</th>
</tr>
<% @map.synapses.each do |synapse| %>
<tr>
<td><%= synapse.id %></td>
<td><%= synapse.desc %></td>
<td><%= synapse.category %></td>
<td><%= synapse.node1_id %></td>
<td><%= synapse.node2_id %></td>
<td><%= synapse.user.name %></td>
<td><%= synapse.permission %></td>
</tr> </tr>
<% end %> <% end %>
</table> </table>