Skip to content

Instantly share code, notes, and snippets.

@mb-dev
mb-dev / gist:1175448
Created August 27, 2011 14:23
LineItem model
class LineItem
include Mongoid::Document
include Mongoid::Timestamps
TYPE = %w[income expense]
INCOME = 0
EXPENSE = 1
TRANSFER_IN = 'Transfer In'
TRANSFER_OUT = 'Transfer Out'
@mb-dev
mb-dev / posts_controller.rb
Last active December 11, 2015 12:39
Russian Doll Caching - Before - Controller
class PostsController < ApplicationController
def show
@post = Post.includes(comments: :user).find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
end
@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>
@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 / 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 / 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 / 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 / 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_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_bucketed_collections
Created January 28, 2013 18:04
MongoDB Bucketed Collections
User:
{
id: 1,
name: 'Joe'
}
MailBucket:
{
user_id: 1,
location: 11,