From 8eeae22c437bf774731ea59716c525841375e8f3 Mon Sep 17 00:00:00 2001 From: Yuki Date: Tue, 21 Apr 2015 17:52:32 +0530 Subject: [PATCH] Don't resubscribe if a user unsubscribes and comments again --- app/models/comment.rb | 2 +- app/models/subscription.rb | 29 +++++++-- app/views/shared/_answerbox_buttons.html.haml | 2 +- ...21120557_add_is_active_to_subscriptions.rb | 5 ++ db/schema.rb | 64 ++++++++++++++++++- 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20150421120557_add_is_active_to_subscriptions.rb diff --git a/app/models/comment.rb b/app/models/comment.rb index 6cced397..ca8f4087 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -7,7 +7,7 @@ class Comment < ActiveRecord::Base validates :content, length: { maximum: 160 } after_create do - Subscription.subscribe self.user, answer + Subscription.subscribe self.user, answer, false Subscription.notify self, answer user.increment! :commented_count answer.increment! :comment_count diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 8a39ccad..bd250772 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -3,9 +3,21 @@ class Subscription < ActiveRecord::Base belongs_to :answer class << self - def subscribe(recipient, target) - if Subscription.find_by(user: recipient, answer: target).nil? + def for(target) + Subscription.where(answer: target) + end + + def is_subscribed(recipient, target) + existing = Subscription.find_by(user: recipient, answer: target) + existing.nil? or existing.is_active + end + + def subscribe(recipient, target, force = true) + existing = Subscription.find_by(user: recipient, answer: target) + if existing.nil? Subscription.new(user: recipient, answer: target).save! + elsif force + existing.update(is_active: true) end end @@ -15,7 +27,7 @@ class Subscription < ActiveRecord::Base end subs = Subscription.find_by(user: recipient, answer: target) - subs.destroy unless subs.nil? + subs.update(is_active: false) unless subs.nil? end def destruct(target) @@ -25,12 +37,21 @@ class Subscription < ActiveRecord::Base Subscription.where(answer: target).destroy_all end + def destruct_by(recipient, target) + if recipient.nil? or target.nil? + return nil + end + + subs = Subscription.find_by(user: recipient, answer: target) + subs.destroy unless subs.nil? + end + def notify(source, target) if source.nil? or target.nil? return nil end - Subscription.where(answer: target).each do |subs| + Subscription.where(answer: target, is_active: true).each do |subs| next unless not subs.user == source.user Notification.notify subs.user, source end diff --git a/app/views/shared/_answerbox_buttons.html.haml b/app/views/shared/_answerbox_buttons.html.haml index 28b15ead..1daa1f2a 100644 --- a/app/views/shared/_answerbox_buttons.html.haml +++ b/app/views/shared/_answerbox_buttons.html.haml @@ -21,7 +21,7 @@ %button.btn.btn-default.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} %span.caret %ul.dropdown-menu.dropdown-menu-right{role: :menu} - - if a.subscriptions.find_by(user: current_user) + - if Subscription.is_subscribed(current_user, a) %li -# fun joke should subscribe? %a{href: '#', data: { a_id: a.id, action: 'ab-submarine', torpedo: "no" }} diff --git a/db/migrate/20150421120557_add_is_active_to_subscriptions.rb b/db/migrate/20150421120557_add_is_active_to_subscriptions.rb new file mode 100644 index 00000000..afdfc81e --- /dev/null +++ b/db/migrate/20150421120557_add_is_active_to_subscriptions.rb @@ -0,0 +1,5 @@ +class AddIsActiveToSubscriptions < ActiveRecord::Migration + def change + add_column :subscriptions, :is_active, :boolean, default: :true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4e3d67ca..a60c6040 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150419201122) do +ActiveRecord::Schema.define(version: 20150421120557) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -101,6 +101,49 @@ ActiveRecord::Schema.define(version: 20150419201122) do t.datetime "updated_at" end + create_table "oauth_access_grants", force: :cascade do |t| + t.integer "resource_owner_id", null: false + t.integer "application_id", null: false + t.string "token", null: false + t.integer "expires_in", null: false + t.text "redirect_uri", null: false + t.datetime "created_at", null: false + t.datetime "revoked_at" + t.string "scopes" + end + + add_index "oauth_access_grants", ["token"], name: "index_oauth_access_grants_on_token", unique: true, using: :btree + + create_table "oauth_access_tokens", force: :cascade do |t| + t.integer "resource_owner_id" + t.integer "application_id" + t.string "token", null: false + t.string "refresh_token" + t.integer "expires_in" + t.datetime "revoked_at" + t.datetime "created_at", null: false + t.string "scopes" + end + + add_index "oauth_access_tokens", ["refresh_token"], name: "index_oauth_access_tokens_on_refresh_token", unique: true, using: :btree + add_index "oauth_access_tokens", ["resource_owner_id"], name: "index_oauth_access_tokens_on_resource_owner_id", using: :btree + add_index "oauth_access_tokens", ["token"], name: "index_oauth_access_tokens_on_token", unique: true, using: :btree + + create_table "oauth_applications", force: :cascade do |t| + t.string "name", null: false + t.string "uid", null: false + t.string "secret", null: false + t.text "redirect_uri", null: false + t.string "scopes", default: "", null: false + t.datetime "created_at" + t.datetime "updated_at" + t.integer "owner_id" + t.string "owner_type" + end + + add_index "oauth_applications", ["owner_id", "owner_type"], name: "index_oauth_applications_on_owner_id_and_owner_type", using: :btree + add_index "oauth_applications", ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree + create_table "questions", force: :cascade do |t| t.string "content" t.boolean "author_is_anonymous" @@ -156,6 +199,25 @@ ActiveRecord::Schema.define(version: 20150419201122) do add_index "smiles", ["user_id", "answer_id"], name: "index_smiles_on_user_id_and_answer_id", unique: true, using: :btree add_index "smiles", ["user_id"], name: "index_smiles_on_user_id", using: :btree + create_table "subscriptions", force: :cascade do |t| + t.integer "user_id" + t.integer "answer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "is_active", default: true + end + + create_table "user_themes", force: :cascade do |t| + t.integer "user_id" + t.string "bg", default: "#fafafa" + t.string "color", default: "#5e35b1" + t.string "color_a", default: "#5e35b1" + t.string "color_alt", default: "#eeeeee" + t.string "bg_primary", default: "#5e35b1" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false