Skip to content

Instantly share code, notes, and snippets.

View CaDs's full-sized avatar
🌊
We are all waves of the same sea

Carlos Donderis CaDs

🌊
We are all waves of the same sea
View GitHub Profile
class PostsController < ApplicationController
before_action :auth_user
def index
@posts = @user.fetch_feed
end
end
class User < ApplicationRecord
def fetch_feed
class Connection < ApplicationRecord
belongs_to :follower, class_name: 'User', foreign_key: :follower_id
belongs_to :following, class_name: 'User', foreign_key: :following_id
end
@CaDs
CaDs / post.rb
Last active October 18, 2018 05:13
class Post < ApplicationRecord
belongs_to :user
after_create_commit :enqueue_delivery
class << self
def cache_key_for(id:)
"cached_post/#{id}"
end
@CaDs
CaDs / user.rb
Last active October 18, 2018 05:54
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
@CaDs
CaDs / partitions.cr
Created April 14, 2018 02:14
john_lynch_scrtip
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|