Skip to content

Instantly share code, notes, and snippets.

@CaDs
Last active October 18, 2018 05:13
Show Gist options
  • Save CaDs/62e4807e4895b1b4d9f9ded3721508e1 to your computer and use it in GitHub Desktop.
Save CaDs/62e4807e4895b1b4d9f9ded3721508e1 to your computer and use it in GitHub Desktop.
class Post < ApplicationRecord
belongs_to :user
after_create_commit :enqueue_delivery
class << self
def cache_key_for(id:)
"cached_post/#{id}"
end
def id_from_cache_key(key:)
key.split('/').last
end
end
def enqueue_delivery
PostDeliveryJob.perform_async(post_id: id)
end
def deliver_to_followers
recipients = user.followers + [user]
recipients.each do |follower|
follower.add_to_cache(post: self)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment