mirror of
https://github.com/ENSL/ensl.org.git
synced 2025-01-14 22:01:28 +00:00
Remove acts_as_rateable
This commit is contained in:
parent
25ee885052
commit
b0b103a518
10 changed files with 0 additions and 174 deletions
|
@ -26,8 +26,6 @@
|
||||||
# index_movies_on_user_id (user_id)
|
# index_movies_on_user_id (user_id)
|
||||||
#
|
#
|
||||||
|
|
||||||
# Rails.root.join('lib/plugins/acts_as_rateable/init.rb')
|
|
||||||
|
|
||||||
class Movie < ActiveRecord::Base
|
class Movie < ActiveRecord::Base
|
||||||
include Extra
|
include Extra
|
||||||
|
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
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
|
|
|
@ -1,23 +0,0 @@
|
||||||
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
|
|
|
@ -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
|
|
|
@ -1,17 +0,0 @@
|
||||||
require 'rails/generators/migration'
|
|
||||||
|
|
||||||
class ActsAsRateableMigrationGenerator < Rails::Generators::Base
|
|
||||||
include Rails::Generators::Migration
|
|
||||||
|
|
||||||
def self.source_root
|
|
||||||
@_acts_as_commentable_source_root ||= File.expand_path("../templates", __FILE__)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.next_migration_number(path)
|
|
||||||
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
||||||
end
|
|
||||||
|
|
||||||
def copy_migration_file
|
|
||||||
migration_template 'migration.rb', 'db/migrate/acts_as_rateable_migration'
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,23 +0,0 @@
|
||||||
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
|
|
|
@ -1,10 +0,0 @@
|
||||||
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
|
|
|
@ -1,6 +0,0 @@
|
||||||
class Rating < ActiveRecord::Base
|
|
||||||
belongs_to :rate
|
|
||||||
belongs_to :rateable, :polymorphic => true
|
|
||||||
|
|
||||||
validates_uniqueness_of :user_id, :scope => [:rateable_id, :rateable_type]
|
|
||||||
end
|
|
|
@ -1,4 +0,0 @@
|
||||||
# desc "Explaining what the task does"
|
|
||||||
# task :acts_as_ratable do
|
|
||||||
# # Task goes here
|
|
||||||
# end
|
|
|
@ -1,8 +0,0 @@
|
||||||
require 'test/unit'
|
|
||||||
|
|
||||||
class ActsAsRateableTest < Test::Unit::TestCase
|
|
||||||
# Replace this with your real tests.
|
|
||||||
def test_this_plugin
|
|
||||||
flunk
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in a new issue