diff --git a/app/models/data_file.rb b/app/models/data_file.rb index 3786b92..2368f91 100644 --- a/app/models/data_file.rb +++ b/app/models/data_file.rb @@ -15,7 +15,6 @@ # article_id :integer # -require File.join(Rails.root, 'vendor', 'plugins', 'acts_as_rateable', 'init.rb') require 'digest/md5' class DataFile < ActiveRecord::Base diff --git a/vendor/plugins/acts_as_rateable/MIT-LICENSE b/lib/plugins/acts_as_rateable/MIT-LICENSE similarity index 100% rename from vendor/plugins/acts_as_rateable/MIT-LICENSE rename to lib/plugins/acts_as_rateable/MIT-LICENSE diff --git a/vendor/plugins/acts_as_rateable/README b/lib/plugins/acts_as_rateable/README similarity index 100% rename from vendor/plugins/acts_as_rateable/README rename to lib/plugins/acts_as_rateable/README diff --git a/vendor/plugins/acts_as_rateable/Rakefile b/lib/plugins/acts_as_rateable/Rakefile similarity index 100% rename from vendor/plugins/acts_as_rateable/Rakefile rename to lib/plugins/acts_as_rateable/Rakefile diff --git a/vendor/plugins/acts_as_rateable/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb b/lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb similarity index 100% rename from vendor/plugins/acts_as_rateable/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb rename to lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb diff --git a/vendor/plugins/acts_as_rateable/generators/acts_as_rateable_migration/templates/migration.rb b/lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/templates/migration.rb similarity index 100% rename from vendor/plugins/acts_as_rateable/generators/acts_as_rateable_migration/templates/migration.rb rename to lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/templates/migration.rb diff --git a/vendor/plugins/acts_as_rateable/init.rb b/lib/plugins/acts_as_rateable/init.rb similarity index 52% rename from vendor/plugins/acts_as_rateable/init.rb rename to lib/plugins/acts_as_rateable/init.rb index a8f173a..fad37bf 100644 --- a/vendor/plugins/acts_as_rateable/init.rb +++ b/lib/plugins/acts_as_rateable/init.rb @@ -1,2 +1,3 @@ -require File.dirname(__FILE__) + '/lib/acts_as_rateable' +require 'acts_as_rateable' + ActiveRecord::Base.send(:include, ActiveRecord::Acts::Rateable) diff --git a/lib/plugins/acts_as_rateable/lib/acts_as_rateable.rb b/lib/plugins/acts_as_rateable/lib/acts_as_rateable.rb new file mode 100644 index 0000000..645e682 --- /dev/null +++ b/lib/plugins/acts_as_rateable/lib/acts_as_rateable.rb @@ -0,0 +1,70 @@ +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 + # Find all objects rated by score. + 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 diff --git a/vendor/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb b/lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb similarity index 100% rename from vendor/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb rename to lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb diff --git a/vendor/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/templates/migration.rb b/lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/templates/migration.rb similarity index 100% rename from vendor/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/templates/migration.rb rename to lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/templates/migration.rb diff --git a/vendor/plugins/acts_as_rateable/lib/rate.rb b/lib/plugins/acts_as_rateable/lib/rate.rb similarity index 85% rename from vendor/plugins/acts_as_rateable/lib/rate.rb rename to lib/plugins/acts_as_rateable/lib/rate.rb index 7bd587c..473d9dc 100644 --- a/vendor/plugins/acts_as_rateable/lib/rate.rb +++ b/lib/plugins/acts_as_rateable/lib/rate.rb @@ -1,10 +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 \ No newline at end of file + + attr_accessor :user_id + +end diff --git a/vendor/plugins/acts_as_rateable/lib/rating.rb b/lib/plugins/acts_as_rateable/lib/rating.rb similarity index 98% rename from vendor/plugins/acts_as_rateable/lib/rating.rb rename to lib/plugins/acts_as_rateable/lib/rating.rb index d138ec4..1200040 100644 --- a/vendor/plugins/acts_as_rateable/lib/rating.rb +++ b/lib/plugins/acts_as_rateable/lib/rating.rb @@ -1,6 +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 diff --git a/vendor/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake b/lib/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake similarity index 100% rename from vendor/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake rename to lib/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake diff --git a/vendor/plugins/acts_as_rateable/test/acts_as_rateable_test.rb b/lib/plugins/acts_as_rateable/test/acts_as_rateable_test.rb similarity index 100% rename from vendor/plugins/acts_as_rateable/test/acts_as_rateable_test.rb rename to lib/plugins/acts_as_rateable/test/acts_as_rateable_test.rb diff --git a/vendor/plugins/acts_as_rateable/lib/acts_as_rateable.rb b/vendor/plugins/acts_as_rateable/lib/acts_as_rateable.rb deleted file mode 100644 index 5f62363..0000000 --- a/vendor/plugins/acts_as_rateable/lib/acts_as_rateable.rb +++ /dev/null @@ -1,70 +0,0 @@ -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 - # Find all objects rated by score. - 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