Refactor dashboard_choices
This commit is contained in:
parent
c0cb77e413
commit
d2894a39e5
2 changed files with 26 additions and 21 deletions
|
@ -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 = {
|
||||||
|
projects: 'Your Projects (default)',
|
||||||
|
stars: 'Starred Projects'
|
||||||
|
}.with_indifferent_access.freeze
|
||||||
|
|
||||||
|
# Returns an Array usable by a select field for more user-friendly option text
|
||||||
def dashboard_choices
|
def dashboard_choices
|
||||||
orig = User.dashboards.keys
|
defined = User.dashboards
|
||||||
|
|
||||||
choices = [
|
if defined.size != DASHBOARD_CHOICES.size
|
||||||
['Your Projects (default)', orig[0]],
|
# Ensure that anyone adding new options updates this method too
|
||||||
['Starred Projects', orig[1]]
|
raise RuntimeError, "`User` defines #{defined.size} dashboard choices," +
|
||||||
]
|
" but `DASHBOARD_CHOICES` defined #{DASHBOARD_CHOICES.size}."
|
||||||
|
|
||||||
if orig.size != choices.size
|
|
||||||
# Assure that anyone adding new options updates this method too
|
|
||||||
raise RuntimeError, "`User` defines #{orig.size} dashboard choices," +
|
|
||||||
" but #{__method__} defined #{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
|
||||||
|
|
||||||
|
|
|
@ -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)', 'projects']
|
['Starred Projects', 'stars']
|
||||||
expect(choices[1]).to eq ['Starred Projects', 'stars']
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue