Class: Commentable::Controller

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/commentable/controller.rb

Instance Method Summary collapse

Instance Method Details

#build_comment(params = nil) ⇒ Object



71
72
73
74
75
# File 'lib/commentable/controller.rb', line 71

def build_comment(params = nil)
  comment = @commentable.comments.build(params)
  comment.user_ip = request.remote_ip if comment.respond_to?(:user_ip)
  comment
end

#comment_urlObject



63
64
65
# File 'lib/commentable/controller.rb', line 63

def comment_url
  polymorphic_url(@commentable, :anchor => "C#{@comment.to_param}")
end

#commentable_urlObject



67
68
69
# File 'lib/commentable/controller.rb', line 67

def commentable_url
  @commentable
end

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/commentable/controller.rb', line 24

def create
  @comment = build_comment(params[:comment])
  @comment.save
  
  respond_with(@commentable, @comment) do |format|
    format.html do
      if @comment.persisted?
        redirect_to comment_url
      else
        render 'new'
      end
    end
  end
end

#destroyObject



57
58
59
60
61
# File 'lib/commentable/controller.rb', line 57

def destroy
  @comment = @commentable.comments.find(params[:id])
  @comment.destroy
  respond_with(@commentable, @comment, :location => commentable_url)
end

#editObject



20
21
22
# File 'lib/commentable/controller.rb', line 20

def edit
  @comment = @commentable.comments.find(params[:id])
end

#indexObject



5
6
7
8
# File 'lib/commentable/controller.rb', line 5

def index
  @comments = @commentable.comments.all
  respond_with(@commentable, @comments)
end

#load_commentableObject



77
78
79
80
# File 'lib/commentable/controller.rb', line 77

def load_commentable
  id = params["#{params[:commentable]}_id"]
  @commentable = params[:commentable].camelize.constantize.find(id)
end

#newObject



15
16
17
18
# File 'lib/commentable/controller.rb', line 15

def new
  @comment = build_comment
  respond_with(@commentable, @comment)
end

#showObject



10
11
12
13
# File 'lib/commentable/controller.rb', line 10

def show
  @comment = @commentable.comments.find(params[:id])
  respond_with(@commentable, @comment)
end

#spamObject



45
46
47
48
49
# File 'lib/commentable/controller.rb', line 45

def spam
  @comment = @commentable.comments.find(params[:id])
  @comment.toggle!(:spam)
  respond_with(@commentable, @comment, :location => comment_url)
end

#trollObject



51
52
53
54
55
# File 'lib/commentable/controller.rb', line 51

def troll
  @comment = @commentable.comments.find(params[:id])
  @comment.toggle!(:troll)
  respond_with(@commentable, @comment, :location => comment_url)
end

#updateObject



39
40
41
42
43
# File 'lib/commentable/controller.rb', line 39

def update
  @comment = @commentable.comments.find(params[:id])
  @comment.update_attributes(params[:comment])
  respond_with(@commentable, @comment, :location => comment_url)
end