Skip to content

Instantly share code, notes, and snippets.

View scottwb's full-sized avatar

Scott W. Bradley scottwb

View GitHub Profile
@scottwb
scottwb / Rakefile
Created February 24, 2012 17:21
Rakefile for Middleman with an rsync deploy task.
SSH_USER = 'root'
SSH_HOST = 'www.example.com'
SSH_DIR = '/var/www/html/www.example.com'
desc "Build the website from source"
task :build do
puts "## Building website"
status = system("middleman build --clean")
puts status ? "OK" : "FAILED"
end
@scottwb
scottwb / rails.js.diff
Created February 18, 2012 04:01
Patch rails.js from rails/jquery-ujs to make it honor jQuery Mobile data-ajax
diff --git a/src/rails.js b/src/rails.js
index 06b4e0b..49ff0b2 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -174,6 +174,11 @@
if (target) { form.attr('target', target); }
+ var ajax = link.data('ajax');
+ if (ajax !== undefined) {
@scottwb
scottwb / devise_filters.rb
Created February 17, 2012 08:06
Example module for adding custom filters to Devise controllers.
# Put this somewhere it will get auto-loaded, like config/initializers
module DeviseFilters
def self.add_filters
# Example of adding a before_filter to all the Devise controller
# actions we care about.
[
Devise::SessionsController,
Devise::RegistrationsController,
Devise::PasswordsController
].each do |controller|
@scottwb
scottwb / application_controller.rb
Created February 17, 2012 06:12
Get a list of all the filters on a given Rails 3 controller.
# Add these methods to your ApplicationController. Then, any controller
# that inherits from it will have these methods and can programmatically
# determine what filters it has set.
class ApplicationController < ActionController::Base
def self.filters(kind = nil)
all_filters = _process_action_callbacks
all_filters = all_filters.select{|f| f.kind == kind} if kind
all_filters.map(&:filter)
end
@scottwb
scottwb / carrierwave_fog_hack.rb
Created February 14, 2012 01:56
Monkey-hack to retry SSL "Connection reset by peer" failures to Rackspace Cloud Files with CarrierWave
module CarrierWave
module Storage
class Fog
class File
alias :store_without_safety_net :store
def store(new_file)
retries_remaining = 3
begin
store_without_safety_net(new_file)
rescue Excon::Errors::SocketError => e
@scottwb
scottwb / README.md
Created February 6, 2012 20:45
Monkey patches for a couple Rails Mime::Type.parse bugs.

Rails Mime::Type.parse Patches

There are two Rails issues in it's handling of the HTTP Accept header which cause a number of spurious exception emails via Airbrake. I am encountering this on Rails 3.0.7. One of these is fixed in a later version of Rails, but for other reasons I can't upgrade right now. The other bug is still present in Rails 3.2 and in master at the time of this writing. This gist includes some monkey patches you can apply to fix these issues until such time that they are fixed in Rails properly.

Rails Issue #736

Issue #736 is that Rails does not correctly parse a q-value in an Accept header when there is only one content-type specified. For example:

Accept: text/html;q=0.9
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
+ _gaq.push(['_setCampSourceKey', 'utm_source']);
+ _gaq.push(['_setCampMediumKey', 'utm_medium']);
+ _gaq.push(['_setCampContentKey', 'utm_content']);
+ _gaq.push(['_setCampTermKey', 'utm_keyword']);
+ _gaq.push(['_setCampNameKey', 'utm_campaign']);
_gaq.push(['_trackPageview']);
@scottwb
scottwb / carrierwave_recreate_version.rb
Created September 4, 2011 18:03
Monkey-patch CarrierWave to add a method that recreates a single version of an uploader.
module CarrierWave
module Uploader
module Versions
def recreate_version!(version)
already_cached = cached?
cache_stored_file! if !already_cached
send(version).store!
if !already_cached && @cache_id
tmp_dir = File.expand_path(File.join(cache_dir, cache_id), CarrierWave.root)
FileUtils.rm_rf(tmp_dir)
Devise::RegistrationsController.before_filter :check_for_invite_code, :only => :new
@scottwb
scottwb / devise.rb
Created August 24, 2011 05:11
Addition to config/initializers/devise.rb to insert before_filters to Devise controllers.
# Add some before filters to all Devise controllers.
[
Devise::SessionsController,
Devise::RegistrationsController,
Devise::PasswordsController
].each do |devise_controller_class|
devise_controller_class.before_filter :prepare_for_mobile
end