Class: CommentsController

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

#approveObject



30
31
32
33
34
35
36
37
38
# File 'app/controllers/comments_controller.rb', line 30

def approve
  @comment = Comment.find(params[:id])
  @comment.ham! if configatron.has_key?(:akismet_key)
  @comment.role = 'published'
  @comment.save!
  respond_to do |format|
    format.js
  end
end

#createObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/comments_controller.rb', line 98

def create
  commentable_type = get_commentable_type(params[:commentable_type])
  @commentable = commentable_type.singularize.constantize.find(params[:commentable_id])

  @comment = @commentable.comments.new(comment_params)

  @comment.recipient = @commentable.owner
  @comment.user_id = current_user.id if current_user
  @comment.author_ip = request.remote_ip #save the ip address for everyone, just because

  respond_to do |format|
    if (logged_in? || verify_recaptcha(@comment)) && @comment.save
      flash.now[:notice] = :comment_was_successfully_created.l
      format.html { redirect_to commentable_url(@comment) }
      format.js
    else
      flash.now[:error] = :comment_save_error.l_with_args(:error => @comment.errors.full_messages.to_sentence)
      format.html { redirect_to commentable_comments_path(commentable_type.tableize, @commentable) }
      format.js
    end
  end
end

#destroyObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/comments_controller.rb', line 121

def destroy
  @comment = Comment.find(params[:id])
  if @comment.can_be_deleted_by(current_user) && @comment.destroy
    if params[:spam] && configatron.has_key?(:akismet_key)
      @comment.spam!
    end
    flash.now[:notice] = :the_comment_was_deleted.l
  else
    flash.now[:error] = :comment_could_not_be_deleted.l
  end
  respond_to do |format|
    format.html { redirect_to users_url }
    format.js   {
      render :inline => flash[:error], :status => 500 if flash[:error]
      render if flash[:notice]
    }
  end
end

#editObject



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

def edit
  @comment = Comment.find(params[:id])
  respond_to do |format|
    format.js
  end
end

#indexObject



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

def index
  commentable_type = get_commentable_type(params[:commentable_type])
  commentable_class = commentable_type.singularize.constantize
  commentable_type_humanized = commentable_type.humanize
  commentable_type_tableized = commentable_type.tableize

  if @commentable = commentable_class.find(params[:commentable_id])
    unless logged_in? || (@commentable.owner && @commentable.owner.profile_public?)
      flash.now[:error] = :private_user_profile_message.l
      redirect_to  and return
    end

    @comments = @commentable.comments.recent.page(params[:page])
    @title = commentable_type_humanized
    @rss_url = commentable_comments_url(commentable_type_tableized, @commentable, :format => :rss)

    if @comments.any?
      first_comment = @comments.first
      @user = first_comment.recipient
      @title = first_comment.commentable_name
      @back_url = commentable_url(first_comment)
      respond_to do |format|
        @rss_title = "#{configatron.community_name}: #{commentable_type_humanized} Comments - #{@title}"
        format.html
        format.rss {
          render_comments_rss_feed_for(@comments, @commentable, @rss_title) and return
        }
      end
    else
      if @commentable.is_a?(User)
        @user = @commentable
        @title = @user.
        @back_url = user_path(@user)
      elsif @user = @commentable.user
        @title = @commentable.respond_to?(:title) ? @commentable.title : @title
        @back_url = url_for([@user, @commentable])
      end

      respond_to do |format|
        format.html
        format.rss {
          @rss_title = "#{configatron.community_name}: #{commentable_type_humanized} Comments - #{@title}"
          render_comments_rss_feed_for([], @commentable, @rss_title) and return
        }
      end
    end
  else
    flash[:notice] = :no_comments_found.l_with_args(:type => commentable_type_humanized)
    redirect_to home_path
  end
end

#newObject



92
93
94
95
# File 'app/controllers/comments_controller.rb', line 92

def new
  @commentable = get_commentable_type(params[:commentable_type]).constantize.find(params[:commentable_id])
  redirect_to commentable_comments_url(@commentable.class.to_s.tableize, @commentable.id)
end

#unsubscribeObject



141
142
143
144
145
146
147
148
# File 'app/controllers/comments_controller.rb', line 141

def unsubscribe
  @comment = Comment.find(params[:id])
  if @comment.token_for(params[:email]).eql?(params[:token])
    @comment.unsubscribe_notifications(params[:email])
    flash[:notice] = :comment_unsubscribe_succeeded.l
  end
  redirect_to commentable_url(@comment)
end

#updateObject



19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/comments_controller.rb', line 19

def update
  @comment = Comment.find(params[:id])
  @comment.update_attributes!(comment_params)
rescue ActiveRecord::RecordInvalid
  flash[:error] = :an_error_occurred.l
ensure
  respond_to do |format|
    format.js
  end
end