Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
431d4b77
Commit
431d4b77
authored
Oct 14, 2016
by
James Edwards-Jones
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CsvBuilder takes collection in initializer instead of render
parent
91ac5c7b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
14 deletions
+17
-14
app/views/projects/issues/index.csv.ruby
app/views/projects/issues/index.csv.ruby
+1
-1
lib/csv_builder.rb
lib/csv_builder.rb
+14
-11
spec/lib/csv_builder_spec.rb
spec/lib/csv_builder_spec.rb
+2
-2
No files found.
app/views/projects/issues/index.csv.ruby
View file @
431d4b77
...
...
@@ -20,4 +20,4 @@ columns = {
'Labels'
=>
->
(
issue
)
{
labels
[
issue
.
id
].
sort
.
join
(
','
).
presence
}
}
CsvBuilder
.
new
(
columns
).
render
(
@issues
.
includes
(
:author
,
:assignee
))
CsvBuilder
.
new
(
@issues
.
includes
(
:author
,
:assignee
),
columns
).
render
lib/csv_builder.rb
View file @
431d4b77
# Generates CSV when given a mapping and a collection.
#
# Takes a hash of 'Column Heading' => 'value_method'.
#
# The value method will be called once for each object in the collection, to
# determine the value for that row. It can either be the name of a method on
# the object, or a lamda to call passing in the object.
# Generates CSV when given a collection and a mapping.
#
# Example:
#
...
...
@@ -15,18 +9,27 @@
# 'Created At (UTC)' => -> (post) { post.created_at&.strftime('%Y-%m-%d %H:%M:%S') }
# }
#
# CsvBuilder.new(
columns).render(@posts)
# CsvBuilder.new(
@posts, columns).render
#
class
CsvBuilder
def
initialize
(
header_to_value_hash
)
#
# * +collection+ - The data collection to be used
# * +header_to_hash_value+ - A hash of 'Column Heading' => 'value_method'.
#
# The value method will be called once for each object in the collection, to
# determine the value for that row. It can either be the name of a method on
# the object, or a lamda to call passing in the object.
def
initialize
(
collection
,
header_to_value_hash
)
@header_to_value_hash
=
header_to_value_hash
@collection
=
collection
end
def
render
(
collection
)
# Renders the csv to a string
def
render
CSV
.
generate
do
|
csv
|
csv
<<
headers
collection
.
each
do
|
object
|
@
collection
.
each
do
|
object
|
csv
<<
row
(
object
)
end
end
...
...
spec/lib/csv_builder_spec.rb
View file @
431d4b77
...
...
@@ -2,8 +2,8 @@ require 'spec_helper'
describe
CsvBuilder
,
lib:
true
do
let
(
:object
)
{
double
(
question: :answer
)
}
let
(
:subject
)
{
CsvBuilder
.
new
(
'Q & A'
=>
:question
,
'Reversed'
=>
->
(
o
)
{
o
.
question
.
to_s
.
reverse
})
}
let
(
:csv_data
)
{
subject
.
render
([
object
])
}
let
(
:subject
)
{
CsvBuilder
.
new
(
[
object
],
'Q & A'
=>
:question
,
'Reversed'
=>
->
(
o
)
{
o
.
question
.
to_s
.
reverse
})
}
let
(
:csv_data
)
{
subject
.
render
}
it
'generates a csv'
do
expect
(
csv_data
.
scan
(
/(,|\n)/
).
join
).
to
include
",
\n
,"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment