|
| 1 | +class ExportController < ApplicationController |
| 2 | + |
| 3 | + def index |
| 4 | + end |
| 5 | + |
| 6 | + # Formats |
| 7 | + |
| 8 | + def universes_csv |
| 9 | + send_data to_csv(current_user.universes), filename: "universes-#{Date.today}.csv" |
| 10 | + end |
| 11 | + |
| 12 | + def characters_csv |
| 13 | + send_data to_csv(current_user.characters), filename: "characters-#{Date.today}.csv" |
| 14 | + end |
| 15 | + |
| 16 | + def locations_csv |
| 17 | + send_data to_csv(current_user.locations), filename: "locations-#{Date.today}.csv" |
| 18 | + end |
| 19 | + |
| 20 | + def items_csv |
| 21 | + send_data to_csv(current_user.items), filename: "items-#{Date.today}.csv" |
| 22 | + end |
| 23 | + |
| 24 | + def outline |
| 25 | + send_data content_to_outline, filename: "notebook-#{Date.today}.txt" |
| 26 | + end |
| 27 | + |
| 28 | + def notebook_json |
| 29 | + json_dump = current_user.content.map { |_, content| to_json(content) }.to_json |
| 30 | + send_data json_dump, filename: "notebook-#{Date.today}.json" |
| 31 | + end |
| 32 | + |
| 33 | + def pdf |
| 34 | + end |
| 35 | + |
| 36 | + def html |
| 37 | + end |
| 38 | + |
| 39 | + def xml |
| 40 | + end |
| 41 | + |
| 42 | + def scrivener |
| 43 | + end |
| 44 | + |
| 45 | + private |
| 46 | + |
| 47 | + def to_csv ar_relation |
| 48 | + ar_class = ar_relation.build.class |
| 49 | + attributes = ar_class.attribute_categories.flat_map { |k, v| v[:attributes] } |
| 50 | + |
| 51 | + CSV.generate(headers: true) do |csv| |
| 52 | + csv << attributes |
| 53 | + |
| 54 | + ar_relation.each do |content| |
| 55 | + csv << attributes.map do |attr| |
| 56 | + value = content.send(attr) |
| 57 | + |
| 58 | + if value.is_a?(ActiveRecord::Associations::CollectionProxy) |
| 59 | + value = value.map(&:name).to_sentence |
| 60 | + elsif attr.end_with?('_id') && value.present? |
| 61 | + value = Universe.find(value.to_i).name |
| 62 | + end |
| 63 | + |
| 64 | + value |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def to_json ar_relation |
| 71 | + ar_class = ar_relation.build.class |
| 72 | + attributes = ar_class.attribute_categories.flat_map { |k, v| v[:attributes] } |
| 73 | + |
| 74 | + ar_relation.map do |content| |
| 75 | + content_json = {} |
| 76 | + |
| 77 | + attributes.each do |attr| |
| 78 | + value = content.send(attr) |
| 79 | + |
| 80 | + if value.is_a?(ActiveRecord::Associations::CollectionProxy) |
| 81 | + value = value.map(&:name).to_sentence |
| 82 | + elsif attr.end_with?('_id') && value.present? |
| 83 | + value = Universe.find(value.to_i).name |
| 84 | + end |
| 85 | + |
| 86 | + content_json[attr] = value |
| 87 | + end |
| 88 | + |
| 89 | + content_json |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def content_to_outline |
| 94 | + content_types = %w(universes characters locations items) |
| 95 | + |
| 96 | + text = "" |
| 97 | + content_types.each do |content_type| |
| 98 | + ar_class = current_user.send(content_type).build.class |
| 99 | + attributes = ar_class.attribute_categories.flat_map { |k, v| v[:attributes] } |
| 100 | + |
| 101 | + text << "\n#{content_type.upcase}\n" |
| 102 | + current_user.send(content_type).each do |content| |
| 103 | + text << " #{content.name}\n" |
| 104 | + |
| 105 | + attributes.each do |attr| |
| 106 | + value = content.send(attr) |
| 107 | + next if value.nil? || value.blank? || (value.respond_to?(:empty) && value.empty?) |
| 108 | + |
| 109 | + if value.is_a?(ActiveRecord::Associations::CollectionProxy) |
| 110 | + value = value.map(&:name).to_sentence |
| 111 | + elsif attr.end_with?('_id') && value.present? |
| 112 | + value = Universe.find(value.to_i).name |
| 113 | + end |
| 114 | + |
| 115 | + text << " #{attr}: #{value.split("\n").join("\n ")}\n" |
| 116 | + end |
| 117 | + |
| 118 | + text << "\n" |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + text |
| 123 | + end |
| 124 | +end |
0 commit comments