BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
e5a29b45
Commit
e5a29b45
authored
Jan 09, 2017
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve presenter factory
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
bf789ff5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
61 deletions
+38
-61
factory.rb
lib/gitlab/view/presenter/factory.rb
+13
-30
factory_spec.rb
spec/lib/gitlab/view/presenter/factory_spec.rb
+25
-31
No files found.
lib/gitlab/view/presenter
_
factory.rb
→
lib/gitlab/view/presenter
/
factory.rb
View file @
e5a29b45
module
Gitlab
module
Gitlab
module
View
module
View
class
PresenterFactory
module
Presenter
def
initialize
(
subject
,
user:
nil
)
class
Factory
@subject
=
subject
def
initialize
(
subject
,
**
attributes
)
@user
=
user
@subject
=
subject
end
@attributes
=
attributes
end
def
fabricate!
presenter
=
if
presenter_class
.
ancestors
.
include?
(
SimpleDelegator
)
delegator_presenter
else
simple_presenter
end
presenter
.
with_subject
(
subject
)
.
with_user
(
user
)
end
private
def
fabricate!
presenter_class
.
new
(
@subject
,
@attributes
)
end
attr_reader
:subject
,
:user
private
def
presenter_class
"
#{
subject
.
class
.
name
.
demodulize
}
Presenter"
.
constantize
end
def
delegator_presenter
presenter_class
.
new
(
subject
)
end
def
simple_presenter
def
presenter_class
presenter_class
.
new
@subject
.
class
.
const_get
(
'Presenter'
)
end
end
end
end
end
end
end
...
...
spec/lib/gitlab/view/presenter
_
factory_spec.rb
→
spec/lib/gitlab/view/presenter
/
factory_spec.rb
View file @
e5a29b45
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
View
::
PresenterFactory
do
describe
Gitlab
::
View
::
Presenter
::
Factory
do
let
(
:appearance
)
{
build
(
:appearance
)
}
let
(
:variable
)
{
create
(
:ci_variable
)
}
let
(
:broadcast_message
)
{
build
(
:broadcast_message
)
}
before
do
class
AppearancePresenter
include
Gitlab
::
View
::
Presenter
end
class
BroadcastMessagePresenter
<
SimpleDelegator
include
Gitlab
::
View
::
Presenter
end
end
describe
'#initialize'
do
describe
'#initialize'
do
subject
do
context
'without optional parameters'
do
described_class
.
new
(
appearance
)
end
it
'takes a subject and optional params'
do
expect
{
subject
}.
not_to
raise_error
end
end
describe
'#fabricate!'
do
context
'without delegation'
do
subject
do
subject
do
described_class
.
new
(
appearance
).
fabricate!
described_class
.
new
(
variable
)
end
end
it
'
does not forward missing methods to subject
'
do
it
'
takes a subject and optional params
'
do
expect
{
subject
.
title
}.
to
raise_error
(
NoMethodError
)
expect
{
subject
}.
not_to
raise_error
end
end
end
end
context
'with
delegation
'
do
context
'with
optional parameters
'
do
subject
do
subject
do
described_class
.
new
(
broadcast_message
).
fabricate!
described_class
.
new
(
variable
,
user:
'user'
)
end
end
it
'
forwards missing methods to subject
'
do
it
'
takes a subject and optional params
'
do
expect
(
subject
.
message
).
to
eq
(
broadcast_message
.
message
)
expect
{
subject
}.
not_to
raise_error
end
end
end
end
end
end
describe
'#fabricate!'
do
subject
do
described_class
.
new
(
variable
,
user:
'user'
,
foo:
'bar'
).
fabricate!
end
it
'exposes given params'
do
expect
(
subject
.
user
).
to
eq
(
'user'
)
expect
(
subject
.
foo
).
to
eq
(
'bar'
)
end
it
'detects the presenter based on the given subject'
do
expect
(
subject
).
to
be_a
(
Ci
::
Variable
::
Presenter
)
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