Class: ReactionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/reactions_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#addObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/reactions_controller.rb', line 7

def add
  reaction_type = ReactionType.find(params[:reaction_id])
  comment_text = params[:comment]
  comment = nil

  if !comment_text.blank?
    if @post.comments_disabled && !current_user.is_moderator && !current_user.is_admin
      render json: { status: 'failed', message: 'Comments have been disabled on this post.' }, status: :forbidden
      return
    end

    thread = CommentThread.find_or_create_by(title: reaction_type.name, post: @post)

    comment = Comment.new(post: @post, content: comment_text, user: current_user, comment_thread: thread)
  elsif reaction_type.requires_comment
    render json: { status: 'failed', message: 'This reaction type requires a comment with an explanation.' },
           status: :forbidden
    return
  end

  old_reaction = Reaction.where(user: current_user, post: @post, reaction_type: reaction_type)
  if old_reaction.any?
    render json: { status: 'failed', message: 'You already added this reaction to this post.' },
           status: :forbidden
    return
  end

  reaction = Reaction.new(user: current_user, post: @post, reaction_type: reaction_type, comment: comment)

  begin
    ActiveRecord::Base.transaction do
      thread&.save!
      comment&.save!
      reaction.save!
    end

    render json: { status: 'success' }
  rescue
    render json: { status: 'failed',
                   message: "Could not create comment thread: #{(thread&.errors&.full_messages.to_a \
                                                      + comment&.errors&.full_messages.to_a \
                                                      + reaction&.errors&.full_messages.to_a).join(', ')}" }
  end
end

#createObject



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

def create
  ReactionType.create reaction_type_params
  PostType.mapping.each_key { |pt| Rails.cache.delete("post_type/#{pt}/reactions") }
  redirect_to reactions_path
end

#editObject



68
# File 'app/controllers/reactions_controller.rb', line 68

def edit; end

#indexObject



66
# File 'app/controllers/reactions_controller.rb', line 66

def index; end

#newObject



76
77
78
# File 'app/controllers/reactions_controller.rb', line 76

def new
  @reaction_type = ReactionType.new
end

#retractObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/reactions_controller.rb', line 52

def retract
  reaction_type = ReactionType.find(params[:reaction_id])

  reaction = Reaction.where(user: current_user, post: @post, reaction_type: reaction_type)
  unless reaction.any?
    render json: { status: 'failed', message: 'You do not have any reactions of this type on this post.' },
           status: :forbidden
    return
  end

  reaction.first.destroy
  render json: { status: 'success' }
end

#updateObject



70
71
72
73
74
# File 'app/controllers/reactions_controller.rb', line 70

def update
  @reaction_type.update reaction_type_params
  PostType.mapping.each_key { |pt| Rails.cache.delete("post_type/#{pt}/reactions") }
  redirect_to reactions_path
end