Skip to content

Instantly share code, notes, and snippets.

@standout
standout / post.rb
Created August 13, 2009 12:56
Super simple spam protection for Ruby on Rails
class Post < ActiveRecord::Base
attr_accessor :comment # fake attribute used for spam trapping
validates_length_of :comment, :in => 0..1
end
Hotel.populate 2..5 do |hostel|
hotel.city_id = city.id
hotel.name = Populator.words(1..2) + " hotel"
hotel.phone = Faker::PhoneNumber.phone_number
hotel.address = Faker::Address.street_address
Event.populate 4...8 do |event|
event.date_and_time = 3.days.from_now...10.months.from_now
event.title = Populator.words(2..5).titleize
ActiveRecord::Base.connection.execute("INSERT INTO events_hotels (event_id,
hotel_id) VALUES (#{event.id}, #{hotel.id})")
anonymous
anonymous / factories.rb
Created February 24, 2010 05:05
Factory.define :item do |f|
include ActionDispatch::TestProcess
f.name "Macbook Pro 15"
f.price_in_dollars 1500
f.photo fixture_file_upload('/files/avatar.jpg', 'image/jpg')
end
@zhengjia
zhengjia / capybara cheat sheet
Created June 7, 2010 01:35
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@fredlee
fredlee / gist:714442
Created November 24, 2010 21:21
'and' vs. '&&'
# This is a contrived example.
# But, this simple example demonstrates some code I have seen where not understanding the difference
# between 'and' and '&&' has caused unexpected behavior.
# The intent is to evaluate the 3rd clause if the 1st and 2nd clause is true.
# Again, this is a contrived example.
# But, imagine that the 1st and 2nd clause is a method invocation or something like that.
# This is a classic example where the goal of terseness really caused problems.
>> x = 10 && y = 5 && x - y
@henrik
henrik / nullify_blank_attributes.rb
Created November 27, 2010 10:11
ActiveRecord mixin to save NULL instead of empty strings to database.
module NullifyBlankAttributes
def write_attribute(attr_name, value)
new_value = value.presence
super(attr_name, new_value)
end
end
@oliverbarnes
oliverbarnes / env.rb
Created March 4, 2011 19:58
Cucumber setup with spork and database_cleaner for mongo_mapper
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
...cucumber and capybara requires...
require 'database_cleaner'
module ApplicationHelper
def private_cache (name = {}, &block)
if @user.blank?
yield and return #note.. I had to add this return..otherwise it rendered twice
else
cache(name, &block)
end
end
end
@mislav
mislav / gist:938183
Created April 23, 2011 02:28
Faraday SSL example
connection = Faraday::Connection.new('http://example.com') do |builder|
builder.request :url_encoded # for POST/PUT params
builder.adapter :net_http
end
# same as above, short form:
connection = Faraday.new 'http://example.com'
# GET
connection.get '/posts'
@ryanb
ryanb / rails_3_1_rc4_changes.md
Created May 6, 2011 01:10
The Changelogs for Rails 3.1 Beta 1

Railties 3.1 RC4

  • The new rake task assets:clean removes precompiled assets. [fxn]

  • Application and plugin generation run bundle install unless --skip-gemfile or --skip-bundle. [fxn]

  • Fixed database tasks for jdbc* adapters #jruby [Rashmi Yadav]

  • Template generation for jdbcpostgresql #jruby [Vishnu Atrai]