summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-10-31 15:40:27 -0200
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-11-15 16:08:21 -0200
commita9d161542aef9fb987b383aa50d1fd48ecd7dce3 (patch)
tree91163f368379f059cbcbe259ee743eaf64a84e75
parent4a4c57f7b8b1fdf5957ad1f2d78d96231eec8ff8 (diff)
downloadgitlab-ce-a9d161542aef9fb987b383aa50d1fd48ecd7dce3.tar.gz
Add project_id to subscriptions
-rw-r--r--app/models/subscription.rb3
-rw-r--r--db/migrate/20161031171301_add_project_id_to_subscriptions.rb14
-rw-r--r--db/schema.rb2
-rw-r--r--spec/models/subscription_spec.rb15
4 files changed, 34 insertions, 0 deletions
diff --git a/app/models/subscription.rb b/app/models/subscription.rb
index 3b8aa1eb866..f77aec0cacf 100644
--- a/app/models/subscription.rb
+++ b/app/models/subscription.rb
@@ -1,7 +1,10 @@
class Subscription < ActiveRecord::Base
belongs_to :user
+ belongs_to :project
belongs_to :subscribable, polymorphic: true
+ validates :user, :project, :subscribable, presence: true
+
validates :user_id,
uniqueness: { scope: [:subscribable_id, :subscribable_type] },
presence: true
diff --git a/db/migrate/20161031171301_add_project_id_to_subscriptions.rb b/db/migrate/20161031171301_add_project_id_to_subscriptions.rb
new file mode 100644
index 00000000000..97534679b59
--- /dev/null
+++ b/db/migrate/20161031171301_add_project_id_to_subscriptions.rb
@@ -0,0 +1,14 @@
+class AddProjectIdToSubscriptions < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ def up
+ add_column :subscriptions, :project_id, :integer
+ add_foreign_key :subscriptions, :projects, column: :project_id, on_delete: :cascade
+ end
+
+ def down
+ remove_column :subscriptions, :project_id
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 886be4520a3..fdd7f179788 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -1052,6 +1052,7 @@ ActiveRecord::Schema.define(version: 20161109150329) do
t.boolean "subscribed"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "project_id"
end
add_index "subscriptions", ["subscribable_id", "subscribable_type", "user_id"], name: "subscriptions_user_id_and_ref_fields", unique: true, using: :btree
@@ -1250,6 +1251,7 @@ ActiveRecord::Schema.define(version: 20161109150329) do
add_foreign_key "personal_access_tokens", "users"
add_foreign_key "protected_branch_merge_access_levels", "protected_branches"
add_foreign_key "protected_branch_push_access_levels", "protected_branches"
+ add_foreign_key "subscriptions", "projects", on_delete: :cascade
add_foreign_key "trending_projects", "projects", on_delete: :cascade
add_foreign_key "u2f_registrations", "users"
end
diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb
new file mode 100644
index 00000000000..6cd6e01d0c7
--- /dev/null
+++ b/spec/models/subscription_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe Subscription, models: true do
+ describe 'relationships' do
+ it { is_expected.to belong_to(:project) }
+ it { is_expected.to belong_to(:subscribable) }
+ it { is_expected.to belong_to(:user) }
+ end
+
+ describe 'validations' do
+ it { is_expected.to validate_presence_of(:project) }
+ it { is_expected.to validate_presence_of(:subscribable) }
+ it { is_expected.to validate_presence_of(:user) }
+ end
+end