Skip to content

Instantly share code, notes, and snippets.

@arches
arches / public-javascripts-base-flash.js
Created March 5, 2012 05:05
Javascript for Flash Messages
var Arch; if (!Arch) Arch = {}; // set up custom namespace 'arch'
Arch.Flash = {
showError: function(message) {
if (message == "") return;
$('#flash-msg .user-msg-detail').removeClass('notice');
$('#flash-msg .user-msg-detail').addClass('error');
$('#flash-msg .user-msg-detail span').html(message);
$('#flash-msg').show();
},
@alno
alno / bench_str_building.rb
Created January 30, 2012 13:39
Benchmark: interpolation vs concatenation in Ruby
require 'benchmark'
count = 1000000
Benchmark.benchmark do |bm|
bm.report("concat") { count.times { 11.to_s + '/' + 12.to_s } }
bm.report("interp") { count.times { "#{11}/#{12}" } }
end
@cgriego
cgriego / evil_object.rb
Created November 30, 2011 02:52
EvilObject
class EvilObject
class << self
def const_defined?(*)
true
end
def const_get(*)
Object
end
@cblunt
cblunt / Gemfile
Created October 21, 2011 08:55
Configure Carrierwave for Amazon S3 Storage and Heroku
# ...
gem 'carrierwave'
gem 'fog', '~> 1.0.0' # Need to specify version, as carrierwave references older (0.9.0) which doesn't allow configuration of Rackspace UK Auth URL
@jwo
jwo / registrations_controller.rb
Created September 30, 2011 23:11
API JSON authentication with Devise
class Api::RegistrationsController < Api::BaseController
respond_to :json
def create
user = User.new(params[:user])
if user.save
render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email), :status=>201
return
else
@runemadsen
runemadsen / description.markdown
Created September 26, 2011 15:23
Reverse polymorphic associations in Rails

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'my_access_key', # required
:aws_secret_access_key => 'my_secret_access_key', # required
:region => 'us-east-1' # optional, defaults to 'us-east-1'
}
config.fog_directory = 'my_bucket_name' # required
config.fog_host = 'https://my_bucket_name.s3.amazonaws.com' # optional, defaults to nil
config.fog_public = false # optional, defaults to true
@dallas
dallas / rails31init.md
Created September 11, 2011 05:22 — forked from docwhat/rails31init.md
Rails 3.1 with Rspec, Factory Girl, Haml, Simple Form, Database Cleaner, Spork, and Guard

Install Rails 3.1

gem install rails

generate new app, skipping Test::Unit file generation

rails new my_app -T

Set up Gemfile

@mimosz
mimosz / active_admin_views_pages_base.rb
Created August 31, 2011 09:50 — forked from jocubeit/active_admin_views_pages_base.rb
Override footer content in Active Admin gem
# encoding: utf-8
ActiveAdmin::Views::Pages::Base.class_eval do
private
# Renders the content for the footer
def build_footer
div :id => "footer" do
para "Copyright &copy; #{Date.today.year.to_s} #{link_to('Example.com', 'http://example.com')}. Powered by #{link_to('Active Admin', 'http://www.activeadmin.info')} #{ActiveAdmin::VERSION}".html_safe
end
end
end