Skip to content

Instantly share code, notes, and snippets.

def test_delete_middle
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
ListMixin.find(2).destroy
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
assert_equal 1, ListMixin.find(1).pos
assert_equal 2, ListMixin.find(3).pos
assert_equal 3, ListMixin.find(4).pos
Creating a Multi Page Form in Rails
Creating a multi-step wizard-like form is pretty difficult in Rails.
Especially when trying to apply REST and validations. There are many
different approaches to this problem, and the right one depends largely
on your application details. Here are four options, and then my final
recommendation. Skip to that if you're in a hurry.
1. Create the model at the beginning on the first page of the form, and
have each successive page update the model. Validations are tricky
@ryanb
ryanb / gist:4688
Created August 9, 2008 16:55 — forked from jlindley/gist:4683
# AR Object dup vs marshal benchmarks
#
# re: http://rails.lighthouseapp.com/projects/8994/tickets/785
#
# user system total real
# string saved in hash 0.060000 0.000000 0.060000 ( 0.051861)
# string marshalling 0.630000 0.000000 0.630000 ( 0.643304)
#
# object saved in hash 0.060000 0.000000 0.060000 ( 0.052789)
# object marshalling 7.020000 0.030000 7.050000 ( 7.073463)
# single controller
map.with_options :controller => 'info' do |info|
info.about 'info/about', :action => 'about'
info.privacy 'legal/privacy', :action => 'privacy'
info.license 'legal/license', :action => 'license'
end
# multiple controllers
map.about 'info/about', :controller => 'info', :action => 'about'
map.privacy 'legal/privacy', :controller => 'legal', :action => 'privacy'
def show
@category = Category.find_by_permalink(params[:category])
@page = @category.pages.find_by_permalink(params[:page])
end
@ryanb
ryanb / hello.c
Created September 19, 2008 15:42
/*
PROBLEM:
>> h = Hello.new('hi!')
=> #<Hello:0x4b730>
>> h.greet
hi!=> nil
>> h.greet
hi!=> nil
..
>> h.greet
require 'rubygems'
require 'gosu'
class GameWindow < Gosu::Window
def initialize
super(640, 480, false)
self.caption = "Ruby Warrior"
@board = Board.new(self, 8, 1)
@unit = Unit.new(self)
@board.units << @unit
@ryanb
ryanb / gist:71618
Created February 27, 2009 18:24 — forked from lukeredpath/gist:71585
class Tag
has_many :taggings
named_scope :important, :include => { :taggings => :important }
end
class Tagging
belongs_to :tag
belongs_to :posts
# uses a string 'flag' column but could be changed to a boolean important
We couldn’t find that file to show.
class Article < ActiveRecord::Base
xapit do |index|
index.text :description do |desc|
desc.split.map do |word|
if word.include? '-'
[word, word.gsub('-', ''), word.gsub('-', ' ')]
else
word
end
end.flatten