| Class | CommentsController |
| In: |
app/controllers/comments_controller.rb
|
| Parent: | ApplicationController |
Crear un comentario
# File app/controllers/comments_controller.rb, line 55
55: def create
56: @comment = @parent.comments.new(params[:comment])
57: @comment.request = request
58: if logged_in?
59: @comment.user_id = current_user.id
60: end
61:
62: tracking_url = "/#{I18n.locale}/commented"
63: respond_to do |format|
64: if @parent.has_comments? && !@parent.comments_closed? && @comment.save
65: format.html {
66: if @comment.spam?
67: msg = t('comments.sorry_marked_as_spam')
68: elsif @comment.approved?
69: msg = t('comments.comentario_guardado')
70: else
71: msg = t('comments.comment_pending')
72: end
73: flash[:notice] = msg
74: flash[:tracking] = tracking_url
75: redirect_to(@parent)
76: }
77: format.js {
78: if @comment.approved?
79: render :update do |page|
80: page[:comment_body].value = ""
81: page.insert_html :bottom, "doc_comments", :partial => "comment", :object => @comment
82: page.visual_effect :highlight, "comment_#{@comment.id}"
83: # page.replace_html :comment_row, :partial => "new_comment_link"
84: page.insert_html :before, "comment_#{@comment.id}", "<div class='flash_notice'>#{t('comments.comentario_guardado')}</div>"
85: page << "_gaq.push(['_trackPageview', '#{tracking_url}']);"
86: end
87: else
88: if @comment.spam?
89: msg = t('comments.sorry_marked_as_spam')
90: else
91: msg = t('comments.comment_pending')
92: end
93: render :update do |page|
94: page.replace_html :comment_row, :partial => "new_comment_link"
95: page.insert_html :bottom, "doc_comments",
96: "<div class='flash_notice'>#{msg}</div>"
97: page << "_gaq.push(['_trackPageview', '#{tracking_url}']);"
98: end
99: end
100: }
101: else
102: format.html { render :action => "new" }
103: format.js {
104: render :update do |page|
105: page.replace_html :comment_error, "<div class='flash_error'>
106: #{t('comments.no_publicado')}<br/>
107: </div>
108: #{error_messages_for(:comment)}"
109: end
110: }
111: end
112: end
113: end
# File app/controllers/comments_controller.rb, line 24
24: def department
25: @department = Department.find(params[:id])
26: @feed_title = t('comments.feed_title', :name => @department.name)
27: organization_ids = [@department.id] + @department.organization_ids
28: # Se supone que si una noticia tiene comentarios es porque esta ya publicada y se muestra en irekia.
29: # No sabemos si esta traducida, pero creo que es mejor mostrarlo de todas formas.
30: # Por lo tanto, no ponemos condiciones en ninguno de estos campos.
31: @comments = Comment.approved.find(:all,
32: :joins => "INNER JOIN documents ON (comments.commentable_id=documents.id AND commentable_type='Document')",
33: :conditions => "organization_id in (#{organization_ids.join(',')})",
34: :order => "created_at DESC", :limit => 20)
35: render :action => "index.rss", :layout => false
36: end
Eliminar un comentario
# File app/controllers/comments_controller.rb, line 116
116: def destroy
117: @comment = @parent.comments.find(params[:id])
118: @comment.destroy
119:
120: respond_to do |format|
121: format.html { redirect_to(comments_url) }
122: format.xml { head :ok }
123: end
124: end
Listado de todos los comentarios
# File app/controllers/comments_controller.rb, line 8
8: def index
9: if params[:document_id]
10: news = News.find(params[:document_id])
11: @comments = news.comments.approved.find(:all, :order => "created_at DESC", :limit => 20)
12: @feed_title = t('comments.feed_title', :name => news.title)
13: else
14: @comments = Comment.approved.find(:all, :order => "created_at DESC", :limit => 20)
15: @feed_title = t('comments.feed_title', :name => Notifier::SERVICENAME)
16: end
17: respond_to :rss
18: # respond_to do |format|
19: # #format.html
20: # format.rss { render :layout=>false}
21: # end
22: end
Añadir un comentario a un contenido de la web
# File app/controllers/comments_controller.rb, line 39
39: def new
40: @comment = @parent.comments.new
41:
42: unless @parent.comments_closed?
43: respond_to do |format|
44: format.html # new.html.erb
45: format.js {
46: render :update do |page|
47: page.replace_html :comment_row, :partial => 'new'
48: end
49: }
50: end
51: end
52: end
Marca un comentario como inadecuado. Actualmente esta funcionalidad está desactivada. Para activarla de nuevo hay que descomentar un partial en app/views/comments/_vote.html.erb e incluir la ruta "report_abuse" en config/routes.rb
# File app/controllers/comments_controller.rb, line 160
160: def report_abuse
161: @comment = Comment.find(params[:id])
162:
163: @comment.abuse_counter += 1
164: if @comment.save
165: respond_to do |format|
166: format.html {
167: flash[:notice] = t('comments.abuse_thank_you2')
168: redirect_to @comment.parent
169: }
170: format.js {
171: render :update do |page|
172: page.replace_html "abuse_#{@comment.id}", "<span>#{t('comments.abuse_thank_you')}</span>"
173: end
174: }
175: end
176: else
177: respond_to do |format|
178: format.html {
179: flash[:error] = t('comments.abuse_unsaved')
180: redirect_to @comment.parent
181: }
182: format.js {
183: logger.info "El aviso no se ha guardado: #{@comment.errors.inspect}"
184: render :nothing => true
185: }
186: end
187: end
188: end
Votar un comentario
# File app/controllers/comments_controller.rb, line 127
127: def vote
128: @comment = Comment.find(params[:id])
129:
130: @comment.ratings.build(:rating => params[:vote].to_i)
131: if @comment.save
132: respond_to do |format|
133: format.html {
134: flash[:notice] = t('comments.vote_thank_you')
135: redirect_to @comment.commentable
136: }
137: format.js {
138: render :update do |page|
139: page.replace "comment_rating_#{@comment.id}", :partial => "/comments/voted", :locals => {:comment => @comment, :vote => params[:vote].to_i}
140: end
141: }
142: end
143: else
144: respond_to do |format|
145: format.html {
146: flash[:error] = t('comments.vote_unsaved')
147: redirect_to @comment.parent
148: }
149: format.js {
150: logger.info "El voto no se ha guardado: #{@comment.errors.inspect}"
151: render :nothing => true
152: }
153: end
154: end
155: end