Skip to content

Instantly share code, notes, and snippets.

04d0cafedd8874f190bf83ba3a7251384eb63f3031d8a44b8f31df574c26044f1104461f42564ef52b4e284b4f66f326c15b32547655709e295c7c5dcdde2737d8
@aarontc
aarontc / active_record_base.rb
Created April 21, 2017 17:40
Monkeypatch for ActiveRecord to allow safe SQL sanitization
require 'active_record'
module ActiveRecord
class Base
class << self
def select_rows_with_params(query, *params)
query = sanitize_sql_array [query, *params]
connection.select_rows query
end
@aarontc
aarontc / lockable.rb
Created May 18, 2016 16:11
A simple generic resource lock implemented using ActiveRecord and PostgreSQL
require 'socket'
# A semi-nice way to lock a record against multiple access for whatever reason in PostgreSQL.
# Uses an atomic test-and-set query to guarantee the caller has gotten a lock.
# Usage:
#
# require_relative 'lockable'
# class MyARClass < ActiveRecord::Base
# include Lockable
# lockable :locked_by
@aarontc
aarontc / test_numeric.rb
Last active February 19, 2016 19:19
numeric?
require 'minitest'
require 'minitest/autorun'
require 'minitest/pride'
class TestNumeric < Minitest::Test
def numeric?(arg)
!/\A[+-]?\d+\z/.match(arg.to_s).nil?
end
#!/bin/bash
set -e
if [[ $UID -ne '0' ]]; then
echo "Must be root" >&2
exit 1
fi
# Check if minion running
set +e
@aarontc
aarontc / scenario.rb
Created May 28, 2014 18:58
Example for AASM feature request
require 'aasm'
require 'timers'
class TimedStateMachineExample
include AASM
TRANSITION_SECONDS = 5
def initialize(timers)
@timers = timers || Timers.new
SELECT
agent_plan_id, plan_id
,agent_plan_cim_customer_id
,plan_trial_interval_price
,agent_plan_cim_payment_profile_id
FROM
(SELECT COUNT(agent_plan_payment_id) AS payment_count FROM agent_plan_payments JOIN agent_plan_payment_reasons ON agent_plan_payment_reasons.agent_plan_payment_reason_id = agent_plan_payments.agent_plan_payment_reason_id WHERE agent_plan_payments.agent_plan_id = agent_plans_with_modifiers.agent_plan_id AND agent_plan_payment_reasons.agent_plan_payment_reason_code='trial-interval-fee') blah
, agent_plans_with_modifiers
WHERE