diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-11-14 20:42:22 -0200 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-11-17 15:10:13 -0200 |
commit | 0aac2e0706cd767993148826d723aa3641cbb2a4 (patch) | |
tree | d3f1708529146be7d3f27fd2227c96068062f37a /app/models | |
parent | 346f58249242c0ccf278175c78fc000976912771 (diff) | |
download | gitlab-ce-0aac2e0706cd767993148826d723aa3641cbb2a4.tar.gz |
Allow subscriptions to be created without a project
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/concerns/subscribable.rb | 21 | ||||
-rw-r--r-- | app/models/subscription.rb | 6 |
2 files changed, 17 insertions, 10 deletions
diff --git a/app/models/concerns/subscribable.rb b/app/models/concerns/subscribable.rb index 5f2725a6d7f..0723db548d8 100644 --- a/app/models/concerns/subscribable.rb +++ b/app/models/concerns/subscribable.rb @@ -12,7 +12,7 @@ module Subscribable has_many :subscriptions, dependent: :destroy, as: :subscribable end - def subscribed?(user, project) + def subscribed?(user, project = nil) if subscription = subscriptions.find_by(user: user, project: project) subscription.subscribed else @@ -27,20 +27,22 @@ module Subscribable end def subscribers(project) - subscriptions.where(project: project, subscribed: true).map(&:user) + subscriptions_available(project). + where(subscribed: true). + map(&:user) end - def toggle_subscription(user, project) + def toggle_subscription(user, project = nil) find_or_initialize_subscription(user, project). update(subscribed: !subscribed?(user, project)) end - def subscribe(user, project) + def subscribe(user, project = nil) find_or_initialize_subscription(user, project). update(subscribed: true) end - def unsubscribe(user, project) + def unsubscribe(user, project = nil) find_or_initialize_subscription(user, project). update(subscribed: false) end @@ -49,6 +51,13 @@ module Subscribable def find_or_initialize_subscription(user, project) subscriptions. - find_or_initialize_by(user_id: user.id, project_id: project.id) + find_or_initialize_by(user_id: user.id, project_id: project.try(:id)) + end + + def subscriptions_available(project) + t = Subscription.arel_table + + subscriptions. + where(t[:project_id].eq(nil).or(t[:project_id].eq(project.try(:id)))) end end diff --git a/app/models/subscription.rb b/app/models/subscription.rb index f881d999384..17869c8bac2 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -3,9 +3,7 @@ class Subscription < ActiveRecord::Base belongs_to :project belongs_to :subscribable, polymorphic: true - validates :user, :project, :subscribable, presence: true + validates :user, :subscribable, presence: true - validates :project_id, - uniqueness: { scope: [:subscribable_id, :subscribable_type, :user_id] }, - presence: true + validates :project_id, uniqueness: { scope: [:subscribable_id, :subscribable_type, :user_id] } end |