Skip to content

Instantly share code, notes, and snippets.

View oriolgual's full-sized avatar

Oriol Gual oriolgual

View GitHub Profile
@oriolgual
oriolgual / Guardfile
Created July 21, 2011 12:18
Example Guardfile with annotate, cucumber, minitest and auto-migrate test database
guard 'annotate', notify: false do
watch( 'db/schema.rb' )
end
guard 'cucumber', cli: '--format progress --no-profile', all_on_start: false do
watch(%r{features/.+\.feature})
watch(%r{features/step_definitions/(.+)_steps\.rb}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] }
end
guard 'minitest', drb: false do
@oriolgual
oriolgual / pdf_to_text.rb
Created September 7, 2011 10:56
Assert the contents of a generated PDF with Capybara and Cucumber
# Dependencies: pdftotext (included in xpdf)
# OSX: brew install xpdf
# Ubuntu/Debian: apt-get install xpdf
Then /^I should have get a pdf with the business name$/ do
page.response_headers['Content-Type'].should include 'application/pdf'
pdf_to_text.should include @current_business.name
end
def pdf_to_text
@oriolgual
oriolgual / capybara_select_dates_and_times.rb
Created September 9, 2011 08:40 — forked from szimek/capybara_select_dates_and_times.rb
Cucumber steps for selecting time and date (using Capybara)
require "xpath" # XPath is a separate gem now
module Cucumber
module Rails
module CapybaraSelectDatesAndTimes
def select_date(field, options = {})
date = Date.parse(options[:with])
find(:xpath, %Q{//select[contains(@id, "#{field}_1i")]}).find(:xpath, ::XPath::HTML.option(date.year.to_s)).select_option
find(:xpath, %Q{//select[contains(@id, "#{field}_2i")]}).find(:xpath, %Q{option[@value='#{date.month.to_s}']}).select_option
find(:xpath, %Q{//select[contains(@id, "#{field}_3i")]}).find(:xpath, ::XPath::HTML.option(date.day.to_s)).select_option
@oriolgual
oriolgual / method
Created October 11, 2011 08:56
Problem with rbx and method.to_proc
Oriols-Mac-Pro:spinach(851m|master!?) $ RBXOPT=-X19 irb
~> Console extensions: wirble hirb ap rails2 rails3 pm interactive_editor blueprints
rbx-head :001 > def foo
rbx-head :002?> 3
rbx-head :003?> end
=> #<Rubinius::CompiledMethod foo file=(irb)>
rbx-head :004 > m = method(:foo)
=> #<Method: Object#foo (defined in Object from "def foo")>
rbx-head :005 > p = proc { 3 }
=> #<Proc:0x3184@(irb):5>
Rubinius Crash Report #rbxcrashreport
Error: signal SIGSEGV
[[Backtrace]]
0 rbx 0x00000001000225b1 _ZN8rubiniusL12segv_handlerEi + 241
1 libSystem.B.dylib 0x00007fff8666067a _sigtramp + 26
2 ??? 0x0000000102ecf080 0x0 + 4344049792
3 rbx 0x00000001002136b6 _ZN8rubinius16GarbageCollector11scan_objectEPNS_6ObjectE + 182
4 rbx 0x0000000100211d6b _ZN8rubinius7BakerGC14copy_unscannedEv + 283
@oriolgual
oriolgual / validate_spanish_vat.rb
Created January 18, 2012 14:10
Spanish VAT validator for Ruby
module ValidateSpanishVat
module ClassMethods
# Implements model level validator for Spanish's VAT number.
# Usage:
#
# Inside a model with a vat field, simply put
#
# validates_spanish_vat field_name
#
def validates_spanish_vat(*attr_names)
@oriolgual
oriolgual / backtrace.sh
Created January 30, 2012 12:05
Bug with Rails 3.2.1 and PG
Oriols-Mac-Pro:test_3_2_1 $ bundle exec rails console
Loading development environment (Rails 3.2.1)
~> Console extensions: wirble hirb ap rails2 rails3 pm interactive_editor blueprints
1.9.3-p0 :001 > Subject.first.student_ids
SCHEMA (0.3ms) SHOW client_min_messages
SCHEMA (0.1ms) SET client_min_messages TO 'panic'
SCHEMA (0.1ms) SET standard_conforming_strings = on
SCHEMA (0.0ms) SET client_min_messages TO 'notice'
SCHEMA (0.3ms) SET time zone 'UTC'
SCHEMA (0.1ms) SHOW TIME ZONE
One of the possible consequences of the welfare state is the lack of culture of the effort. Everything is easy and free.
A clear example is the university (at least in Spain), it's very easy to get to college (both financially and academically),
but since it is almost free it is not given enough importance: it's OK if I spend the day lying on the grass or don't
go to class or I just left college because the cost is zero.
Details of abandonment: http://www.consumer.es/web/es/educacion/universidad/2010/03/21/191878.php (Spanish)
Just see the last few years in Spain, the real estate boom is the best example of the lack of the culture of effort.
@oriolgual
oriolgual / no_tap.rb
Created July 6, 2012 10:09
to tap or not to tap?
def do_it(ids)
contacts = contact_finder(ids)
contacts = contacts.my_nice_scope if should_apply_nice_scope?
contacts
end
@oriolgual
oriolgual / hyperclient.rb
Created August 5, 2012 17:30
Hyperclient refactor
# Read last HAL draft first: http://tools.ietf.org/html/draft-kelly-json-hal-03
api = MyBlogAPI.new
# Entry point
api.links #=> {'self' => link_to_self, 'relation' => link1}
api.links.self.url #=> '/'
api.embedded #=> {}
# Links