Skip to content

Instantly share code, notes, and snippets.

View mrrooijen's full-sized avatar

Michael van Rooijen mrrooijen

View GitHub Profile
def paginate(collection, controller, action)
return if collection.empty?
if collection.total_pages > collection.per_page
html = 1.upto(collection.total_pages).map { |i|
options = { :page => i }
options.merge!(:q => params[:q]) if params[:q].present?
link_to(i, url(controller, action, options), :class => [(params[:page].to_i == i ? :current : :page), ' link'])
}.join
content_tag(:div, html, :class => "pagination")
end
def coderay(text)
return if text.nil?
text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do
content_tag("notextile", CodeRay.scan($3, $2).div(:css => :class))
end
end
## config/boot.rb
# Place this snippet right above the "Rails.boot!" command at the bottom of the file.
class Rails::Boot
def run
load_initializer
extend_environment
Rails::Initializer.run(:set_load_path)
end
def extend_environment
@mrrooijen
mrrooijen / parseexcel.rb
Created May 14, 2010 12:28
Simple Excel Parse script.
# Load in required libraries
require 'rubygems'
require 'parseexcel'
# Load workbook into memory
workbook = Spreadsheet::ParseExcel.parse("file.xls")
# Open first worksheet
worksheet = workbook.worksheet(0)
@mrrooijen
mrrooijen / mongoid.rb
Created May 18, 2010 07:19
Mongoid settings file for Rails 2.
We couldn’t find that file to show.
@mrrooijen
mrrooijen / paperclip.rake
Created May 22, 2010 23:11
A Fix for Paperclip Rake Task (paperclip:refresh:thumbnails) - Compatible with ActiveRecord and Mongoid
def obtain_class
class_name = ENV['CLASS'] || ENV['class']
raise "Must specify CLASS" unless class_name
@klass = Object.const_get(class_name)
end
def obtain_attachments
name = ENV['ATTACHMENT'] || ENV['attachment']
raise "Class #{@klass.name} has no attachments specified" unless @klass.respond_to?(:attachment_definitions)
if !name.blank? && @klass.attachment_definitions.keys.include?(name)
@mrrooijen
mrrooijen / heroku-backup.sh
Created May 28, 2010 21:32
Simple script that creates and downloads a Heroku backup (App Repository, DBDump) and then destroys the remote backup.
bundle=backup
app=myapp
heroku bundles:capture $bundle --app $app
sleep 15
heroku bundles:download $bundle --app $app
heroku bundles:destroy $bundle --app $app
@mrrooijen
mrrooijen / carrierwave_tasks.rb
Created June 2, 2010 00:27
Rake Tasks for Carrier Wave for reprocessing versions.
##
# CarrierWave Amazon S3 File Reprocessor Rake Task
#
# Written (specifically) for:
# - CarrierWave
# - Ruby on Rails 3
# - Amazon S3
#
# Works with:
# - Any server of which you have write-permissions in the Rails.root/tmp directory
@mrrooijen
mrrooijen / attr_accessor_block.rb
Created June 18, 2010 15:05
A simple Ruby module that, when extended inside a class, will provide a method that will enable the creation of methods that could be used to create nice block-notation setters.
##
# AttrAccessorBlock
#
# This module is to be "extended" (not included) into a class
# Then just invoke the "attr_accessor_block" method, like you would with "attr_accessor"
# This will enable you to make use of "block notations" and "instance_eval" to create "prettier"
# and more "readable" attribute setters.
#
# class Example
# extend AttrAccessorBlock
@mrrooijen
mrrooijen / jquery.xhr.js
Created June 19, 2010 00:54
jQuery XHR settings
// At the top of application.js
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})