Skip to content

Instantly share code, notes, and snippets.

#
# An improvement on http://stackoverflow.com/a/9094206/284612
#
# Place this file in spec/support/signed_cookies.rb
#
module SignedCookies
def signed_cookie(name, opts={})
verifier = ActiveSupport::MessageVerifier.new(request.env["action_dispatch.secret_token".freeze])
if opts[:value]
@request.cookies[name] = verifier.generate(opts[:value])

RubyMotion comes with some syntax idioms that are not strictly idiomatic Ruby.

For example, in an iOS app we'd often see these two methods defined in a subclass of UITableViewController

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // callback code for selecting table rows goes here...
cat example.com.access.log | cut -f1 -d ' ' | sort | uniq -c | sort -g -r
@chrisgaunt
chrisgaunt / gitio
Created February 10, 2013 07:38 — forked from defunkt/gitio
#!/usr/bin/env ruby
# Usage: gitio URL [CODE]
#
# Turns a github.com URL
# into a git.io URL
#
# Copies the git.io URL to your clipboard.
url = ARGV[0]
code = ARGV[1]
# We need this to fix the random timeout error that we were seeing in CI.
# May be related to: http://code.google.com/p/selenium/issues/detail?id=1439
Capybara.register_driver :selenium_with_long_timeout do |app|
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120
Capybara::Driver::Selenium.new(app, :browser => :firefox, :http_client => client)
end
Capybara.javascript_driver = :selenium_with_long_timeout
@chrisgaunt
chrisgaunt / gist:3635477
Created September 5, 2012 11:46 — forked from dhh/gist:1014971
Use concerns to keep your models manageable
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@chrisgaunt
chrisgaunt / passwords_controller.rb
Created August 29, 2012 02:01 — forked from guilleiguaran/passwords_controller.rb
StrongParameters with Devise
# app/controllers/users/password_controller.rb
class Users::PasswordsController < Devise::PasswordsController
def resource_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
private :resource_params
end
@chrisgaunt
chrisgaunt / hashtag_counter.rb
Created May 24, 2012 22:57 — forked from kuntoaji/hashtag_counter.rb
Ruby script to count a twitter hashtag and save the result and last_tweet_id to hashtag_counter_result.txt and last_tweet_id.tx
#!/usr/bin/env ruby
# Twitter Hashtag Counter
#
# Script to count a twitter hashtag and save the result and last_tweet_id to
# hashtag_counter_result.txt and last_tweet_id.txt
#
# Requirements: twitter gem.
# To install twitter gem: [sudo] gem install twitter
require 'rubygems'
@chrisgaunt
chrisgaunt / address_spec.rb
Created April 26, 2012 04:21 — forked from moklett/address_spec.rb
Toying around with an RSpec DSL for permutations. My real use case is more complex than this example - the large numbers of inputs and outputs makes it more concise to "demonstrate" correctness and expectations than to "spec" it out with traditional, one
describe Address do
permute "validations" do
cases [
[:address, :address_2, :city, :state, :zip, :country, :valid, :errors_on],
[ nil, nil, nil, nil, nil, nil, false, ['address', 'city', 'state', 'zip', 'country']],
['123 Main St', 'Apt 100', 'Pleasantville', 'NC', '12345', 'US', true, []],
['123 Main St' 'Apt 100', 'Pleasantville' 'NC' '12345' nil, false ['country']]
['123 Main St' 'Apt 100', 'Pleasantville' 'NC' '12345' 'U', false ['country']]
['123 Main St' 'Apt 100', 'Pleasantville' 'NC' '12345' 'USA', false ['country']]
]
@chrisgaunt
chrisgaunt / rails_test_engine_cucumber.markdown
Created August 3, 2011 03:57
Rails 3.1: Test engine with Cucumber & test/dummy app

Run from the root of the engine project:

rails generate cucumber:install --capybara

Edit features/support/env.rb and add to the top:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../../test/dummy/config/environment.rb",  __FILE__)
ENV["RAILS_ROOT"] ||= File.dirname(__FILE__) + "../../../test/dummy"