Skip to content

Instantly share code, notes, and snippets.

View cmorss's full-sized avatar

Charlie Morss cmorss

View GitHub Profile
@cmorss
cmorss / stateful_spec.rb
Created May 19, 2021 22:29
Stateful specs
require 'rails_helper'
RSpec.describe Stateful do
let(:model_class) do
Struct.new(:status, :saved, :scopes, :running_at, :finished_at, :finished_count, :privileged) do
include Stateful
def self.scope(*args)
define_singleton_method(args.first) {}
end
@cmorss
cmorss / stateful.rb
Created May 19, 2021 22:29
Statemachine
# Mark an ActiveRecord model as having a state machine.
# The state of the model defaults to being stored on an attribute
# called `state` or can be specified via the `column` arg to
# the `stateful` call. To set the default state for a
# class, set a default column value for `state` in the database
# or use `initial: true` when defining the initial state.
#
# Use Symbols for all keys and values in state definitions.
module Stateful
with assessment_block_index as (
select
entity_key as assessment_block_key,
parent_key as step_key
from entity_indices
where entity_type = 'AssessmentBlock'
),
step_index as (
select
entity_key as step_key,
has_one :intro_progress, inverse_of: :skill_progress, autosave: true, required: false, dependent: :destroy
has_one :video_challenge_progress, inverse_of: :skill_progress, autosave: true, required: false, dependent: :destroy
has_many :step_progresses, -> { includes(:step).order(:order) }, inverse_of: :skill_progress, autosave: true,
dependent: :destroy
@cmorss
cmorss / stateful.rb
Created April 8, 2020 22:55
ActiveRecord state machine
module Instructify
# Mark an ActiveRecord model as having a state machine. Requires a
# string attribute called `state`. To set the default state for a
# class, set a default column value for `state` in the database.
#
# Use Symbols for all keys and values in state definitions.
module Stateful
ANY = Object.new
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
auth = request.env['omniauth.auth']
manager = Manager.where(provider: auth['provider'], uid: auth['uid'])
.first_or_initialize(email: auth['info']['email'])
manager.first_name = auth['info']['first_name']
manager.last_name = auth['info']['last_name']
manager.save!
def create
self.resource = resource_class.send_confirmation_instructions(resource_params)
yield resource if block_given?
render_resource(resource, blueprint: :admin)
end
@cmorss
cmorss / orm.dsl.js
Last active November 7, 2019 22:52
// In express middleware when a request comes in we start a transaction
// and store it in continuation local store (think thread local store)
// Create a new user in the DB and return the newly created User instance
let user = await User.create({name: "Sally Smith", email: "sally@gmail.com"});
user.setAddressStreet1("123 Main St."); // With no Typescript
user.addressStreet1 = "123 Main St."; // If we introduce Typescript this would be okay too
@cmorss
cmorss / dau.rb
Last active February 3, 2017 17:06
This will use Stuart's retention service (which pulls from Redis and stores in Cassandra) to get DAU for the specified dates and apps.
require 'faraday'
require 'faraday_middleware'
require 'active_support/all'
def period(app_id, start_date, end_date)
date = start_date
results = []
while date <= end_date do
results << { date: date.strftime("%F"), dau: day(app_id, date) }
date = (date + 1)
module PullUpErrors
extend ActiveSupport::Concern
# Used to pull up errors that are on an associated collection or object
included do
after_validation :pull_up_errors
def pull_up_errors(*attrs)
@pull_up_attrs = Array(attrs)