Fold acts_as_rateable plugin into lib

This commit is contained in:
Luke Barratt 2015-08-23 13:25:41 +01:00
parent 865a0cae5a
commit e3f0a79479
15 changed files with 78 additions and 78 deletions

View file

@ -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

View file

@ -1,2 +1,3 @@
require File.dirname(__FILE__) + '/lib/acts_as_rateable'
require 'acts_as_rateable'
ActiveRecord::Base.send(:include, ActiveRecord::Acts::Rateable)

View file

@ -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

View file

@ -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
attr_accessor :user_id
end

View file

@ -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

View file

@ -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