Skip to content

Instantly share code, notes, and snippets.

View jasoares's full-sized avatar

João Soares jasoares

View GitHub Profile
# uber-hacky dynamic collection names for MongoID models:
#
# class MyModel
# include Mongoid::PrefixableDocument # ...instead of Mongoid::Document
# include Mongoid::Timestamps
#
# field :foobar, :type => Integer
#
# def my_method; 123; end
#
If you want to use multiple dbs at once there are several different ways...
1) If you want to do this on a per-model level, use .store_in (This is for all threads):
class Band
include Mongoid::Document
store_in database: "secondary" # This can be any name you want, no need to put it in the mongoid.yml.
end
class Artist
@jasoares
jasoares / rails dump request headers.rb
Created February 18, 2014 18:40
Dump the request headers from a request inside a rails controller or authentication before_filter/action
request.env.select { |k,v| k.match("^HTTP.*") }.each do |header|
puts header[0].split('_').last + ': ' + header[1]
end
@jasoares
jasoares / ruby_oop_sample
Created February 10, 2014 13:25
Sample of ruby yield methods, modules and includes...
class Array
def each_valid
self.each do |val|
valid = yield val
puts val if valid
end
end
def map
internal_array = []
@jasoares
jasoares / fb_user_picture.java
Created October 22, 2013 11:08
Get user info with image specifying size from Facebook on Android
Bundle params = new Bundle();
params.putString("fields", "picture.height(200).width(200)");
Request meRequest = new Request(session, "me", params, HttpMethod.GET, new FacebookMeResponseHandler());
meRequest.executeAsync();
# migrate all images to S3
namespace :paperclip_migration do
desc "migrate files from filesystem to s3"
task :migrate_to_s3 => :environment do
local_filesystem_root = Rails.root.to_s + "/public/system"
# Fetch all models and find out which ones have paperclip attachments
Rails.application.eager_load!
models = ActiveRecord::Base.send(:descendants).select {|model| model.respond_to?(:attachment_definitions) }
#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog
# Required-Stop: $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# By Steve Leung
# steve@leungs.me
class ActiveRecord::Relation
# temporarily hack for allowing combining scopes with OR
# doesn't add the join tables so need to use .includes manually
# ie. Jobship.includes(:job).or(Jobship.accepted, Jobship.declined).count
def or(*scopes)
clauses = *scopes.map do |relation|
@jasoares
jasoares / default_object_to_s.rb
Last active December 11, 2015 22:19
Override default Object#to_s preserving class and encoded id like #<Nokogiri::XML::Element:0x11ab674 @var="123" ...
# This reimplementation produces the following sample result
# => #<Object:0x000000018436d0 @var=3>
def to_s
"#<%s:%#0.14x @var=%s>" % [self.class, self.__id__.abs*2, var]
end
# It is also possible to minify it by tweaking precision an getting rid
# of all those zeros like so:
# => #<Object:0x18436d0 @var=3>
def to_s
@jasoares
jasoares / ubuntu java sun installation.md
Created December 12, 2012 23:36
Installation guide for Sun's JDK in Ubuntu

Installing Sun JDK 6 on Ubuntu 12.04:

Download the sun jdk 6 bin

Make the bin file executeable:

chmod +x jdk-6u32-linux-x64.bin

Extract the bin file:

./jdk-6u32-linux-x64.bin