Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / mongo_bucketed_collections
Created January 28, 2013 18:04
MongoDB Bucketed Collections
User:
{
id: 1,
name: 'Joe'
}
MailBucket:
{
user_id: 1,
location: 11,
@mb-dev
mb-dev / mongo_has_many_through.rb
Last active December 11, 2015 20:48
MongoDB Has many through
class Book
field :id
field :title
embeds_many :book_authors
# Mongoid does not offer has_many :through
def authors
book_authors.collect(&:author)
end
@mb-dev
mb-dev / mongo_after.rb
Created January 28, 2013 18:02
Storing more data in MongoDB
Book:
{
id: 1,
title: 'Article 1',
authors: [{author_id: 1, name: 'Joe'}, {author_id: 2, name: 'Robert'}]
}
@mb-dev
mb-dev / mongo_before.rb
Created January 28, 2013 18:01
Storing more data in MongoDB
Book:
{
id: 1,
title: 'Article 1',
authors: [1,2]
}
Author:
{
id: 1,
@mb-dev
mb-dev / posts_controler.rb
Created January 27, 2013 21:41
Russian Doll Caching - Controller - After
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
@presenter = PostPresenter.new(@post)
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
end
@mb-dev
mb-dev / post_presenter.rb
Created January 27, 2013 21:40
Russian Doll Caching - Post Presenter
class PostPresenter
def initialize(post)
@post = post
end
def comments
Comment.includes(:user).where(post_id: @post.id)
end
end
@mb-dev
mb-dev / show.html.erb
Created January 27, 2013 21:39
Russian Doll Caching - View - After
<% cache @post do %>
<div class="post">
<%= render partial: 'post', locals: {post: @post} %>
<hr/>
<div class="comments">
<% @presenter.comments.each do |comment| %>
<% cache comment do %>
<div id="comment-<%=comment.id%>">
@mb-dev
mb-dev / show.html.erb
Last active December 11, 2015 12:39
Russian Doll Caching - View - Before
<div class="post">
<%= render partial: 'post', locals: {post: @post} %>
<hr/>
<div class="comments">
<% @post.comments.each do |comment| %>
<div id="comment-<%=comment.id%>">
<%= comment.user.name %> says:
<p>