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
Jérome Perrin
gitlab-ce
Commits
9a2157a6
Commit
9a2157a6
authored
Oct 31, 2016
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring Subscribable concern to accept a project
parent
de629eaa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
38 deletions
+46
-38
app/models/concerns/issuable.rb
app/models/concerns/issuable.rb
+1
-1
app/models/concerns/subscribable.rb
app/models/concerns/subscribable.rb
+22
-16
spec/models/concerns/subscribable_spec.rb
spec/models/concerns/subscribable_spec.rb
+23
-21
No files found.
app/models/concerns/issuable.rb
View file @
9a2157a6
...
...
@@ -215,7 +215,7 @@ module Issuable
end
end
def
subscribed_without_subscriptions?
(
user
)
def
subscribed_without_subscriptions?
(
user
,
project
)
participants
(
user
).
include?
(
user
)
end
...
...
app/models/concerns/subscribable.rb
View file @
9a2157a6
...
...
@@ -12,39 +12,45 @@ module Subscribable
has_many
:subscriptions
,
dependent: :destroy
,
as: :subscribable
end
def
subscribed?
(
user
)
if
subscription
=
subscriptions
.
find_by
_user_id
(
user
.
id
)
def
subscribed?
(
user
,
to_project
=
nil
)
if
subscription
=
subscriptions
.
find_by
(
user:
user
,
project:
(
to_project
||
project
)
)
subscription
.
subscribed
else
subscribed_without_subscriptions?
(
user
)
subscribed_without_subscriptions?
(
user
,
to_project
)
end
end
# Override this method to define custom logic to consider a subscribable as
# subscribed without an explicit subscription record.
def
subscribed_without_subscriptions?
(
user
)
def
subscribed_without_subscriptions?
(
user
,
to_project
=
nil
)
false
end
def
subscribers
subscriptions
.
where
(
subscribed:
true
).
map
(
&
:user
)
def
subscribers
(
to_project
=
nil
)
subscriptions
.
where
(
project:
(
to_project
||
project
),
subscribed:
true
).
map
(
&
:user
)
end
def
toggle_subscription
(
user
)
subscriptions
.
find_or_initialize_by
(
user_id:
user
.
id
).
update
(
subscribed:
!
subscribed?
(
user
))
def
toggle_subscription
(
user
,
to_project
=
nil
)
subscribed
=
subscribed?
(
user
,
(
to_project
||
project
))
find_or_initialize_subscription
(
user
,
to_project
).
update
(
subscribed:
!
subscribed
)
end
def
subscribe
(
user
)
subscriptions
.
find_or_initialize_by
(
user_id:
user
.
id
).
def
subscribe
(
user
,
to_project
=
nil
)
find_or_initialize_subscription
(
user
,
to_project
).
update
(
subscribed:
true
)
end
def
unsubscribe
(
user
)
subscriptions
.
find_or_initialize_by
(
user_id:
user
.
id
).
def
unsubscribe
(
user
,
to_project
=
nil
)
find_or_initialize_subscription
(
user
,
to_project
).
update
(
subscribed:
false
)
end
private
def
find_or_initialize_subscription
(
user
,
to_project
=
nil
)
subscriptions
.
find_or_initialize_by
(
user_id:
user
.
id
,
project_id:
(
to_project
||
project
).
id
)
end
end
spec/models/concerns/subscribable_spec.rb
View file @
9a2157a6
require
'spec_helper'
describe
Subscribable
,
'Subscribable'
do
let
(
:resource
)
{
create
(
:issue
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:resource
)
{
create
(
:issue
,
project:
project
)
}
let
(
:user
)
{
create
(
:user
)
}
describe
'#subscribed?'
do
it
'returns false when no subcription exists'
do
expect
(
resource
.
subscribed?
(
user
)).
to
be_falsey
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_falsey
end
it
'returns true when a subcription exists and subscribed is true'
do
resource
.
subscriptions
.
create
(
user:
user
,
subscribed:
true
)
resource
.
subscriptions
.
create
(
user:
user
,
project:
project
,
subscribed:
true
)
expect
(
resource
.
subscribed?
(
user
)).
to
be_truthy
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_truthy
end
it
'returns false when a subcription exists and subscribed is false'
do
resource
.
subscriptions
.
create
(
user:
user
,
subscribed:
false
)
resource
.
subscriptions
.
create
(
user:
user
,
project:
project
,
subscribed:
false
)
expect
(
resource
.
subscribed?
(
user
)).
to
be_falsey
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_falsey
end
end
describe
'#subscribers'
do
it
'returns [] when no subcribers exists'
do
expect
(
resource
.
subscribers
).
to
be_empty
expect
(
resource
.
subscribers
(
project
)
).
to
be_empty
end
it
'returns the subscribed users'
do
resource
.
subscriptions
.
create
(
user:
user
,
subscribed:
true
)
resource
.
subscriptions
.
create
(
user:
create
(
:user
),
subscribed:
false
)
resource
.
subscriptions
.
create
(
user:
user
,
project:
project
,
subscribed:
true
)
resource
.
subscriptions
.
create
(
user:
create
(
:user
),
project:
project
,
subscribed:
false
)
expect
(
resource
.
subscribers
).
to
eq
[
user
]
expect
(
resource
.
subscribers
(
project
)
).
to
eq
[
user
]
end
end
describe
'#toggle_subscription'
do
it
'toggles the current subscription state for the given user'
do
expect
(
resource
.
subscribed?
(
user
)).
to
be_falsey
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_falsey
resource
.
toggle_subscription
(
user
)
resource
.
toggle_subscription
(
user
,
project
)
expect
(
resource
.
subscribed?
(
user
)).
to
be_truthy
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_truthy
end
end
describe
'#subscribe'
do
it
'subscribes the given user'
do
expect
(
resource
.
subscribed?
(
user
)).
to
be_falsey
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_falsey
resource
.
subscribe
(
user
)
resource
.
subscribe
(
user
,
project
)
expect
(
resource
.
subscribed?
(
user
)).
to
be_truthy
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_truthy
end
end
describe
'#unsubscribe'
do
it
'unsubscribes the given current user'
do
resource
.
subscriptions
.
create
(
user:
user
,
subscribed:
true
)
expect
(
resource
.
subscribed?
(
user
)).
to
be_truthy
resource
.
subscriptions
.
create
(
user:
user
,
project:
project
,
subscribed:
true
)
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_truthy
resource
.
unsubscribe
(
user
)
resource
.
unsubscribe
(
user
,
project
)
expect
(
resource
.
subscribed?
(
user
)).
to
be_falsey
expect
(
resource
.
subscribed?
(
user
,
project
)).
to
be_falsey
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