This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File: config/initializers/date.rb | |
# Parse date using Rails I18n or Ruby parse method if it failed. | |
class Date | |
class << self | |
def _parse_with_i18n(str, format = :default) | |
format ||= :default | |
date = Date._strptime(str, I18n.translate("date.formats.#{format}")) || _parse_without_i18n(str) | |
date[:year] += increment_year(date[:year].to_i) if date[:year] | |
date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This enables the '/' resource: | |
# map.resources :knickers, :as => '' | |
# | |
# GET /blabla | |
# => KnickersController#show params = {:controller = 'knickers', :action => 'show, :id => 'blabla'} | |
module ActionController | |
module Resources | |
class Resource #:nodoc: | |
def path | |
@path ||= path_segment.blank? ? path_prefix.to_s : "#{path_prefix}/#{path_segment}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generates a random String | |
# 'abc'.rand(10) # => 'aacbacccbc' | |
class String | |
def rand(length) | |
(1..length).inject('') {|str, i| str << self[Kernel::rand(self.length)]} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fluent Factory overrides the new class method to provide an intuitive factory. | |
# Instead of using | |
# MyFactory.new_instance(args) | |
# Which may return an instance of MyClass, you now use | |
# MyClass.new(args) | |
# The second way is more intuitive and the big advantage is to use it rails and | |
# inheritance. | |
# | |
# Read the full example below to understand how to use it. | |
# class Animal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Called dynamically named scopes on a model | |
module Fluent | |
module Scopes | |
def self.included(klass) | |
klass.extend(ClassMethods) | |
# The code below fixes the following statement: | |
# @project = Project.find(123) | |
# @project.developers.apply_scopes(filters).find | |
# Without the code below the condition where developers.project_id = 123 is erased | |
# by apply_scopes, because filters are built from the class Developer instead of |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$LOAD_PATH.unshift '/home/alexis/veritas/lib' | |
require 'veritas' | |
include Veritas | |
header = Relation::Header.new([[:name, String], [:is_admin, Integer], [:is_author, Integer]]) | |
body = [['Foo', 1, 1], ['Bar', 1, 0], ['Baz', 0, 1]].each | |
users = Relation.new(header, body) | |
admins = Relation.new(header, body).restrict { |r| r[:is_admin].eq(1) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# File: .git/hooks/prepare-commit-msg | |
# Append the Pivotal's story tag at the end of the commit message. | |
# Your branch must be named like this: my_branch_12345. | |
story=$(git symbolic-ref HEAD|sed -rn 's/^.*_([[:digit:]]+)/\1/p') # -E instead of -r for Mac OS X | |
test $story && echo "[Story#$story]" >> $1 | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# InfiniteStringStub.new.foo.bar.baz # => 'baz' | |
class InfiniteStringStub < String | |
def method_missing(name) | |
InfiniteStringStub.new(name.to_s) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAznBfbfnCJMibMMPSI7Fxeo02Y9fgy7yrMVlON2AKhTB3QMhjUORqONRCQG6g1qHJX2Osg/NQ6kxZEU7pHC8aTCXpPnKMrSWz/73B3vSsfV2dZIp1q5pz35MGqCkXKhfM7xO8NHGbzzVOs9mNb0txWU1fPI9parPqCvosjFKN5uJDpMUKV3yVwqztsSgRp9hwMtErg5SkBeRbFYRl2Aa1pEmRwjwdQyS23B4uwus9cFE2xkR86FOkJu4/5Rr1WKmqOLFyObjk1MSwYqfmHtCgwO0GWB3OTYtQbBD/4UC159efKDKvivVqCnItzg2huHsSRr4JugnQ7AIpKhbnrIUx9w== alexis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convenient way to fetch URLs. It follows redirections and handles SSL. Usage: | |
# Net::HTTP.fetch_url('http://google.com') | |
# Net::HTTP.fetch_url('https://www.google.fr/?q=ruby') | |
module Net | |
class HTTP | |
def self.fetch_url(url, limit = 10) | |
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 | |
url = URI.parse(url) | |
options = {use_ssl: url.scheme.downcase == 'https'} | |
request = Net::HTTP::Get.new(url.path.to_s + '?' + url.query.to_s) |
OlderNewer