fix a few embarassing errors - export is working

This commit is contained in:
Devin Howard 2016-03-26 15:21:55 +08:00
parent 53867caae8
commit ae9f4a51a2
3 changed files with 16 additions and 12 deletions

View file

@ -85,8 +85,8 @@ class MapsController < ApplicationController
respond_with(@allmappers, @allmappings, @allsynapses, @alltopics, @allmessages, @map) respond_with(@allmappers, @allmappings, @allsynapses, @alltopics, @allmessages, @map)
} }
format.json { render json: @map } format.json { render json: @map }
format.csv { redirect_to :export } format.csv { redirect_to action: :export, format: :csv }
format.xls { redirect_to :export } format.xls { redirect_to action: :export, format: :xls }
end end
end end
@ -94,7 +94,7 @@ class MapsController < ApplicationController
def export def export
map = Map.find(params[:id]) map = Map.find(params[:id])
authorize map authorize map
exporter = MapExportService(current_user, map) exporter = MapExportService.new(current_user, map)
respond_to do |format| respond_to do |format|
format.json { render json: exporter.json } format.json { render json: exporter.json }
format.csv { send_data exporter.csv } format.csv { send_data exporter.csv }

View file

@ -31,6 +31,10 @@ class MapPolicy < ApplicationPolicy
record.permission == 'commons' || record.permission == 'public' || record.user == user record.permission == 'commons' || record.permission == 'public' || record.user == user
end end
def export?
show?
end
def contains? def contains?
show? show?
end end

View file

@ -2,8 +2,8 @@ class MapExportService < Struct.new(:user, :map)
def json def json
# marshal_dump turns OpenStruct into a Hash # marshal_dump turns OpenStruct into a Hash
{ {
topics: exportable_topics.map(:marshal_dump), topics: exportable_topics.map(&:marshal_dump),
synapses: exportable_synapses.map(:marshal_dump) synapses: exportable_synapses.map(&:marshal_dump)
} }
end end
@ -29,9 +29,9 @@ class MapExportService < Struct.new(:user, :map)
end end
def exportable_topics def exportable_topics
visible_topics ||= Pundit.policy_scope!(@user, @map.topics) visible_topics ||= Pundit.policy_scope!(user, map.topics)
topic_mappings = Mapping.includes(mappable: [:metacode, :user]) topic_mappings = Mapping.includes(mappable: [:metacode, :user])
.where(mappable: visible_topics, map: @map) .where(mappable: visible_topics, map: map)
topic_mappings.map do |mapping| topic_mappings.map do |mapping|
topic = mapping.mappable topic = mapping.mappable
OpenStruct.new( OpenStruct.new(
@ -49,7 +49,7 @@ class MapExportService < Struct.new(:user, :map)
end end
def exportable_synapses def exportable_synapses
visible_synapses = Pundit.policy_scope!(@user, @map.synapses) visible_synapses = Pundit.policy_scope!(user, map.synapses)
visible_synapses.map do |synapse| visible_synapses.map do |synapse|
OpenStruct.new( OpenStruct.new(
topic1: synapse.node1_id, topic1: synapse.node1_id,
@ -65,18 +65,18 @@ class MapExportService < Struct.new(:user, :map)
def to_spreadsheet def to_spreadsheet
spreadsheet = [] spreadsheet = []
spreadsheet << ["Topics"] spreadsheet << ["Topics"]
spreadsheet << topic_headings.map(:capitalize) spreadsheet << topic_headings.map(&:capitalize)
exportable_topics.each do |topics| exportable_topics.each do |topics|
# convert exportable_topics into an array of arrays # convert exportable_topics into an array of arrays
topic_headings.map do { |h| topics.send(h) } spreadsheet << topic_headings.map { |h| topics.send(h) }
end end
spreadsheet << [] spreadsheet << []
spreadsheet << ["Synapses"] spreadsheet << ["Synapses"]
spreadsheet << synapse_headings.map(:capitalize) spreadsheet << synapse_headings.map(&:capitalize)
exportable_synapses.each do |synapse| exportable_synapses.each do |synapse|
# convert exportable_synapses into an array of arrays # convert exportable_synapses into an array of arrays
synapse_headings.map do { |h| synapse.send(h) } spreadsheet << synapse_headings.map { |h| synapse.send(h) }
end end
spreadsheet spreadsheet