-
Notifications
You must be signed in to change notification settings - Fork 4
/
ratings_controller.rb
99 lines (79 loc) · 4.07 KB
/
ratings_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class RatingsController < ApplicationController
before_action :authenticate_user!
def update
@quote = Quote.find(params[:id])
Rating.transaction do
# in case we edit already existing rating, we need to adjust categories preferences for current user
# we need to decrement it
if Rating.exists?(quote_id: @quote.id, user_id: current_user.id)
adjust_user_category_preference Rating.find_by(quote_id: @quote.id, user_id: current_user.id), true
adjust_user_quote_length_preference Rating.find_by(quote_id: @quote.id, user_id: current_user.id), true
adjust_user_word_length_preference Rating.find_by(quote_id: @quote.id, user_id: current_user.id), true
end
@rating = Rating.find_or_create_by!(quote_id: @quote.id, user_id: current_user.id)
respond_to do |format|
if @rating.update(user_rating: params[:user_rating])
adjust_user_category_preference
adjust_user_quote_length_preference
adjust_user_word_length_preference
format.html
format.json { render :show, status: :ok, location: root_path }
format.js
else
format.html { redirect_to root_path, notice: 'Rating was NOT successfully updated.' }
format.json { render json: @rating.errors, status: :unprocessable_entity }
end
head :no_content
end
end
end
private
def adjust_user_category_preference(rating = @rating, decrement = false)
@quote.categories.each do |category|
@user_category_preference = UserCategoryPreference.find_or_create_by!(category_id: category.id, user_id: current_user.id)
adjustment_value = rating.user_rating / Math.sqrt(@quote.categories.size)
if @user_category_preference.preference.present?
preference_value = decrement ? @user_category_preference.preference - adjustment_value : @user_category_preference.preference + adjustment_value
else
preference_value = adjustment_value
end
@user_category_preference.update(preference: preference_value)
end
end
def adjust_user_quote_length_preference(rating = @rating, decrement = false)
adjustments = {}
adjustments[@quote.length] = rating.user_rating
adjustments[@quote.length + 1] = 0.8 * rating.user_rating
adjustments[@quote.length - 1] = 0.8 * rating.user_rating
adjustments[@quote.length + 2] = 0.6 * rating.user_rating
adjustments[@quote.length - 2] = 0.6 * rating.user_rating
adjustments[@quote.length + 3] = 0.3 * rating.user_rating
adjustments[@quote.length - 3] = 0.3 * rating.user_rating
adjustments.each do |length, adjustment_value|
@user_quote_length_preference = UserQuoteLengthPreference.find_or_create_by!(length: length, user_id: current_user.id)
if @user_quote_length_preference.preference.present?
preference_value = decrement ? @user_quote_length_preference.preference - adjustment_value : @user_quote_length_preference.preference + adjustment_value
else
preference_value = adjustment_value
end
@user_quote_length_preference.update(preference: preference_value)
end
end
def adjust_user_word_length_preference(rating = @rating, decrement = false)
adjustments = {}
adjustments[@quote.word_avg_length] = rating.user_rating
adjustments[@quote.word_avg_length + 1] = 0.8 * rating.user_rating
adjustments[@quote.word_avg_length - 1] = 0.8 * rating.user_rating
adjustments[@quote.word_avg_length + 2] = 0.4 * rating.user_rating
adjustments[@quote.word_avg_length - 2] = 0.4 * rating.user_rating
adjustments.each do |length, adjustment_value|
@user_word_length_preference = UserWordLengthPreference.find_or_create_by!(length: length, user_id: current_user.id)
if @user_word_length_preference.preference.present?
preference_value = decrement ? @user_word_length_preference.preference - adjustment_value : @user_word_length_preference.preference + adjustment_value
else
preference_value = adjustment_value
end
@user_word_length_preference.update(preference: preference_value)
end
end
end