public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
insoshi / app / controllers / posts_controller.rb
100644 213 lines (182 sloc) 5.509 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# NOTE: We use "posts" for both forum topic posts and blog posts,
# There is some trickery to handle the two in a unified manner.
class PostsController < ApplicationController
  include ApplicationHelper
  
  before_filter :login_required
  before_filter :get_instance_vars
  before_filter :check_blog_mismatch, :only => :show
  before_filter :authorize_new, :only => [:create, :new]
  before_filter :authorize_change, :only => [:edit, :update]
  before_filter :authorize_destroy, :only => :destroy
 
  def index
    redirect_to blog_url(@blog) if blog?
    redirect_to forum_topic_url(@forum, @topic) if forum?
  end
 
  # Show a blog post.
  # Forum posts don't get shown individually.
  def show
    @post = BlogPost.find(params[:id])
 
    respond_to do |format|
      format.html # show.html.erb
    end
  end
 
  # Used for both forum and blog posts.
  def new
    @post = model.new
 
    respond_to do |format|
      format.html { render :action => resource_template("new") }
    end
  end
 
  # Used for both forum and blog posts.
  def edit
    respond_to do |format|
      format.html { render :action => resource_template("edit") }
    end
  end
 
  # Used for both forum and blog posts.
  def create
    @post = new_resource_post
    
    respond_to do |format|
      if @post.save
        flash[:success] = 'Post created'
        format.html { redirect_to post_url }
      else
        format.html { render :action => resource_template("new") }
      end
    end
  end
 
  def update
    respond_to do |format|
      if @post.update_attributes(params[:post])
        flash[:success] = 'Post updated'
        format.html { redirect_to post_url }
      else
        format.html { render :action => resource_template("edit") }
      end
    end
  end
 
  def destroy
    @post = model.find(params[:id])
    @post.destroy
    flash[:success] = "Post destroyed"
 
    respond_to do |format|
      format.html { redirect_to posts_url }
    end
  end
  
  private
  
    ## Before filters
  
    def get_instance_vars
      @post = model.find(params[:id]) unless params[:id].nil?
      if forum?
        @forum = Forum.find(:first)
        @topic = Topic.find(params[:topic_id])
        @body = "forum"
      elsif blog?
        @blog = Blog.find(params[:blog_id])
        @body = "blog"
      end
    end
    
    def check_blog_mismatch
      redirect_to home_url unless @post.blog == @blog
    end
 
    # Verify the person is authorized to create a post.
    def authorize_new
      if forum?
        true # This will change once there are groups
      elsif blog?
        redirect_to home_url unless current_person?(@blog.person)
      end
    end
 
    # Make sure the current user is authorized to edit a post.
    def authorize_change
      if forum?
        authorized = current_person?(@post.person) || current_person.admin?
        redirect_to home_url unless authorized
      elsif blog?
        authorized = current_person?(@blog.person) && valid_post?
        redirect_to home_url unless authorized
      end
    end
    
    # A post is valid if its blog is the current blog.
    def valid_post?
      @post.blog == @blog
    end
    
    # Authorize post deletions.
    # Only admin users can destroy forum posts.
    # Only blog owners can destroy blog posts.
    def authorize_destroy
      if forum?
        redirect_to home_url unless current_person.admin?
      elsif blog?
        authorize_change
      end
    end
 
    ## Handle forum and blog posts in a uniform manner.
    
    # Return the appropriate model corresponding to the type of post.
    def model
      if forum?
        ForumPost
      elsif blog?
        BlogPost
      end
    end
    
    # Return the posts array for the given resource.
    def resource_posts
      if forum?
        @topic.posts
      elsif blog?
        @blog.posts.paginate(:page => params[:page])
      end
    end
    
    # Return a new post for the given resource.
    def new_resource_post
      if forum?
        @post = @topic.posts.new(params[:post].merge(:person => current_person))
      elsif blog?
        @post = @blog.posts.new(params[:post])
      end
    end
    
    # Return the template for the current resource given the name.
    # For example, on a blog resource_template("new") gives "blog_new"
    def resource_template(name)
      "#{resource}_#{name}"
    end
 
    # Return a string for the resource.
    def resource
      if forum?
        "forum"
      elsif blog?
        "blog"
      end
    end
    
    # Return URL to redirect to after post creation.
    def post_url
      if forum?
        # By using including :posts, we ensure that the user's browser
        # will display the link as 'followed' when he makes a post,
        # so the link color will only change back to 'unfollowed'
        # if someone *else* makes a post.
        forum_topic_url(@forum, @topic, :posts => @topic.posts.count)
      elsif blog?
        blog_post_url(@blog, @post)
      end
    end
 
    # Return the URL for the resource posts (forum topic or blog).
    def posts_url
      if forum?
       forum_topic_url(@forum, @topic)
      elsif blog?
        blog_url(@blog)
      end
    end
 
    # True if resource lives in a discussion forum.
    # We reserve the right to suppress forum_id since there's only one forum,
    # so use topic_id to tell that it's a forum.
    def forum?
      !params[:topic_id].nil?
    end
 
    # True if resource lives in a blog.
    def blog?
      !params[:blog_id].nil?
    end
end