Skip to content

Instantly share code, notes, and snippets.

View acts_as_list_test.rb
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
View multi_page_form_in_rails.txt
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
View gist:4688
# 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)
View routes.rb
# 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'
View pages_controller.rb
def show
@category = Category.find_by_permalink(params[:category])
@page = @category.pages.find_by_permalink(params[:page])
end
View hello.c
/*
PROBLEM:
>> h = Hello.new('hi!')
=> #<Hello:0x4b730>
>> h.greet
hi!=> nil
>> h.greet
hi!=> nil
..
>> h.greet
View gosu_example.rb
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
View gist:71618
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.
View gist:96659
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