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
Boxiang Sun
gitlab-ce
Commits
bf789ff5
Commit
bf789ff5
authored
Jan 09, 2017
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve presenter architecture
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
fd72c0f4
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
288 additions
and
84 deletions
+288
-84
app/presenters/build_presenter.rb
app/presenters/build_presenter.rb
+0
-15
app/presenters/ci/build/presenter.rb
app/presenters/ci/build/presenter.rb
+17
-0
app/presenters/ci/variable/presenter.rb
app/presenters/ci/variable/presenter.rb
+7
-0
app/presenters/variable_presenter.rb
app/presenters/variable_presenter.rb
+0
-5
lib/gitlab/view/presenter/base.rb
lib/gitlab/view/presenter/base.rb
+26
-0
lib/gitlab/view/presenter/delegated.rb
lib/gitlab/view/presenter/delegated.rb
+19
-0
lib/gitlab/view/presenter/simple.rb
lib/gitlab/view/presenter/simple.rb
+17
-0
spec/lib/gitlab/view/presenter/base_spec.rb
spec/lib/gitlab/view/presenter/base_spec.rb
+38
-0
spec/lib/gitlab/view/presenter/delegated_spec.rb
spec/lib/gitlab/view/presenter/delegated_spec.rb
+33
-0
spec/lib/gitlab/view/presenter/simple_spec.rb
spec/lib/gitlab/view/presenter/simple_spec.rb
+33
-0
spec/lib/gitlab/view/presenter_spec.rb
spec/lib/gitlab/view/presenter_spec.rb
+0
-29
spec/presenters/build_presenter_spec.rb
spec/presenters/build_presenter_spec.rb
+0
-35
spec/presenters/ci/build/presenter_spec.rb
spec/presenters/ci/build/presenter_spec.rb
+75
-0
spec/presenters/ci/variable/presenter_spec.rb
spec/presenters/ci/variable/presenter_spec.rb
+23
-0
No files found.
app/presenters/build_presenter.rb
deleted
100644 → 0
View file @
fd72c0f4
class
BuildPresenter
<
SimpleDelegator
include
Gitlab
::
View
::
Presenter
presents
:build
def
erased_by_user?
# Build can be erased through API, therefore it does not have
# `erase_by` user assigned in that case.
erased?
&&
erased_by
end
def
self
.
ancestors
super
+
[
Ci
::
Build
]
end
end
app/presenters/ci/build/presenter.rb
0 → 100644
View file @
bf789ff5
module
Ci
class
Build
class
Presenter
<
Gitlab
::
View
::
Presenter
::
Delegated
presents
:build
def
erased_by_user?
# Build can be erased through API, therefore it does not have
# `erase_by` user assigned in that case.
erased?
&&
erased_by
end
def
erased_by_name
erased_by
.
name
if
erased_by
end
end
end
end
app/presenters/ci/variable/presenter.rb
0 → 100644
View file @
bf789ff5
module
Ci
class
Variable
class
Presenter
<
Gitlab
::
View
::
Presenter
::
Simple
presents
:variable
end
end
end
app/presenters/variable_presenter.rb
deleted
100644 → 0
View file @
fd72c0f4
class
VariablePresenter
include
Gitlab
::
View
::
Presenter
presents
:variable
end
lib/gitlab/view/presenter.rb
→
lib/gitlab/view/presenter
/base
.rb
View file @
bf789ff5
module
Gitlab
module
View
module
Presenter
extend
ActiveSupport
::
Concern
module
Base
extend
ActiveSupport
::
Concern
included
do
include
Gitlab
::
Routing
include
Gitlab
::
Allowable
end
def
with_subject
(
subject
)
tap
{
@subject
=
subject
}
end
attr_reader
:subject
def
with_user
(
user
)
tap
{
@user
=
user
}
end
private
def
can?
(
user
,
action
)
super
(
user
,
action
,
subject
)
end
attr_reader
:subject
,
:user
private
class_methods
do
def
presents
(
name
)
define_method
(
name
)
do
subject
class_methods
do
def
presents
(
name
)
define_method
(
name
)
{
subject
}
end
end
end
...
...
lib/gitlab/view/presenter/delegated.rb
0 → 100644
View file @
bf789ff5
module
Gitlab
module
View
module
Presenter
class
Delegated
<
SimpleDelegator
include
Gitlab
::
View
::
Presenter
::
Base
def
initialize
(
subject
,
**
attributes
)
@subject
=
subject
attributes
.
each
do
|
key
,
value
|
define_singleton_method
(
key
)
{
value
}
end
super
(
subject
)
end
end
end
end
end
lib/gitlab/view/presenter/simple.rb
0 → 100644
View file @
bf789ff5
module
Gitlab
module
View
module
Presenter
class
Simple
include
Gitlab
::
View
::
Presenter
::
Base
def
initialize
(
subject
,
**
attributes
)
@subject
=
subject
attributes
.
each
do
|
key
,
value
|
define_singleton_method
(
key
)
{
value
}
end
end
end
end
end
end
spec/lib/gitlab/view/presenter/base_spec.rb
0 → 100644
View file @
bf789ff5
require
'spec_helper'
describe
Gitlab
::
View
::
Presenter
::
Base
do
let
(
:project
)
{
double
(
:project
)
}
let
(
:presenter_class
)
do
Struct
.
new
(
:subject
).
include
(
described_class
)
end
subject
do
presenter_class
.
new
(
project
)
end
describe
'.presents'
do
it
'exposes #subject with the given keyword'
do
presenter_class
.
presents
(
:foo
)
expect
(
subject
.
foo
).
to
eq
(
project
)
end
end
describe
'#can?'
do
let
(
:project
)
{
create
(
:empty_project
)
}
context
'user is not allowed'
do
it
'returns false'
do
expect
(
subject
.
can?
(
nil
,
:read_project
)).
to
be_falsy
end
end
context
'user is allowed'
do
let
(
:project
)
{
create
(
:empty_project
,
:public
)
}
it
'returns true'
do
expect
(
subject
.
can?
(
nil
,
:read_project
)).
to
be_truthy
end
end
end
end
spec/lib/gitlab/view/presenter/delegated_spec.rb
0 → 100644
View file @
bf789ff5
require
'spec_helper'
describe
Gitlab
::
View
::
Presenter
::
Delegated
do
let
(
:project
)
{
double
(
:project
,
foo:
'bar'
)
}
let
(
:presenter_class
)
do
Class
.
new
(
described_class
)
end
subject
do
presenter_class
.
new
(
project
)
end
it
'includes Gitlab::View::Presenter::Base'
do
expect
(
described_class
).
to
include
(
Gitlab
::
View
::
Presenter
::
Base
)
end
describe
'#initialize'
do
subject
do
presenter_class
.
new
(
project
,
user:
'user'
,
foo:
'bar'
)
end
it
'takes arbitrary key/values and exposes them'
do
expect
(
subject
.
user
).
to
eq
(
'user'
)
expect
(
subject
.
foo
).
to
eq
(
'bar'
)
end
end
describe
'delegation'
do
it
'does not forward missing methods to subject'
do
expect
(
subject
.
foo
).
to
eq
(
'bar'
)
end
end
end
spec/lib/gitlab/view/presenter/simple_spec.rb
0 → 100644
View file @
bf789ff5
require
'spec_helper'
describe
Gitlab
::
View
::
Presenter
::
Simple
do
let
(
:project
)
{
double
(
:project
)
}
let
(
:presenter_class
)
do
Class
.
new
(
described_class
)
end
subject
do
presenter_class
.
new
(
project
)
end
it
'includes Gitlab::View::Presenter::Base'
do
expect
(
described_class
).
to
include
(
Gitlab
::
View
::
Presenter
::
Base
)
end
describe
'#initialize'
do
subject
do
presenter_class
.
new
(
project
,
user:
'user'
,
foo:
'bar'
)
end
it
'takes arbitrary key/values and exposes them'
do
expect
(
subject
.
user
).
to
eq
(
'user'
)
expect
(
subject
.
foo
).
to
eq
(
'bar'
)
end
end
describe
'delegation'
do
it
'does not forward missing methods to subject'
do
expect
{
subject
.
foo
}.
to
raise_error
(
NoMethodError
)
end
end
end
spec/lib/gitlab/view/presenter_spec.rb
deleted
100644 → 0
View file @
fd72c0f4
require
'spec_helper'
describe
Gitlab
::
View
::
Presenter
do
let
(
:project
)
{
double
(
:project
,
bar:
'baz!'
)
}
let
(
:presenter
)
do
base_presenter
=
described_class
Class
.
new
do
include
base_presenter
presents
:foo
end
end
subject
do
presenter
.
new
.
with_subject
(
project
)
end
describe
'#initialize'
do
it
'takes an object accessible via a reader'
do
expect
(
subject
.
foo
).
to
eq
(
project
)
end
end
describe
'common helpers'
do
it
'responds to #can?'
do
expect
(
subject
).
to
respond_to
(
:can?
)
end
end
end
spec/presenters/build_presenter_spec.rb
deleted
100644 → 0
View file @
fd72c0f4
require
'spec_helper'
describe
BuildPresenter
do
let
(
:build
)
{
create
(
:ci_build
)
}
subject
do
described_class
.
new
(
build
).
with_subject
(
build
)
end
describe
'#initialize'
do
it
'takes a build and optional params'
do
expect
{
subject
}.
not_to
raise_error
end
it
'exposes build'
do
expect
(
subject
.
build
).
to
eq
(
build
)
end
it
'forwards missing methods to build'
do
expect
(
subject
.
ref
).
to
eq
(
'master'
)
end
end
describe
'#erased_by_user?'
do
it
'takes a build and optional params'
do
expect
(
subject
).
not_to
be_erased_by_user
end
end
describe
'quack like a Ci::Build'
do
it
'takes a build and optional params'
do
expect
(
described_class
.
ancestors
).
to
include
(
Ci
::
Build
)
end
end
end
spec/presenters/ci/build/presenter_spec.rb
0 → 100644
View file @
bf789ff5
require
'spec_helper'
describe
Ci
::
Build
::
Presenter
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:pipeline
)
{
create
(
:ci_pipeline
,
project:
project
)
}
let
(
:build
)
{
create
(
:ci_build
,
pipeline:
pipeline
)
}
subject
do
described_class
.
new
(
build
)
end
it
'inherits from Gitlab::View::Presenter::Delegated'
do
expect
(
described_class
.
superclass
).
to
eq
(
Gitlab
::
View
::
Presenter
::
Delegated
)
end
describe
'#initialize'
do
it
'takes a build and optional params'
do
expect
{
subject
}.
not_to
raise_error
end
it
'exposes build'
do
expect
(
subject
.
build
).
to
eq
(
build
)
end
it
'forwards missing methods to build'
do
expect
(
subject
.
ref
).
to
eq
(
'master'
)
end
end
describe
'#erased_by_user?'
do
it
'takes a build and optional params'
do
expect
(
subject
).
not_to
be_erased_by_user
end
end
describe
'#erased_by_name'
do
context
'when build is not erased'
do
before
do
expect
(
build
).
to
receive
(
:erased_by
).
and_return
(
nil
)
end
it
'returns nil'
do
expect
(
subject
.
erased_by_name
).
to
be_nil
end
end
context
'when build is erased'
do
before
do
expect
(
build
).
to
receive
(
:erased_by
).
twice
.
and_return
(
double
(
:user
,
name:
'John Doe'
))
end
it
'returns the name of the eraser'
do
expect
(
subject
.
erased_by_name
).
to
eq
(
'John Doe'
)
end
end
end
describe
'quack like a Ci::Build permission-wise'
do
context
'user is not allowed'
do
let
(
:project
)
{
create
(
:empty_project
,
public_builds:
false
)
}
it
'returns false'
do
expect
(
subject
.
can?
(
nil
,
:read_build
)).
to
be_falsy
end
end
context
'user is allowed'
do
let
(
:project
)
{
create
(
:empty_project
,
:public
)
}
it
'returns true'
do
expect
(
subject
.
can?
(
nil
,
:read_build
)).
to
be_truthy
end
end
end
end
spec/presenters/
variable_
presenter_spec.rb
→
spec/presenters/
ci/variable/
presenter_spec.rb
View file @
bf789ff5
require
'spec_helper'
describe
VariablePresenter
do
let
(
:variable
)
{
double
(
:variable
,
foo:
'bar'
)
}
describe
Ci
::
Variable
::
Presenter
do
let
(
:variable
)
{
double
(
:variable
)
}
subject
do
described_class
.
new
.
with_subject
(
variable
)
described_class
.
new
(
variable
)
end
it
'inherits from Gitlab::View::Presenter::Simple'
do
expect
(
described_class
.
superclass
).
to
eq
(
Gitlab
::
View
::
Presenter
::
Simple
)
end
describe
'#initialize'
do
it
'takes a variable and optional params'
do
expect
{
subject
}.
not_to
raise_error
expect
{
subject
}.
not_to
raise_error
end
it
'exposes variable'
do
expect
(
subject
.
variable
).
to
eq
(
variable
)
end
it
'does not forward missing methods to variable'
do
expect
{
subject
.
foo
}.
to
raise_error
(
NoMethodError
)
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