-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from indentlabs/exporting
Add notebook exporting to TXT, CSV, and JSON
- Loading branch information
Showing
13 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
class ExportController < ApplicationController | ||
|
||
def index | ||
end | ||
|
||
# Formats | ||
|
||
def universes_csv | ||
send_data to_csv(current_user.universes), filename: "universes-#{Date.today}.csv" | ||
end | ||
|
||
def characters_csv | ||
send_data to_csv(current_user.characters), filename: "characters-#{Date.today}.csv" | ||
end | ||
|
||
def locations_csv | ||
send_data to_csv(current_user.locations), filename: "locations-#{Date.today}.csv" | ||
end | ||
|
||
def items_csv | ||
send_data to_csv(current_user.items), filename: "items-#{Date.today}.csv" | ||
end | ||
|
||
def outline | ||
send_data content_to_outline, filename: "notebook-#{Date.today}.txt" | ||
end | ||
|
||
def notebook_json | ||
json_dump = current_user.content.map { |_, content| to_json(content) }.to_json | ||
send_data json_dump, filename: "notebook-#{Date.today}.json" | ||
end | ||
|
||
def pdf | ||
end | ||
|
||
def html | ||
end | ||
|
||
def xml | ||
end | ||
|
||
def scrivener | ||
end | ||
|
||
private | ||
|
||
def to_csv ar_relation | ||
ar_class = ar_relation.build.class | ||
attributes = ar_class.attribute_categories.flat_map { |k, v| v[:attributes] } | ||
|
||
CSV.generate(headers: true) do |csv| | ||
csv << attributes | ||
|
||
ar_relation.each do |content| | ||
csv << attributes.map do |attr| | ||
value = content.send(attr) | ||
|
||
if value.is_a?(ActiveRecord::Associations::CollectionProxy) | ||
value = value.map(&:name).to_sentence | ||
elsif attr.end_with?('_id') && value.present? | ||
value = Universe.find(value.to_i).name | ||
end | ||
|
||
value | ||
end | ||
end | ||
end | ||
end | ||
|
||
def to_json ar_relation | ||
ar_class = ar_relation.build.class | ||
attributes = ar_class.attribute_categories.flat_map { |k, v| v[:attributes] } | ||
|
||
ar_relation.map do |content| | ||
content_json = {} | ||
|
||
attributes.each do |attr| | ||
value = content.send(attr) | ||
|
||
if value.is_a?(ActiveRecord::Associations::CollectionProxy) | ||
value = value.map(&:name).to_sentence | ||
elsif attr.end_with?('_id') && value.present? | ||
value = Universe.find(value.to_i).name | ||
end | ||
|
||
content_json[attr] = value | ||
end | ||
|
||
content_json | ||
end | ||
end | ||
|
||
def content_to_outline | ||
content_types = %w(universes characters locations items) | ||
|
||
text = "" | ||
content_types.each do |content_type| | ||
ar_class = current_user.send(content_type).build.class | ||
attributes = ar_class.attribute_categories.flat_map { |k, v| v[:attributes] } | ||
|
||
text << "\n#{content_type.upcase}\n" | ||
current_user.send(content_type).each do |content| | ||
text << " #{content.name}\n" | ||
|
||
attributes.each do |attr| | ||
value = content.send(attr) | ||
next if value.nil? || value.blank? || (value.respond_to?(:empty) && value.empty?) | ||
|
||
if value.is_a?(ActiveRecord::Associations::CollectionProxy) | ||
value = value.map(&:name).to_sentence | ||
elsif attr.end_with?('_id') && value.present? | ||
value = Universe.find(value.to_i).name | ||
end | ||
|
||
text << " #{attr}: #{value.split("\n").join("\n ")}\n" | ||
end | ||
|
||
text << "\n" | ||
end | ||
end | ||
|
||
text | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ExportHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Export#csv</h1> | ||
<p>Find me in app/views/export/csv.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Export#html</h1> | ||
<p>Find me in app/views/export/html.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<div class="row"> | ||
<div class="col s12"> | ||
<ul class="collection with-header hoverable"> | ||
<li class="collection-header"> | ||
<h4>Download your Notebook</h4> | ||
<div>We offer the following download formats, with more coming soon.</div> | ||
</li> | ||
<li class="collection-item avatar"> | ||
<i class="material-icons circle">file_download</i> | ||
<div class="title">Outline (TXT)</div> | ||
<ul> | ||
<li><%= link_to "notebook.txt", notebook_outline_path %></li> | ||
</ul> | ||
</li> | ||
<li class="collection-item avatar"> | ||
<i class="material-icons circle">file_download</i> | ||
<div class="title">Excel (CSV)</div> | ||
<ul> | ||
<li><%= link_to "universes.csv", universes_csv_path %></li> | ||
<li><%= link_to "characters.csv", characters_csv_path %></li> | ||
<li><%= link_to "locations.csv", locations_csv_path %></li> | ||
<li><%= link_to "items.csv", items_csv_path %></li> | ||
</ul> | ||
</li> | ||
<li class="collection-item avatar"> | ||
<i class="material-icons circle">file_download</i> | ||
<div class="title">JSON</div> | ||
<ul> | ||
<li><%= link_to "notebook.json", notebook_json_path %></li> | ||
</ul> | ||
</li> | ||
</ul> | ||
|
||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Export#json</h1> | ||
<p>Find me in app/views/export/json.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Export#outline</h1> | ||
<p>Find me in app/views/export/outline.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Export#pdf</h1> | ||
<p>Find me in app/views/export/pdf.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Export#scrivener</h1> | ||
<p>Find me in app/views/export/scrivener.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Export#xml</h1> | ||
<p>Find me in app/views/export/xml.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters