Skip to content

Instantly share code, notes, and snippets.

View watson's full-sized avatar

Thomas Watson watson

View GitHub Profile
@watson
watson / mailer.rb
Created February 27, 2011 08:54
Setup SendGrid SMTP
class Mailer < ActionMailer::Base
def test
recipients "you@example.com"
from "you@example.com"
subject "Testing SendGrid"
end
end
@watson
watson / _form.html.erb
Created August 2, 2011 09:44
ActiveAdmin helper issue: Changes in user_helper.rb doesn't take effect until after a server restart
<%= my_helper %>
@watson
watson / dynamic_const_reference.rb
Created August 17, 2011 12:29
How to access a class constant dynamically from inside a module when the module is included into the class
module Foo
def foo
# This does not work:
# TYPES
# This works:
self.class.const_get('TYPES')
end
end
@watson
watson / development.log
Created September 5, 2011 10:46
This is a part of my development.log for a Rails 3.1.0.rc5 project. I was submitting an edit view just as the browser was requesting a file from the server (content.css). As you can see from the log it has mixed up the GET and the PUT call
Started GET "/assets/tiny_mce/themes/advanced/skins/default/content.css" for 127.0.0.1 at 2011-09-05 12:40:25 +0200
Processing by Admin::DealsController#update as HTML
Parameters: {...}, "commit"=>"Update Deal", "id"=>"127", "locale"=>"en"}
User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 165 LIMIT 1
Served asset /tiny_mce/themes/advanced/skins/default/content.css - 304 Not Modified (3ms)
Organization Load (1.4ms) SELECT `organizations`.* FROM `organizations` INNER JOIN `configurations` ON `configurations`.`organization_id` = `organizations`.`id` WHERE `configurations`.`domain` = 'downtown.dev' LIMIT 1
<snip>
(0.8ms) COMMIT
Redirected to http://localhost:3000/en/admin/deals/127
Completed 302 Found in 1601ms
@watson
watson / ability.rb
Created September 20, 2011 14:46
CanCan abilities based on attribute value
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :manage, Post
cannot [:create, :update, :destroy], Post, :state => 'published'
if user.role?('moderator')
@watson
watson / unblock.rb
Created October 3, 2011 07:59
Use this shell-script to change your DNS servers to the European Unblock-US servers via one simple command. Requires the 'open4' gem.
#!/usr/bin/env ruby
begin
require 'rubygems' if RUBY_VERSION =~ /^1\.8/
require 'open4'
rescue LoadError
puts "Please install the `open4` gem"
exit 1
end
@watson
watson / ability.rb
Created October 5, 2011 09:50
Active Admin CanCan integration with shared front/backend User model and multi-level autherization
# app/models/ability.rb
# All front end users are authorized using this class
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
@watson
watson / dashboards.rb
Created October 19, 2011 08:59
As per request by http://groups.google.com/group/activeadmin/msg/87ae141ae19e889d here's my Active Admin dashboard and routes
ActiveAdmin::Dashboards.build do
end
@watson
watson / gist:3256106
Created August 4, 2012 08:45
Node.js server and the difference between `curl -i` and `curl -I`
watson% curl -I http://localhost:8080/v1/alive
HTTP/1.1 404 Not Found
Date: Sat, 04 Aug 2012 08:43:58 GMT
Connection: keep-alive
watson% curl -i http://localhost:8080/v1/alive
HTTP/1.1 200 OK
Date: Sat, 04 Aug 2012 08:43:59 GMT
Connection: keep-alive
Transfer-Encoding: chunked
@watson
watson / break_execution_automatically1.js
Created October 9, 2012 04:52
Gotchas when handling errors in the connect module
// This will break the program and exist to console with code 1
var connect = require('connect');
connect()
.use(function(req, res) {
setTimeout(function() {
// this is wrapped inside a setTimeout callback and will not get
// catched by the connect module. Instead node.js will see it as
// an uncaught exception and exist with code 1