Created
August 19, 2014 03:37
-
-
Save jgaskins/d4b705bb8aefe1878124 to your computer and use it in GitHub Desktop.
Controllers with AR vs Perpetuity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArticlesController < ApplicationController | |
# with ActiveRecord | |
def create | |
@article = Article.new(params[:article]) | |
if @article.save | |
redirect_to @article, notice: 'Article created!' | |
else | |
render :new | |
end | |
end | |
# with Perpetuity | |
def create | |
@article = Article.new(params[:article]) | |
if @article.valid? | |
article_mapper.insert @article | |
redirect_to @article, notice: 'Article created!' | |
else | |
render :new | |
end | |
end | |
# I memoize mappers to get the benefit of their identity map | |
def article_mapper | |
@article_mapper ||= Perpetuity[Article] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment