Skip to content

Commit b303197

Browse files
authored
Merge pull request #107 from indentlabs/exporting
Add notebook exporting to TXT, CSV, and JSON
2 parents e53201e + 00563e5 commit b303197

File tree

13 files changed

+192
-4
lines changed

13 files changed

+192
-4
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

app/helpers/export_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ExportHelper
2+
end

app/views/export/csv.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Export#csv</h1>
2+
<p>Find me in app/views/export/csv.html.erb</p>

app/views/export/html.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Export#html</h1>
2+
<p>Find me in app/views/export/html.html.erb</p>

app/views/export/index.html.erb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<div class="row">
2+
<div class="col s12">
3+
<ul class="collection with-header hoverable">
4+
<li class="collection-header">
5+
<h4>Download your Notebook</h4>
6+
<div>We offer the following download formats, with more coming soon.</div>
7+
</li>
8+
<li class="collection-item avatar">
9+
<i class="material-icons circle">file_download</i>
10+
<div class="title">Outline (TXT)</div>
11+
<ul>
12+
<li><%= link_to "notebook.txt", notebook_outline_path %></li>
13+
</ul>
14+
</li>
15+
<li class="collection-item avatar">
16+
<i class="material-icons circle">file_download</i>
17+
<div class="title">Excel (CSV)</div>
18+
<ul>
19+
<li><%= link_to "universes.csv", universes_csv_path %></li>
20+
<li><%= link_to "characters.csv", characters_csv_path %></li>
21+
<li><%= link_to "locations.csv", locations_csv_path %></li>
22+
<li><%= link_to "items.csv", items_csv_path %></li>
23+
</ul>
24+
</li>
25+
<li class="collection-item avatar">
26+
<i class="material-icons circle">file_download</i>
27+
<div class="title">JSON</div>
28+
<ul>
29+
<li><%= link_to "notebook.json", notebook_json_path %></li>
30+
</ul>
31+
</li>
32+
</ul>
33+
34+
</div>
35+
</div>

app/views/export/json.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Export#json</h1>
2+
<p>Find me in app/views/export/json.html.erb</p>

app/views/export/outline.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Export#outline</h1>
2+
<p>Find me in app/views/export/outline.html.erb</p>

app/views/export/pdf.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Export#pdf</h1>
2+
<p>Find me in app/views/export/pdf.html.erb</p>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Export#scrivener</h1>
2+
<p>Find me in app/views/export/scrivener.html.erb</p>

app/views/export/xml.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Export#xml</h1>
2+
<p>Find me in app/views/export/xml.html.erb</p>

0 commit comments

Comments
 (0)