Skip to content

Instantly share code, notes, and snippets.

@arches
Created February 13, 2012 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arches/1812305 to your computer and use it in GitHub Desktop.
Save arches/1812305 to your computer and use it in GitHub Desktop.
News articles with voting
# 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
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