Don't resubscribe if a user unsubscribes and comments again
This commit is contained in:
parent
1c485b20e9
commit
8eeae22c43
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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" }}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class AddIsActiveToSubscriptions < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :subscriptions, :is_active, :boolean, default: :true
|
||||
end
|
||||
end
|
64
db/schema.rb
64
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
|
||||
|
|
Loading…
Reference in New Issue