This file contains hidden or 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
# Teams with a subscription are issued a set of credits every month, based on their plan level. | |
# Those credits can be used to “pay” for various types of reports. Small reports may cost 1 credit, large reports may cost 500 credits. | |
# Reports may also vary in cost, depending on the contents and configured options at time of initiation. | |
# Credits have an expiration date, based on when they were issued. When expired, credits can no longer be used to “pay” for reports. | |
# When reports are “paid” for with credits, the system should use credits that are closest to expiration. |
This file contains hidden or 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
# Using Oaken like factories | |
# | |
# 1. only include `db:seed` seeds in development: | |
# db/seeds/development/**/* | |
# db/seeds.rb | |
Oaken.prepare do | |
# 2. Then setup any common defaults | |
defaults name: -> { Faker::Name.name } | |
accounts.defaults name: -> { Faker::Business.name } |
This file contains hidden or 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
require "bundler/inline" | |
gemfile do | |
gem "activerecord", require: "active_record" | |
gem "pg" | |
end | |
ActiveRecord::Base.logger = Logger.new STDOUT | |
begin |
This file contains hidden or 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
# app/models/feature.rb | |
module Feature | |
include Flipper::Context | |
flag :like # Assumed to be via: :user | |
flag :soon_to_ship_cool_feature, via: :account | |
end | |
module Flipper::Context | |
def self.included(klass) = klass.extend(ClassMethods, self) |
This file contains hidden or 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
# Maybe there's a way to support ActiveSupport::Concern's dependencies too? | |
# Although this doesn't use the module hierarchy, so `prepend` can't work. | |
class Concern < Module | |
def initialize(&block) = @block = block | |
def included(klass) = klass.class_eval(&@block) | |
end | |
module Kernel | |
def Concern(...) = Concern.new(...) | |
end |
This file contains hidden or 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
require "bundler/inline" | |
gemfile do | |
gem "rails" | |
gem "sqlite3" | |
end | |
require "active_record" | |
require "action_controller" | |
require "rails" |
This file contains hidden or 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 ActiveRecord::Relation::Loaded < Data.define(:records) | |
def where(**conditions) | |
records = @records.dup | |
conditions.each do |key, value| | |
records.select! { |record| record.send(key) == value } | |
end | |
with(records:) | |
end | |
# Not sure how to maintain the order of the passed in keys? |
This file contains hidden or 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 Tapper < BasicObject | |
def initialize(object) = @object = object | |
def method_missing(meth) | |
@object.tap(&meth) | |
self | |
end | |
end | |
class Object |
This file contains hidden or 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
# Spotify Mix Playlists: Assuming we have a history of played songs for a user, | |
# we have song recommendations via nearest neighbor search, | |
# and we have categorizations (genre, mood, era, instrumental/vocal, cultural/regional, theme), | |
# let system admins create mix templates based on music categorizations | |
# and then generate refreshable custom playlists for each user. | |
class User | |
has_many :playlists | |
has_one :history | |
end |
This file contains hidden or 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
# Here's the problem statement we worked on: | |
# 1. Spam post detection: Run a forum post through a series of configurable checks to give it a spam score, | |
# and flag the post when it crosses the threshold, with details on what led to the score. | |
# We had a Gemfile with `gem "rails"` and `gem "sqlite3"` in it, but that was it. | |
require "bundler/setup" | |
require "active_record" | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") |
NewerOlder