Skip to content

Instantly share code, notes, and snippets.

View alexisbernard's full-sized avatar

Alexis Bernard alexisbernard

View GitHub Profile
@alexisbernard
alexisbernard / gist:270730
Created January 6, 2010 22:14
Generate random string
# 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
@alexisbernard
alexisbernard / gist:270727
Created January 6, 2010 22:12
The root resource
# 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}"
@alexisbernard
alexisbernard / gist:179712
Created September 2, 2009 13:28
Parse date with I18n
# 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