discourse/app/controllers/list_controller.rb

80 lines
2.3 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
class ListController < ApplicationController
before_filter :ensure_logged_in, except: [:index, :category]
skip_before_filter :check_xhr
# Create our filters
[:popular, :favorited, :read, :posted, :unread, :new].each do |filter|
2013-02-07 10:45:24 -05:00
define_method(filter) do
2013-02-05 14:16:51 -05:00
list_opts = {page: params[:page]}
# html format means we need to farm exclude from the site options
if params[:format].blank? or params[:format] == "html"
#TODO objectify this stuff
SiteSetting.top_menu.split('|').each do |f|
s = f.split(",")
if s[0] == action_name || (action_name == "index" && s[0] == "popular")
list_opts[:exclude_category] = s[1][1..-1] if s.length == 2
end
end
end
list_opts[:exclude_category] = params[:exclude_category] if params[:exclude_category].present?
list = TopicQuery.new(current_user, list_opts).send("list_#{filter}")
list.more_topics_url = url_for(self.send "#{filter}_path".to_sym, list_opts.merge(format: 'json', page: next_page))
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
respond(list)
2013-02-07 10:45:24 -05:00
end
2013-02-05 14:16:51 -05:00
end
alias_method :index, :popular
def category
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
query = TopicQuery.new(current_user, page: params[:page])
list = nil
# If they choose uncategorized, return topics NOT in a category
if params[:category] == SiteSetting.uncategorized_name
list = query.list_uncategorized
else
category = Category.where(slug: params[:category]).includes(:featured_users).first
guardian.ensure_can_see!(category)
list = query.list_category(category)
end
list.more_topics_url = url_for(category_path(params[:category], page: next_page, format: "json"))
respond(list)
end
protected
def respond(list)
list.draft_key = Draft::NEW_TOPIC
list.draft_sequence = DraftSequence.current(current_user, Draft::NEW_TOPIC)
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
draft = Draft.get(current_user, list.draft_key, list.draft_sequence) if current_user
list.draft = draft
discourse_expires_in 1.minute
2013-02-05 14:16:51 -05:00
respond_to do |format|
format.html do
2013-02-07 10:45:24 -05:00
@list = list
2013-02-05 14:16:51 -05:00
store_preloaded('topic_list', MultiJson.dump(TopicListSerializer.new(list, scope: guardian)))
render 'list'
end
format.json do
render_serialized(list, TopicListSerializer)
end
end
end
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
def next_page
params[:page].to_i + 1
end
end