Skip to content

Instantly share code, notes, and snippets.

# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
$:.unshift File.dirname(__FILE__)
require "rails_app/config/environment"
require 'rails/test_help'
require 'rspec/rails'
require 'active_record'
require 'logger'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:database => 'test'
)
@ericallam
ericallam / show.html.erb
Created August 26, 2011 16:37
Rails for Zombies 2 – Challenge 5-9
<p id="notice"><%= notice %></p>
<ul>
<li>
<em>Name:</em>
<%= @weapon.name %>
</li>
<li>
<em>Condition:</em>
<span id="condition"><%= @weapon.condition %></span>
<%= link_to "Toggle", toggle_condition_user_weapon_path(@user, @weapon), remote: true %>
@ericallam
ericallam / Gemfile
Created August 28, 2011 14:55
Using the Asset Pipeline outside of Rails - Serving CoffeeScript and SASS
source "http://rubygems.org"
gem 'sprockets', :git => 'git://github.com/sstephenson/sprockets.git'
gem 'coffee-script'
gem 'sass'
gem 'rack-test'
gem 'sinatra'
@ericallam
ericallam / module_function.rb
Created September 12, 2011 23:13
Using module_function instead of extend self
# a lot of people in ruby do this
module Foo
extend self
def bar
'bar'
end
end
Foo.bar
@ericallam
ericallam / heredocs.rb
Created September 14, 2011 14:05
Ruby Heredocs
# by default, heredoc strings are treated like double-quoted ruby strings, which means you can do string interpolation:
<<-EOF
The time is #{Time.now}
EOF
# => The time is Wed Sep 14 10:04:29 -0400 2011
# But you can create heredoc strings that are treated like single-quoted string, like this:
<<-'EOF'
def products_page
@page.our_products?
end
helper_method :products_page
class Page
OUR_PRODUCTS_TITLE = 'Our Products'
def our_products?
@ericallam
ericallam / app.coffee
Created November 8, 2011 20:02
Set certain regions of an Ace editor to readOnly
event = require('pilot/event')
Anchor = require('ace/anchor').Anchor
doc = ace_editor.session.getDocument()
editablePositions = [[1, 0, 2, 0]] # allow editong only the second row
jQuery.each editable, (index, row) ->
editablePositions.push [new Anchor(doc, row[0], row[1]), new Anchor(doc, row[2], row[3])]
Range = require('ace/range').Range
@ericallam
ericallam / routes.rb
Created November 9, 2011 15:23
Add routes outside of routes.draw
# Unfortunately, you still have to create the routes somewhere in routes.rb
Your::Application.routes.draw do
root :to => 'controller#action'
end
mapper = ActionDispatch::Routing::Mapper.new(Rails.application.routes)
mapper.instance_exec do
match '/some/action' => "some#action"
end
@ericallam
ericallam / foo.rb
Created November 21, 2011 19:34 — forked from ngauthier/foo.rb
currying an instance method
class Foo
def foo(x, y)
"foo! #{x} and #{y}"
end
def bar
# my imaginary syntax for "create functional reference from method foo with argument bar.
&(:foo, 5)
end
end