Refactoring Subscribable concern to accept a project

parent de629eaa
...@@ -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
......
...@@ -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
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment