Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 442
- Log:
Implement per-page selection (25, 50, 100 items) for lists of posts
and posts within topics, as per Ticket #261. RSS feeds for posts within
topics have been increased from 25 to 50 items in passing as these are
quite lightweight to process.
- Author:
- rool
- Date:
- Fri Aug 30 07:46:48 +0100 2013
- Size:
- 5912 Bytes
1 | class PostsController < ApplicationController |
2 | before_filter :find_post, :except => [:index, :index_rss, :create, :monitored, :search] |
3 | @@query_options = { :per_page => 25, :select => 'posts.*, topics.title as topic_title, forums.name as forum_name', :joins => 'inner join topics on posts.topic_id = topics.id inner join forums on topics.forum_id = forums.id', :order => 'posts.created_at desc' } |
4 | |
5 | @@hubssolib_permissions = HubSsoLib::Permissions.new({ |
6 | :new => [ :admin, :webmaster, :privileged, :normal ], |
7 | :create => [ :admin, :webmaster, :privileged, :normal ], |
8 | :edit => [ :admin, :webmaster, :privileged, :normal ], |
9 | :update => [ :admin, :webmaster, :privileged, :normal ], |
10 | :destroy => [ :admin, :webmaster ], |
11 | }) |
12 | |
13 | def PostsController.hubssolib_permissions |
14 | @@hubssolib_permissions |
15 | end |
16 | |
17 | def index |
18 | index_initialise |
19 | render_posts_or_xml |
20 | end |
21 | |
22 | # Backwards compatibility with RForum global feed via a routing hack. |
23 | # |
24 | def index_rss |
25 | index_initialise |
26 | render :action => "index.rxml", :layout => false |
27 | end |
28 | |
29 | def search |
30 | conditions = params[:q].blank? ? nil : Post.send(:sanitize_sql, ['LOWER(posts.body) LIKE ?', "%#{params[:q].downcase}%"]) |
31 | @post_pages, @posts = paginate(:posts, @@query_options.merge(:conditions => conditions).merge(per_page())) |
32 | @users = User.find(:all, :select => 'distinct *', :conditions => ['id in (?)', @posts.collect(&:user_id).uniq]).index_by(&:id) |
33 | render_posts_or_xml :index |
34 | end |
35 | |
36 | def monitored |
37 | @user = User.find params[:user_id] |
38 | options = @@query_options.merge(:conditions => ['monitorships.user_id = ? and posts.user_id != ?', params[:user_id], @user.id]) |
39 | options[:joins] += ' inner join monitorships on monitorships.topic_id = topics.id' |
40 | @post_pages, @posts = paginate(:posts, options.merge(per_page())) |
41 | render_posts_or_xml |
42 | end |
43 | |
44 | def show |
45 | respond_to do |format| |
46 | format.html { redirect_to topic_path(@post.forum_id, @post.topic_id) } |
47 | format.xml { render :xml => @post.to_xml } |
48 | end |
49 | end |
50 | |
51 | def create |
52 | @topic = Topic.find_by_id_and_forum_id(params[:topic_id],params[:forum_id], :include => :forum) |
53 | if @topic.locked? |
54 | respond_to do |format| |
55 | format.html do |
56 | flash[:notice] = 'This topic is locked.' |
57 | redirect_to(topic_path(:forum_id => params[:forum_id], :id => params[:topic_id])) |
58 | end |
59 | format.xml do |
60 | render :text => 'This topic is locked.', :status => 400 |
61 | end |
62 | end |
63 | return |
64 | end |
65 | @forum = @topic.forum |
66 | @post = @topic.posts.build(params[:post]) |
67 | @post.user = current_user |
68 | @post.save! |
69 | respond_to do |format| |
70 | format.html do |
71 | redirect_to topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => @post.dom_id, :page => params[:page] || '1') |
72 | end |
73 | format.xml { head :created, :location => formatted_post_url(:forum_id => params[:forum_id], :topic_id => params[:topic_id], :id => @post, :format => :xml) } |
74 | end |
75 | rescue ActiveRecord::RecordInvalid |
76 | flash[:bad_reply] = 'Please post something at least...' |
77 | respond_to do |format| |
78 | format.html do |
79 | redirect_to topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => 'reply-form', :page => params[:page] || '1') |
80 | end |
81 | format.xml { render :xml => @post.errors.to_xml, :status => 400 } |
82 | end |
83 | end |
84 | |
85 | def edit |
86 | respond_to do |format| |
87 | format.html |
88 | format.js |
89 | end |
90 | end |
91 | |
92 | def update |
93 | @post.attributes = params[:post] |
94 | @post.save! |
95 | rescue ActiveRecord::RecordInvalid |
96 | flash[:bad_reply] = 'An error occurred' |
97 | ensure |
98 | respond_to do |format| |
99 | format.html do |
100 | redirect_to topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => @post.dom_id, :page => params[:page] || '1') |
101 | end |
102 | format.js |
103 | format.xml { head 200 } |
104 | end |
105 | end |
106 | |
107 | def destroy |
108 | @post.destroy |
109 | flash[:notice] = "Post of '#{CGI::escapeHTML @post.topic.title}' was deleted." |
110 | # check for posts_count == 1 because its cached and counting the currently deleted post |
111 | @post.topic.destroy and redirect_to forum_path(params[:forum_id]) if @post.topic.posts_count == 1 |
112 | respond_to do |format| |
113 | format.html do |
114 | redirect_to topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :page => params[:page]) unless performed? |
115 | end |
116 | format.xml { head 200 } |
117 | end |
118 | end |
119 | |
120 | protected |
121 | def index_initialise |
122 | conditions = [] |
123 | [:user_id, :forum_id].each { |attr| conditions << Post.send(:sanitize_sql, ["posts.#{attr} = ?", params[attr]]) if params[attr] } |
124 | conditions = conditions.any? ? conditions.collect { |c| "(#{c})" }.join(' AND ') : nil |
125 | @post_pages, @posts = paginate(:posts, @@query_options.merge(:conditions => conditions).merge(per_page())) |
126 | @users = User.find(:all, :select => 'distinct *', :conditions => ['id in (?)', @posts.collect(&:user_id).uniq]).index_by(&:id) |
127 | end |
128 | |
129 | def authorized? |
130 | action_name == 'create' || @post.editable_by?(current_user) |
131 | end |
132 | |
133 | def find_post |
134 | @post = Post.find_by_id_and_topic_id_and_forum_id(params[:id], params[:topic_id], params[:forum_id]) || raise(ActiveRecord::RecordNotFound) |
135 | end |
136 | |
137 | def render_posts_or_xml(template_name = action_name) |
138 | respond_to do |format| |
139 | format.html { render :action => "#{template_name}.rhtml" } |
140 | format.rss { render :action => "#{template_name}.rxml", :layout => false } |
141 | format.xml { render :xml => @posts.to_xml } |
142 | end |
143 | end |
144 | |
145 | def per_page |
146 | per_page = {} |
147 | per_page[:per_page] = [params[:posts_per_page].to_i, 25].max if (params.has_key?(:posts_per_page)) |
148 | |
149 | return per_page |
150 | end |
151 | end |