Permalink
Browse files

* Added all plugin files.

  • Loading branch information...
1 parent 8f0307e commit 963620dead52e6bb1fdaf854511966409c1946ff @azabaj azabaj committed May 29, 2008
View
@@ -0,0 +1,20 @@
+Copyright (c) 2007 [name of plugin creator]
+
+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
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the acts_as_ratable plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the acts_as_ratable plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ActsAsRatable'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,11 @@
+class ActsAsRateableMigrationGenerator < Rails::Generator::Base
+ def manifest
+ record do |m|
+ m.migration_template 'migration.rb', 'db/migrate'
+ end
+ end
+
+ def file_name
+ "acts_as_rateable_migration"
+ end
+end
@@ -0,0 +1,23 @@
+class ActsAsRateableMigration < ActiveRecord::Migration
+ def self.up
+ create_table :rates do |t|
+ t.column :score, :integer
+ end
+
+ create_table :ratings do |t|
+ t.column :user_id, :integer
+ t.column :rate_id, :integer
+ t.column :rateable_id, :integer
+ t.column :rateable_type, :string, :limit => 32
+ t.timestamps
+ end
+
+ add_index :ratings, :rate_id
+ add_index :ratings, [:rateable_id, :rateable_type]
+ end
+
+ def self.down
+ drop_table :ratings
+ drop_table :rates
+ end
+end
View
@@ -0,0 +1,2 @@
+require File.dirname(__FILE__) + '/lib/acts_as_rateable'
+ActiveRecord::Base.send(:include, ActiveRecord::Acts::Rateable)
@@ -0,0 +1,69 @@
+module ActiveRecord
+ module Acts
+ module Rateable
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module AssignRateWithUserId
+ def <<( rate )
+ r = Rating.new
+ r.rate = rate
+ r.rateable = proxy_owner
+ r.user_id = rate.user_id
+ r.save
+ end
+ end
+
+ module ClassMethods
+ def acts_as_rateable(options = {})
+ has_many :ratings, :as => :rateable, :dependent => :destroy, :include => :rate
+ has_many :rates, :through => :ratings, :extend => AssignRateWithUserId
+
+ include ActiveRecord::Acts::Rateable::InstanceMethods
+ extend ActiveRecord::Acts::Rateable::SingletonMethods
+ end
+ end
+
+ module SingletonMethods
+ def find_average_of( score )
+ find(:all, :include => [:rates] ).collect {|i| i if i.average_rating.to_i == score }.compact
+ end
+ end
+
+ module InstanceMethods
+ # Rates the object by a given score. A user object can be passed to the method.
+ def rate_it( score, user_id )
+ return unless score
+ rate = Rate.find_or_create_by_score( score.to_i )
+ rate.user_id = user_id
+ rates << rate
+ end
+
+ # Calculates the average rating. Calculation based on the already given scores.
+ def average_rating
+ return 0 if rates.empty?
+ ( rates.inject(0){|total, rate| total += rate.score }.to_f / rates.size )
+ end
+
+ # Rounds the average rating value.
+ def average_rating_round
+ average_rating.round
+ end
+
+ # Returns the average rating in percent. The maximal score must be provided or the default value (5) will be used.
+ # TODO make maximum_rating automatically calculated.
+ def average_rating_percent( maximum_rating = 5 )
+ f = 100 / maximum_rating.to_f
+ average_rating * f
+ end
+
+ # Checks wheter a user rated the object or not.
+ def rated_by?( user )
+ ratings.detect {|r| r.user_id == user.id }
+ end
+ end
+
+ end
+ end
+end
View
@@ -0,0 +1,10 @@
+class Rate < ActiveRecord::Base
+ has_many :ratings
+
+ validates_presence_of :score
+ validates_uniqueness_of :score
+ validates_numericality_of :score, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 10
+
+ attr_accessor :user_id
+
+end
View
@@ -0,0 +1,6 @@
+class Rating < ActiveRecord::Base
+ belongs_to :rate
+ belongs_to :rateable, :polymorphic => true
+
+ validates_uniqueness_of :user_id, :scope => [:rateable_id, :rateable_type]
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :acts_as_ratable do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class ActsAsRateableTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end

0 comments on commit 963620d

Please sign in to comment.