Don't resubscribe if a user unsubscribes and comments again

This commit is contained in:
Yuki 2015-04-21 17:52:32 +05:30
parent 1c485b20e9
commit 8eeae22c43
5 changed files with 95 additions and 7 deletions

View file

@ -7,7 +7,7 @@ class Comment < ActiveRecord::Base
validates :content, length: { maximum: 160 } validates :content, length: { maximum: 160 }
after_create do after_create do
Subscription.subscribe self.user, answer Subscription.subscribe self.user, answer, false
Subscription.notify self, answer Subscription.notify self, answer
user.increment! :commented_count user.increment! :commented_count
answer.increment! :comment_count answer.increment! :comment_count

View file

@ -3,9 +3,21 @@ class Subscription < ActiveRecord::Base
belongs_to :answer belongs_to :answer
class << self class << self
def subscribe(recipient, target) def for(target)
if Subscription.find_by(user: recipient, answer: target).nil? 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! Subscription.new(user: recipient, answer: target).save!
elsif force
existing.update(is_active: true)
end end
end end
@ -15,7 +27,7 @@ class Subscription < ActiveRecord::Base
end end
subs = Subscription.find_by(user: recipient, answer: target) subs = Subscription.find_by(user: recipient, answer: target)
subs.destroy unless subs.nil? subs.update(is_active: false) unless subs.nil?
end end
def destruct(target) def destruct(target)
@ -25,12 +37,21 @@ class Subscription < ActiveRecord::Base
Subscription.where(answer: target).destroy_all Subscription.where(answer: target).destroy_all
end 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) def notify(source, target)
if source.nil? or target.nil? if source.nil? or target.nil?
return nil return nil
end 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 next unless not subs.user == source.user
Notification.notify subs.user, source Notification.notify subs.user, source
end end

View file

@ -21,7 +21,7 @@
%button.btn.btn-default.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} %button.btn.btn-default.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }}
%span.caret %span.caret
%ul.dropdown-menu.dropdown-menu-right{role: :menu} %ul.dropdown-menu.dropdown-menu-right{role: :menu}
- if a.subscriptions.find_by(user: current_user) - if Subscription.is_subscribed(current_user, a)
%li %li
-# fun joke should subscribe? -# fun joke should subscribe?
%a{href: '#', data: { a_id: a.id, action: 'ab-submarine', torpedo: "no" }} %a{href: '#', data: { a_id: a.id, action: 'ab-submarine', torpedo: "no" }}

View file

@ -0,0 +1,5 @@
class AddIsActiveToSubscriptions < ActiveRecord::Migration
def change
add_column :subscriptions, :is_active, :boolean, default: :true
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -101,6 +101,49 @@ ActiveRecord::Schema.define(version: 20150419201122) do
t.datetime "updated_at" t.datetime "updated_at"
end 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| create_table "questions", force: :cascade do |t|
t.string "content" t.string "content"
t.boolean "author_is_anonymous" 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", "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 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| create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false t.string "encrypted_password", default: "", null: false