This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def show | |
@category = Category.find_by_permalink(params[:category]) | |
@page = @category.pages.find_by_permalink(params[:page]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Examples of Nesting Associated Attributes | |
# This is a look at the various ways to set associated attributes through | |
# nested parameters. For example, let's say Project has_many :tasks and | |
# we want to update the tasks the same time we edit a project. There are | |
# several ways this interface could be handled, and here are a few. | |
# | |
# For these examples, we're trying to create two new tasks and update an | |
# existing task (with id 3) through a project. | |
# Approach 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
PROBLEM: | |
>> h = Hello.new('hi!') | |
=> #<Hello:0x4b730> | |
>> h.greet | |
hi!=> nil | |
>> h.greet | |
hi!=> nil | |
.. | |
>> h.greet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer