Skip to content

Instantly share code, notes, and snippets.

def StandardError *names
names.each{ |n| const_set n, Class.new(StandardError) }
end
@seanlinsley
seanlinsley / ransack_form.rb
Created April 17, 2013 23:06
Debugging for github.com/ernie/ransack/issues/173 and github.com/gregbell/active_admin/pull/1979
gem 'rails', '3.2.13'
gem 'ransack'
require 'rails/all'
require 'ransack'
ActiveRecord::Base.establish_connection adapter: 'sqlite3',
database: ':memory:'
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
@seanlinsley
seanlinsley / wire.rb
Created June 5, 2013 19:28
Ever wonder what a block is doing?
# This class lets you evaluate any block
# and record any methods it calls.
#
# Wire.listen { foo + bar }
# # => [:foo, :bar, :+]
#
# You're also able to chain method calls!
#
# Wire.listen { foo + bar.baz }
# # => [:foo, :bar, :baz, :+]
@seanlinsley
seanlinsley / application_helper.rb
Created January 12, 2014 23:45
Use Arbre in a helper!
module ApplicationHelper
def arbre(&block)
Arbre::Context.new(&block).to_s
end
end
@seanlinsley
seanlinsley / gist:9196255
Created February 24, 2014 20:23
NGINX: cache those compiled assets!
location ~* "-[a-z0-9]{32}\.(png|gif|jpg|jpeg|css|js)$" {
expires max;
}
# The Active Admin equivalent of putting this in your application layout:
# <head>
# <%= cloudinary_js_config %>
# </head>
module ActiveAdmin
module Views
module Pages
class Base < Arbre::HTML::Document
@seanlinsley
seanlinsley / active_admin.scss
Last active April 18, 2018 19:33
Make Active Admin's primary color change between dev/staging/production
// SASS variable overrides must be declared before loading up Active Admin's styles.
//
// To view the variables that Active Admin provides, take a look at
// `app/assets/stylesheets/active_admin/mixins/_variables.css.scss` in the
// Active Admin source.
//
// For example, to change the sidebar width:
// $sidebar-width: 242px;
$primary-color: dynamic_active_admin_color();
@seanlinsley
seanlinsley / foo.rb
Created May 18, 2014 03:56
Strip empty strings!
class Foo < ActiveRecord::Base
before_save :strip_empty_strings
def strip_empty_strings
self.class.columns.select{ |c| c.type == :string }.each do |c|
send "#{c.name}=", send(c.name).try(:strip).presence # sets to nil if completely empty
end
end
end
@seanlinsley
seanlinsley / application.rb
Created May 19, 2014 01:40
Custom lib loading
module Example
class Application < Rails::Application
# Loads ruby files in a given directory.
# NOTE: `require_dependency` auto-reloads on file change when in development.
# POSTERITY: this was needed to prevent auto-namespacing of lib/a/b.rb files.
def loader(*dirs)
dirs.each do |dir|
Dir["#{Rails.root}/#{dir}/*.rb"].each do |path|
d = dir.gsub /lib\/?/, '' # removes lib and lib/
@seanlinsley
seanlinsley / def!.rb
Last active January 12, 2016 23:56
For slightly more future-proof monkey patches, raise an error if a method name starts being used upstream
# class SomethingYouDontControl
# def! :something_you_expect_not_to_exist do |arg|
# arg + 1
# end
# end
#
# I wanted to provide built-in memoization, but it's not currenlty possible
# while preserving strict argument checking. https://bugs.ruby-lang.org/issues/9777
#
module Kernel