Skip to content

Instantly share code, notes, and snippets.

View mgreenly's full-sized avatar

Michael Greenly mgreenly

View GitHub Profile
@mgreenly
mgreenly / error_steps.rb
Created February 3, 2012 04:20
binary paths in integration tests
Then /^I should see an error$/ do
page.should have_selector('div.error')
end
Then /^I should not see any errors$/ do
page.should have_no_selector('div.error')
end
@mgreenly
mgreenly / gist:1753832
Created February 6, 2012 18:17
.rvmrc
rvm use @$(basename `pwd`) --create
@mgreenly
mgreenly / gist:1754934
Created February 6, 2012 21:15
sql with constant values
select
ta.*,
coalesce(tb.weight, 0) as weight
from
(values ('A1','B1', 3),
('A2','B2', 7) )
as ta(a_d,b_id,qty)
left join
(values ('B1',2) )
as tb(b_id, weight)
@mgreenly
mgreenly / gist:1777441
Created February 9, 2012 05:00
sinking stdout in rspec
class Foo
def run
puts "usage: foo"
0
end
end
RSpec.configure do |config|
config.before(:all) do
@null_out = File.open('/dev/null', 'w')
original_zone = Time.zone
Time.zone= TZInfo::Timezone.all.shuffle.first
# do tests
Time.zone= original_zone
@mgreenly
mgreenly / gist:1871238
Created February 20, 2012 20:34
Extending Date parsing in Ruby 1.9.3
require 'date'
class Date
class << self
def convert_date_string(input)
# mm-dd-yyyy
if input =~ /^(\d{1,2})[^\d\w\s](\d{1,2})[^\d\w\s](\d{4})([T|\W].*)?/
"%0.4d/%0.2d/%0.2d#{$4}" % [$3, $1, $2].map(&:to_i)
@mgreenly
mgreenly / gist:1924765
Created February 27, 2012 15:41
Apache http to https redirect
<VirtualHost *:80>
RedirectMatch permanent /(.*)$ https://example.com/$1
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
# config...
</VirtualHost>
@mgreenly
mgreenly / gist:1990590
Created March 7, 2012 02:55
mass assignment params slice
# a variation on DHH's slice pattern for the mass assignment problem that may save on typing
# in common situations. Since it seems to me that most of the time there are only a small
# number of attributes that have authorization limited by role it would be nice if they were
# the only one's you had to specify.
# none of this was tested it may not actually work!
# since this is the same for every class just put it in a common base class
class ActiveRecord::Base
module ApplicationHelper
def present(subject, klass = nil)
klass ||= "#{subject.class}Presenter".constantize
presenter = klass.new(subject, self)
yield presenter if block_given?
presenter
end
end
class SalesOrder < ActiveRecord::Base
def self.stock
# complex logic to define what a stock order is
end
def stock?
SalesOrder.stock.exists?(self)
end