Class: ForemanDatacenter::CommentsController

Inherits:
ApplicationController show all
Includes:
ForemanDatacenter::Controller::Parameters::Comment
Defined in:
app/controllers/foreman_datacenter/comments_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#action_permission, #controller_permission, #find_resource, #not_found, #resource_class, #resource_class_for, #resource_finder, #resource_name, #resource_scope, #scope_for

Instance Method Details

#createObject



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

def create
  @comment = @commentable.comments.new(comment_params.merge(user_id: current_user.id))
  if @comment.save
    process_success success_redirect: commentable_path(@comment),
                    success_msg: "Comment successfully created."
  else
    process_error redirect: "/datacenter/#{@resource}/#{@id}", :error_msg => _("Failed: %s") % (e)
  end
end

#destroyObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/foreman_datacenter/comments_controller.rb', line 46

def destroy
  @comment = ForemanDatacenter::Comment.find(params[:id])
  if @comment.user == current_user or @comment.user.nil? or current_user.admin?
    if @comment.destroy
      process_success :success_redirect => commentable_path(@comment, true), success_msg: "Comment successfully deleted."
    else
      process_error :redirect => commentable_path(@comment, true), :error_msg => _("Failed: %s") % (e)
    end
  else
      process_error :redirect => commentable_path(@comment, true), :error_msg => _("You can delete only your own comments")
  end
end

#editObject



15
16
17
18
19
20
# File 'app/controllers/foreman_datacenter/comments_controller.rb', line 15

def edit
  @comment = ForemanDatacenter::Comment.find(params[:id])
  @parent = @comment.parent
  @resource_class = @comment.commentable_type.split("::")[1]
  @current_user = current_user
end

#newObject



7
8
9
10
11
12
13
# File 'app/controllers/foreman_datacenter/comments_controller.rb', line 7

def new
  @comment = ForemanDatacenter::Comment.new
  @parent = ForemanDatacenter::Comment.find(params[:parent_id])
  @resource_class = @parent.commentable_type.gsub("ForemanDatacenter::","")
  @resource = @resource_class.pluralize.split(/(?<!\s)(?=[A-Z])/).join('_').downcase
  @commentable = load_resource(@resource, @parent.commentable_id)
end

#updateObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/foreman_datacenter/comments_controller.rb', line 32

def update
  @comment = ForemanDatacenter::Comment.find(params[:id])
  @resource = @comment.commentable_type.split("::")[1]
  if @comment.user == current_user or @comment.user.nil? or current_user.admin?
    if @comment.update(comment_params.merge(user_id: current_user.id))
      process_success :success_redirect => commentable_path(@comment), success_msg: "Comment successfully updated."
    else
      process_error :redirect => commentable_path(@comment), :error_msg => _("Failed: %s") % (e)
    end
  else
      process_error :redirect => commentable_path(@comment), :error_msg => _("You can edit only your own comments")
  end
end