mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
rubocop fixing + helper methods
This commit is contained in:
+14
-8
@@ -22,21 +22,27 @@ class User < ApplicationRecord
|
||||
def favorite_style
|
||||
# create hash with key being style name and value first 0 and then the score
|
||||
styles = {}
|
||||
self.ratings.each do |rating|
|
||||
styles[rating.beer.style] = 0
|
||||
ratings.each do |rating|
|
||||
if styles[rating.beer.style].nil?
|
||||
styles[rating.beer.style] = rating.score
|
||||
else
|
||||
styles[rating.beer.style] += rating.score
|
||||
end
|
||||
end
|
||||
self.ratings.each do |rating|
|
||||
styles[rating.beer.style] += rating.score
|
||||
end
|
||||
|
||||
# determine which style hast the most points
|
||||
highest = {name: "", total: 0}
|
||||
style_with_most_points(styles)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def style_with_most_points(styles)
|
||||
highest = { name: "", total: 0 }
|
||||
styles.each do |style|
|
||||
if style[1] > highest[:total]
|
||||
highest[:name] = style[0]
|
||||
highest[:total] = style[1]
|
||||
end
|
||||
end
|
||||
return highest[:name]
|
||||
highest[:name]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -52,6 +52,8 @@ RSpec.describe User, type: :model do
|
||||
create_beer_with_many_ratings({user: user}, 20, 30, 20)
|
||||
beer_with_favorite_style = FactoryBot.create(:beer, style: "No")
|
||||
FactoryBot.create(:rating, beer: beer_with_favorite_style, score: 50, user: user)
|
||||
|
||||
expect(user.favorite_style).to eq "Yes"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user