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
0
Merge Requests
0
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
iv
gitlab-ce
Commits
d2894a39
Commit
d2894a39
authored
Jun 12, 2015
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor dashboard_choices
parent
c0cb77e4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
21 deletions
+26
-21
app/helpers/preferences_helper.rb
app/helpers/preferences_helper.rb
+16
-13
spec/helpers/preferences_helper_spec.rb
spec/helpers/preferences_helper_spec.rb
+10
-8
No files found.
app/helpers/preferences_helper.rb
View file @
d2894a39
...
@@ -20,22 +20,25 @@ module PreferencesHelper
...
@@ -20,22 +20,25 @@ module PreferencesHelper
COLOR_SCHEMES
.
freeze
COLOR_SCHEMES
.
freeze
end
end
# Populates the dashboard preference select field with more user-friendly
# Maps `dashboard` values to more user-friendly option text
# values.
DASHBOARD_CHOICES
=
{
def
dashboard_choices
projects:
'Your Projects (default)'
,
orig
=
User
.
dashboards
.
keys
stars:
'Starred Projects'
}.
with_indifferent_access
.
freeze
choices
=
[
# Returns an Array usable by a select field for more user-friendly option text
[
'Your Projects (default)'
,
orig
[
0
]],
def
dashboard_choices
[
'Starred Projects'
,
orig
[
1
]]
defined
=
User
.
dashboards
]
if
orig
.
size
!=
choices
.
size
if
defined
.
size
!=
DASHBOARD_CHOICES
.
size
#
As
sure that anyone adding new options updates this method too
#
En
sure that anyone adding new options updates this method too
raise
RuntimeError
,
"`User` defines
#{
orig
.
size
}
dashboard choices,"
+
raise
RuntimeError
,
"`User` defines
#{
defined
.
size
}
dashboard choices,"
+
" but
#{
__method__
}
defined
#{
choices
.
size
}
"
" but
`DASHBOARD_CHOICES` defined
#{
DASHBOARD_CHOICES
.
size
}
.
"
else
else
choices
defined
.
map
do
|
key
,
_
|
# Use `fetch` so `KeyError` gets raised when a key is missing
[
DASHBOARD_CHOICES
.
fetch
(
key
),
key
]
end
end
end
end
end
...
...
spec/helpers/preferences_helper_spec.rb
View file @
d2894a39
...
@@ -32,18 +32,20 @@ describe PreferencesHelper do
...
@@ -32,18 +32,20 @@ describe PreferencesHelper do
describe
'dashboard_choices'
do
describe
'dashboard_choices'
do
it
'raises an exception when defined choices may be missing'
do
it
'raises an exception when defined choices may be missing'
do
dashboards
=
User
.
dashboards
expect
(
User
).
to
receive
(
:dashboards
).
and_return
(
foo:
'foo'
)
expect
(
User
).
to
receive
(
:dashboards
).
expect
{
dashboard_choices
}.
to
raise_error
(
RuntimeError
)
and_return
(
dashboards
.
merge
(
foo:
'foo'
))
end
expect
{
dashboard_choices
}.
to
raise_error
it
'raises an exception when defined choices may be using the wrong key'
do
expect
(
User
).
to
receive
(
:dashboards
).
and_return
(
foo:
'foo'
,
bar:
'bar'
)
expect
{
dashboard_choices
}.
to
raise_error
(
KeyError
)
end
end
it
'provides better option descriptions'
do
it
'provides better option descriptions'
do
choices
=
dashboard_choices
expect
(
dashboard_choices
).
to
match_array
[
[
'Your Projects (default)'
,
'projects'
],
expect
(
choices
[
0
]).
to
eq
[
'Your Projects (default)'
,
'project
s'
]
[
'Starred Projects'
,
'star
s'
]
expect
(
choices
[
1
]).
to
eq
[
'Starred Projects'
,
'stars'
]
]
end
end
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