Skip to content

Instantly share code, notes, and snippets.

Here's how it works

  • A Variant can not have less than zero count_on_hand
  • When a purchase invoice is created, a LineItem is created for each item purchased
  • When a LineItem is created and is awaiting payment (or if an existing one is changed to be in this status), we pass a reserve_stock message to the associated Variant
  • The code for reserve_stock does the following:
    1. Obtains a read/write row level lock on the Variant
    2. Reloads the Variant from the database
    3. Decrements the count_on_hand by the requested quantity
  1. Saves the Variant
class BigDecimal
def inspect
"#<BigDecimal: #{to_s}>"
end
end
@mipearson
mipearson / check_coverage.rb
Created January 22, 2013 23:21
Assert that a set of specs actually give 100% coverage for the files they're supposed to test.
#!/usr/bin/env ruby
COVERED = %w{
app/models/ ...
}
failures = COVERED.select do |app_file|
spec_file = app_file.gsub('app', 'spec').gsub('.rb', '_spec.rb')
cmd = "sh -c 'SIMPLECOV_FOR=#{app_file} rspec #{spec_file}'"
puts "Running #{cmd}"
# -*- encoding : us-ascii -*-
if "a string".respond_to?(:valid_encoding?)
require 'rack/mock'
require 'rack/contrib/enforce_valid_encoding'
VALID_PATH = "h%C3%A4ll%C3%B2"
INVALID_PATH = "/%D1%A1%D4%F1%D7%A2%B2%E1%D3%C3%BB%A7%C3%FB"
describe "Rack::EnforceValidEncoding" do
before do
module Rack
class EnforceValidEncoding
def initialize app
@app = app
end
def call env
full_path = (env.fetch('PATH_INFO', '') + env.fetch('QUERY_STRING', ''))
if full_path.valid_encoding? && Rack::Utils.unescape(full_path).valid_encoding?
def ensure_utf8
# TODO Proper solution using https://github.com/brianmario/charlock_holmes & detection
# processing copied from http://trackingrails.com/posts/video-encoding-processor-for-carrierwave
cache_stored_file! if !cached?
file_data = File.read(current_path)
# Ugh. Only way to detect bad UTF8. See
# http://bibwild.wordpress.com/2012/04/17/checkingfixing-bad-bytes-in-ruby-1-9-char-encoding/
is_valid_utf8 = begin
activerecord (3.2.6) lib/active_record/persistence.rb:343:in `create_or_update'
activerecord (3.2.6) lib/active_record/callbacks.rb:264:in `block in create_or_update'
activesupport (3.2.6) lib/active_support/callbacks.rb:447:in `_run__1997728820393337820__save__2098716428440842939__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.6) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.6) lib/active_record/persistence.rb:104:in `save!'
activerecord (3.2.6) lib/active_record/validations.rb:56:in `save!'
activerecord (3.2.6) lib/active_record/attribute_methods/dirty.rb:33:in `save!'
@mipearson
mipearson / fmserverpositions.md
Created November 15, 2012 09:05 — forked from mjdgraaf/fmserverpositions
Firemonkeys server engineer positions

November 15, 2012

Firemonkeys, Melbourne-based game development studio, is looking to contract server engineers on a short term basis. There are three positions we are looking to fill:

DBA:

  • Knows how to use “EXPLAIN”
  • Preferably has used maatkit or percona-toolkit
  • Can optimise MySQL queries and apply indexes, and understands the impact of adding too many indexes
@mipearson
mipearson / fix_bad_utf8.rb
Created November 11, 2012 23:44
Fix mis-encoded UTF8
module Migrations
class FixBadUTF8
def initialize
@connection = ActiveRecord::Base.connection
@tables = @connection.tables
end
def mappings
@mappings ||= begin
@mipearson
mipearson / gist:4043848
Created November 9, 2012 05:18
UTF8 -> LATIN1 -> UTF8 muckup

Our database is collated as UTF8. It contains only valid UTF8 multi-byte strings.

However, some of those UTF8 multi-byte strings come from mis-encoded data.

Here's an example: "... waterproof to 197′"

That should actually be "... waterproof to 197º"

How I think this happened: