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 / rails_test_descriptions.md
Last active August 29, 2015 13:57
Short description of the various tests (testunit, minitest) typically found in a Rails app
  • Unit tests are very narrowly focused on testing a single model
  • Functional tests are very narrowly focused on testing a single controller and the interactions between the models it employs
  • Integration tests are broad story-level tests that verify the interactions between the various actions supported by the application, across all controllers
@chrisbloom7
chrisbloom7 / meet_chef_notes.md
Created March 19, 2014 19:55
Notes from the Meet Chef video tutorial at http://pluralsight.com/
@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 / 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 / example_table.html
Created June 11, 2012 18:52
A cucumber/capybara step to test the order of data in a table, either with or without headers
<html>
<body>
<section class="left">
<article id="customers">
<h2>New Customers</h2>
<table class="data">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
@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
@chrisbloom7
chrisbloom7 / git_make_branch.sh
Last active October 3, 2015 15:58
Unix function and alias to create git branches based off arbitrary text (e.g. A ticket number and description)
# Create a git branch based off of an arbitrary string. Call the function from
# the command line inside a git project and pass in the description (quoted or
# not) as the argument. Your description will be converted to slug format and
# used to checkout a new branch. Useful for quickly creating branches named
# after a ticket # and description.
# Example:
$ git-mb 123 Add a new feature \(Don't forget the tests\!\)
Switched to a new branch '123-add-a-new-feature-don-t-forget-the-tests'