Skip to content

Instantly share code, notes, and snippets.

#Taken from http://coderrr.wordpress.com/2008/04/22/building-the-right-class-with-sti-in-rails/
class GenericClass < ActiveRecord::Base
class << self
def new_with_cast(*a, &b)
if (h = a.first).is_a? Hash and (type = h[:type] || h['type']) and (klass = type.constantize) != self
raise "wtF hax!!" unless klass < self # klass should be a descendant of us
return klass.new(*a, &b)
end
class Commit < Struct.new(:path)
class << self
def all
commits = `git log --decorate --pretty=oneline`
[].tap do |refs|
commits.split(/\n/).each do |commit|
refs << Commit.new(commit)
end
end
osascript -e "set Volume 10" && say "this will turn your volume on and say something"
# This allows you to have namespaced models in fixjour
module Fixjour
class Builder
def name
@name ||= (@options[:as] || @klass.name.underscore).to_s.gsub("/", "_")
end
end
end
# created by Peter Jaros and Jeff Dean
require "rubygems"
require "spec"
require "active_support"
module Weekdays
def advance_weekdays(days)
d = self
days.times do |day|
@zilkey
zilkey / data centric state machine.rb
Created July 24, 2009 06:17
Data-backed state machine example
=begin
See http://www.websequencediagrams.com/ for awesome diagrams. It has an api as well.
DSL-based state machine implementations make it hard to follow good coding practices, such as:
- keeping things DRY
- making sure that objects have single responsibility
- making sure that classes change at the same pace (open closed)
- making sure that classes only depend on classes that change less often than they do
def in_memory_database?
ENV["RAILS_ENV"] == "test" and
ENV["IN_MEMORY_DB"] and
Rails::Configuration.new.database_configuration['test-in-memory']['database'] == ':memory:'
end
$TESTING=true
if in_memory_database?
puts "connecting to in-memory database ..."
When /^I type "([^\"]*)" into the "([^\"]*)" rich text editor$/ do |description, iframe_id|
selenium.select_frame "id=#{iframe_id}"
selenium.type_keys "css=body", description
selenium.select_frame "relative=top"
end
# app/views/hellos/erb.html.erb
# note the leading spaces - this makes it exactly the same number of bytes that erector outputs
<%- 1000.times do -%><%= render :partial => "simple" %><%- end -%>
# app/views/hellos/_simple.html.erb
<p><%= "hello world" %></p>
class Views::Layouts::Application < Erector::Widget
def render
div :class => "header" do
text "header"
end
div :class => "body" do
inner_content
end