Skip to content

Instantly share code, notes, and snippets.

View cjbottaro's full-sized avatar

Christopher J. Bottaro cjbottaro

  • AcademicWorks, Inc
  • Austin, Tx
View GitHub Profile
class Person < ActiveRecord::Base
has_many :addresses
validates_associated :addresses
validates_length_of :addresses, :less_than => 5
before_save :save_addresses
def addresses_from_form=(address_hashes)
addresses = address_hashes.collect{ |address_hash| Address.new(address_hash) }
self.addresses.target = addresses # sets the association target, doesn't trigger any saves or destroys
# this works because validates_associated basically just does self.addresses.each{ |address| address.valid? }
require 'rubygems'
require 'activesupport'
dt = DateTime.now
puts dt.to_f
# => 1240455468.91318
t = YAML.load(dt.to_yaml)
puts t.class.name
# => Time
puts t.to_f
# => 1240455468.0
require 'rubygems'
require 'activesupport'
dt = DateTime.now
puts dt.utc.to_f
# => 1240456717.12572
puts dt.to_time.to_f
# => 1240456717.12572
puts dt.utc.to_time.to_f
# => 1240455680.0
puts dt.to_time.utc.to_f
if File.exists?("my_git_ignored_file.yml")
::AppConfig = ApplicationConfiguration.new("my_git_ignored_file.yml")
else
::AppConfig = ApplicationConfiguration.new("my_normal_config_file.yml")
end
AppConfig.use_environment!(RAILS_ENV)
# app_config.yml
# --------------
# development:
# env: dev
# server: dev.domain.com
# production:
# env: prod
# server: prod.domain.com
#
# local_override.yml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>
<body></body>
</html>
class ApplicationController < ActionController::Base
param_accessible [:id, :page]
end
# This will filter any param except for :id and :page for ALL controllers in your entire application (because you're calling param_accessible on ApplicationController).
@cjbottaro
cjbottaro / gist:1128091
Created August 5, 2011 17:50
DRY'ed rspec using shared_context with a block
shared_context "for_extending" do
context "extended with MyModule" do
before(:all){ subject.extend(MyModule) }
it "should define #some_method" do
subject.should respond_to(:some_method)
end
yield
end
end
@cjbottaro
cjbottaro / gist:1128108
Created August 5, 2011 17:54
unDRY rspec code
describe Object do
context "extented with MyModule" do
before(:all){ subject.extend(MyModule) }
it "should define #some_method" do
subject.should respond_to(:some_method)
end
end
end
describe Class do
@cjbottaro
cjbottaro / gist:1152006
Created August 17, 2011 16:55
cron job
#!/bin/bash
RVM_BIN=/usr/local/rvm/bin/rvm
EDITOR_UI=/home/onespot/editor-ui/current
$RVM_BIN 1.9.2@editor-ui ruby $EDITOR_UI/bin/openx_report.rb -e production > $EDITOR_UI/log/openx_report.log 2>&1