journalduhacker/app/controllers/home_controller.rb

61 lines
1.4 KiB
Ruby
Raw Normal View History

class HomeController < ApplicationController
def index
conds = [ "is_expired = 0 " ]
2012-06-30 21:14:35 +02:00
if @user && !@newest
# exclude downvoted items
conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " <<
"WHERE user_id = ? AND vote < 0) "
conds.push @user.id
end
if @tag
conds[0] << "AND taggings.tag_id = ?"
conds.push @tag.id
@stories = Story.find(:all, :conditions => conds,
:include => [ :user, :taggings ], :joins => [ :user, :taggings ],
:limit => 30)
@title = @tag.description.blank?? @tag.tag : @tag.description
@title_url = tag_url(@tag.tag)
else
@stories = Story.find(:all, :conditions => conds,
:include => [ :user, :taggings ], :joins => [ :user ],
:limit => 30)
end
if @user
votes = Vote.votes_by_user_for_stories_hash(@user.id,
@stories.map{|s| s.id })
@stories.each do |s|
if votes[s.id]
s.vote = votes[s.id]
end
end
end
2012-06-30 21:14:35 +02:00
if @newest
# TODO: better algorithm here
@stories.sort_by!{|s| s.created_at }.reverse!
else
@stories.sort_by!{|s| s.hotness }
end
render :action => "index"
end
2012-06-30 21:14:35 +02:00
def newest
@newest = true
index
end
def tagged
if !(@tag = Tag.find_by_tag(params[:tag]))
raise ActionController::RoutingError.new("tag not found")
end
index
end
end