Skip to content

Instantly share code, notes, and snippets.

View ohammersmith's full-sized avatar

Otto Hammersmith ohammersmith

View GitHub Profile
@ohammersmith
ohammersmith / generate.sh
Created April 29, 2009 22:38
Snippets needed to make webrat work in rails integration tests.
./script/generate integration selenium_attach_file
describe "prototype widget helper", :type => :view do
describe "tab1_link" do
it "link does some stuff" do
pending
render(:inline => "<%= tab1_link %>")
# response.should have_tag("a")
# puts "<pre>"
# puts response.body
# puts "</pre>"
map.resources :users, :controller => 'clearance/users' do |users|
users.resource :password,
:controller => 'clearance/passwords',
:only => [:create, :edit, :update]
users.resource :confirmation,
:controller => 'clearance/confirmations',
:only => [:new, :create]
end
irb(main):001:0> h = {:foo => "bar", :bar => "foo"}
=> {:bar=>"foo", :foo=>"bar"}
irb(main):002:0> h
=> {:bar=>"foo", :foo=>"bar"}
irb(main):003:0> h.respond_to?(:all?)
=> true
irb(main):004:0> h.all? { |k, v| puts k }
bar
=> false
Given /^There (?:is|are) (\d+|a) (.*?)(.*?)(?: with "(.*?)" for (.*?))?$/ do |count, model, factory_suffix, value, attribute|
count = 1 if count == "a"
factory_name = model.singularize << factory_suffix.gsub(" ", "_")
if attribute and value
count.to_i.times { Factory(factory_name.to_sym, attribute.to_sym => value) }
else
count.to_i.times { Factory(factory_name.to_sym) }
end
end
describe "#route_for" do
it "should be nested shallowly under forums as topics" do
params_from(:get, '/forums/123/topics').should == {:controller => "forum_posts", :action => "index", :forum_id => "123" }
end
it "should be nested shallowly under topics as posts" do
params_from(:get, '/topics/123/posts').should == {:controller => "forum_posts", :action => "index", :topic_id => "123" }
params_from(:get, '/posts/321').should == {:controller => "forum_posts", :action => "show", :id => "321" }
end
Given /^WH([O]+)AH (.*)$/ do |ohs, actual_step|
ohs.length.times { sleep 10 }
Given actual_step
end
# save and open page before
Given /^SAOP([BA]) (.*)$/ do |before_or_after, actual_step|
save_and_open_page if before_or_after == "B"
Given actual_step
save_and_open_page if before_or_after == "A"
<h1>Custom form demo</h1>
<!-- maybe the real thing -->
<% form_tag(:action => "create") do %>
<% for question in @questions %>
<%= hidden_field_tag "responses[][#{question.id}][type]", question.q_type %>
<% if question.q_type == "checkbox" %>
Checkbox <br/>
<% form_tag(:action => "create") do %>
<% for question in @questions %>
<%= h question[:name] %>
<%= hidden_field_tag "responses[][question_id]", question[:id] %>
<%= check_box_tag "responses[][flag]" %> <br />
<% end %>
<%= submit_tag "Post" %>
<% end -%>
@ohammersmith
ohammersmith / 00-fully-operational-rake-routes.txt
Created April 14, 2009 23:41
Working out nested resources with a self-referential association
(in /Users/otto/Desktop/Incubator/shallow_routes)
communities GET /communities(.:format) {:action=>"index", :controller=>"communities"}
POST /communities(.:format) {:action=>"create", :controller=>"communities"}
new_community GET /communities/new(.:format) {:action=>"new", :controller=>"communities"}
edit_community GET /communities/:id/edit(.:format) {:action=>"edit", :controller=>"communities"}
community GET /communities/:id(.:format) {:action=>"show", :controller=>"communities"}
PUT /communities/:id(.:format) {:action=>"update", :controller=>"communities"}
DELETE /communities/:id(.:format) {:action=>"destroy", :controller=>"communities"}
community_forums GET /communities/:community_id/forums(.:format) {:action=>"index", :controller=>"forums"}