Class: CommentsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, CommentsHelper, PostsHelper
Defined in:
app/controllers/comments_controller.rb

Instance Method Summary collapse

Methods included from CommentsHelper

#comment_path, #comment_url, #comments_link

Methods included from PostsHelper

#permalink_url, #post_description_short, #post_descritpion_has_image?, #post_teaser_for, #smart_post_path, #smart_post_url, #text_short, #typography

Methods included from ApplicationHelper

#benchmark, #bootstrap_class_for, #counter_tag, #format_date, #glyphicon, #js_redirect_to, #recaptcha_tags, #send_ga_events, #smart_date, #snippet, #time_or_date

Instance Method Details

#createObject

POST /comments


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

def create
  @post = Post.find params[:post_id]
  @user = User.find_or_initialize_by user_params
  @user.password = SecureRandom.hex if @user.new_record?
  @comment = @post.comments.new(comment_params.merge(user: @user))

  flag_spammer

  if !verify_recaptcha
    redirect_to smart_post_path(@post), error: t('security.robot_atack')
  elsif cooldown_spammer?(@user)
    redirect_to smart_post_path(@post), error: t('security.spammer_atack')
  else

    if @user.save && @comment.save
      ga_event category: :comments, action: :create, label: @post.title, interaction: 1, value: 1

      EventTracker.notify 'comments', 'create', <<-MSG
      #{@user.name}:
      #{@comment.comment}
      #{@post.title} #{(begin
                          comment_url(@comment)
                        rescue
                          :cant_get_comment_url
                        end)}
      MSG

      begin
         @user
      rescue
        nil
      end

      @user.follow(@post) if params[:subscribe] == '1'

      notify_post_subscribers

      redirect_to comment_path(@comment), notice: t('comments.thank_you')
    else
      redirect_to smart_post_path(@post)
    end
  end
end

#destroyObject

DELETE /comments/1


90
91
92
93
# File 'app/controllers/comments_controller.rb', line 90

def destroy
  @comment.destroy
  redirect_to comments_url, notice: 'Comment was successfully destroyed.'
end

#editObject

GET /comments/1/edit


33
# File 'app/controllers/comments_controller.rb', line 33

def edit; end

#indexObject

GET /comments


11
12
13
# File 'app/controllers/comments_controller.rb', line 11

def index
  @comments = Comment.all
end

#newObject

GET /comments/new


19
20
21
# File 'app/controllers/comments_controller.rb', line 19

def new
  @comment = Comment.new
end

#showObject

GET /comments/1


16
# File 'app/controllers/comments_controller.rb', line 16

def show; end

#spamObject


23
24
25
26
27
28
29
30
# File 'app/controllers/comments_controller.rb', line 23

def spam
  if current_user.admin?
    @comment.update_attributes(spam: true)
    @comment.user.update_attributes(flagged: true)
  end

  redirect_to smart_post_path(@comment.commentable), notice: 'Comment marked as SPAM.'
end

#updateObject

PATCH/PUT /comments/1


81
82
83
84
85
86
87
# File 'app/controllers/comments_controller.rb', line 81

def update
  if @comment.update(comment_params)
    redirect_to @comment, notice: 'Comment was successfully updated.'
  else
    render :edit
  end
end