Class: Play::User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/play/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.authenticate(auth) ⇒ Object

Authenticates a user. This will either select the existing user account, or if it doesn’t exist yet, create it on the system.

auth - the Hash representation returned by OmniAuth after
       authenticating

Returns the User account.



49
50
51
52
53
54
55
56
57
# File 'lib/play/user.rb', line 49

def self.authenticate(auth)
  if user = User.where(:login => auth['nickname']).first
    user
  else
    user = User.create(:login => auth['nickname'],
                       :name => auth['name'],
                       :email => auth['email'])
 end
end

Instance Method Details

#favorite_artistsObject

Queries the database for a user’s favorite artists. It’s culled just from the historical votes of that user.

Returns an Array of five Artist objects.



25
26
27
28
29
30
31
32
# File 'lib/play/user.rb', line 25

def favorite_artists
  Artist.includes(:votes).
    where("votes.user_id = ?",id).
    group("votes.artist_id").
    order("count(votes.artist_id) desc").
    limit(5).
    all
end

#gravatar_idObject

The MD5 hash of the user’s email account. Used for showing their Gravatar.

Returns the String MD5 hash.



38
39
40
# File 'lib/play/user.rb', line 38

def gravatar_id
  Digest::MD5.hexdigest(email)
end

#vote_for(song) ⇒ Object

Let the user vote for a particular song.

song - the Song that the user wants to vote up

Returns the Vote object.



10
11
12
# File 'lib/play/user.rb', line 10

def vote_for(song)
  votes.create(:song => song, :artist => song.artist)
end

#votes_countObject

The count of the votes for this user. Used for Mustache purposes.

Returns the Integer number of votes.



17
18
19
# File 'lib/play/user.rb', line 17

def votes_count
  votes.count
end