Skip to content

Instantly share code, notes, and snippets.

Gabriel Sobrinho sobrinho

Block or report user

Report or block sobrinho

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View 00_active_record_patch_includes_without_primary_key.rb
if ActiveRecord::VERSION::STRING != '3.2.22.5'
raise 'This patch only works with 3.2.22.5, be sure to repatch when upgrade'
end
module ActiveRecord
# Fix an issue where includes on models without primary key raises a crypto
# error.
#
# What we are essentially doing is ignoring the projection of blank column
# names because the array of columns is computed using the primary key which
View gist:1dcfc2c8035a1bfa7c480fcbfa60069f
[89] pry(main)> User.primary_key = nil
=> nil
[90] pry(main)> User.includes(:person).where(Person.arel_table[:id].eq(1)).all
SQL (0.4ms) SELECT "usuarios"."" AS t0_r0, "usuarios"."id" AS t0_r1, [[ommited]]
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: SELECT "usuarios"."" AS t0_r0, "usuarios"."id" AS t0_r1, "us...
^
View diff.patch
diff --git a/src/authentication.c b/src/authentication.c
index bba7cef..4ea48c9 100644
--- a/src/authentication.c
+++ b/src/authentication.c
@@ -143,11 +143,5 @@ static bool CheckPassword() {
#endif /* ENABLE_PAM */
void Authenticate() {
- uint8_t tries = 0;
- while (tries < 3) {
View unaccent.rb
class String
def unaccent
string = ''
each_char do |char|
ascii = I18n.transliterate(char)
if ascii == '?'
string << char
else
View active_record.rb
# Robust JSON output for active record errors
#
# See https://groups.google.com/forum/#!topic/rubyonrails-core/hxgX6D9s2uM
module ActiveRecord
module AutosaveAssociation
def validate_collection_association(reflection)
if association = association_instance_get(reflection.name)
if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave])
records.each_with_index do |record, index|
association_valid?(reflection, record, index)
View form_object.rb
class SongForm
include ActiveModel::Model
attr_accessor :song_name, :artist_name
def song
@song ||= Song.new
end
def artist
@sobrinho
sobrinho / track_habtm_changes.rb
Last active Dec 21, 2015
Track habtm changes on active record
View track_habtm_changes.rb
# See https://groups.google.com/forum/#!topic/rubyonrails-core/Lb9rBkZnZSo
module TrackHabtmChanges
def self.included(model)
model.after_initialize :track_habtm_initial_state
model.after_save :track_habtm_initial_state
end
def changes
super.merge(habtm_changes)
end
View water_tank.rb
class WaterTank
attr_reader :volume
def initialize(volume)
@volume = volume
end
def drain(quantity)
if quantity > volume
quantity = volume
@sobrinho
sobrinho / deploy.sh
Last active Oct 12, 2016
Provides build-essential, git, memcached, postgres, redis, node, ruby, bundler, foreman and heroku. To be used against a precise64 machine (http://files.vagrantup.com/precise64.box).
View deploy.sh
#!/bin/bash
set -e
set -x
provision_command="wget -O- https://gist.github.com/sobrinho/4774041 | sh"
ssh root@server1 "$provision_command"
ssh root@server2 "$provision_command"
@sobrinho
sobrinho / or.rb
Created Oct 3, 2012
Proof of concept for `(a | b).empty?` on ruby
View or.rb
class Or < BasicObject
def initialize(a, b)
@a = a
@b = b
end
def method_missing(method_name, *arguments, &block)
@a.send(method_name, *arguments, &block) || @b.send(method_name, *arguments, &block)
end
end
You can’t perform that action at this time.