Skip to content

Instantly share code, notes, and snippets.

View leods92's full-sized avatar

Leonardo Schlossmacher leods92

View GitHub Profile
@leods92
leods92 / br_phone_validator.rb
Last active December 22, 2015 02:48
BrPhoneValidator. Validator para Rails (testado em 3.2) para validação de telefones brasileiros. Válido em 1º de setembro de 2013, em constante alteração já que celulares estão gradualmente adotando o 9º dígito.
class BrPhoneValidator < ActiveModel::EachValidator
# first 2 digits represent dialing code
# dialing codes in Brazil don't start with 0
#
# the following represent the phone
# must have 8 digits
# first digit must start with 2, 3, 4, 5 or 6
#
LANDLINE_REGEXP = /\A[1-9]\d[2-6]\d{7}\z/
@leods92
leods92 / group_date.rb
Last active December 26, 2015 14:18
Adds time zone aware data grouping method to ActiveRecord. If you want to group a large set of database rows by date using a timestamp as reference, the optimal way is to do it right in the database. When doing it though there are some issues with time zones that #group_date solves. For example, if a UTC timestamp is set to 2013-12-01 01:00 and …
module ActiveRecord
module Querying
delegate :group_date, :to => :scoped
end
module QueryMethods
def group_date(column_name = nil)
return self if column_name.blank?
# Rails uses a non standard time zone naming.
@leods92
leods92 / order_direction_bug.rb
Last active August 29, 2015 13:58
Demonstrating a Rails 4.0.4 bug.
# Tested on Rails 4.0.4
# Rails 4.1.0.rc2 has fixed it.
require 'active_record'
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Migration.create_table :library
ActiveRecord::Migration.create_table :books do |t|
t.string :name