Skip to content

Instantly share code, notes, and snippets.

require 'spec_helper'
describe 'thread safe tenants' do
let(:db_names) { [db1, db2] }
before do
Apartment.configure do |config|
config.excluded_models = ["Company"]
config.tenant_names = lambda{ Company.pluck(:database) }
config.use_schemas = false
class UserInfo
attr_accessor :contact
def initialize(contact = nil)
self.contact = contact || NullContact.new
end
def contact_name
contact.name
@apneadiving
apneadiving / file.rb
Created December 4, 2012 16:38
refactoring
def set_username_as_name_if_empty
if self.username && self.username.present?
self.name = self.username if self.name && self.name.empty?
end
end
def set_username_as_name_if_empty
return if !username.blank? || !name.blank?
self.name = username
end
def update_discussion_with_client( author, client, document, message )
email_vars = {
:locale => client.locale,
:job_ref => document.ref,
:message => message,
:job_link => render_link(review_clients_project_path(id: document.project_id), client.locale) # I cannot find the route to open a document for review from the client's perspective
}
base_mail(
class Syracuse < Struct.new(:initial, :chiffre, :altitude, :duree_vol, :success)
HIGHER_THRESHOLD = 100000000000
def initialize(chiffre)
self.initial = self.chiffre = chiffre
self.altitude = self.duree_vol = 0
end
def next
require 'rubygems'
require 'tire'
Tire.configure { logger 'elasticsearch.log', :level => 'debug' }
class Document
attr_accessor :title, :content
include Tire::Model::Persistence
include Tire::Model::Search
include Tire::Model::Callbacks
@apneadiving
apneadiving / gist:7323051
Created November 5, 2013 17:44
qbox issue
curl -X GET 'https://cc88d7d5589d309a000.qbox.io/entities/timeline_post/_search?size=100000&pretty' -d '{query: { match_all: {} }, fields: ["_source", "_parent"] }'
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
require 'rubygems'
require 'monadic'
class MonadicService
def initialize(user_id)
@user_id = user_id
end
def call
response = HTTParty.get("https://jsonplaceholder.typicode.com/users/#{@user_id}")
# User model
class User < ActiveRecord::Base
has_many :invitations
end
# Invitation Model
class Invitation < ActiveRecord::Base
belongs_to :inviter, class_name: 'User'
def accept
self.accepted = true
end
# controller
def accept_invitation
if invitation.accepted?
render json: { errors: ['Invitation already accepted'] }, status: 422
else
user = User.build_from_invitation(invitation)
user.save!
invitation.accept
invitation.save!
UserMailer.notify_affiliate_payment(invitation).deliver_later