Skip to content

Commit

Permalink
Moving the csv generation into their own respective methods to fix co…
Browse files Browse the repository at this point in the history
…de climate issue
  • Loading branch information
torresga committed Oct 26, 2024
1 parent 0ca7f68 commit 0863212
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,45 @@ def import

def export
csv = if params[:export_with_comments] == "1"
CSV.generate(headers: true) do |csv|
csv << CSV_HEADERS + ["comment"]
stories = if params.include?(:export_all) && params[:export_all] == "1"
@project.stories.includes(:comments)
else
@project.stories.includes(:comments).approved
end
stories.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
generate_csv_with_comments
else
CSV.generate(headers: true) do |csv|
csv << CSV_HEADERS
stories = if params.include?(:export_all) && params[:export_all] == "1"
@project.stories
else
@project.stories.approved
end
generate_csv_without_comments
end
filename = "#{@project.title.gsub(/[^\w]/, "_")}-#{Time.now.to_formatted_s(:short).tr(" ", "_")}.csv"
send_data csv, filename: filename
end

stories.by_position.each do |story|
csv << story.attributes.slice(*CSV_HEADERS)
def generate_csv_with_comments
CSV.generate(headers: true) do |csv|
csv << CSV_HEADERS + ["comment"]
stories = if params.include?(:export_all) && params[:export_all] == "1"
@project.stories.includes(:comments)
else
@project.stories.includes(:comments).approved
end
stories.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
end

def generate_csv_without_comments
CSV.generate(headers: true) do |csv|
csv << CSV_HEADERS
stories = if params.include?(:export_all) && params[:export_all] == "1"
@project.stories
else
@project.stories.approved
end

stories.by_position.each do |story|
csv << story.attributes.slice(*CSV_HEADERS)
end
end
filename = "#{@project.title.gsub(/[^\w]/, "_")}-#{Time.now.to_formatted_s(:short).tr(" ", "_")}.csv"
send_data csv, filename: filename
end

def render_markdown
Expand Down

0 comments on commit 0863212

Please sign in to comment.