-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
381 additions
and
36 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
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 was deleted.
Oops, something went wrong.
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Exporters | ||
class Default | ||
attr_reader :collection | ||
|
||
def initialize(collection = []) | ||
@collection = collection | ||
end | ||
|
||
def generate | ||
raise NotImplementedError | ||
end | ||
|
||
class << self | ||
def content(collection) | ||
new(collection).generate | ||
end | ||
|
||
def filename | ||
'export.csv' | ||
end | ||
end | ||
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Exporters | ||
class Stories < Default | ||
EXTRA_COLUMNS = { | ||
note: 'Note', document: 'Document', task: ['Task', 'Task Status'] | ||
}.freeze | ||
|
||
alias stories collection | ||
|
||
def generate | ||
columns = { | ||
notes: note_columns_size, documents: document_columns_size, tasks: task_columns_size | ||
} | ||
|
||
CSV.generate do |csv| | ||
csv << headers | ||
|
||
stories.each do |story| | ||
csv << story.to_csv(columns) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def headers | ||
notes_columns = Array.new(note_columns_size, EXTRA_COLUMNS[:note]) | ||
documents_columns = Array.new(document_columns_size, EXTRA_COLUMNS[:document]) | ||
tasks_columns = Array.new(task_columns_size, EXTRA_COLUMNS[:task]).flatten | ||
|
||
@headers ||= Story.csv_headers + [ | ||
notes_columns, documents_columns, tasks_columns | ||
].flatten | ||
end | ||
|
||
def note_columns_size | ||
@note_columns_size ||= stories.map { |story| story.notes.size }.max || 0 | ||
end | ||
|
||
def document_columns_size | ||
@document_columns_size ||= stories.map { |story| story.documents.size }.max || 0 | ||
end | ||
|
||
def task_columns_size | ||
@task_columns_size ||= stories.map { |story| story.tasks.size }.max || 0 | ||
end | ||
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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Renderers | ||
module CSV | ||
ActionController::Renderers.add :csv do |collection, options| | ||
exporter, filename, type = Renderers::CSVOptions.new(options) | ||
.attributes | ||
|
||
send_data(exporter.content(collection), type: type, filename: filename) | ||
end | ||
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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Renderers | ||
class CSVOptions | ||
def initialize(options = {}) | ||
@options = options | ||
end | ||
|
||
def exporter | ||
@exporter ||= options[:exporter] || ::Exporters::Default | ||
end | ||
|
||
def filename | ||
@filename ||= options[:filename] || exporter.filename | ||
end | ||
|
||
def type | ||
Mime[:csv] | ||
end | ||
|
||
def attributes | ||
[exporter, filename, type] | ||
end | ||
|
||
private | ||
|
||
attr_reader :options | ||
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
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,14 @@ | ||
FactoryBot.define do | ||
factory :attachinary_file, class: Attachinary::File do | ||
public_id { 'public_id' } | ||
version { Time.now.to_i } | ||
format { 'png' } | ||
resource_type { 'image' } | ||
attachinariable_id { nil } | ||
|
||
trait :story do | ||
scope { 'documents' } | ||
attachinariable_type { 'Story' } | ||
end | ||
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,15 @@ | ||
require 'rails_helper' | ||
|
||
describe Exporters::Default do | ||
it { is_expected.to respond_to(:collection) } | ||
|
||
describe '.filename' do | ||
subject { described_class.filename } | ||
|
||
it { is_expected.to eq 'export.csv' } | ||
end | ||
|
||
describe '.content' do | ||
it { expect { described_class.content([]) }.to raise_error NotImplementedError } | ||
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,121 @@ | ||
require 'rails_helper' | ||
|
||
describe Exporters::Stories do | ||
describe '.content' do | ||
let(:user) { create(:user) } | ||
let(:project) { create(:project, users: [user]) } | ||
|
||
let(:csv) do | ||
[ | ||
csv_headers.to_csv, | ||
project.stories.map { |story| story.to_csv(csv_extra_columns) } | ||
.map(&:to_csv) | ||
].flatten.join | ||
end | ||
|
||
subject { described_class.content(stories) } | ||
|
||
context 'when stories is empty' do | ||
let(:stories) { [] } | ||
|
||
it { is_expected.to eq Story.csv_headers.to_csv } | ||
end | ||
|
||
context 'when stories has not notes nor documents nor tasks' do | ||
let(:stories) { create_list(:story, 3, :done, project: project, requested_by: user) } | ||
|
||
let(:csv_headers) { Story.csv_headers } | ||
let(:csv_extra_columns) { { notes: 0, documents: 0, tasks: 0 } } | ||
|
||
it { is_expected.to eq csv } | ||
end | ||
|
||
context 'when only one story has notes' do | ||
let(:csv_headers) { Story.csv_headers << 'Note' } | ||
let(:csv_extra_columns) { { notes: 1, documents: 0, tasks: 0 } } | ||
|
||
let(:stories) { create_list(:story, 1, :active, project: project, requested_by: user) } | ||
|
||
before { create(:note, story: stories.first) } | ||
|
||
it { is_expected.to eq csv } | ||
end | ||
|
||
context 'when more than one story has notes' do | ||
let(:csv_headers) { Story.csv_headers.concat Array.new(2, 'Note') } | ||
let(:csv_extra_columns) { { notes: 2, documents: 0, tasks: 0 } } | ||
|
||
let(:stories) { create_list(:story, 1, :active, project: project, requested_by: user) } | ||
|
||
before { create_list(:note, 2, story: stories.first) } | ||
|
||
it { is_expected.to eq csv } | ||
end | ||
|
||
context 'when only one story has documents' do | ||
let(:csv_headers) { Story.csv_headers << 'Document' } | ||
let(:csv_extra_columns) { { notes: 0, documents: 1, tasks: 0 } } | ||
|
||
let(:stories) { create_list(:story, 3, :done, project: project, requested_by: user) } | ||
let(:story) { stories.first } | ||
|
||
before do | ||
Attachinary::File.skip_callback(:create, :after, :remove_temporary_tag) | ||
|
||
create(:attachinary_file, :story, attachinariable_id: story.id) | ||
end | ||
|
||
after do | ||
Attachinary::File.set_callback(:create, :after, :remove_temporary_tag) | ||
end | ||
|
||
it { is_expected.to eq csv } | ||
end | ||
|
||
context 'when more than one story has documents' do | ||
let(:csv_headers) { Story.csv_headers.concat Array.new(2, 'Document') } | ||
let(:csv_extra_columns) { { notes: 0, documents: 2, tasks: 0 } } | ||
|
||
let(:stories) { create_list(:story, 3, :done, project: project, requested_by: user) } | ||
let(:story) { stories.first } | ||
|
||
before do | ||
Attachinary::File.skip_callback(:create, :after, :remove_temporary_tag) | ||
|
||
create_list(:attachinary_file, 2, :story, attachinariable_id: story.id) | ||
end | ||
|
||
after do | ||
Attachinary::File.set_callback(:create, :after, :remove_temporary_tag) | ||
end | ||
|
||
it { is_expected.to eq csv } | ||
end | ||
|
||
context 'when only one story has tasks' do | ||
let(:csv_headers) { Story.csv_headers.concat(['Task', 'Task Status']) } | ||
let(:csv_extra_columns) { { notes: 0, documents: 0, tasks: 1 } } | ||
|
||
let(:stories) { create_list(:story, 1, :active, project: project, requested_by: user) } | ||
|
||
before { create(:task, story: stories.first) } | ||
|
||
it { is_expected.to eq csv } | ||
end | ||
|
||
context 'when more than one story has tasks' do | ||
let(:csv_headers) do | ||
Story.csv_headers.concat( | ||
Array.new(2, ['Task', 'Task Status']).flatten | ||
) | ||
end | ||
let(:csv_extra_columns) { { notes: 0, documents: 0, tasks: 2 } } | ||
|
||
let(:stories) { create_list(:story, 1, :active, project: project, requested_by: user) } | ||
|
||
before { create_list(:task, 2, story: stories.first) } | ||
|
||
it { is_expected.to eq csv } | ||
end | ||
end | ||
end |
Oops, something went wrong.