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
Tatuya Kamada
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
Show 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
...
@@ -215,7 +215,7 @@ module Issuable
end
end
end
end
def
subscribed_without_subscriptions?
(
user
)
def
subscribed_without_subscriptions?
(
user
,
project
)
participants
(
user
).
include?
(
user
)
participants
(
user
).
include?
(
user
)
end
end
...
...
app/models/concerns/subscribable.rb
View file @
9a2157a6
...
@@ -12,39 +12,45 @@ module Subscribable
...
@@ -12,39 +12,45 @@ module Subscribable
has_many
:subscriptions
,
dependent: :destroy
,
as: :subscribable
has_many
:subscriptions
,
dependent: :destroy
,
as: :subscribable
end
end
def
subscribed?
(
user
)
def
subscribed?
(
user
,
to_project
=
nil
)
if
subscription
=
subscriptions
.
find_by
_user_id
(
user
.
id
)
if
subscription
=
subscriptions
.
find_by
(
user:
user
,
project:
(
to_project
||
project
)
)
subscription
.
subscribed
subscription
.
subscribed
else
else
subscribed_without_subscriptions?
(
user
)
subscribed_without_subscriptions?
(
user
,
to_project
)
end
end
end
end
# Override this method to define custom logic to consider a subscribable as
# Override this method to define custom logic to consider a subscribable as
# subscribed without an explicit subscription record.
# subscribed without an explicit subscription record.
def
subscribed_without_subscriptions?
(
user
)
def
subscribed_without_subscriptions?
(
user
,
to_project
=
nil
)
false
false
end
end
def
subscribers
def
subscribers
(
to_project
=
nil
)
subscriptions
.
where
(
subscribed:
true
).
map
(
&
:user
)
subscriptions
.
where
(
project:
(
to_project
||
project
),
subscribed:
true
).
map
(
&
:user
)
end
end
def
toggle_subscription
(
user
)
def
toggle_subscription
(
user
,
to_project
=
nil
)
subscriptions
.
subscribed
=
subscribed?
(
user
,
(
to_project
||
project
))
find_or_initialize_by
(
user_id:
user
.
id
).
update
(
subscribed:
!
subscribed?
(
user
))
find_or_initialize_subscription
(
user
,
to_project
).
update
(
subscribed:
!
subscribed
)
end
end
def
subscribe
(
user
)
def
subscribe
(
user
,
to_project
=
nil
)
subscriptions
.
find_or_initialize_subscription
(
user
,
to_project
).
find_or_initialize_by
(
user_id:
user
.
id
).
update
(
subscribed:
true
)
update
(
subscribed:
true
)
end
end
def
unsubscribe
(
user
)
def
unsubscribe
(
user
,
to_project
=
nil
)
subscriptions
.
find_or_initialize_subscription
(
user
,
to_project
).
find_or_initialize_by
(
user_id:
user
.
id
).
update
(
subscribed:
false
)
update
(
subscribed:
false
)
end
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
end
spec/models/concerns/subscribable_spec.rb
View file @
9a2157a6
require
'spec_helper'
require
'spec_helper'
describe
Subscribable
,
'Subscribable'
do
describe
Subscribable
,
'Subscribable'
do
let
(
:resource
)
{
create
(
:issue
)
}
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:resource
)
{
create
(
:issue
,
project:
project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
describe
'#subscribed?'
do
describe
'#subscribed?'
do
it
'returns false when no subcription exists'
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
end
it
'returns true when a subcription exists and subscribed is true'
do
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
end
it
'returns false when a subcription exists and subscribed is false'
do
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
end
end
describe
'#subscribers'
do
describe
'#subscribers'
do
it
'returns [] when no subcribers exists'
do
it
'returns [] when no subcribers exists'
do
expect
(
resource
.
subscribers
).
to
be_empty
expect
(
resource
.
subscribers
(
project
)
).
to
be_empty
end
end
it
'returns the subscribed users'
do
it
'returns the subscribed users'
do
resource
.
subscriptions
.
create
(
user:
user
,
subscribed:
true
)
resource
.
subscriptions
.
create
(
user:
user
,
project:
project
,
subscribed:
true
)
resource
.
subscriptions
.
create
(
user:
create
(
:user
),
subscribed:
false
)
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
end
end
describe
'#toggle_subscription'
do
describe
'#toggle_subscription'
do
it
'toggles the current subscription state for the given user'
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
end
end
describe
'#subscribe'
do
describe
'#subscribe'
do
it
'subscribes the given user'
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
end
end
describe
'#unsubscribe'
do
describe
'#unsubscribe'
do
it
'unsubscribes the given current user'
do
it
'unsubscribes the given current user'
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
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
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