Class: PostsController

Inherits:
BeastApplicationController show all
Defined in:
app/controllers/posts_controller.rb

Constant Summary collapse

@@query_options =
{ :select => "#{Post.table_name}.*, #{Topic.table_name}.title as topic_title, #{Forum.table_name}.name as forum_name", :joins => "inner join #{Topic.table_name} on #{Post.table_name}.topic_id = #{Topic.table_name}.id inner join #{Forum.table_name} on #{Topic.table_name}.forum_id = #{Forum.table_name}.id" }

Instance Method Summary collapse

Instance Method Details

#createObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/posts_controller.rb', line 45

def create
  @topic = Topic.find_by_id_and_forum_id(params[:topic_id],params[:forum_id])
  if @topic.locked?
    respond_to do |format|
      format.html do
        flash[:notice] = 'This topic is locked.'[:locked_topic]
        redirect_to(forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id]))
      end
      format.xml do
        render :text => 'This topic is locked.'[:locked_topic], :status => 400
      end
    end
    return
  end
  @forum = @topic.forum
  @post  = @topic.posts.build(params[:post])
  @post.user = current_user
  @post.save!
  respond_to do |format|
    format.html do
      redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => @post.dom_id, :page => params[:page] || '1')
    end
    format.xml { head :created, :location => post_url(:forum_id => params[:forum_id], :topic_id => params[:topic_id], :id => @post, :format => :xml) }
  end
rescue ActiveRecord::RecordInvalid
  flash[:bad_reply] = 'Please post something at least...'[:post_something_message]
  respond_to do |format|
    format.html do
      redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => 'reply-form', :page => params[:page] || '1')
    end
    format.xml { render :xml => @post.errors.to_xml, :status => 400 }
  end
end

#destroyObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/posts_controller.rb', line 101

def destroy
  @post.destroy
  flash[:notice] = "Post of '{title}' was deleted."[:post_deleted_message, @post.topic.title]
  respond_to do |format|
    format.html do
      redirect_to(@post.topic.frozen? ? 
        forum_path(params[:forum_id]) :
        forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :page => params[:page]))
    end
    format.xml { head 200 }
  end
end

#editObject



79
80
81
82
83
84
# File 'app/controllers/posts_controller.rb', line 79

def edit
  respond_to do |format| 
    format.html
    format.js
  end
end

#indexObject



11
12
13
14
15
16
17
18
# File 'app/controllers/posts_controller.rb', line 11

def index
  conditions = []
  [:user_id, :forum_id, :topic_id].each { |attr| conditions << Post.send(:sanitize_sql, ["#{Post.table_name}.#{attr} = ?", params[attr]]) if params[attr] }
  conditions = conditions.empty? ? nil : conditions.collect { |c| "(#{c})" }.join(' AND ')
  @posts = Post.paginate @@query_options.merge(:conditions => conditions, :page => params[:page], :count => {:select => "#{Post.table_name}.id"}, :order => post_order)
  @users = User.find(:all, :select => 'distinct *', :conditions => ['id in (?)', @posts.collect(&:user_id).uniq]).index_by(&:id)
  render_posts_or_xml
end

#monitoredObject



27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/posts_controller.rb', line 27

def monitored
  @user = User.find params[:user_id]
  options = @@query_options.merge(:conditions => ["#{Monitorship.table_name}.user_id = ? and #{Post.table_name}.user_id != ? and #{Monitorship.table_name}.active = ?", params[:user_id], @user.id, true])
  options[:order]  = post_order
  options[:joins] += " inner join #{Monitorship.table_name} on #{Monitorship.table_name}.topic_id = #{Topic.table_name}.id"
  options[:page]   = params[:page]
  options[:count]  = {:select => "#{Post.table_name}.id"}
  @posts = Post.paginate options
  render_posts_or_xml
end

#searchObject



20
21
22
23
24
25
# File 'app/controllers/posts_controller.rb', line 20

def search		
  conditions = params[:q].blank? ? nil : Post.send(:sanitize_sql, ["LOWER(#{Post.table_name}.body) LIKE ?", "%#{params[:q]}%"])
  @posts = Post.paginate @@query_options.merge(:conditions => conditions, :page => params[:page], :count => {:select => "#{Post.table_name}.id"}, :order => post_order)
  @users = User.find(:all, :select => 'distinct *', :conditions => ['id in (?)', @posts.collect(&:user_id).uniq]).index_by(&:id)
  render_posts_or_xml :index
end

#showObject



38
39
40
41
42
43
# File 'app/controllers/posts_controller.rb', line 38

def show
  respond_to do |format|
    format.html { redirect_to forum_topic_path(@post.forum_id, @post.topic_id) }
    format.xml  { render :xml => @post.to_xml }
  end
end

#updateObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/posts_controller.rb', line 86

def update
  @post.attributes = params[:post]
  @post.save!
rescue ActiveRecord::RecordInvalid
  flash[:bad_reply] = 'An error occurred'[:error_occured_message]
ensure
  respond_to do |format|
    format.html do
      redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => @post.dom_id, :page => params[:page] || '1')
    end
    format.js
    format.xml { head 200 }
  end
end