Skip to content
Browse files

Initial commit

  • Loading branch information...
1 parent 95e8c55 commit 2e3776e80a932a594392af8496b6d924abca2b79 @aprihodko committed Feb 6, 2009
Showing with 216 additions and 0 deletions.
  1. +1 −0 CHANGELOG
  2. +20 −0 MIT-LICENSE
  3. +59 −0 README
  4. +3 −0 init.rb
  5. +1 −0 install.rb
  6. +86 −0 lib/acts_as_reportable.rb
  7. +34 −0 lib/content_report.rb
  8. +4 −0 tasks/acts_as_reportable_tasks.rake
  9. +8 −0 test/acts_as_reportable_content_test.rb
View
1 CHANGELOG
@@ -0,0 +1 @@
+v1: Initial version
View
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Andrey Prihodko "Railslink"
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
59 README
@@ -0,0 +1,59 @@
+Acts As Reportable
+=================
+
+Allows for content_reports to be added to multiple and different models.
+
+== Resources
+
+Install
+ * Run the following command:
+
+ script/plugin install http://railslink.com/svn/acts_as_reportable
+
+ * Create a new rails migration and add the following self.up and self.down methods
+
+ def self.up
+ create_table "content_reports", :force => true do |t|
+ t.column "content_type", :int, :default => 0
+ t.column "comment", :string, :limit => 255, :default => ""
+ t.column "reportable_type", :string, :limit => 15, :default => "", :null => false
+ t.column "reportable_id", :integer, :default => 0, :null => false
+ t.column "user_id", :integer, :default => 0, :null => false
+ t.column "created_at", :datetime, :null => false
+ end
+
+ add_index "content_reports", ["user_id"], :name => "fk_content_reports_user"
+ end
+
+ def self.down
+ drop_table :content_reports
+ end
+
+== Usage
+
+ * Make you ActiveRecord model act as reportable.
+
+ class Model < ActiveRecord::Base
+ acts_as_reportable
+ end
+
+ * Add a content_report to a model instance
+
+ model = Model.new
+ content_report = ContentReport.new
+ content_report.type =
+ content_report.content = 'Some comment'
+ model.content_reports << content_report
+
+ * Each content_report reference reportable object
+
+ model = Model.find(1)
+ model.content_reports.get(0).reportable == model
+
+== Credits
+
+This plugin is very similar by Acts As Commentable.
+
+== More
+
+
View
3 init.rb
@@ -0,0 +1,3 @@
+# Include hook code here
+require 'acts_as_reportable'
+ActiveRecord::Base.send(:include, FanParty::Acts::Reportable)
View
1 install.rb
@@ -0,0 +1 @@
+# Install hook code here
View
86 lib/acts_as_reportable.rb
@@ -0,0 +1,86 @@
+# ActsAsReportable
+module FanParty
+ module Acts #:nodoc:
+ module Reportable #:nodoc:
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def acts_as_reportable
+ has_many :content_reports, :class_name => "ContentReport", :as => :reportable, :dependent => :destroy, :order => 'created_at ASC'
+ include FanParty::Acts::Reportable::InstanceMethods
+ extend FanParty::Acts::Reportable::SingletonMethods
+ end
+
+ def before_destroy
+ self.content_reports.each do |content|
+ ContentReport.delete(content.id)
+ end
+ end
+ end
+
+ # This module contains class methods
+ module SingletonMethods
+ # Helper method to lookup for content_reports for a given object.
+ # This method is equivalent to obj.content_reports.
+ def find_content_reports_for(obj)
+ reportable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
+
+ ContentReport.find(:all,
+ :conditions => ["reportable_id = ? and reportable_type = ?", obj.id, reportable],
+ :order => "created_at DESC"
+ )
+ end
+
+ # Helper class method to lookup content_reports for
+ # the mixin reportable type written by a given fan.
+ # This method is NOT equivalent to ContentReport.find_content_reports_for_fan
+ def find_content_reports_by_fan(fan)
+ reportable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
+
+ ContentReport.find(:all,
+ :conditions => ["fan_id = ? and reportable_type = ?", fan.id, reportable],
+ :order => "created_at DESC"
+ )
+ end
+ end
+
+ # This module contains instance methods
+ module InstanceMethods
+
+ def content_report(fan, type=0, comment=nil)
+ report = ContentReport.new
+ report.comment = comment
+ report.content_type = type
+ report.fan_id = fan
+ self.content_reports << report
+ end
+
+ # Helper method to sort content_reports by date
+ def content_reports_ordered_by_submitted
+ ContentReport.find(:all,
+ :conditions => ["reportable_id = ? and reportable_type = ?", id, self.class.name],
+ :order => "created_at DESC"
+ )
+ end
+
+ # Helper method that defaults the submitted time.
+ def add_content_report(content_report)
+ content_reports << content_report
+ end
+
+ def reported_by_fan?(fan)
+ rtn = false
+ if fan
+ self.content_reports.each { |v|
+ rtn = true if fan.id == v.fan_id
+ }
+ end
+ rtn
+ end
+ end
+ end
+ end
+end
View
34 lib/content_report.rb
@@ -0,0 +1,34 @@
+class ContentReport < ActiveRecord::Base
+ belongs_to :reportable, :polymorphic => true
+
+ # NOTE: Content reports belong to a fan
+ belongs_to :fan
+
+ def self.content_report_types
+ [["Spam","0"], ["Duplicate","1"], ["Inappropriate","2"], ["Offensive","3"], ["Pornography", "4"]]
+ end
+
+ # Helper class method to lookup all content reports assigned
+ # to all reportable types for a given fan.
+ def self.find_content_reports_by_fan(fan)
+ find(:all,
+ :conditions => ["fan_id = ?", fan.id],
+ :order => "created_at DESC"
+ )
+ end
+
+ # Helper class method to look up all content reports for
+ # reportable class name and reportable id.
+ def self.find_content_reports_for_reportable(reportable_str, reportable_id)
+ find(:all,
+ :conditions => ["reportable_type = ? and reportable_id = ?", reportable_str, reportable_id],
+ :order => "created_at DESC"
+ )
+ end
+
+ # Helper class method to look up a reportable object
+ # given the reportable class name and id
+ def self.find_reportable(reportable_str, reportable_id)
+ reportable_str.constantize.find(reportable_id)
+ end
+end
View
4 tasks/acts_as_reportable_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :acts_as_reportable do
+# # Task goes here
+# end
View
8 test/acts_as_reportable_content_test.rb
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class ActsAsCommentableTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end

0 comments on commit 2e3776e

Please sign in to comment.
Something went wrong with that request. Please try again.