Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar
💭
I may be slow to respond.

Chris Bloom chrisbloom7

💭
I may be slow to respond.
View GitHub Profile
@chrisbloom7
chrisbloom7 / Capfile
Last active June 14, 2016 07:37 — forked from toobulkeh/deploy.rb
Open a Rails console via Capistrano 3 (requires capistrano-rvm gem)
require 'capistrano/rvm'
@chrisbloom7
chrisbloom7 / rake_benchmark.rb
Created June 8, 2016 15:09 — forked from harley/rake_benchmark.rb
Benchmarking a rake task
# Put this in Rakefile (doesn't matter where)
require 'benchmark'
class Rake::Task
def execute_with_benchmark(*args)
bm = Benchmark.measure { execute_without_benchmark(*args) }
puts " #{name} --> #{bm}"
end
alias_method :execute_without_benchmark, :execute
# 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:
@chrisbloom7
chrisbloom7 / benchmark_net_http.rb
Created July 30, 2013 18:17
Benchmarking several different ways of fetching a URI
require 'benchmark'
def validate_by_request(value)
uri = URI.parse(value)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(uri.request_uri)
http.request(request)
@chrisbloom7
chrisbloom7 / model.rb
Last active December 20, 2015 10:30 — forked from joshuap/environment.rb
ActiveRecord validator for URLs
require 'uri_validator'
class SomeModel < ActiveRecord::Base
validates :url, :presence => true, :uri => { :format => /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?([\/].*)?$)/ix }
end
@chrisbloom7
chrisbloom7 / acts_as_list_skip_validation.rb
Created July 22, 2013 17:20
Override acts_as_list instance methods to save but skip validation
# config/initializers/acts_as_list_skip_validation.rb
module ActiveRecord
module Acts
module List
module InstanceMethods
# Move the item within scope, but skips validation
def move_within_scope(scope_id)
send("#{scope_name}=", scope_id)
save(false)
end
@chrisbloom7
chrisbloom7 / object_is_integer.rb
Created March 18, 2013 18:37
Determine if an object represents an integer
class Object
def is_integer?
!!(check = Integer(self) rescue false) && check.try(:to_s) == self.try(:to_s)
end
end
@chrisbloom7
chrisbloom7 / Gemfile
Last active December 8, 2015 23:38
Integration tests for PayPal Express Checkout using TestUnit in Rails 2.3
# Gemfile
gem 'paypal-sdk-merchant'
group :test do
gem 'fakeweb', '1.2.6'
end
@chrisbloom7
chrisbloom7 / gist:4286262
Created December 14, 2012 15:28
Split a query string into key/value pairs in JavaScript
var q = {}; window.location.search.replace(/^\?/, '').split('&').map(function(e){
var a = e.split('=');
q[a[0]] = a[1];
});
for (a in q) { console.log(a, q[a]); }
@chrisbloom7
chrisbloom7 / db.rake
Created June 8, 2012 01:51
Extend Rails' defaut set of rake tasks to add a db:bootstrap task. Similar to db:reset but doesn't use the schema file. Useful for testing your complete migration set (i.e. simulating a fresh setup) or for resetting your seed data in development.
namespace :db do
desc "Completely tears down the database, rebuilds it from scratch using your migrations (i.e. ignoring the current schema file), re-seeds the database based on the current environment, and finally prepares the test database"
task :bootstrap => ["db:drop", "db:create", "db:migrate", "db:seed", "db:test:prepare"]
end