Skip to content

Instantly share code, notes, and snippets.

View andreacampi's full-sized avatar

Andrea Campi andreacampi

  • Apple, inc
  • London, UK
View GitHub Profile
@wagenet
wagenet / better_default_scope.rb
Created March 18, 2009 15:30
Improved Nested Scopes, Including Blocks for Rails 2.3
# Based on http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1812-default_scope-cant-take-procs
ActiveRecord::Base.class_eval do
class << self
private
def find_last(options)
order = options[:order]
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :admin
can :manage, :all
else
can :read, :all
namespace :sass do
desc 'Updates stylesheets if necessary from their Sass templates.'
task :update => :environment do
sh "rm -rf public/stylesheets/compiled tmp/sass-cache"
Sass::Plugin.options[:never_update] = false
Sass::Plugin.update_stylesheets
end
end
@mnutt
mnutt / Instrument Anything in Rails 3.md
Created September 6, 2010 06:50
How to use Rails 3.0's new notification system to inject custom log events

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

@tnine
tnine / ebs_raid.rb
Created April 27, 2011 03:03
Ebs raid mounting. Links to this jira issue http://tickets.opscode.com/browse/CHEF-2275
include Opscode::Aws::Ec2
#Auto locates and attached ebs devices based on the data bag they reside in. The following test cases need to be performed
# Create multiple resources with new node: PASS
#
# Re-attach multiple resources after a reboot: PASS
#
# Create resources across 2 runs. First run creates first raid set, second run re-attaches then creates: PASS
#
@pbosetti
pbosetti / shared.rb
Created April 28, 2011 18:35
IPC shared memory access via FFI
#!/usr/bin/env ruby
# untitled.rb
# Created by Paolo Bosetti on 2011-04-22.
# Copyright (c) 2011 University of Trento. All rights reserved.
require "rubygems"
require 'ffi'
module Shared
@nstielau
nstielau / Bash example
Created May 10, 2011 17:23
A Chef report_handler and knife plugin to persist and query updated actions
Nick-Stielaus-MacBook-Pro:chef-repo nstielau$ knife node log nstielau_vagrantup.com
Time Recipe Action Resource Type Resource
Tue May 10 10:25:20 -0700 2011 nginx::source create template proxy.conf
Tue May 10 10:25:20 -0700 2011 nginx::source enable service nginx
Tue May 10 09:50:42 -0700 2011 nginx::source create template /etc/init.d/nginx
@MEHColeman
MEHColeman / matz_keynote
Created May 28, 2011 08:25
Matz EuRuKo keynote notes
Matz keynote
What Matz is working on now: ruby is now a JIS standard language, and has just been submitted to ISO. 
The standard is a subset of the language and is 1.8 and 1.9 compatible (so doesn't cover the differences between the two).
Hopefully this will encourage interest from larger enterprises. 
Matz looking to hire full time ruby core developers
Matz's goal is to make ruby developers happy. A few years ago, the Ruby Association was formed to create a dialog with businesses, to make ruby more business friendly. 
Matz points to jruby and rubinius (as well as 1.8 and 1.9) as great examples of ruby implementations  (as well as a bunch of others)
Jruby is fast. And has a great gc. 
@grantr
grantr / gist:1105416
Created July 25, 2011 22:31
Chef mysql master/slave recipes
## mysql::master
ruby_block "store_mysql_master_status" do
block do
node.set[:mysql][:master] = true
m = Mysql.new("localhost", "root", node[:mysql][:server_root_password])
m.query("show master status") do |row|
row.each_hash do |h|
node.set[:mysql][:master_file] = h['File']
node.set[:mysql][:master_position] = h['Position']
end
@hyle
hyle / ko.utils.signatures.js
Last active August 6, 2024 08:17
KnockoutJS utils (ko.utils) signatures
// knockout 2.2.1
ko.utils.arrayFilter = function (array, predicate) { /* .. */ }
ko.utils.arrayFirst = function (array, predicate, predicateOwner) { /* .. */ }
ko.utils.arrayForEach = function (array, action) { /* .. */ }
ko.utils.arrayGetDistinctValues = function (array) { /* .. */ }