Permalink
Browse files

Added some widgets

  • Loading branch information...
1 parent 199296c commit 77553b7836732766adce12006b7b8fe21e49c376 @html committed Jul 17, 2011
View
105 lib/comments.rb
@@ -120,14 +120,18 @@ def self.plugins
Plugins.constants.each do |const|
plugin = Plugins.const_get(const)
- if plugin.respond_to?(:superclass) && plugin.superclass == AbstractPlugin
+ if plugin_superclass_is_abstract_plugin?(plugin)
plugins[plugin.plugin_name] = plugin
end
end
plugins.merge(@@plugins)
end
+ def self.plugin_superclass_is_abstract_plugin?(plugin)
+ plugin.respond_to?(:ancestors) && plugin.ancestors.include?(AbstractPlugin)
+ end
+
def self.add_plugin(plugin)
@@plugins[plugin.plugin_name] = plugin
end
@@ -148,6 +152,10 @@ def each_plugin_collect(m)
end
class ViewManager
+ # Used just as static variables
+ cattr_accessor :current_comments_template
+ cattr_accessor :current_comment
+
@@view = nil
def self.set_view(view)
@@ -222,30 +230,69 @@ def widgets
end
end
- if false
- class Field < AbstractPlugin
- @@name = nil
+ class RemoveComment < AbstractPlugin
+ def self.initially_enabled
+ false
+ end
+
+ def self.plugin_name
+ 'remove_comment'
+ end
+
+
+ def widgets
+ { :remove_comment_link => (lambda do ViewManager::template('widgets/remove_comment_link', :comment => ViewManager.current_comment) end) }
+ end
+
+ def self.reserved_options
+ [:remove_comment_enabled]
+ end
+ end
+ class RemoveCommentAsSpam < AbstractPlugin
def self.initially_enabled
false
end
def self.plugin_name
- @@name
+ 'remove_comment_as_spam'
+ end
+
+ def self.reserved_options
+ [:remove_comment_as_spam_enabled]
+ end
+
+ def search_options
+ { :conditions => { :status => ['', 'possibly_spam', 'not_spam'] }}
end
def widgets
- { @@name => ViewManager::template("widgets/#{@@name}") }
+ { :remove_comment_as_spam_link => (lambda do ViewManager::template('widgets/remove_comment_as_spam_link', :comment => ViewManager.current_comment) end) }
end
end
- class ContentField < Field
- @@name = "content_field"
+ class Field < AbstractPlugin
+ def self.initially_enabled
+ false
+ end
+
+ def self.plugin_name
+ 'field'
+ end
+ end
+ class ContentField < Field
def self.initially_enabled
true
end
- end
+
+ def self.plugin_name
+ 'content_field'
+ end
+
+ def widgets
+ { :content_field => (lambda do ViewManager::template("widgets/content_field", :comment => ViewManager.current_comment) end) }
+ end
end
end
@@ -266,33 +313,59 @@ def #{key.to_s}(&block)
@comments_response.get_assigned_variable(#{key.inspect})
block.call
else
- @comments_response.get_assigned_variable(#{key.inspect})
+ var = @comments_response.get_assigned_variable(#{key.inspect})
+ if var.respond_to?(:call)
+ var.call
+ else
+ var
+ end
end
end
INSTANCE_EVAL
end
out = render :partial => response.template, :locals => { :items => actually_comments }
- response.check_for_not_rendered_widgets
+ unless actually_comments.empty?
+ response.check_for_not_rendered_widgets
+ end
out
end
- def count_comments_for(item)
- (CommentTopic::record_for(item) || CommentTopic::create_record_for(item)).comments.count
+ def count_comments_for(item, args = {})
+ Comments::Manager::list_comments_for(item, args).count
end
def display_comments(args = {})
- comments_for(@controller, args)
+ comments_template :comments do
+ comments_for(@controller, args)
+ end
end
def display_comment(comment)
- render :partial => @comment_partial, :locals => { :comment => comment }
+ ViewManager.current_comment = comment
+ comments_template :comment do
+ render :partial => @comment_partial, :locals => { :comment => comment }
+ end
end
def comment_add_form
- render :partial => @comment_add_form_partial
+ comments_template :add_comment do
+ render :partial => @comment_add_form_partial
+ end
+ end
+
+ def comments_template(name, &block)
+ old_template = ViewManager.current_comments_template
+ ViewManager.current_comments_template = name
+ return_value = yield
+ ViewManager.current_comments_template = old_template
+ return_value
+ end
+
+ def current_comments_template
+ ViewManager.current_comments_template
end
end
end
View
7 test/comments_test.rb
@@ -230,7 +230,7 @@ def self.reserved_options
Comments::ViewManager::set_view(ActionView::Base.new)
resp = Comments::Manager::comments_response('xxxx')
- assert_equal [:record_name], resp.assign_variables.keys
+ assert_equal [:record_name, :content_field], resp.assign_variables.keys
assert_raise RuntimeError do
resp.check_for_not_rendered_widgets
end
@@ -239,10 +239,11 @@ def self.reserved_options
should "not raise anything if all is ok" do
Comments::ViewManager::set_view(ActionView::Base.new)
resp = Comments::Manager::comments_response('xxxx')
- assert_equal [:record_name], resp.assign_variables.keys
+ assert_equal [:record_name, :content_field], resp.assign_variables.keys
resp.get_assigned_variable(:record_name)
+ resp.get_assigned_variable(:content_field)
- assert_equal [:record_name], resp.assign_variables.keys
+ assert_equal [:record_name, :content_field], resp.assign_variables.keys
assert_nothing_raised do
resp.check_for_not_rendered_widgets
end
View
26 test/plugins/content_field_test.rb
@@ -0,0 +1,26 @@
+require 'test_helper'
+
+class ContentFieldTest < ActiveSupport::TestCase
+ context "#widgets" do
+ should "render comment content in comment show view" do
+ @view = ActionView::Base.new
+ Comments::ViewManager::set_view(@view)
+ Comments::ViewManager.current_comment = Comments::Comment.create! :content => "<Test>"
+ resp = @view.render :partial => '/widgets/content_field', :locals => { :current_comments_template => :comment, :comment => Comments::ViewManager.current_comment }
+
+ assert_equal resp.strip, '&lt;Test&gt;'
+ end
+
+ should "render content textarea in comment add view" do
+ @view = ActionView::Base.new
+ Comments::ViewManager::set_view(@view)
+ Comments::ViewManager.current_comment = Comments::Comment.create! :content => "<Test>"
+ resp = @view.render :partial => '/widgets/content_field', :locals => { :current_comments_template => :add_comment, :comment => Comments::ViewManager.current_comment }
+
+ text_area = Hpricot(resp).at('textarea')
+ assert_not_nil text_area
+ assert_equal 'comment[content]', text_area['name']
+ end
+
+ end
+end
View
51 test/plugins/remove_comment_as_spam_test.rb
@@ -0,0 +1,51 @@
+require "test_helper"
+require "action_controller/test_process"
+
+class TestController < ActionController::Base
+ attr_accessor :delegate_attr
+ def delegate_method() end
+ def rescue_action(e) raise end
+end
+
+ActiveRecord::Schema.define(:version => 2) do
+ add_column :comments, :status, :string, :default => '', :null => false
+end
+
+class RemoveCommentTest < ActiveSupport::TestCase
+ context "#widgets" do
+ should "render one link if comment is clean" do
+ @view = ActionView::Base.new
+ @view.controller = TestController.new
+ @view.controller.request = ActionController::TestRequest.new
+ @view.controller.response = ActionController::TestResponse.new
+
+ Comments::ViewManager::set_view(@view)
+ Comments::ViewManager.current_comment = Comments::Comment.create! :content => "<Test>"
+
+ stub(@view).t('remove comment as spam') { 'remove comment as spam' }
+ stub(@view).link_to('remove comment as spam', :update_comment_status => Comments::ViewManager.current_comment.id, :status => 'spam')
+
+ resp = @view.render :partial => '/widgets/remove_comment_as_spam_link', :locals => { :current_comments_template => :comment, :comment => Comments::ViewManager.current_comment }
+ end
+
+ should "render two links if comment is possibly spam" do
+ @view = ActionView::Base.new
+ @view.controller = TestController.new
+ @view.controller.request = ActionController::TestRequest.new
+ @view.controller.response = ActionController::TestResponse.new
+
+ Comments::ViewManager::set_view(@view)
+ Comments::ViewManager.current_comment = Comments::Comment.create! :content => "<Test>", :status => 'possibly_spam'
+
+ # First link
+ stub(@view).t('remove comment as spam') { 'remove comment as spam' }
+ stub(@view).link_to('remove comment as spam', :update_comment_status => Comments::ViewManager.current_comment.id, :status => 'spam')
+
+ # Second link
+ stub(@view).t('mark comment as checked') { 'mark comment as checked' }
+ stub(@view).link_to('mark comment as checked', :update_comment_status => Comments::ViewManager.current_comment.id, :status => 'not_spam')
+
+ resp = @view.render :partial => '/widgets/remove_comment_as_spam_link', :locals => { :current_comments_template => :comment, :comment => Comments::ViewManager.current_comment }
+ end
+ end
+end
View
27 test/plugins/remove_comment_test.rb
@@ -0,0 +1,27 @@
+require "test_helper"
+require "action_controller/test_process"
+
+class TestController < ActionController::Base
+ attr_accessor :delegate_attr
+ def delegate_method() end
+ def rescue_action(e) raise end
+end
+
+class RemoveCommentTest < ActiveSupport::TestCase
+ context "#widgets" do
+ should "render link" do
+ @view = ActionView::Base.new
+ @view.controller = TestController.new
+ @view.controller.request = ActionController::TestRequest.new
+ @view.controller.response = ActionController::TestResponse.new
+
+ Comments::ViewManager::set_view(@view)
+ Comments::ViewManager.current_comment = Comments::Comment.create! :content => "<Test>"
+
+ stub(@view).t('remove comment') { 'remove comment' }
+ stub(@view).link_to('remove comment', :remove_comment => Comments::ViewManager.current_comment.id)
+
+ resp = @view.render :partial => '/widgets/remove_comment_link', :locals => { :current_comments_template => :comment, :comment => Comments::ViewManager.current_comment }
+ end
+ end
+end
View
2 views/_comment.haml
@@ -1,2 +1,2 @@
-= comment.content
+= content_field
%br
View
2 views/_comment_add_form.haml
@@ -3,6 +3,6 @@
- form_for :comment, :url => url_for do |f|
= error_messages_for :comment
= record_name
- = f.text_area :content, :rows => 5
+ = content_field
%br
= f.submit t(:post_comment)
View
5 views/widgets/_content_field.haml
@@ -0,0 +1,5 @@
+- case current_comments_template
+ - when :comment then
+ = h comment.content
+ - when :add_comment then
+ = text_area :comment, :content, :rows => 5
View
6 views/widgets/_remove_comment_as_spam_link.haml
@@ -0,0 +1,6 @@
+- is_possibly_spam_comment = comment.status == 'possibly_spam'
+%div{ :style => (is_possibly_spam_comment ? 'background-color:red' : '') }
+ = link_to t("remove comment as spam"), :update_comment_status => comment.id, :status => 'spam'
+ - if is_possibly_spam_comment
+ %br
+ = link_to t("mark comment as checked"), :update_comment_status => comment.id, :status => 'not_spam'
View
1 views/widgets/_remove_comment_link.haml
@@ -0,0 +1 @@
+= link_to t("remove comment"), :remove_comment => comment.id

0 comments on commit 77553b7

Please sign in to comment.