Permalink
Browse files

tags index

  • Loading branch information...
1 parent e5061c8 commit 5ba4483b8d64abe2fd1589908ee286594d2807a2 @adamwiggins adamwiggins committed Jun 21, 2009
Showing with 20 additions and 6 deletions.
  1. +15 −1 lib/post.rb
  2. +3 −3 main.rb
  3. +2 −2 views/tagged.erb
View
@@ -48,6 +48,12 @@ def self.all
find_range(0, 9999999)
end
+ def self.all_tagged(tag)
+ DB.list_range("#{self}:tagged:#{tag}", 0, 99999).map do |slug|
+ find_by_slug(slug)
+ end
+ end
+
def db_key
"#{self.class}:slug:#{slug}"
end
@@ -63,10 +69,18 @@ def save
def self.create(params)
post = new(params)
post.save
- DB.push_head(chrono_key, post.slug)
+ post.build_indexes
post
end
+ def build_indexes
+ DB.push_head(self.class.chrono_key, slug)
+
+ tags.split.each do |tag|
+ DB.push_head("#{self.class}:tagged:#{tag}", slug)
+ end
+ end
+
def destroy
DB.list_rm(self.class.chrono_key, db_key, 0)
DB.delete(db_key)
View
@@ -58,14 +58,14 @@ def auth
end
get '/past' do
- posts = Post.reverse_order(:created_at)
+ posts = Post.all
@title = "Archive"
erb :archive, :locals => { :posts => posts }
end
get '/past/tags/:tag' do
- tag = params[:tag]
- posts = Post.filter(:tags.like("%#{tag}%")).reverse_order(:created_at).limit(30)
+ tag = params[:tag].downcase.strip
+ posts = Post.all_tagged(tag)
@title = "Posts tagged #{tag}"
erb :tagged, :locals => { :posts => posts, :tag => tag }
end
View
@@ -1,8 +1,8 @@
<h1>Posts tagged <%= tag %></h1>
<% posts.each do |post| %>
<div class="post">
- <p class="date"><%= post[:created_at].strftime("%b") %><b><%= post[:created_at].strftime("%d") %></b></p>
- <h2 class="title"><a href="<%= post.url %>"><%= post[:title] %></a></h2>
+ <p class="date"><%= post.created_at.strftime("%b") %><b><%= post.created_at.strftime("%d") %></b></p>
+ <h2 class="title"><a href="<%= post.url %>"><%= post.title %></a></h2>
<p class="meta"><small><%= post.linked_tags %> | <a href="<%= post.url %>#disqus_thread">comments</a></small></p>
<div class="entry">
<%= post.summary_html %>

0 comments on commit 5ba4483

Please sign in to comment.