Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,40 @@ def import
end

def export
csv = if params[:export_with_comments] == "1"
CSV.generate(headers: true) do |csv|
csv << CSV_HEADERS + ["comment"]
@project.stories.includes(:comments).approved.by_position.each do |story|
comments = []
story.comments.each do |comment|
comments << "#{display_name(comment.user)}: #{comment.body}"
end
csv << [story.id, story.title, story.description, story.position] + comments
end
end
csv = if params[:export_with_comments] == "1" && params[:export_all] == "1"
generate_csv(@project.stories.includes(:comments), with_comments: true, export_all: true)
elsif params[:export_with_comments] == "1"
generate_csv(@project.stories.includes(:comments).approved, with_comments: true, export_all: false)
elsif params[:export_all] == "1"
generate_csv(@project.stories, with_comments: false, export_all: true)
else
CSV.generate(headers: true) do |csv|
csv << CSV_HEADERS
@project.stories.approved.by_position.each do |story|
csv << story.attributes.slice(*CSV_HEADERS)
end
end
generate_csv(@project.stories.approved, with_comments: false, export_all: false)
end

filename = "#{@project.title.gsub(/[^\w]/, "_")}-#{Time.now.to_formatted_s(:short).tr(" ", "_")}.csv"
send_data csv, filename: filename
end

def generate_csv(stories, with_comments: false, export_all: false)
CSV.generate(headers: true) do |csv|
headers = CSV_HEADERS.dup
headers << "comment" if with_comments
csv << headers

stories.by_position.each do |story|
comments = []

if with_comments
comments = story.comments.map do |comment|
"#{display_name(comment.user)}: #{comment.body}"
end
end

csv << [story.id, story.title, story.description, story.position] + comments
end
end
end

def render_markdown
render plain: markdown(params[:markdown])
end
Expand Down
8 changes: 7 additions & 1 deletion app/views/projects/_import_export.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
<div class="card-body">
<%= form_with url: export_project_stories_path(@project), method: :get do |f| %>
<%= f.submit "Export", class: "button green", data: { disable_with: false } %>
<br/>
<% if current_user.admin? %>
<br />
<%= f.label :export_all do %>
<%= f.check_box :export_all %>
Export all stories
<% end %>
<% end %>
<%= f.label :export_with_comments do %>
<%= f.check_box :export_with_comments %>
Export with comments
Expand Down
21 changes: 21 additions & 0 deletions spec/controllers/stories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,27 @@
expect(csv_data).to eq(expected_csv_content)
end

context "when an admin" do
it "exports a CSV file with all stories" do
user = FactoryBot.create(:user)
user.admin = true

story2 = FactoryBot.create(:story, project: project, status: :rejected)
story3 = FactoryBot.create(:story, project: project, status: :pending)
get :export, params: {project_id: project.id, export_all: "1"}
expect(response).to have_http_status(:ok)

csv_data = CSV.parse(response.body)
expected_csv_content = [
["id", "title", "description", "position"],
[story.id.to_s, story.title, story.description, story.position.to_s],
[story2.id.to_s, story2.title, story2.description, story2.position.to_s],
[story3.id.to_s, story3.title, story3.description, story3.position.to_s]
]
expect(csv_data).to eq(expected_csv_content)
end
end

context "with comments" do
it "exports a CSV file with only approved stories" do
user = FactoryBot.create(:user)
Expand Down
Loading