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?, #footer_content, #homepage_features, #plaxo, #rss_site_index, #site_index

Methods included from BaseHelper

#add_friend_link, #ajax_spinner_for, #avatar_for, #block_to_partial, #box, #city_cloud, #clippings_link, #commentable_url, #container_title, #excerpt_with_jump, #flash_class, #forum_page?, #is_current_user_and_featured?, #jumbotron, #last_active, #more_comments_links, #page_title, #paginating_links, #possesive, #profile_completeness, #render_jumbotron, #render_widgets, #rounded, #search_posts_title, #search_user_posts_path, #show_footer_content?, #tag_auto_complete_field, #time_ago_in_words, #time_ago_in_words_or_date, #tiny_mce_init_if_needed, #tiny_mce_js, #topnav_tab, #truncate_words, #truncate_words_with_highlight, #widget

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


241
242
243
244
245
246
247
# File 'app/controllers/posts_controller.rb', line 241

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


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 107

def create
  @user = User.find(params[:user_id])
  @post = Post.new(post_params)
  @post.user = @user

  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


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

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


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

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

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/controllers/posts_controller.rb', line 225

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).to_a

  @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[:q])
    @posts = @search.result
    @posts = @posts.where(:user_id => @user.id).order('created_at DESC').page(params[:page]).per(params[:size]||10)
  end
end

#newObject

GET /posts/new


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

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

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/posts_controller.rb', line 186

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


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/posts_controller.rb', line 207

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


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/posts_controller.rb', line 164

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))
    flash[:notice] = "Your message has been sent."
    respond_to do |format|
      format.html {render :partial => 'shared/messages'}
      format.js
    end
  else
    flash[:error] = "You entered invalid addresses: "+ @post.invalid_emails.join(', ')+". Please correct these and try again."

    respond_to do |format|
      format.html {render :partial => 'shared/messages', :status => 500}
      format.js
    end
  end
end

#showObject

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


60
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 60

def show
  @post = Post.unscoped.find(params[:id])
  redirect_to user_posts_path(@user), :alert => :post_not_published_yet.l and return false unless @post.is_live? || @post.user.eql?(current_user) || admin? || moderator?

  @rss_title = "#{configatron.community_name}: #{@user.}'s posts"
  @rss_url = user_posts_path(@user,:format => :rss)

  @user = @post.user
  @is_current_user = @user.eql?(current_user)
  @comment = Comment.new

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

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

#updateObject

patch /posts/1 patch /posts/1.xml


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

def update
  @post = Post.unscoped.find(params[:id])
  @user = @post.user

  respond_to do |format|
    if @post.update_attributes(post_params)
      @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