rename table subscribe; make it polymorfic

This commit is contained in:
Valery Sizov 2015-03-16 15:22:50 +02:00
parent 0e20dc910f
commit 410d25c8ca
11 changed files with 41 additions and 44 deletions

View File

@ -100,7 +100,7 @@ class Projects::IssuesController < Projects::ApplicationController
def set_subscription
subscribed = params[:subscription] == "Subscribe"
sub = @issue.subscribes.find_or_create_by(user_id: current_user.id)
sub = @issue.subscriptions.find_or_create_by(user_id: current_user.id)
sub.update(subscribed: subscribed)
render nothing: true

View File

@ -177,7 +177,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
def set_subscription
subscribed = params[:subscription] == "Subscribe"
sub = @merge_request.subscribes.find_or_create_by(user_id: current_user.id)
sub = @merge_request.subscriptions.find_or_create_by(user_id: current_user.id)
sub.update(subscribed: subscribed)
render nothing: true

View File

@ -15,7 +15,7 @@ module Issuable
has_many :notes, as: :noteable, dependent: :destroy
has_many :label_links, as: :target, dependent: :destroy
has_many :labels, through: :label_links
has_many :subscribes, dependent: :destroy
has_many :subscriptions, dependent: :destroy, as: :subscribable
validates :author, presence: true
validates :title, presence: true, length: { within: 0..255 }
@ -133,10 +133,11 @@ module Issuable
users.concat(mentions.reduce([], :|)).uniq
end
def subscribe_status(user)
subscribe = subscribes.find_by_user_id(user.id)
if subscribe
return subscribe.subscribed
def subscription_status(user)
subscription = subscriptions.find_by_user_id(user.id)
if subscription
return subscription.subscribed
end
participants.include?(user)

View File

@ -1,6 +0,0 @@
class Subscribe < ActiveRecord::Base
belongs_to :user
validates :issue_id, uniqueness: { scope: :user_id, allow_nil: true }
validates :merge_request_id, uniqueness: { scope: :user_id, allow_nil: true }
end

View File

@ -0,0 +1,7 @@
class Subscription < ActiveRecord::Base
belongs_to :subscribable, polymorphic: true
validates :user_id,
uniqueness: { scope: [:subscribable_id, :subscribable_type]},
presence: true
end

View File

@ -316,8 +316,8 @@ class NotificationService
def reject_unsubscribed_users(recipients, target)
recipients.reject do |user|
subscribe = target.subscribes.find_by_user_id(user.id)
subscribe && !subscribe.subscribed
subscription = target.subscriptions.find_by_user_id(user.id)
subscription && !subscription.subscribed
end
end
@ -375,9 +375,9 @@ class NotificationService
end
def add_subscribed_users(recipients, target)
subscribes = target.subscribes
if subscribes.any?
recipients.merge(subscribes.where("subscribed is true").map(&:user))
subscriptions = target.subscriptions
if subscriptions.any?
recipients.merge(subscriptions.where("subscribed is true").map(&:user))
else
recipients
end

View File

@ -33,8 +33,8 @@
Subscription:
%i.fa.fa-spinner.fa-spin.hidden.subscription
%span.sub_status
= @issue.subscribe_status(current_user) ? "subscribed" : "unsubscribed"
- subscribe_action = @issue.subscribe_status(current_user) ? "Unsubscribe" : "Subscribe"
= @issue.subscription_status(current_user) ? "subscribed" : "unsubscribed"
- subscribe_action = @issue.subscription_status(current_user) ? "Unsubscribe" : "Subscribe"
%input.btn.subscribe-button{:type => "button", :value => subscribe_action}
:coffeescript

View File

@ -35,8 +35,8 @@
Subscription:
%i.fa.fa-spinner.fa-spin.hidden.subscription
%span.sub_status
= @merge_request.subscribe_status(current_user) ? "subscribed" : "unsubscribed"
- subscribe_action = @merge_request.subscribe_status(current_user) ? "Unsubscribe" : "Subscribe"
= @merge_request.subscription_status(current_user) ? "subscribed" : "unsubscribed"
- subscribe_action = @merge_request.subscription_status(current_user) ? "Unsubscribe" : "Subscribe"
%input.btn.subscribe-button{:type => "button", :value => subscribe_action}
:coffeescript

View File

@ -1,16 +0,0 @@
class CreateSubscribesTable < ActiveRecord::Migration
def change
create_table :subscribes do |t|
t.integer :user_id
t.integer :merge_request_id
t.integer :issue_id
t.boolean :subscribed
t.timestamps
end
add_index :subscribes, :user_id
add_index :subscribes, :issue_id
add_index :subscribes, :merge_request_id
end
end

View File

@ -0,0 +1,13 @@
class CreateSubscriptionsTable < ActiveRecord::Migration
def change
create_table :subscriptions do |t|
t.integer :user_id
t.references :subscribable, polymorphic: true
t.boolean :subscribed
t.timestamps
end
add_index :subscriptions, [:subscribable_id, :subscribable_type, :user_id], unique: true, name: 'subscriptions_user_id_and_ref_fields'
end
end

View File

@ -397,18 +397,16 @@ ActiveRecord::Schema.define(version: 20150313012111) do
add_index "snippets", ["project_id"], name: "index_snippets_on_project_id", using: :btree
add_index "snippets", ["visibility_level"], name: "index_snippets_on_visibility_level", using: :btree
create_table "subscribes", force: true do |t|
create_table "subscriptions", force: true do |t|
t.integer "user_id"
t.integer "merge_request_id"
t.integer "issue_id"
t.integer "subscribable_id"
t.string "subscribable_type"
t.boolean "subscribed"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "subscribes", ["issue_id"], name: "index_subscribes_on_issue_id", using: :btree
add_index "subscribes", ["merge_request_id"], name: "index_subscribes_on_merge_request_id", using: :btree
add_index "subscribes", ["user_id"], name: "index_subscribes_on_user_id", using: :btree
add_index "subscriptions", ["subscribable_id", "subscribable_type", "user_id"], name: "subscriptions_user_id_and_ref_fields", unique: true, using: :btree
create_table "taggings", force: true do |t|
t.integer "tag_id"