module Linkr::Controllers class Feed < R '/new.rss', '/tags/(.+).rss' def get(tag=nil) @title = tag ? "New links tagged with #{tag}" : @title = "New links" @links = Link.find_all_by_date(tag) render :_rss end end class List < R '/', '/(tags|users)/(.+)' def get(type=nil, tag=nil, format=nil) @page = input.page ? input.page.to_i : 1 if type == 'users' user = tag @title = "Links submited by #{user}" @links = Link.find_all_by_user(user, @page) else @tag = tag @title = "Links tagged with #{tag}" if tag @links = Link.find_all_by_score(tag, @page) end @user = current_user @tags = Link.tag_cloud render :list end end class Search < R '/search' def get @query = input.query @user = current_user @title = "Search results for #{@query}" @tags = Link.tag_cloud @links = Link.search(@query) render :list end end class New < R '/new' def get @link = Link.find_by_url(input.url) || Link.new(:title => input.title, :url => input.url || 'http://') @tags = Link.find_tags @link.new_record? ? render(:new) : redirect(Links, @link.id) end def post @tags = Link.find_tags @link = Link.create :title => input.title, :url => input.url, :tags => input.tags, :user => current_user, :description => input.description if @link.valid? @state.notice = 'Link saved !' redirect List else render :new end end end class Links < R '/links/(\d+)' def get(link_id) @link = Link.find link_id @user = current_user render :view end # Called to modify tags with InPlaceEditor def post(link_id) link = Link.find link_id link.update_attribute(:tags, input.value) h link.tags end end class Comments < R '/links/(\d+)/comment' def post(link_id) @link = Link.find link_id @user = current_user @comment = Comment.create :user => @user, :body => input.comment_body, :link_id => @link.id if @comment.valid? @state.notice = 'Comment saved !' redirect Links, @link.id else render :view end end end class Votes < R '/links/(\d+)/vote/(\w+)' def post(link_id, direction) @user = current_user @link = Link.find link_id Vote.for @user, @link, direction end end class Static < R '/(.+.[js|gif|css])' MIME_TYPES = {'.css' => 'text/css', '.js' => 'text/javascript', '.gif' => 'image/gif'} PATH = File.expand_path(__FILE__[/(.*)\//, 1]) def get(path) @headers['Content-Type'] = MIME_TYPES[path[/\.\w+$/, 0]] || "text/plain" unless path.include? '..' @headers['X-Sendfile'] = "#{PATH}/../public/#{path}" else @status = 404; "Invalid path" end end end end