Skip to content

Instantly share code, notes, and snippets.

View ilpoldo's full-sized avatar

Leandro Pedroni ilpoldo

  • Double Negative
  • London, UK
View GitHub Profile
@ilpoldo
ilpoldo / config.ru
Created November 24, 2014 17:09
geminabox + pow
require "rubygems"
require "geminabox"
Geminabox.data = File.expand_path('data', File.dirname(__FILE__))
run Geminabox
@ilpoldo
ilpoldo / compress.sh
Created April 26, 2009 23:29
bash script to run yui compressor on my javascripts
#!/bin/bash
# this script and the yuicompressor jar are in the /compressor folder distributed with the script
# TODO: Copy the license notes in the full version and prepend them to the minified one.
abspath="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
dir=`dirname "$abspath"`
echo $dir
java -jar $dir/yuicompressor-2.4.2.jar --nomunge $dir/../myscript.js -o $dir/../myscript.min.js
require 'rubygems'
require 'active_support'
require 'active_record'
TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), '..', 'test.sqlite3')
File.unlink(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
ActiveRecord::Base.establish_connection(
"adapter" => "sqlite3", "database" => TEST_DATABASE_FILE
@ilpoldo
ilpoldo / in_memory_test_db.rb
Created January 15, 2010 19:38
Runs rspec test database in memory
# Reloads the db every time in memory. Faster (30%)!!
# Drop this file in spec/support
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.up('db/migrate')
@ilpoldo
ilpoldo / page_order.rb
Created January 25, 2010 18:18
Creates an array of pages not bigger than 9 elements, with ellipsis to signal gaps.
def page_order(page, total_pages, links=9)
bck = ((page > links ? page-links : 1)..page).entries.reverse
fwd = (page..((total_pages - page > links ? page+links : total_pages))).entries
order = fwd.size > page ? fwd.zip(bck) : bck.zip(fwd)
order = order.flatten.compact.uniq[0,links].sort
if order[0] !=1
order[1]='...'
order[0]=1
@ilpoldo
ilpoldo / partition_hashes.rb
Created February 20, 2010 18:28
One-liner to partition an hash
# Divides the hash in two, the half that meets the criteria and the one that does not.
yes, no = h.partition {|k,v| v>1}.map {|side| Hash[*side.flatten]}
class Album < ActiveRecord::Base
has_one :feature, :as => :featured
end
@ilpoldo
ilpoldo / leandro_pickle_steps.rb
Created February 21, 2010 12:04
Pickle step to define model and associated model through a table
# create models and associations from a table
Given(/^the following #{capture_plural_factory} and (\w*) #{capture_plural_factory} exists?:?$/) do |plural_factory, association, plural_associated_factory, table|
name = plural_factory.singularize
associated_name = plural_associated_factory.singularize
table.hashes.each do |hash|
associated_hash, hash = hash.partition {|k,v| String(k) =~ /^#{association}_.*/}.map {|h| Hash[*h.flatten]}
create_model(associated_name, Hash[*associated_hash.map {|k,v| [String(k).gsub(/^#{association}_/,''),v]}.flatten] )
@ilpoldo
ilpoldo / factories.rb
Created February 21, 2010 22:54 — forked from kris/factories.rb
Factory.define :application do |factory|
factory.attachment(:sample, "public/samples/sample.doc", "application/msword")
end