Skip to content

Instantly share code, notes, and snippets.

require 'bundler/setup'
require 'fiber'
require 'csv'
require 'pp'
require 'eventmachine'
def a_slow_api_call(row)
sleep 0.3
row.collect(&:to_i).inject(&:+)
@sapienza
sapienza / gist:5987322
Last active December 19, 2015 16:58
Simple search with Ruby on Rails ;)
#routes
match 'posts/search(/:search)' => 'posts#search', as: 'post_search'
#model
def self.search(search)
search_condition = "%" + search + "%"
find(:all, :conditions => ['title LIKE ? OR content LIKE ?', search_condition, search_condition])
end
#controller
def search
@sapienza
sapienza / gist:5987088
Created July 12, 2013 19:28
Rails and Memcache. Different Cached Scopes for each user.
def self.cached_scope_for_user(user_id)
Rails.cache.fetch "scope_for_user_#{user_id}" do
results = Model.where("user_id = ?)", user_id)
end
end
@sapienza
sapienza / gist:5987040
Created July 12, 2013 19:23
Rails and Memcache with Dalli. How to refresh the cache just after updating or destroying it (force)
class Contact < ActiveRecord::Base
after_save :expire_contact_all_cache
after_destroy :expire_contact_all_cache
def self.all_cached
Rails.cache.fetch('Contact.all') { all }
end
def expire_contact_all_cache
Rails.cache.fetch('Contact.all', :force => true) do
@sapienza
sapienza / gist:5814173
Created June 19, 2013 13:06
Vagrant - php enviroment
# Puppet configurations
class base {
## Update apt-get ##
exec { 'apt-get update':
command => '/usr/bin/apt-get update'
}
}
@sapienza
sapienza / gist:5768695
Created June 12, 2013 20:11
Javascript Class to detect browser versions by it's user agent.
var Browser = {
getAgent: function()
{
return navigator.userAgent.toLowerCase();
},
isMac: function(userAgent)
{
var agent = userAgent || this.getAgent();
return !!agent.match(/mac/i);