Skip to content

Instantly share code, notes, and snippets.

### Basic routes ###
Rails 2:
# singular resource
map.resource :account
# resources with addition member & collection actions
map.resources :games, :member => { :download => :get }, :collection => { :favourites => :get }
Rails 3:
# Factory girl, relaxed.
#
# Factory.define :user do |f|
# f.login 'johndoe%d' # Sequence.
# f.email '%{login}@example.com' # Interpolate.
# f.password f.password_confirmation('foobar') # Chain.
# end
#
# Factory.define :post do |f|
# f.user { Factory :user } # Blocks, if you must.
class ImageMagick
def self.palette(image_file, number_of_colours = 5)
image_details = %x(convert #{image_file} +dither -depth 8 -colors #{number_of_colours} -unique-colors txt:-)
image_details.scan(/#[a-z0-9]{6}/i)
end
end
gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
# Rails 3 generator for miniskirt
# Add the factory_girl gem
append_file 'Gemfile', <<-GEMS
group :test do
gem 'miniskirt'
end
GEMS
# touch the file for storing factories
MVIM Cheat Sheet:
* auto complete
ctrl+n
or
ctrl+p
* Running Rake for test
:Rake
# Create the javascripts dir
run 'mkdir public/javascripts'
# pull down jquery, jquery-ui and the appropriate rails driver
run 'curl -L http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js > public/javascripts/jquery.js'
run 'curl -L http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js > public/javascripts/jquery-ui.js'
run 'curl -L http://github.com/rails/jquery-ujs/raw/master/src/rails.js > public/javascripts/rails.js'
# Create a fresh git repo
git :init
git :add => "."
git :commit => "-m 'Initial commit.'"
# Create the javascripts dir
run 'mkdir public/javascripts'
<link rel="apple-touch-icon" media="screen and (resolution: 163dpi)" href="57px.png" />
<link rel="apple-touch-icon" media="screen and (resolution: 132dpi)" href="72px.png" />
<link rel="apple-touch-icon" media="screen and (resolution: 326dpi)" href="114px.png" />
# divide an array with one pint too many
class Array
def /(divider)
divider = divider.to_i
collection_size = size / divider
array = []
each_with_index do |element, index|
index % collection_size == 0 && divider != array.size ? array << [element] : array.last.push(element)
end
array.size == 1 ? array.flatten! : array