Skip to content

Instantly share code, notes, and snippets.

@bf4
bf4 / Gemfile
Created Sep 7, 2012
Ruby Time and Timezone walkthrough
View Gemfile
source "https://rubygems.org"
gem "rack"
gem "tzinfo"
@bf4
bf4 / Gemfile
Last active Nov 11, 2017
Download my destroyallsoftware videos
View Gemfile
# A sample Gemfile
source "https://rubygems.org"
gem 'mechanize'
View string_to_date.rb
# method to evaluate the block for a default value if the
# conversion to date fails
# string.to_date return nil if the string is blank
# so we yield to get the default value from the block there
# or on a parse error
def string_to_date(string,&block)
string.to_date || yield
rescue => e
block_given? ? yield : raise
end
@bf4
bf4 / child_index.html.haml
Created Dec 24, 2012
Rails template / layout inheritance
View child_index.html.haml
-content_for :title do
Child Title
The rest of the page goes here
@bf4
bf4 / lib_user_cache.rb
Created Dec 24, 2012
Two ways of clearing the cache in rails... needs work
View lib_user_cache.rb
module UserCache
def self.expire_cache(id)
@base ||= ActionController::Base.new
fragments = [ "user_#{id}", "mini_user_#{id}",
"admin_user_#{id}", "admin_mini_user_#{id}",
"admin_video_user_#{id}", "video_user_#{id}",
"archive_column", "archive",
"admin_archive_column", "admin_archive",
"embedded_video_#{id}"]
fragments.each do |fragment|
@bf4
bf4 / notes.rb
Created Dec 26, 2012
Rails error handling for the view
View notes.rb
# sources:
# http://www.igodigital.com/blog/blog/notes-on-cyber-weekend-targeted-email-campaigns/custom-error-handling-in-rails-303
# To test in dev, use the production setting below
# config/environments/development.rb
config.consider_all_requests_local = false
# delete the 404.html, and 500.html files.
# generate an error controller
`rails g controller Errors 404 500`
View build.sh
#!/usr/bin/env bash
export PATH="$PATH:/usr/texbin/"
which pandoc &>/dev/null
if [ $? -eq 0 ]
then
if [ ! -n "$1" ]
then
echo 'defaulting to pdf'
file_name='scalingphp.pdf'
else
View Rakefile
gem 'rake', '=0.8.7'
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
#TODO don't shell out quite as much
namespace :bootstrap do
task :dev => ['bootstrap:copy_configs', 'bootstrap:bundle_gems', 'bootstrap:database'] do
run_cmd "bundle exec rake db:test:prepare"
end
View contemplative.rb
require 'action_view'
require 'ostruct'
require 'erb'
require 'flog'
require 'ruby_parser'
require 'sexp_processor'
ERBHandler = ActionView::Template::Handlers::ERB.new
def new_template(body, details={format: :html})
View contemplative.rb
require 'action_view'
require 'ostruct'
require 'erb'
require 'flog'
require 'ruby_parser'
require 'sexp_processor'
ERBHandler = ActionView::Template::Handlers::ERB.new
def new_template(body, details={format: :html})