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
# 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 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 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 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 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 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 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 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 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:") |
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
create_table :stowaway_records do |t| | |
t.string :record_type, null: false | |
t.jsonb :record_attributes, null: false, default: {} | |
t.timestamps | |
t.index :record_type | |
end | |
class Stowaway::Record < ActiveRecord::Base | |
def self.preserve(record) | |
create! record_type: record.class, record_attributes: record.attributes |
NewerOlder