This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
before_action :auth_user | |
def index | |
@posts = @user.fetch_feed | |
end | |
end | |
class User < ApplicationRecord | |
def fetch_feed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Connection < ApplicationRecord | |
belongs_to :follower, class_name: 'User', foreign_key: :follower_id | |
belongs_to :following, class_name: 'User', foreign_key: :following_id | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ApplicationRecord | |
belongs_to :user | |
after_create_commit :enqueue_delivery | |
class << self | |
def cache_key_for(id:) | |
"cached_post/#{id}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ApplicationRecord | |
has_many :posts, dependent: :destroy | |
has_many :connections, dependent: :destroy | |
has_many :follower_connections, foreign_key: :following_id, class_name: 'Connection' | |
has_many :followers, through: :follower_connections, source: :follower | |
has_many :following_connections, foreign_key: :follower_id, class_name: 'Connection' | |
has_many :following, through: :following_connections, source: :following |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_partition_original(ss) | |
return (ss.size == 0 ? [[[] of Int32]] : [[ss]]) if ss.size <= 1 | |
out = [[Array(Int32).new, Array(Int32).new]].clear | |
(0...2**ss.size / 2).each { |i| | |
parts = [Array(Int32).new, Array(Int32).new] | |
ss.each { |item| | |
parts[i&1] << item | |
i >>= 1 | |
} | |
get_partition_original(parts[1]).each { |b| |