Class: PostsController

Inherits:
BaseController show all
Includes:
Viewable
Defined in:
app/controllers/posts_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#advertise, #cache_action?, #css_help, #footer_content, #homepage_features, #plaxo, #rss_site_index, #site_index

Methods included from LocalizedApplication

#get_matching_ui_locale, #get_sorted_langs_from_accept_header, #get_valid_lang_from_accept_header, #set_locale

Methods included from AuthenticatedSystem

#login_by_token, #update_last_seen_at

Instance Method Details

#category_tips_updateObject



233
234
235
236
237
238
239
# File 'app/controllers/posts_controller.rb', line 233

def category_tips_update
  return unless request.xhr?
  @category = Category.find(params[:post_category_id] )
  render :partial => "categories/tips", :locals => {:category => @category}    
rescue ActiveRecord::RecordNotFound
  render :partial => "categories/tips", :locals => {:category => nil}    
end

#createObject

POST /posts POST /posts.xml



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
# File 'app/controllers/posts_controller.rb', line 106

def create    
  @user = User.find(params[:user_id])
  @post = Post.new(params[:post])
  @post.user = @user
  @post.tag_list = params[:tag_list] || ''
  
  respond_to do |format|
    if @post.save
      @post.create_poll(params[:poll], params[:choices]) if params[:poll]
      
      flash[:notice] = @post.category ? :post_created_for_category.l_with_args(:category => @post.category.name.singularize) : :your_post_was_successfully_created.l
      format.html { 
        if @post.is_live?
          redirect_to @post.category ? category_path(@post.category) : user_post_path(@user, @post) 
        else
          redirect_to manage_user_posts_path(@user)
        end
      }
      format.js
    else
      format.html { render :action => "new" }
      format.js        
    end
  end
end

#destroyObject

DELETE /posts/1 DELETE /posts/1.xml



152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/posts_controller.rb', line 152

def destroy
  @user = User.find(params[:user_id])
  @post = Post.find(params[:id])
  @post.destroy
  
  respond_to do |format|
    format.html { 
      flash[:notice] = :your_post_was_deleted.l
      redirect_to manage_user_posts_url(@user)   
      }
  end
end

#editObject

GET /posts/1;edit



100
101
102
# File 'app/controllers/posts_controller.rb', line 100

def edit
  @post = Post.unscoped.find(params[:id])
end


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/controllers/posts_controller.rb', line 217

def featured
  @posts = Post.by_featured_writers.recent.page(params[:page])
  @featured_writers = User.featured
      
  @rss_title = "#{configatron.community_name} "+:featured_posts.l
  @rss_url = featured_rss_url
  respond_to do |format|
    format.html 
    format.rss {
      render_rss_feed_for(@posts, { :feed => {:title => @rss_title, :link => recent_url},
        :item => {:title => :title, :link => Proc.new {|post| user_post_url(post.user, post)}, :description => :post, :pub_date => :published_at}
        })
    }
  end    
end

#indexObject



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
# File 'app/controllers/posts_controller.rb', line 30

def index
  @user = User.find(params[:user_id])            
  @category = Category.find_by_name(params[:category_name]) if params[:category_name]

  @posts = @user.posts.recent
  @posts = @post.where('category_id = ?', @category.id) if @category
  @posts = @posts.page(params[:page]).per(10)
  
  @is_current_user = @user.eql?(current_user)

  @popular_posts = @user.posts.order("view_count DESC").limit(10).all
  
  @rss_title = "#{configatron.community_name}: #{@user.}'s posts"
  @rss_url = user_posts_path(@user,:format => :rss)
      
  respond_to do |format|
    format.html # index.rhtml
    format.rss {
      render_rss_feed_for(@posts,
         { :feed => {:title => @rss_title, :link => url_for(:controller => 'posts', :action => 'index', :user_id => @user) },
           :item => {:title => :title,
                     :description => :post,
                     :link => Proc.new {|post| user_post_url(post.user, post)},
                     :pub_date => :published_at} })
    }
  end
end

#manageObject



22
23
24
25
26
27
28
# File 'app/controllers/posts_controller.rb', line 22

def manage
  Post.unscoped do
    @search = Post.search(params[:search])
    @search.meta_sort ||= 'created_at.desc'
    @posts = @search.where(:user_id => @user.id).page(params[:page]).per(params[:size]||10)
  end
end

#newObject

GET /posts/new



92
93
94
95
96
97
# File 'app/controllers/posts_controller.rb', line 92

def new
  @user = User.find(params[:user_id])    
  @post = Post.new(params[:post])
  @post.published_as = 'live'
  @categories = Category.find(:all)
end


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/posts_controller.rb', line 178

def popular
  @posts = Post.find_popular({:limit => 15, :since => 3.days})

  @monthly_popular_posts = Post.find_popular({:limit => 20, :since => 30.days})
  
  @related_tags = ActsAsTaggableOn::Tag.find_by_sql("SELECT tags.id, tags.name, count(*) AS count 
    FROM taggings, tags 
    WHERE tags.id = taggings.tag_id GROUP BY tags.id, tags.name");

  @rss_title = "#{configatron.community_name} "+:popular_posts.l
  @rss_url = popular_rss_url    
  respond_to do |format|
    format.html # index.rhtml
    format.rss {
      render_rss_feed_for(@posts, { :feed => {:title => @rss_title, :link => popular_url},
        :item => {:title => :title, :link => Proc.new {|post| user_post_url(post.user, post)}, :description => :post, :pub_date => :published_at}
        })
    }
  end
end

#previewObject



86
87
88
89
# File 'app/controllers/posts_controller.rb', line 86

def preview
  @post = Post.unscoped.find(params[:id])
  redirect_to(:controller => 'sessions', :action => 'new') and return false unless @post.user.eql?(current_user) || admin? || moderator?
end

#recentObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/posts_controller.rb', line 199

def recent
  @posts = Post.recent.page(params[:page]).per(20)

  @recent_clippings = Clipping.find_recent(:limit => 15)
  @recent_photos = Photo.find_recent(:limit => 10)
  
  @rss_title = "#{configatron.community_name} "+:recent_posts.l
  @rss_url = recent_rss_url
  respond_to do |format|
    format.html 
    format.rss {
      render_rss_feed_for(@posts, { :feed => {:title => @rss_title, :link => recent_url},
        :item => {:title => :title, :link => Proc.new {|post| user_post_url(post.user, post)}, :description => :post, :pub_date => :published_at}
        })
    }
  end    
end

#send_to_friendObject



165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/posts_controller.rb', line 165

def send_to_friend
  unless params[:emails]
    render :partial => 'posts/send_to_friend', :locals => {:user_id => params[:user_id], :post_id => params[:post_id]} and return
  end
  @post = Post.find(params[:id])
  if @post.send_to(params[:emails], params[:message], (current_user || nil))
    render :inline => "It worked!"            
  else
    render :inline => "You entered invalid addresses: <ul>"+ @post.invalid_emails.collect{|email| '<li>'+email+'</li>' }.join+"</ul> Please correct these and try again.", :status => 500
  end
end

#showObject

GET /posts/1 GET /posts/1.xml



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/posts_controller.rb', line 61

def show
  @rss_title = "#{configatron.community_name}: #{@user.}'s posts"
  @rss_url = user_posts_path(@user,:format => :rss)
  
  @post = Post.unscoped.find(params[:id])

  @user = @post.user
  @is_current_user = @user.eql?(current_user)
  @comment = Comment.new(params[:comment])

  @comments = @post.comments.includes(:user).order('created_at DESC').limit(20)

  @previous = @post.previous_post
  @next = @post.next_post    
  @popular_posts = @user.posts.except(:order).order('view_count DESC').limit(10).all
  @related = Post.find_related_to(@post)
  @most_commented = Post.find_most_commented    
end

#updateObject

PUT /posts/1 PUT /posts/1.xml



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/posts_controller.rb', line 134

def update
  @post = Post.unscoped.find(params[:id])
  @user = @post.user
  @post.tag_list = params[:tag_list] || ''
  
  respond_to do |format|
    if @post.update_attributes(params[:post])
      @post.update_poll(params[:poll], params[:choices]) if params[:poll]
      
      format.html { redirect_to user_post_path(@post.user, @post) }
    else
      format.html { render :action => "edit" }  
    end
  end
end

#update_viewsObject



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

def update_views
  @post = Post.find(params[:id])
  updated = update_view_count(@post)
  render :text => updated ? 'updated' : 'duplicate'
end