Created
February 13, 2012 00:57
-
-
Save arches/1812305 to your computer and use it in GitHub Desktop.
News articles with voting
This file contains 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
# Entities | |
class Post | |
attr_accessor :id, :title, :link, :summary | |
attr_accessor :reactions | |
def score | |
self.reactions.inject(0){|r, sum| sum += r.score } | |
end | |
end | |
class Reaction | |
attr_accessor :score | |
attr_accessor :post | |
end | |
# Interactors | |
class FindPosts | |
# set up some "records" in memory | |
@@posts = [] | |
@@posts << Post.new(...) | |
@@posts << Post.new(...) | |
@@posts << Post.new(...) | |
def self.all | |
@@posts | |
end | |
def self.by_id(id) | |
@@posts.detect{|p| p.id == id} | |
end | |
end | |
This file contains 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
get "/" do | |
post_html = FindPosts.all.collect{|p| "<div class='post'>....</div>"} | |
return "<html><body>#{post_html}</body></html>" | |
end | |
get "/vote/:id/:direction" do | |
score = (direction == "up") ? 1 : -1 | |
post = FindPosts.by_id(id) | |
post.reactions << Reaction.new(:score = score) | |
redirect to("/") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment