Class: SbPostsController

Inherits:
BaseController show all
Defined in:
app/controllers/sb_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

#createObject


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

def create
  @topic = Topic.includes(:forum).where(:id => params[:topic_id].to_i, :forum_id => params[:forum_id].to_i).first
  if @topic.locked?
    respond_to do |format|
      format.html do
        flash[:notice] = :this_topic_is_locked.l
        redirect_to(forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id]))
      end
    end
    return
  end

  @forum = @topic.forum
  @post  = @topic.sb_posts.new(sb_post_params)

  @post.user = current_user if current_user
  @post.author_ip = request.remote_ip #save the ip address for everyone, just because

  if (logged_in? || verify_recaptcha(@post)) && @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.js
    end
  else
    flash.now[:notice] = @post.errors.full_messages.to_sentence
    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')}.merge({:sb_post => params[:sb_post]}))
      end
      format.js
    end
  end
end

#destroyObject


115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/sb_posts_controller.rb', line 115

def destroy
  @post.destroy
  flash[:notice] = :sb_post_was_deleted.l_with_args(:title => CGI::escapeHTML(@post.topic.title))
  # check for posts_count == 1 because its cached and counting the currently deleted post
  @post.topic.destroy and redirect_to forum_path(params[:forum_id]) if @post.topic.sb_posts_count == 1
  respond_to do |format|
    format.html do
      redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :page => params[:page]) unless performed?
    end
    format.js
    format.xml { head 200 }
  end
end

#editObject


94
95
96
97
98
99
# File 'app/controllers/sb_posts_controller.rb', line 94

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

#indexObject


20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/sb_posts_controller.rb', line 20

def index
  conditions = []
  [:user_id, :forum_id].each { |attr|
    conditions << SbPost.send(:sanitize_sql, ["sb_posts.#{attr} = ?", params[attr].to_i]) if params[attr]
  }
  conditions = conditions.any? ? conditions.collect { |c| "(#{c})" }.join(' AND ') : nil

  @posts = SbPost.with_query_options.where(conditions).page(params[:page])

  @users = User.distinct.where(:id => @posts.collect(&:user_id).uniq).to_a.index_by(&:id)
end

#monitoredObject


41
42
43
44
# File 'app/controllers/sb_posts_controller.rb', line 41

def monitored
  @user = User.find params[:user_id]
  @posts = SbPost.with_query_options.joins('INNER JOIN monitorships ON monitorships.topic_id = topics.id').where('monitorships.user_id = ? AND sb_posts.user_id != ?', params[:user_id], @user.id).page(params[:page])
end

#newObject


52
53
54
55
56
# File 'app/controllers/sb_posts_controller.rb', line 52

def new
  if logged_in?
    redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => 'reply-form', :page => params[:page] || '1') and return
  end
end

#searchObject


32
33
34
35
36
37
38
39
# File 'app/controllers/sb_posts_controller.rb', line 32

def search
  conditions = params[:q].blank? ? nil : SbPost.send(:sanitize_sql, ['LOWER(sb_posts.body) LIKE ?', "%#{params[:q]}%"])

  @posts = SbPost.with_query_options.where(conditions).page(params[:page])

  @users = User.distinct.where(:id => @posts.collect(&:user_id).uniq).to_a.index_by(&:id)
  render :action => :index
end

#showObject


46
47
48
49
50
# File 'app/controllers/sb_posts_controller.rb', line 46

def show
  respond_to do |format|
    format.html { redirect_to forum_topic_path(@post.forum_id, @post.topic_id) }
  end
end

#updateObject


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

def update
  @post.update_attributes!(sb_post_params)
rescue ActiveRecord::RecordInvalid
  flash[:bad_reply] = :an_error_occurred.l
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