Class: Parrot::CommentsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#default_url_options

Instance Method Details

#commentable(id = nil) ⇒ Object



49
50
51
# File 'app/controllers/parrot/comments_controller.rb', line 49

def commentable(id = nil)
  commentable_type.classify.constantize.find(id || commentable_id)
end

#commentable_fkObject

Following methods should belong to ApplicationController



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

def commentable_fk
  commentable_fk = params.select{|k,v| k =~ /_id/ }.keys.first
end

#commentable_idObject



40
41
42
43
44
45
46
47
# File 'app/controllers/parrot/comments_controller.rb', line 40

def commentable_id
  id = params[commentable_fk]
  if id.to_i.to_s == id # Numeric id
    id.to_i
  else
    commentable(id).id # Slugged (text, but we store integers)
  end
end

#commentable_typeObject



36
37
38
# File 'app/controllers/parrot/comments_controller.rb', line 36

def commentable_type
  commentable_fk.gsub('_id', '')
end

#createObject



15
16
17
18
19
20
21
22
23
# File 'app/controllers/parrot/comments_controller.rb', line 15

def create
  @comment = Comment.new(params[:parrot_comment])
  @comment.commentable_type = commentable_type.classify
  @comment.commentable_id = commentable_id
  @comment.author_id = current_user.id
  @comment.save
  @commentable_type = commentable_type
  respond_with @comment, :location => commentable
end

#debug(object) ⇒ Object



53
54
55
# File 'app/controllers/parrot/comments_controller.rb', line 53

def debug(object)
  logger.warn "DEBUGGING: #{object.inspect}"
end

#destroyObject



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

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

#indexObject



5
6
7
8
# File 'app/controllers/parrot/comments_controller.rb', line 5

def index
  @comments = Comment.where(commentable_type: commentable_type.classify, commentable_id: commentable_id)
  respond_with @comments
end

#newObject



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

def new
  @comment = Comment.new
  respond_with @comment
end