Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.DS_Store
/pkg/*

3 changes: 2 additions & 1 deletion lib/generators/ratyrate/templates/average_cache_migration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateAverageCaches < ActiveRecord::Migration
class CreateAverageCaches < ActiveRecord::Migration[5.2]

def self.up
create_table :average_caches do |t|
Expand All @@ -13,6 +13,7 @@ def self.up

def self.down
drop_table :average_caches
remove_index :rating_caches, [:cacheable_id, :cacheable_type]
end

end
Expand Down
4 changes: 3 additions & 1 deletion lib/generators/ratyrate/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateRates < ActiveRecord::Migration
class CreateRates < ActiveRecord::Migration[5.2]

def self.up
create_table :rates do |t|
Expand All @@ -15,6 +15,8 @@ def self.up

def self.down
drop_table :rates
remove_index :rates, :rater_id
remove_index :rates, [:rateable_id, :rateable_type]
end

end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateOverallAverages < ActiveRecord::Migration
class CreateOverallAverages < ActiveRecord::Migration[5.2]

def self.up
create_table :overall_averages do |t|
Expand Down
41 changes: 21 additions & 20 deletions lib/ratyrate/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,27 @@ def update_current_rate(stars, user, dimension)
end

def overall_avg(user)
# avg = OverallAverage.where(rateable_id: self.id)
# #FIXME: Fix the bug when the movie has no ratings
# unless avg.empty?
# return avg.take.avg unless avg.take.avg == 0
# else # calculate average, and save it
# dimensions_count = overall_score = 0
# user.ratings_given.select('DISTINCT dimension').each do |d|
# dimensions_count = dimensions_count + 1
# unless average(d.dimension).nil?
# overall_score = overall_score + average(d.dimension).avg
# end
# end
# overall_avg = (overall_score / dimensions_count).to_f.round(1)
# AverageCache.create! do |a|
# a.rater_id = user.id
# a.rateable_id = self.id
# a.avg = overall_avg
# end
# overall_avg
# end
avg = OverallAverage.where(rateable_id: self.id)
#FIXME: Fix the bug when the movie has no ratings
unless avg.empty?
return avg.take.avg unless avg.take.avg == 0
else # calculate average, and save it
dimensions_count = overall_score = 0
user.ratings_given.select('DISTINCT dimension').each do |d|
dimensions_count = dimensions_count + 1
unless average(d.dimension).nil?
overall_score = overall_score + average(d.dimension).avg
end
end
overall_avg = (overall_score / dimensions_count).to_f.round(1)
AverageCache.create! do |a|
a.rater_id = user.id
a.rateable_id = self.id
a.rateable_type = self.class.to_s
a.avg = overall_avg
end
overall_avg
end
end

# calculates the movie overall average rating for all users
Expand Down