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 / Gemfile
Last active August 29, 2015 14:15
TestUnit setup/teardown order in Ruby 2.1 with shoulda-context, factory_girl_rails and database_cleaner
source 'https://rubygems.org'
ruby '2.1.4'
gem 'rails', '~> 3.2.0'
gem 'mysql2'
group :development, :test do
gem 'byebug'
@chrisbloom7
chrisbloom7 / .rails_aliases
Last active January 31, 2019 16:15
Bash shortcuts for routing Rails commands through the binstubs if they are present, or fallback to the other version-dependent methods
# RUBY / RUBY ON RAILS COMMANDS
alias bexec='bundle exec'
alias rails_mv="bexec rails -v | sed 's/Rails \([0-9]\).*/\1/g'"
# Alias the rake command to Spring binstubs or fallback to "bundle exec"
# http://goo.gl/HkhHAf, http://goo.gl/STtIvF
function brake {
if [ -f bin/rake ]
then
bin/rake "$@"
else
@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'
# 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 / 20140101000000_add_hstore_field_to_trip_tickets.rb
Last active August 29, 2015 14:06
Using a Postgres HStore field in a Rails ActionView form
# db/migrations/20140101000000_add_hstore_field_to_trip_tickets.rb
class AddHstoreFieldToTripTickets < ActiveRecord::Migration
def change
add_column :trip_tickets, :customer_identifiers, :hstore
end
end
@chrisbloom7
chrisbloom7 / README.md
Last active October 31, 2016 04:28
Bust AJAX request caching

The following will prevent all future AJAX requests from being cached, regardless of which jQuery method you use ($.get, $.ajax, etc.)

$.ajaxSetup({ cache: false });

Source: http://stackoverflow.com/a/735101/83743

However, jQuery's AJAX object will follow redirects, so even if you disable caching globally the "_={timestamp}" parameter that jQuery adds to bust the browser's cache may not be forwarded with the redirect. In that case, your request can still be cached by the browser. The solution is to either make sure that special param is passed along with redirects, or to send the appropriate cache-busting response headers from the server-side code for those requests.

Reference: http://api.jquery.com/jQuery.ajax/

@chrisbloom7
chrisbloom7 / change.rb
Last active September 10, 2019 11:40
Dynamic enum field values in nested association forms in Rails Admin
# app/models/change.rb
class Change < ActiveRecord::Base
belongs_to :changeset, inverse_of: :changed_fields
validates :changeset, presence: true, associated: true
validates :field, presence: true, uniqueness: {scope: :changeset_id}, inclusion: { in: :field_enum }
def field_enum
if changeset.try(:model).present?
changeset.model_attributes
else
@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