Skip to content

Instantly share code, notes, and snippets.

@nicholasjhenry
Created July 2, 2012 20:35
Show Gist options
  • Select an option

  • Save nicholasjhenry/3035546 to your computer and use it in GitHub Desktop.

Select an option

Save nicholasjhenry/3035546 to your computer and use it in GitHub Desktop.
Simplified Persistence Separation
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
@nicholasjhenry
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment