From 4e90e4808deeea17e486799ba562b96e33a054a2 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 13 Feb 2016 12:53:07 +0800 Subject: [PATCH] DRY up csv/xls rendering, put it into model --- app/controllers/maps_controller.rb | 1 - app/models/map.rb | 69 ++++++++++++++++-------------- app/views/maps/show.xls.erb | 54 +++-------------------- 3 files changed, 44 insertions(+), 80 deletions(-) diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 65cc76cb..1226834e 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -46,7 +46,6 @@ class MapsController < ApplicationController # GET maps/:id def show - @current = current_user @map = Map.find(params[:id]).authorize_to_show(@current) diff --git a/app/models/map.rb b/app/models/map.rb index 980173cc..87ee5839 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -79,39 +79,46 @@ class Map < ActiveRecord::Base json end + def to_spreadsheet + spreadsheet = [] + spreadsheet << ["Topics"] + spreadsheet << ["Id", "Name", "Metacode", "X", "Y", "Description", "Link", "User", "Permission"] + self.topicmappings.each do |mapping| + topic = mapping.mappable + next if topic.nil? + spreadsheet << [ + topic.id, + topic.name, + topic.metacode.name, + mapping.xloc, + mapping.yloc, + topic.desc, + topic.link, + topic.user.name, + topic.permission + ] + end + spreadsheet << [] + spreadsheet << ["Synapses"] + spreadsheet << ["Id", "Description", "Category", "Topic1", "Topic2", "User", "Permission"] + self.synapses.each do |synapse| + spreadsheet << [ + synapse.id, + synapse.desc, + synapse.category, + synapse.node1_id, + synapse.node2_id, + synapse.user.name, + synapse.permission + ] + end + spreadsheet + end + def to_csv(options = {}) CSV.generate(options) do |csv| - csv << ["topics"] - csv << ["id", "name", "metacode", "x", "y", "desc", "link", "user.name", "permission"] - self.topicmappings.each do |mapping| - topic = mapping.mappable - next if topic.nil? - csv << [ - topic.id, - topic.name, - topic.metacode.name, - mapping.x, - mapping.y, - topic.desc, - topic.link, - topic.user.name, - topic.permission, - topic.synapses_csv("text") - ] - end - csv << [] - csv << ["synapses"] - csv << ["id", "description", "category", "topic1", "topic2", "username", "permission"] - self.synapses.each do |synapse| - csv << [ - synapse.id, - synapse.desc, - synapse.category, - synapse.node1_id, - synapse.node2_id, - synapse.user.name, - synapse.permission - ] + to_spreadsheet.each do |line| + csv << line end end end diff --git a/app/views/maps/show.xls.erb b/app/views/maps/show.xls.erb index 4b22257e..2f11a946 100644 --- a/app/views/maps/show.xls.erb +++ b/app/views/maps/show.xls.erb @@ -1,51 +1,9 @@ - - - - - - - - - - - - - <% @map.topicmappings.each do |mapping| %> - <% topic = mapping.mappable %> - <% next if topic.nil? %> - - - - - - - - - - - - <% end %> - - - - - - - - - - - - <% @map.synapses.each do |synapse| %> - - - - - - - - - + <% @map.to_spreadsheet.each do |line| %> + + <% line.each do |field| %> + + <% end %> + <% end %>
Topics
IDNameMetacodeXYDescriptionLinkUsernamePermission
<%= topic.id %><%= topic.name %><%= topic.metacode.name %><%= mapping.xloc %><%= mapping.yloc %><%= topic.desc %><%= topic.link %><%= topic.user.name %><%= topic.permission %>
Synapses
IDDescriptionCategoryTopic1Topic2UsernamePermission
<%= synapse.id %><%= synapse.desc %><%= synapse.category %><%= synapse.node1_id %><%= synapse.node2_id %><%= synapse.user.name %><%= synapse.permission %>
<%= field %>