Skip to content

Commit 2d268d8

Browse files
committed
Refactoring generate_csv method
1 parent 8b295e6 commit 2d268d8

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

app/controllers/stories_controller.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,32 @@ def import
8787
end
8888

8989
def export
90-
csv = generate_csv(params[:export_with_comments], params[:export_all])
90+
csv = if params[:export_with_comments] == "1" && params[:export_all] == "1"
91+
generate_csv(@project.stories.includes(:comments), with_comments: true, export_all: true)
92+
elsif params[:export_with_comments] == "1"
93+
generate_csv(@project.stories.includes(:comments).approved, with_comments: true, export_all: false)
94+
elsif params[:export_all] == "1"
95+
generate_csv(@project.stories, with_comments: false, export_all: true)
96+
else
97+
generate_csv(@project.stories.approved, with_comments: false, export_all: false)
98+
end
99+
91100
filename = "#{@project.title.gsub(/[^\w]/, "_")}-#{Time.now.to_formatted_s(:short).tr(" ", "_")}.csv"
92101
send_data csv, filename: filename
93102
end
94103

95-
def generate_csv(with_comments, export_all)
96-
stories = if with_comments == "1" && export_all == "1"
97-
@project.stories.includes(:comments)
98-
elsif with_comments == "1"
99-
@project.stories.includes(:comments).approved
100-
elsif export_all == "1"
101-
@project.stories
102-
else
103-
@project.stories.approved
104-
end
105-
104+
def generate_csv(stories, with_comments: false, export_all: false)
106105
CSV.generate(headers: true) do |csv|
107-
csv << ((with_comments == "1") ? (CSV_HEADERS + ["comment"]) : CSV_HEADERS)
106+
csv << (with_comments ? (CSV_HEADERS + ["comment"]) : CSV_HEADERS)
108107

109108
stories.by_position.each do |story|
110-
if with_comments == "1"
109+
if with_comments
111110
comments = []
112111
story.comments.each do |comment|
113112
comments << "#{display_name(comment.user)}: #{comment.body}"
114113
end
115114
end
116-
csv << ((with_comments == "1") ? ([story.id, story.title, story.description, story.position] + comments) : story.attributes.slice(*CSV_HEADERS))
115+
csv << (with_comments ? ([story.id, story.title, story.description, story.position] + comments) : story.attributes.slice(*CSV_HEADERS))
117116
end
118117
end
119118
end

0 commit comments

Comments
 (0)