Created
July 2, 2012 20:35
-
-
Save nicholasjhenry/3035546 to your computer and use it in GitHub Desktop.
Simplified Persistence Separation
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 MyApp::Post | |
| attr_reader :record | |
| def initialize(args) | |
| @record = args[:record] | |
| end | |
| def comment(attributes) | |
| create_comment(attributes) | |
| end | |
| def create_comment(attributes) | |
| comment_record = record.build_comment(attributes) | |
| comment = Comment.new(comment_record) | |
| self.add_comment?(comment) | |
| comment.add_post?(self) | |
| return comment_record | |
| end | |
| def add_comment?(comment) | |
| true | |
| end | |
| end | |
| class MyApp::Comment | |
| attr_reader :record | |
| def initialize(args) | |
| @record = args[:record] | |
| end | |
| def add_post?(post) | |
| true | |
| end | |
| end | |
| # controller.rb | |
| def create | |
| @post = Post.find(1) | |
| @comment = comment(post, attributes) | |
| if @comment.save | |
| redirect @post | |
| else | |
| render :new | |
| end | |
| end | |
| def comment(post_record, comment_attributes) | |
| post = MyPost::Post.new(post_record) | |
| post.comment(attributes) | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Review: http://en.wikipedia.org/wiki/Visitor_pattern