Skip to content

Instantly share code, notes, and snippets.

View dscataglini's full-sized avatar

Diego Scataglini dscataglini

View GitHub Profile
@dscataglini
dscataglini / Product.rb
Created July 19, 2011 17:38
Product using bitmask for context filtering
class Product < ActiveRecord::Base
CONTEXTS = %w{institutions individuals basic_individuals}
acts_as_list
named_scope :with_context, lambda{ |context| { :conditions => "contexts_mask & #{ 2**CONTEXTS.index( context ) } > 0" } }
named_scope :visible, :conditions => {:visible => true}
named_scope :visible_position, :order => "position"
has_many :media_assets, :as => :attachable, :dependent => :destroy
after_update :save_media_assets
def new_media_assets=(attributes)
@dscataglini
dscataglini / font_size_matrix.rb
Created December 14, 2010 20:39
silly script to print out a matrix of font-sizes
def font_size_matrix(default = 16)
puts "px\t%\t\s\sem"
(10..26).each do |n|
val = (n * 100) / default.to_f
puts "#{n}px\t%0.1f%%\t\s\s%0.3fem" % [val, val/100]
end
end
font_size_matrix
@dscataglini
dscataglini / linecache on rbx
Created November 24, 2010 20:58
can't compile
gem install linecache
Building native extensions. This could take a while...
ERROR: Error installing linecache:
ERROR: Failed to build gem native extension.
/Users/developer/.rvm/rubies/rbx-head/bin/rbx extconf.rb
creating Makefile
make
gcc -I. -I/Users/developer/.rvm/rubies/rbx-head/include -I/Users/developer/.rvm/rubies/rbx-head/include -I. -fPIC -ggdb3 -O2 -fPIC -c trace_nums.c
@dscataglini
dscataglini / 0001-gsub-should-raise-runtime-error-on-frozen-strings-wh.patch
Created November 24, 2010 17:12
gsub! should raise runtime error on frozen strings when block is given and match is found
From 02edb2d2de470bec7bcdd030bb05ed7c7c967907 Mon Sep 17 00:00:00 2001
From: Diego Scataglini <[email protected]>
Date: Wed, 24 Nov 2010 12:11:20 -0500
Subject: [PATCH] gsub! should raise runtime error on frozen strings when block is given and match is found
---
kernel/common/string.rb | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)
diff --git a/kernel/common/string.rb b/kernel/common/string.rb
@dscataglini
dscataglini / yield_if.rb
Created November 19, 2010 22:04
It should execute the block only if the passed in value is true, and return the conditional. Caller can optionally pass a value to be yield
def yield_if?(bool = false, value = nil)
yield(value) if bool && block_given?
bool
end
module MyRakeContext
class MyClass < ::MyClass
#... different logic here
end
rakeCommand()
end
hash = Hash.new(&(p = lambda{|hsh, key| hsh[key] = Hash.new(&p)}))
# now you can do this
hash[:this][:is][:very][:cool] = "Yeah baby, yeah ... "
#=> "Yeah baby, yeah ... "
hash
#=> {:this=>{:is=>{:very=>{:cool=>"Yeah baby, yeah ... "}}}}
# this is an experiment that I tried about 3 years ago.
# instead of calling asset.attachable.post.user.login just calling asset.login
class User < ActiveRecord::Base #fields are :login, :email
has_many :posts
has_many :channels
end
class Channel < ActiveRecord::Base #fields are :private(bool), :channel_type, :name
belongs_to :user
# Usage include Categorizable::Categorized or Categorizable::Categorizable
module Categorizable
module Categorized
def self.included(klass)
associations = [
[:has_many, :categorings],
[:has_many, :content_categories, {:through => :categorings}],
[:has_one, :primary_categoring, {:conditions => {:primary => true}, :class_name => "Categoring"}],
[:has_one, :primary_content_category, {:through => :primary_categoring}]
var start = new Date();
var end = new Date();
end.setDate(end.getDate() + 7);
$.fullCalendar.gcalFeed("http://www.google.com/calendar/feeds/b878ac83tpkjckoqh1mccmsrfk%40group.calendar.google.com/public/basic",{currentTimezone: 'America/New_York'})(start, end, function(events){
var now = new Date();
var VALID_SHOW_TITLES = ["The Ruby Show", "The Conversation", "The Big Web Show", "The Dev Show", "EE Podcast"];
// auto detect from the page title if we should check for the next show for this page
var current_page_belongs_to_show = $.grep(VALID_SHOW_TITLES, function(el, idx){
return document.title.indexOf(el) > -1;