Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar
✈️
Traveling

Chris Bloom chrisbloom7

✈️
Traveling
View GitHub Profile
@chrisbloom7
chrisbloom7 / active_record.rb
Created August 11, 2014 17:19
Rails ActiveRecord finder that accepts an array of IDs and preserves the order in the results
# config/initializers/active_record.rb
module ActiveRecord
class Base
# Find by array of IDs, preserving order of IDs
# See http://stackoverflow.com/a/25248122/83743
def self.find_with_relevance(array_of_ids)
array_of_ids = Array(array_of_ids) unless array_of_ids.is_a?(Array)
self.find(array_of_ids).index_by(&:id).slice(*array_of_ids).values
end
@chrisbloom7
chrisbloom7 / acts_as_indexed.rb
Created August 5, 2014 16:53
Setup acts_as_indexed for Rails testing (Tested on Rails 2.3 with TestUnit)
# config/initializers/acts_as_indexed.rb
ActsAsIndexed.configure do |config|
# Disable acts_as_indexed auto indexing in test by default
config.disable_auto_indexing = Rails.env.test?
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 / 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]); }