2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 21:27:52 -04:00
|
|
|
class UnsubscribeKey < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :topic
|
|
|
|
|
|
|
|
before_create :generate_random_key
|
|
|
|
|
|
|
|
def self.create_key_for(user, type)
|
|
|
|
if Post === type
|
|
|
|
create(user_id: user.id, unsubscribe_key_type: "topic", topic_id: type.topic_id, post_id: type.id).key
|
|
|
|
else
|
|
|
|
create(user_id: user.id, unsubscribe_key_type: type).key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.user_for_key(key)
|
|
|
|
where(key: key).first.try(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def generate_random_key
|
|
|
|
self.key = SecureRandom.hex(32)
|
|
|
|
end
|
2016-06-16 21:27:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: unsubscribe_keys
|
|
|
|
#
|
|
|
|
# key :string(64) not null, primary key
|
|
|
|
# user_id :integer not null
|
2019-01-11 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2016-06-16 21:27:52 -04:00
|
|
|
# unsubscribe_key_type :string
|
|
|
|
# topic_id :integer
|
2016-07-22 17:27:30 -04:00
|
|
|
# post_id :integer
|
2016-06-16 21:27:52 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_unsubscribe_keys_on_created_at (created_at)
|
|
|
|
#
|