diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 9ad05810..65cc76cb 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -1,7 +1,7 @@ class MapsController < ApplicationController 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] @@ -67,6 +67,8 @@ class MapsController < ApplicationController respond_with(@allmappers, @allmappings, @allsynapses, @alltopics, @map) } format.json { render json: @map } + format.csv { send_data @map.to_csv } + format.xls end end diff --git a/app/models/map.rb b/app/models/map.rb index 6c2caca2..166afb4a 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -79,6 +79,24 @@ class Map < ActiveRecord::Base json 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 ###### def authorize_to_delete(user) diff --git a/app/models/topic.rb b/app/models/topic.rb index c528aa6e..3e66366b 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -87,6 +87,38 @@ class Topic < ActiveRecord::Base end result 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 ###### diff --git a/app/views/maps/show.xls.erb b/app/views/maps/show.xls.erb new file mode 100644 index 00000000..d00dd36e --- /dev/null +++ b/app/views/maps/show.xls.erb @@ -0,0 +1,26 @@ +
ID | +Name | +Metacode | +Description | +Link | +Username | +Permission | +Synapses | +
---|---|---|---|---|---|---|---|
<%= topic.id %> | +<%= topic.name %> | +<%= topic.metacode.name %> | +<%= topic.desc %> | +<%= topic.link %> | +<%= topic.user.name %> | +<%= topic.permission %> | + <% topic.synapses_csv.each do |s_text| %> +<%= s_text %> | + <% end %> +