Skip to content

Instantly share code, notes, and snippets.

module ActiveRecord
class Base
def self.merge_conditions(*conditions)
segments = []
conditions.each do |condition|
unless condition.blank?
sql = sanitize_sql(condition)
segments << sql unless sql.blank?
end
@joshdvir
joshdvir / Links to articles
Created July 27, 2011 19:23
Switching to Capybara from Webrat + configuring a headless firefox with selenium
@henrik
henrik / luhn_checksum.rb
Created November 30, 2011 17:02
Luhn checksum/check digit generation in Ruby.
class Luhn
def self.checksum(number)
digits = number.to_s.reverse.scan(/\d/).map { |x| x.to_i }
digits = digits.each_with_index.map { |d, i|
d *= 2 if i.even?
d > 9 ? d - 9 : d
}
sum = digits.inject(0) { |m, x| m + x }
mod = 10 - sum % 10
mod==10 ? 0 : mod
@pat
pat / gist:1776846
Created February 9, 2012 02:47
SSL Redirection RSpec Matcher
RSpec::Matchers.define(:redirect_to_ssl) do
description { 'redirect to same page using the HTTPS protocol' }
match { |status|
response.redirect_url == request.url.gsub(/^http:/, 'https:')
}
end
# in spec/requests or similar...
get('/admin/sign_in').should redirect_to_ssl
@zachpendleton
zachpendleton / pianobar-lastfm.rb
Created July 5, 2012 20:50
Pianobar/LastFM Integration
#!/usr/bin/env ruby
# coding: utf-8
require 'digest/md5'
require 'net/http'
class LastFM
attr_reader :api_key, :secret, :session_key, :base_uri, :namespace
@r35krag0th
r35krag0th / Get-TLS-Fingerprint.sh
Created November 30, 2012 02:10
Get Pandora TLS Fingerprint
#!/bin/bash
##
## A simple little shell script that will return the current
## fingerprint on the SSL certificate. It's crude but works :D
##
## Author: Bob Saska (r35krag0th) <git@r35.net>
openssl s_client -connect tuner.pandora.com:443 < /dev/null 2> /dev/null | \
openssl x509 -noout -fingerprint | tr -d ':' | cut -d'=' -f2
@tcaddy
tcaddy / rspec.rake
Last active December 10, 2015 00:08
Place this file in the `lib/tasks` folder of your Rails project. Run `rake -T` to see available rake tasks. This will dynamically create a single rake task for each single `*_spec.rb` file. It will also create additional rake tasks that run all `*_spec.rb` files in a given sub-directory.
require 'rspec/core/rake_task'
orm_setting = Rails.configuration.generators.options[:rails][:orm]
spec_prereq = if(orm_setting == :active_record)
Rails.configuration.active_record[:schema_format] == :sql ? "db:test:clone_structure" : "db:test:prepare"
else
:noop
end
namespace :spec do
[:requests, :models, :controllers, :views, :helpers, :mailers, :lib, :routing].each do |sub|
dn = "#{File.expand_path(::Rails.root.to_s)}/spec/#{sub}"
@jamesgary
jamesgary / gist:5491390
Last active July 26, 2022 09:06
The Magic Tricks of Testing - Sandi Metz - RailsConf2013

The Magic Tricks of Testing

Sandi Metz

  • Many people say "I hate my tests"
  • They kill your productivity when they're slow
  • A little change can break your tests (even if they shouldn't)
  • They're expensive
  • They are misery incarnate
  • Just delete some tests
  • You may have too many tests testing the wrong tests
record = Post.new(:title => 'Yay', :body => 'This is some insert SQL')
# easiest way to achieve this is by calling protected #arel_attributes_values (tested in
# rails 3.2.13). the alternative is to build the entire insert statement using arel >_>
record.class.arel_table.create_insert \
.tap { |im| im.insert(record.send(:arel_attributes_values, false)) } \
.to_sql
@SabretWoW
SabretWoW / rspec_model_testing_template.rb
Last active May 28, 2024 17:41
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems: