Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am r38y on github.
  • I am r38y (https://keybase.io/r38y) on keybase.
  • I have a public key whose fingerprint is 9C92 BD4F 93E3 2505 CC1E AB3F 6CDC BC3B A534 087B

To claim this, I am signing this object:

@r38y
r38y / view_path_registry.rb
Last active August 29, 2015 13:57
Initializes a registry of view paths for the various themes in our apps.
# this is an initializer and used to set up various themes
# so we can use the same instances on each request
# I'm wondering if $path_set_registry should be a
# constant instead of a global variable
$path_set_registry = {}
Dir.entries('app/views/site_overrides').reject{|e| e.in?(['.', '..'])}.each do|domain|
paths = ["app/views/site_overrides/#{domain}"]
$path_set_registry[domain] = ActionView::PathSet.new(paths)
end
@r38y
r38y / class_method.rb
Last active August 29, 2015 13:57
View paths using the prepend_view_path class method vs instance method
#<ActionView::PathSet:0x007fc51b65c240 @paths=[#<ActionView::OptimizedFileSystemResolver:0x007fc513e22810 @pattern=":prefix/:action{.:locale,}{.:formats,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fc513e227e8 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fc513e227c0 @backend={}, @default_proc=#<Proc:0x007fc51b545208@/Users/r38y/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/actionpack-4.0.3/lib/action_view/template/resolver.rb:48 (lambda)>>>, @path="/Users/r38y/Sites/bntp.bauernet.me/app/views/site_overrides/j-14.com">, #<ActionView::OptimizedFileSystemResolver:0x007fc51b54d4f8 @pattern=":prefix/:action{.:locale,}{.:formats,}{.:handlers,}", @cache=#<ActionView::Resolver::Cache:0x007fc51b54d4d0 @data=#<ActionView::Resolver::Cache::SmallCache:0x007fc51b54d4a8 @backend={}, @default_proc=#<Proc:0x007fc51b545208@/Users/r38y/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/actionpack-4.0.3/lib/action_view/template/resolver.rb:48 (lambda)>>>, @path="/Users/r38y/Sites/bntp.bauernet.me/app/views">]
➜ disko git:(fix-rubypants) ✗ rbenv local 2.1.0
➜ disko git:(fix-rubypants) ✗ rspec
.......................................................................................................................................................................................
Finished in 9.92 seconds
183 examples, 0 failures
Randomized with seed 4522
# Add the following to your Gemfile
# gem 'rubypants-unicode', '~>0.2.3', require: false
begin
require 'rubypants-unicode'
rescue LoadError => _
abort "Missing dependency 'rubypants-unicode' for RubyPantsFilter."
end
module HTML
@r38y
r38y / four_oh_four.rb
Last active December 21, 2015 18:09
Render custom template for 404s
module FourOhFour
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::RecordNotFound, ActionController::RoutingError, with: :render_404
end
def render_404
render template: 'error_pages/404', status: :not_found, format: [:html] and return false
end
$(document).on 'click', '[data-confirm]', (event) ->
$target = $(event.target)
unless confirm($target.data('confirm'))
event.preventDefault()
return false
@r38y
r38y / console_fallback.js
Last active August 15, 2016 08:36
Make console.log bulletproof.
// see if the browser supports console.log - fail gracefully if it doesn't
try {
console.log();
console.debug();
console.assert();
} catch(e) {
console = {
log: function() {},
debug: function() {},
assert: function() {}
@r38y
r38y / rangy.rb
Created August 22, 2013 20:13
Controller concern for dealing with some range errors from misbehaving visitors.
module Rangy
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::StatementInvalid, RangeError, ArgumentError do |exception|
if exception.message =~ /is out of range for type integer|invalid offset|invalid value for Integer/
render text: 'Not found', status: :not_found
end
end
end
$(document).ready(function() {
var token = $('meta[name="csrf-token"]').attr('content')
$.ajaxPrefilter(function(options, originalOptions, xhr){
xhr.setRequestHeader('X-CSRF-Token', token);
})
})