From b0b103a518ad03c36f145d43307a6059f3aa9b34 Mon Sep 17 00:00:00 2001 From: Ari Timonen Date: Sun, 22 Mar 2020 14:55:37 +0200 Subject: [PATCH] Remove acts_as_rateable --- app/models/movie.rb | 2 - .../acts_as_rateable_migration_generator.rb | 11 --- .../templates/migration.rb | 23 ------ .../acts_as_rateable/lib/acts_as_rateable.rb | 70 ------------------- .../acts_as_rateable_migration_generator.rb | 17 ----- .../templates/migration.rb | 23 ------ lib/plugins/acts_as_rateable/lib/rate.rb | 10 --- lib/plugins/acts_as_rateable/lib/rating.rb | 6 -- .../tasks/acts_as_rateable_tasks.rake | 4 -- .../test/acts_as_rateable_test.rb | 8 --- 10 files changed, 174 deletions(-) delete mode 100644 lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb delete mode 100644 lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/templates/migration.rb delete mode 100644 lib/plugins/acts_as_rateable/lib/acts_as_rateable.rb delete mode 100644 lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb delete mode 100644 lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/templates/migration.rb delete mode 100644 lib/plugins/acts_as_rateable/lib/rate.rb delete mode 100644 lib/plugins/acts_as_rateable/lib/rating.rb delete mode 100644 lib/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake delete mode 100644 lib/plugins/acts_as_rateable/test/acts_as_rateable_test.rb diff --git a/app/models/movie.rb b/app/models/movie.rb index 566ff4d..2376b15 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -26,8 +26,6 @@ # index_movies_on_user_id (user_id) # -# Rails.root.join('lib/plugins/acts_as_rateable/init.rb') - class Movie < ActiveRecord::Base include Extra diff --git a/lib/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 deleted file mode 100644 index 85ab389..0000000 --- a/lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb +++ /dev/null @@ -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 diff --git a/lib/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 deleted file mode 100644 index 607e82c..0000000 --- a/lib/plugins/acts_as_rateable/generators/acts_as_rateable_migration/templates/migration.rb +++ /dev/null @@ -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 diff --git a/lib/plugins/acts_as_rateable/lib/acts_as_rateable.rb b/lib/plugins/acts_as_rateable/lib/acts_as_rateable.rb deleted file mode 100644 index a1a558d..0000000 --- a/lib/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 diff --git a/lib/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 deleted file mode 100644 index 8837f5d..0000000 --- a/lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/acts_as_rateable_migration_generator.rb +++ /dev/null @@ -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 diff --git a/lib/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 deleted file mode 100644 index 607e82c..0000000 --- a/lib/plugins/acts_as_rateable/lib/generators/acts_as_rateable_migration/templates/migration.rb +++ /dev/null @@ -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 diff --git a/lib/plugins/acts_as_rateable/lib/rate.rb b/lib/plugins/acts_as_rateable/lib/rate.rb deleted file mode 100644 index 473d9dc..0000000 --- a/lib/plugins/acts_as_rateable/lib/rate.rb +++ /dev/null @@ -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 diff --git a/lib/plugins/acts_as_rateable/lib/rating.rb b/lib/plugins/acts_as_rateable/lib/rating.rb deleted file mode 100644 index 1200040..0000000 --- a/lib/plugins/acts_as_rateable/lib/rating.rb +++ /dev/null @@ -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 diff --git a/lib/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake b/lib/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake deleted file mode 100644 index 38a5f47..0000000 --- a/lib/plugins/acts_as_rateable/tasks/acts_as_rateable_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :acts_as_ratable do -# # Task goes here -# end diff --git a/lib/plugins/acts_as_rateable/test/acts_as_rateable_test.rb b/lib/plugins/acts_as_rateable/test/acts_as_rateable_test.rb deleted file mode 100644 index bdcb7a2..0000000 --- a/lib/plugins/acts_as_rateable/test/acts_as_rateable_test.rb +++ /dev/null @@ -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