Skip to content

Instantly share code, notes, and snippets.

@mb-dev
mb-dev / products_controller.rb
Created February 5, 2013 05:18
Master Detail Page - Product Controller
class ProductsController < ApplicationController
def index
@products = Product.all
if params[:product_id].present?
@product = Product.find(params[:product_id])
end
if params[:product_id].present? and request.xhr?
render 'product', layout: false
@mb-dev
mb-dev / index.html.erb
Created February 5, 2013 05:44
Master Detail Page - View
<div class="container">
<div class="header">
Header - <a href="/products/">Product List</a>
</div>
<div id="product_details" style="display: none">
<% if @product.present? %>
<%= render file: 'products/product' %>
<% end %>
</div>
<ul class="products" id="product_list" style="display: none">
@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>
@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 / 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 / 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_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_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 / 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 / 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.