Skip to content

Instantly share code, notes, and snippets.

View mecampbellsoup's full-sized avatar

Matt Campbell mecampbellsoup

View GitHub Profile
def move_disbursements_from_vendor_relationship_and_merge_users(from_user, to_user)
ActiveRecord::Base.transaction do
old_vendor_relationship = from_user.vendor_relationships.first
new_vendor_relationship = to_user.vendor_relationships.first
new_customer_id = new_vendor_relationship.customer_id
old_disbursements = old_vendor_relationship.disbursements
# Update old VR's disbursements' vendor_relationship and customer associatons
def balance
# Works but exposes potential race condition
ledger.items.where("created_at <= ?", created_at).sum(:amount_cents)
# Doesn't work consistently, seems to do a string comparison of the UUIDs...
#ledger.items.where("id <= ?", id).sum(:amount_cents)
end
Deposit.find_in_batches(batch_size: 500) do |batch|
GC.start
batch.each { |deposit|
item = deposit.ledger_item || next
unless item.try(:created_at) == deposit.created_at
item.update_column(:created_at, deposit.created_at)
end
}
end
@mecampbellsoup
mecampbellsoup / open_class_effect_on_superclass.rb
Created September 10, 2015 17:23
If you open class an existing Ruby class WITHOUT specifying a superclass, the superclass will not change after opening the class
[1] pry(main)> class Animal
[1] pry(main)* @@kingdom = "Animalia"
[1] pry(main)* end
=> "Animalia"
[6] pry(main)> class Dog < Animal
[6] pry(main)* end
=> nil
[7] pry(main)> Dog.class_variable_get "@@kingdom"
=> "Animalia"
# NOTE: Not specifying the original superclass of Animalia from the original class definition of the Dog class
class ApplicationController
private
def authenticate_mt_user!
# returns the resource when signed in
# returns nil if not
super if additional_logic_check_passes?
end
end
require 'faker'
class Person
attr_reader :first_name, :last_name
def initialize(hash_like_object = {})
@first_name = hash_like_object[:first_name]
@last_name = hash_like_object[:last_name]
end
public class BinarySearch {
public static int BinarySearch(int[] a, int key)
{
int lo = 0, hi = a.length-1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *price = [self getDataFrom:@"https://api.coinbase.com/v1/prices/spot_rate"];
NSLog(@"The current spot price is: %@", price);
[self activateStatusMenu:price];
}
- (void)activateStatusMenu:(NSString *)price {
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
NSLog(@"The status item's class is: %@", NSStringFromClass([_statusItem class]));
class CheckMessageStatusJob
include SuckerPunch::Job
def perform(status, message)
@message = message
previous_status = status
# If status hasn't changed, it's stuck so re-send the message
resend_message! if message.reload.status == previous_status
end
rails_env = new_resource.environment["RAILS_ENV"]
bitgo_env = rails_env == "production" ? "live" : "test"
Chef::Log.info("Starting Bitgo proxy with Rails env of #{rails_env} and Bitgo env of #{bitgo_env}...")
execute "start Bitgo proxy" do
cwd release_path
command "nodejs ./node_modules/bitgo/bin/bitgo-express --debug --port 3080 --env #{bitgo_env} --bind localhost"
end