class PlaylistController < ApplicationController def data @playlist = Playlist.find_by_id(params[:id]) render json: @playlist, include: { playlist_items: { include: { track: { include: { artists: {} }}}}} end def lastfm_top50 artist = Artist.find_by_name(get_artist_name_from_query) return if artist.nil? compiler = lambda do playlist = Playlist.new(name: "#{artist.name}: Last.fm TOP 50", artist: artist, pic_url: artist.pic_url) LastFM::Artist.get_top_tracks(artist: artist.name)["toptracks"]["track"].each do |track| tracks = Track.joins(:album, :artists).where(name: track["name"], "track_artists.artist_id" => artist.id) playlist.playlist_items << PlaylistItem.new(track_id: tracks.first.id) unless tracks.empty? end playlist.serialize end compile_page( data: compiler, partial: "playlist/tracks", title: "#{artist.name}: Last.fm TOP 50", callback: {object: :player, action: :updateLibrary}, cache_for: 7.days, cache_key: "lastfmtop50_#{artist.id}" ) end end