Created
March 31, 2016 14:56
-
-
Save rigelstpierre/2d2bd5763921f37480e84d8cf8466365 to your computer and use it in GitHub Desktop.
artist_presenter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArtistPresenter < BasePresenter | |
include DecoratorHelpers | |
def initialize(artist, options={}) | |
super(artist) | |
@current_user = options[:user] | |
@score = options[:score] | |
end | |
def to_hash | |
{ | |
artist: { | |
id: id, | |
name: name, | |
media_id: media_id, | |
verified: verified?, | |
tracker_count: tracker_count | |
}, | |
trackers: get_tracking_users, | |
similar_artists: get_similar_artists, | |
events: get_artist_events_hash, | |
venues: get_venues_hash | |
} | |
end | |
def artist_essentials | |
{ | |
id: id, | |
name: name, | |
media_id: media_id, | |
on_tour: on_tour, | |
tracker_count: tracker_count | |
} | |
end | |
def artist_essentials_with_score | |
unless @score | |
@score = Bit::Common::ArtistList.new(@current_user).score_for(id) | |
end | |
artist_essentials.merge!(score: @score) | |
end | |
def tracked_artists_essentials_and_source | |
artist_essentials.merge!(source: source) | |
end | |
def get_venues_hash | |
venues = artist_events.future_events.collect { |artist_event| artist_event.event.venue } | |
VenuePresenter.wrap(venues).map &:venue_essentials | |
end | |
def get_artist_events_hash | |
ArtistEventPresenter.wrap(artist_events.future_events).map &:artist_event_essentials | |
end | |
def get_similar_artists | |
Bit::Common::SimilarArtist.connection_proxy = SlaveDB | |
result = Bit::Common::SimilarArtist.recommendations_for_artists([self.id]).first(12) | |
artist_ids = result.map do |record| | |
artist = Artist.find(record[:similar_artist_id]) | |
ArtistPresenter.new(artist, score: record[:match_score]).artist_essentials_with_score | |
end | |
end | |
def get_tracking_users | |
cache_key = "artist_trackers:artist:#{id}" | |
users = Rails.cache.fetch(cache_key, expires_in: 7.days) do | |
User.joins(:artist_trackings) | |
.where("artist_trackings.artist_id = ?", id) | |
.has_facebook_uid_or_media_id | |
.order("artist_trackings.id DESC") | |
.limit(12) | |
end | |
UserPresenter.wrap(users).map &:user_essentials | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment