Skip to content

Instantly share code, notes, and snippets.

View iamnader's full-sized avatar

Nader Akhnoukh iamnader

View GitHub Profile
### Models ###
class Post
include Mongoid::Document
references_many :properties, :stored_as => :array, :inverse_of => :posts, :index => true
end
class Property
include Mongoid::Document
field :name
references_many :posts, :stored_as => :array, :inverse_of => :properties
# /test/integration/subdomain_test.rb
require 'integration_test_helper'
class SubdomainTest < ActionController::IntegrationTest
test "simple subdomain test" do
subdomain = "something"
# this test passes using selenium, fails using the default envjs
#Capybara.current_driver = :selenium
Capybara.app_host = "http://#{subdomain}.lvh.me:9887"
### subdomain_test.rb
require 'integration_test_helper'
class SubdomainTest < ActionController::IntegrationTest
test "simple subdomain test" do
sub1 = "sub1"
sub2 = "sub2"
# this test passes using selenium
#Capybara.current_driver = :selenium
@iamnader
iamnader / gist:641569
Created October 22, 2010 23:58
Mongoid Set Defaults Rake Task
require 'config/environment'
require 'mongo'
namespace :mongoid do
desc 'Update defaults'
task :defaults do
db = Mongoid.config.master
models.each do |m|
m = eval m
@iamnader
iamnader / devise_mailer_with_attributes.rb
Created December 9, 2010 18:49
Extending Devise::Mailer to accept attributes available to the view
module DeviseMailerWithAttributes
class Devise::Mailer < ::ActionMailer::Base
alias_method :old_confirmation_instructions, :confirmation_instructions
alias_method :old_reset_password_instructions, :reset_password_instructions
alias_method :old_unlock_instructions, :unlock_instructions
alias_method :old_invitation_instructions, :invitation_instructions
def confirmation_instructions(record, attributes={})
set_attributes(attributes)
# in /config/devise.rb
...
# Configure the class responsible to send e-mails.
#config.mailer = "Devise::Mailer"
config.mailer = "MailerWithInfo"
...
# in /lib
class MailerInfo
def self.attributes=(attributes={})
@iamnader
iamnader / sprite_helper.rb
Created June 26, 2012 16:43
sprite helper
def link_type_sprite(image, options = {})
sprites = {
ReferrerClassifier::FACEBOOK => {:w => 20, :h => 20, :x => 5, :y => 0},
ReferrerClassifier::TWITTER => {:w => 20, :h => 20, :x => 30, :y => 0},
ReferrerClassifier::SEARCH => {:w => 20, :h => 20, :x => 55, :y => 0},
ReferrerClassifier::BASIC => {:w => 20, :h => 20, :x => 80, :y => 0}
}
%(<div class="#{options[:class]}" style="background: url(#{path_to_image('/images/sn_icons/link-type-icon-sprite.png')}) no-repeat -#{sprites[image][:x]}px -#{sprites[image][:y]}px; width: #{sprites[image][:w]}px; height: #{sprites[image][:w]}px; #{options[:style]}" title="#{options[:title]}">#{options[:title]}</div>).html_safe
end
@iamnader
iamnader / mongostat
Created July 11, 2012 23:06
Slow Upserts on Indexed Data (though not all in RAM)
insert query update delete getmore command flushes mapped vsize res locked % idx miss % qr|qw ar|aw netIn netOut conn time
0 11 739 0 0 399 0 30.9g 64.4g 550m 43.9 0 0|0 0|1 183k 45k 10 16:03:25
0 5 423 0 0 232 0 30.9g 64.4g 549m 65.1 0 0|0 0|1 106k 26k 10 16:03:26
0 5 471 0 0 261 0 30.9g 64.4g 549m 58.9 0 0|0 0|0 119k 29k 10 16:03:27
0 6 666 0 0 371 0 30.9g 64.4g 548m 50.4 0 0|0 0|0 166k 40k 10 16:03:28
0 8 637 0 0 398 0 30.9g 64.4g 548m 43.8 0 0|0 0|0 167k 44k 10 16:03:29
0 7 727 0 0 419 0 30.9g 64.4g 549m 32.1 0 0|0 0|1 185k 45k 10 16:03:30
0 6 821
@iamnader
iamnader / gist:3094364
Created July 11, 2012 23:08
profiler output
update bokchoy.unique_datas 261ms Tue Jul 10 2012 14:39:07
query:{
"n_id" : ObjectId("4e4afa8bac36dd000100006b"),
"dim" : "cat",
"dim_val" : "Best Of",
"cl_id" : ObjectId("4f6891ed0572c9000100017b"),
"t" : "u",
"v" : "7f4b9eb126a209da653852a6ec25baa9"
} updateobj:{
"n_id" : ObjectId("4e4afa8bac36dd000100006b"),
class MyMq
def self.client
unless @client
c = Bunny.new(APP_CONFIG["cloudamqp_url"], :logging => true)
c.start
@client = c
end
@client
end