Skip to content

Instantly share code, notes, and snippets.

View mgreenly's full-sized avatar

Michael Greenly mgreenly

View GitHub Profile
dealers = Dealer.arel_table
sales_reps = SalesRep.arel_table
dealer_counts = dealers.
group(dealers[:sales_rep_id]).
where(dealers[:deleted_at].eq(nil)).
where(dealers[:ended].eq(nil)).
project(dealers[:sales_rep_id], dealers[:id].count.as('value'))
def left_outer_join(subselect,as,on)
result = Arel.sql("")
@mgreenly
mgreenly / gist:1014710
Created June 8, 2011 16:01
ruby catch/throw
def next
loop do
if cache.empty?
cache.concat(next_block)
if cache.empty?
nil
else
redo
end
else
class Datatable::Base
#....
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
def method_missing(symbol, *args, &block)
if symbol.to_s =~ /(path|url)$/
return Rails.application.routes.url_helpers.send(symbol, *args)
end
super(symbol, *args, &block)
end
@mgreenly
mgreenly / gist:1109325
Created July 27, 2011 13:11
database cleaner multiple connections single orm outside of rails
RSpec.configure do |config|
config.before(:suite) do
ActiveRecord::Base.establish_connection database['one']
DatabaseCleaner.strategy = :deletion
ActiveRecord::Base.establish_connection config.database['two']
DatabaseCleaner.strategy = :deletion
end
config.before(:each) do
bundle config build.linecache19 --with-ruby-include=$rvm_path/src/ruby-1.9.2-p290
bundle config build.ruby-debug-base19 --with-ruby-include=$rvm_path/src/ruby-1.9.2-p290
@mgreenly
mgreenly / gist:1176783
Created August 28, 2011 15:21
robust net/ssh connection attempt
tries, connection = 0, nil
print "connecting to #{dns_name}"
until connection || ((tries += 1) > 10)
begin
Timeout::timeout(5) do
begin
connection = Net::SSH.start(dns_name, user_name, ssh_options)
rescue Exception => e
connection = nil
@mgreenly
mgreenly / gist:1239432
Created September 24, 2011 15:05
Ideas for Datable v0.3
# ./config/initializers/datatable.rb
Datatable::Base.configure do |config|
config.jquery_ui = true
config.table_tools = true
config.sDom = '<"H"lrf>t<"F"ip>'
config.iDisplayLength = 25
config.oLanguage = { 'sProcessing' => "<img alt='Spinner' src='/images/loader.gif'/>", 'sInfoFiltered' => '' }
end
@mgreenly
mgreenly / gist:1294403
Created October 18, 2011 01:36
cron file for ubuntu using multi-user rvm install
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
@mgreenly
mgreenly / dealer_warranty.feature
Created November 16, 2011 04:04
Cucumber/RSpec onion layer 1
Feature: dealer creates warranty claim
Background:
Given I am a dealer
Scenario: Dealer fails to create a warranty claim
Given I'm on the "new warranty claim" page
When I submit invalid warranty claim details
Then I should see an error
@mgreenly
mgreenly / warranty_claim_spec.rb
Created November 16, 2011 04:15
Cucumber/RSpec onion layer 3
describe WarrantyClaim do
it "validates presence of failure_date" do
warranty_claim = Factory.build(:warranty_claim)
warranty_claim.failure_date = nil
warranty_claim.valid?.should be_false
warranty_claim.failure_date = Date.today
warranty_claim.valid?.should be_true
end