Skip to content

Instantly share code, notes, and snippets.

@mb-dev
mb-dev / application.rb
Created March 3, 2013 21:36
Bundler command change on application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Assets should be precompiled for production (so we don't need the gems loaded then)
Bundler.require(*Rails.groups(assets: %w(development test)))
module Sample
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
@mb-dev
mb-dev / test.rb
Created March 3, 2013 21:33
Rails 4 test.rb
Sample::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
@mb-dev
mb-dev / production.rb
Created March 3, 2013 21:32
Rails 4 production.rb
Sample::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both thread web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
@mb-dev
mb-dev / development.rb
Created March 3, 2013 21:32
Rails 4 development.rb
Sample::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
@mb-dev
mb-dev / dogpile_usage.rb
Last active December 14, 2015 02:48
Dogpile Protection - Usage
<% custom_cache custom_cache_key('product-tile', @product.id), @product.updated_at, dogpile_protection: true do %>
<%= @product.name %>
<% end %>
@mb-dev
mb-dev / dogpile_read.rb
Created February 22, 2013 19:00
Dogpile Effect - read
def custom_read_dogpile(name, timestamp, options)
result = Rails.cache.read(timestamp_key(name, timestamp))
if result.blank?
Rails.cache.write(name + ':refresh-thread', 0, raw: true, unless_exist: true, expires_in: 5.seconds)
if Rails.cache.increment(name + ':refresh-thread') == 1
result = nil
else
result = Rails.cache.read(name + ':last')
end
@mb-dev
mb-dev / dogpile_key.rb
Created February 22, 2013 18:58
Dogpile Effect - key
def custom_write_dogpile(name, timestamp, fragment, options)
Rails.cache.write(timestamp_key(name, timestamp), fragment)
Rails.cache.write(name + ':last', fragment)
Rails.cache.delete(name + ':refresh-thread')
fragment
end
@mb-dev
mb-dev / dogpile_lock.rb
Created February 22, 2013 18:52
Dogpile effect - capture a lock using memcache
if result.blank?
Rails.cache.write(name + ':refresh-thread', 0, raw: true, unless_exist: true, expires_in: 5.seconds)
if Rails.cache.increment(name + ':refresh-thread') == 1
# we are the first visitor
end
end
@mb-dev
mb-dev / products.js.coffee
Created February 5, 2013 05:45
Master Detail Page - Backbone
# Model to hold the current state
# position: last scrollTop before clicking an item
# state: Can be ITEM_VIEW or PRODUCT_LIST
# productDetailsHTML: The HTML for the detail view
class window.ProductViewState extends Backbone.Model
@PRODUCT_LIST = 'product_list'
@ITEM_VIEW = 'item_view'
# View to manage transitions between two states
class window.ProductView extends Backbone.View
@mb-dev
mb-dev / product.html.erb
Created February 5, 2013 05:45
Master Detail Page - Product Partial
<h1><%= @product.name %></h1>
<div>
<img src="<%= @product.image_url%>"/>
</div>