Skip to content

Instantly share code, notes, and snippets.

named_scope :active, :conditions => {:active => true}
def self.random
self.active.find(:first, :order => ‘RAND()’)
end
namespace :db do
namespace :fixtures do
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
task :dump => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info"]
ActiveRecord::Base.establish_connection(:development)
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
PORT = 22
USER = 'username'
SERVER = 'server name or ip'
DEST = 'web root on server'
desc 'Builds site using StaticMatic'
task :build do
system 'staticmatic build .'
end
class Contact
include ActiveModel::Validations
include ActiveModel::Serialization # should be => include ActiveModel::Serializers::Xml
validates_presence_of :email, :subject, :body
validates :email, :format => {
:with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
attr_accessor :email
attr_accessor :subject
def output(data, format)
case format
when :html
return "<p>#{data}</p>"
when :text
return data
when :pdf
return "<pdf>#{data}</pdf>" # pseudocode -- obviously not valid PDF output
else
raise ArgumentError, "Invalid format (#{format})."
HTMLFormatter = lambda { |data| "<p>#{data}</p>" }
TextFormatter = lambda { |data| data }
PDFFormatter = lambda { |data| "<pdf>#{data}</pdf" }
def output(data, format)
case format
when :html
return HTMLFormatter.call(data)
when :text
return TextFormatter.call(data)
# Lambda's are used here for simplicity. In reality each formatter would
# probably be a separate class.
FORMATTERS = {
:html => lambda { |data| "<p>#{data}</p>" },
:text => lambda { |data| data },
:pdf => lambda { |data| "<pdf>#{data}</pdf>" }
}
def output(data, format)
raise ArgumentError,
class Range
# Pick a random value from a Range.
# === Example
# (1..10).rand # => 3
def rand
a = self.to_a
a[Kernel::rand(a.size)]
end
end
def rand_between(min, max)
Kernel::rand(max-min+1) + min
end
@bnadlerjr
bnadlerjr / update_bundles
Created September 21, 2010 02:12 — forked from dmcinnes/update_bundles
Script for downloading vim files for use with pathogen.
#!/usr/bin/env ruby
git_bundles = [
"git://github.com/vim-bundles/fuzzyfinder.git",
"git://github.com/scrooloose/nerdcommenter.git",
"git://github.com/msanders/snipmate.vim.git",
"git://github.com/tpope/vim-cucumber.git",
"git://github.com/tpope/vim-haml.git",
"git://github.com/tpope/vim-rails.git",
"git://github.com/tpope/vim-surround.git"