Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / peaceful_asset_logger.rb
Created February 22, 2013 16:14
Peaceful Asset Logger. You know, for Rails. Silence all of the `assets` noise in your development logs. Inspired by middleware I once saw from @macournoyer.
# Usage: in develoopment.rb
#
# config.middleware.insert_before Rails::Rack::Logger, PeacefulAssetsLogger
#
class PeacefulAssetsLogger
def initialize(app)
unless ENV[ 'LOG_ASSETS' ]
puts 'For more peaceful logs we have silenced the asset logs.'
puts 'To see asset requests in the logs, set LOG_ASSETS=true env variable.'
Rails.application.assets.logger = Logger.new('/dev/null')
@stevenharman
stevenharman / select_from_subquery.sql
Created January 21, 2013 20:44
Using Arel/ActiveRecord to execute a subquery, including a SUM.
select fund_sums.total, fund_products.name
from (
select sum(allocation_amount) total, fund_product_id
from transactions
where transactions.investor_id = 490
group by fund_product_id
) fund_sums
left join fund_products on fund_sums.fund_product_id = fund_products.id
@stevenharman
stevenharman / using_structs.rb
Created January 15, 2013 17:53
Pro Tip™: Do not inherit from Struct.
class Foo < Struct.new(:bar); end
puts Foo.ancestors #=> [Foo, #<Class:0x007fae624e79e8>, Struct, Enumerable, Object, PP::ObjectMixin, Kernel, BasicObject]
Bar = Struct.new(:foo)
puts Bar.ancestors #=> [Bar, Struct, Enumerable, Object, PP::ObjectMixin, Kernel, BasicObject]
# The first way adds an extra, unnecessary, anonymous module in the ancestors chain.
@stevenharman
stevenharman / _compose_query_objects.md
Last active December 19, 2017 21:33
Build compose-able query objects by delaying query execution and delegating to the underlying `ActiveRecord::Relation` for everything else. #ruby #rails

Compose-able Query Objects in Rails

Example usage:

old_accounts = Account.where("created_at < ?", 1.month.ago)
old_abandoned_trials = AbandonedTrialQuery.new(old_accounts)

old_abandoned_trials.find_each do |account|
 account.send_offer_for_support
@stevenharman
stevenharman / heroku-signals-ticket.md
Last active November 9, 2018 11:12
Sending user signals to Heroku workers/process...

me:

Is it possible to send a signal to a worker/process? I realize the platform sends SIGTERM and SIGKILL when restarting a dyno, but I need to send a USR1 to one of my workers to tell it to stop picking up new jobs. Normally this is achieved via kill -USR1 <pid>, but on the Heroku platform not only do we not know know the pid, we also don't run one-off commands on the same dyno.

Caio (heroku support):

We had this feature experimentally at some point but it was never productized. I recommend you find other ways to signal your processes, like setting a database flag.

me:

@stevenharman
stevenharman / service_keys.rb
Created October 30, 2012 14:41
Hiding configuration of keys/secrets behind an explicit interface.
module ServiceKeys
def self.brewery_db
ENV['BREWERY_DB_API_KEY']
end
end
@stevenharman
stevenharman / adapt_brewery_db_webhook.rb
Created October 27, 2012 02:51
Trying to "rename" the `action` param sent by BreweryDB so it doesn't collide with Rails' `action` param.
class AdaptBreweryDBWebhook
def initialize(app)
@app = app
end
def call(env)
adapt_action_param(env)
@app.call(env)
end
@stevenharman
stevenharman / constant_in_namespace.rb
Created October 24, 2012 18:58
Ruby: How can you get a hold of the constant a class/module is namespaced in?
module World
module USA
class Directory
def move_to(a_state)
state = a_state.class.name.split('::').last #=> 'Georgia'
# fails
self.class.const_get(state) #=> Want World::USA::Georgia
# but this would work
@stevenharman
stevenharman / cloudsheet_map.rb
Created October 19, 2012 14:57
Ideas for Cloudsheet mapping API
Cloudsheet::Map.new(
1 => { month: Date },
2 => { income: Curreny },
3 => { other: -> (raw) { some_custom_transform(raw) } }
)
@stevenharman
stevenharman / _dev_badge.html.slim
Created October 17, 2012 18:45
A simple badge that shows your Rails environment.
/ In app/views/shared/
#dev-badge
span= Rails.env
span It ain't production!