Skip to content

Instantly share code, notes, and snippets.

# https://www.agileplannerapp.com/blog/building-agile-planner/refactoring-with-hexagonal-rails
def checkout
cart = CartCheckout.new(many, params)
cart.save!
rescue OutOfStock => e
OutOfStockNotification.new(e.line_item).deliver
redirect_to cart_path, notice: t('.failure', reason: cart.error_message)
rescue CartError
redirect_to cart_path, notice: t('.failure', reason: cart.error_message)
else
@gma
gma / gist:3b24e28bde2dfe3c2d02
Last active August 29, 2015 14:19
Commit message for email delivery race condition fix

When running code in a thread ActiveRecord uses a separate database connection, which can't see the results of any queries run by the main thread until they're committed. But Rails doesn't commit transactions in the test environment, making this difficult to test.

The fix for this was in two parts, either of which should be enough to solve the problem on their own.

  1. Load the ActiveRecord objects before entering the thread, as the database connection that we'll be using inside the thread may not be able to access the (freshly created) database records yet.

  2. Commit the transaction that wraps the creation of the database records before publishing the :message_created message (that triggers the thread to run). This ought to mean that code with a separate database connection will be able to load the subscribers anyway.

@gma
gma / sidekiq.rb
Created April 10, 2015 12:42
How I might try changing a Sidekiq queue in a rake task
# This obviously isn't thread safe, but I bet that doesn't matter a monkeys
# if you're running it in a rake task.
#
# Also, I don't use Sidekiq, so I might have misunderstood how to use the API
# in a fundamental manner which could mean that what follows is useless...
def with_queue(job_class, queue_name, &block)
default_queue = job_class.get_sidekick_options.fetch(:queue)
job_class.sidekick_options(queue: queue_name)
yield
// The query that (in the Sense console) produced the contents of results.js
// GET planner_development/_search?search_type=count
{
"query": {
"terms": {
"tag_ngrams": [
"fo"
]
}
module Publisher
def self.included(base)
base.instance_eval do
def republish(*messages)
messages.each do |message|
define_method(message) do |args|
publish(message, *args)
end
end
end
@gma
gma / sync-staging-database.sh
Created February 14, 2014 10:37
Copy production backup to staging (Heroku)
#!/bin/bash
PRODUCTION_REMOTE="production"
STAGING_REMOTE="staging"
STAGING_DATABASE_NAME="HEROKU_POSTGRESQL_COBALT"
## Functions
reset_staging_database()
{
@gma
gma / Gemfile.local
Created August 12, 2013 16:45
Handy stuff for running gems via bundler, without bundling them
group :development do
gem 'debugger'
gem 'spring'
end
group :test do
gem 'minitest', '4.3.0'
gem 'tconsole', '1.2.8'
end
@gma
gma / gist:5100428
Last active December 14, 2015 14:28
Do not want this behaviour. Mmmkay? It's bad. Got it? Good…
(rdb:1) p cache_control
{"max-age"=>"0", "private"=>true, "must-revalidate"=>true}
# It's a hash? That's weird...
(rdb:1) p cache_control.class
Rack::Cache::CacheControl
# Oh you dirty liar. Let's check.
(rdb:1) p cache_control.class.ancestors
[Rack::Cache::CacheControl, Hash, Mocha::HashMethods, JSON::Ext::Generator::GeneratorMethods::Hash, Enumerable, Object, Metaclass::ObjectMethods, Mocha::ObjectMethods, PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
@gma
gma / master.sass
Created March 6, 2013 15:01
Styles used on effectif.com.
@import "mixins.sass"
@import "normalize"
body
font: $size1 Bitter, serif
line-height: 1.6
color: $base-color
div#container
position: relative