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
1a56196b
Commit
1a56196b
authored
Oct 05, 2016
by
James Edwards-Jones
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extracted CSV generation logic into CsvBuilder
parent
f79b51ec
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
15 deletions
+59
-15
app/views/projects/issues/index.csv.ruby
app/views/projects/issues/index.csv.ruby
+1
-15
lib/csv_builder.rb
lib/csv_builder.rb
+35
-0
spec/lib/csv_builder_spec.rb
spec/lib/csv_builder_spec.rb
+23
-0
No files found.
app/views/projects/issues/index.csv.ruby
View file @
1a56196b
...
...
@@ -13,18 +13,4 @@ columns = {
'Labels'
=>
->
(
issue
)
{
issue
.
label_names
.
join
(
','
).
presence
},
}
CSV
.
generate
do
|
csv
|
csv
<<
columns
.
keys
@issues
.
each
do
|
issue
|
row
=
columns
.
values
.
map
do
|
attribute
|
if
attribute
.
respond_to?
(
:call
)
attribute
.
call
(
issue
)
else
issue
.
send
(
attribute
)
end
end
csv
<<
row
end
end
CsvBuilder
.
new
(
columns
).
render
(
@issues
)
lib/csv_builder.rb
0 → 100644
View file @
1a56196b
class
CsvBuilder
def
initialize
(
header_to_value_hash
)
@header_to_value_hash
=
header_to_value_hash
end
def
render
(
collection
)
CSV
.
generate
do
|
csv
|
csv
<<
headers
collection
.
each
do
|
object
|
csv
<<
row
(
object
)
end
end
end
private
def
headers
@header_to_value_hash
.
keys
end
def
attributes
@header_to_value_hash
.
values
end
def
row
(
object
)
attributes
.
map
do
|
attribute
|
if
attribute
.
respond_to?
(
:call
)
attribute
.
call
(
object
)
else
object
.
send
(
attribute
)
end
end
end
end
spec/lib/csv_builder_spec.rb
0 → 100644
View file @
1a56196b
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
])
}
it
'generates a csv'
do
expect
(
csv_data
.
scan
(
/(,|\n)/
).
join
).
to
include
",
\n
,"
end
it
'uses hash keys as headers'
do
expect
(
csv_data
).
to
start_with
'Q & A'
end
it
'gets data by calling method provided as hash value'
do
expect
(
csv_data
).
to
include
'answer'
end
it
'allows lamdas to look up more complicated data'
do
expect
(
csv_data
).
to
include
'rewsna'
end
end
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