1
0
Fork 0

Render artist page with serialized data, artist playlist by name

This commit is contained in:
magnolia-fan 2011-11-27 17:01:46 +04:00
parent 9ec28708c5
commit 9032e8380b
7 changed files with 47 additions and 42 deletions

View File

@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base
def compile_page params def compile_page params
@data = params[:data] unless params[:data].nil? @data = params[:data] unless params[:data].nil?
@status = params[:status] unless params[:status].nil?
render json: { render json: {
renderer: "unified", renderer: "unified",
data: @data, data: @data,
@ -21,6 +22,10 @@ class ApplicationController < ActionController::Base
}, include: (params[:include] unless params[:include].nil?) }, include: (params[:include] unless params[:include].nil?)
end end
def get_artist_name_from_query
params[:artist].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
end
protected protected
def authorize def authorize

View File

@ -1,19 +1,19 @@
class ArtistController < ApplicationController class ArtistController < ApplicationController
def data def data
# Bad params # Bad params
if params[:name].nil? or params[:name].empty? if params[:artist].nil? or params[:artist].empty?
return render json: { status: 'loading_failed' } return render json: { status: 'loading_failed' }, status: 404
end end
# Searching for artist # Searching for artist
artist_name = get_artist_name_from_query artist_name = get_artist_name_from_query
@artist = Artist.find_by_name(artist_name, include: { artist_links: {}, albums: { tracks: {}}}) @artist = Artist.find_by_name(artist_name)
# Artist not found in DB # Artist not found in DB
unless @artist unless @artist
results = MusicBrainz::Artist.search(artist_name) results = MusicBrainz::Artist.search(artist_name)
if results.empty? if results.empty?
return render json: { status: 'not_found' } return render json: { status: 'not_found' }, status: 404
end end
best_match = results[0][:sort_name] best_match = results[0][:sort_name]
if best_match != artist_name and similar_names(artist_name, best_match) if best_match != artist_name and similar_names(artist_name, best_match)
@ -44,10 +44,6 @@ class ArtistController < ApplicationController
private private
def get_artist_name_from_query
params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
end
def queue_loading artist_name, mbid def queue_loading artist_name, mbid
@artist = Artist.create( name: artist_name, mbid: mbid, status: 0 ) @artist = Artist.create( name: artist_name, mbid: mbid, status: 0 )
Delayed::Job.enqueue(LoadArtistJob.new(artist_name)) Delayed::Job.enqueue(LoadArtistJob.new(artist_name))

View File

@ -5,7 +5,7 @@ class PlaylistController < ApplicationController
end end
def lastfm_top50 def lastfm_top50
artist = Artist.find_by_id(params[:id]) artist = Artist.find_by_name(get_artist_name_from_query)
return if artist.nil? return if artist.nil?
playlist = Playlist.new(name: "#{artist.name}: Last.fm TOP 50", artist: artist, pic_url: artist.pic_url) playlist = Playlist.new(name: "#{artist.name}: Last.fm TOP 50", artist: artist, pic_url: artist.pic_url)

View File

@ -23,9 +23,12 @@ class Artist < ActiveRecord::Base
def serialize def serialize
{ {
name: name, name: name,
original_name: original_name,
url_name: name.gsub(" ", "+"),
pic_url: pic_url, pic_url: pic_url,
desc: desc, desc: desc,
albums: albums.map(&:serialize) albums: albums.map(&:serialize),
artist_links: artist_links.map(&:serialize)
} }
end end

View File

@ -1,3 +1,10 @@
class ArtistLink < ActiveRecord::Base class ArtistLink < ActiveRecord::Base
belongs_to :artist belongs_to :artist
def serialize
{
service: service,
url: url
}
end
end end

View File

@ -1,22 +1,22 @@
- if @artist.status == 0 - if @status == "loading"
.alert-message.warning .alert-message.warning
%p= t('search.loading') %p= t('search.loading')
.row.artist-info .row.artist-info
.span4.columns.pic .span4.columns.pic
= image_tag @artist.pic_url unless @artist.pic_url.nil? = image_tag @data[:pic_url] unless @data[:pic_url].nil?
.span7.columns.desc .span7.columns.desc
%h2 %h2
=@artist.name =@data[:name]
%small= " "+@artist.original_name.to_s %small= " "+@data[:original_name].to_s
%p= @artist.desc.html_safe unless @artist.desc.nil? %p= @data[:desc].html_safe unless @data[:desc].nil?
- unless @artist.artist_links.empty? - unless @data[:artist_links].empty?
.service-icons .service-icons
%a.foreign-link{ href: "http://last.fm/artist/#{@artist.name.gsub(" ", "+")}", target: '_blank' } %a.foreign-link{ href: "http://last.fm/artist/#{@data[:url_name]}", target: '_blank' }
= image_tag 'services/lastfm.ico' = image_tag 'services/lastfm.ico'
- @artist.artist_links.each do |service| - @data[:artist_links].each do |link|
- if ['wikipedia', 'microblog', 'official_homepage', 'social_network', 'youtube'].include?(service.service) - if ['wikipedia', 'microblog', 'official_homepage', 'social_network', 'youtube'].include?(link[:service])
%a.foreign-link{ href: service.url, target: '_blank' } %a.foreign-link{ href: link[:url], target: '_blank' }
= image_tag 'services/'+service.service+(service.service == 'official_homepage' ? '.png' : '.ico') = image_tag 'services/'+link[:service]+(link[:service] == 'official_homepage' ? '.png' : '.ico')
.row .row
.span7.offset4 .span7.offset4
@ -24,29 +24,23 @@
%table.zebra-striped %table.zebra-striped
%tr %tr
%td %td
%span.label.success.set-playlist{ href: "", 'data-playlist' => "lastfm-top50/#{@artist.id}" }= t('player.set_playlist') %span.label.success.set-playlist{ href: "", 'data-playlist' => "lastfm-top50/#{@data[:url_name]}" }= t('player.set_playlist')
%a.page-link.playlist-name{ href: "/playlist/lastfm-top50/#{@artist.id}" }= "#{@artist.name}: Last.fm TOP" %a.page-link.playlist-name{ href: "/playlist/lastfm-top50/#{@data[:url_name]}" }= "#{@data[:name]}: Last.fm TOP"
- @artist.playlists.each do |playlist|
%tr
- @artist.playlists.each do |playlist|
%td
%span.label.success.set-playlist{ href: "", 'data-playlist' => playlist.id }= t('player.set_playlist')
%a.playlist-name{ href: "", 'data-playlist-id' => playlist.id }= playlist.name
- @artist.albums.each do |album| - @data[:albums].each do |album|
.row.album .row.album
.span4.columns.art .span4.columns.art
%img{ src: album.pic_url } %img{ src: album[:pic_url] }
.button-container .button-container
%a.btn.add-album{ 'data-tracks' => album.tracks.map(&:id).join(",") }= t('player.add') %a.btn.add-album{ 'data-tracks' => album[:tracks].map{|_|_[:id]}.join(",") }= t('player.add')
.span7.columns.tracks .span7.columns.tracks
%h3{ 'data-album-id' => album.id } %h3
= album.name = album[:name]
%small= " ("+album.year.to_s+")" %small= " ("+album[:year].to_s+")"
%table.zebra-striped.tracklist %table.zebra-striped.tracklist
- album.tracks.each do |track| - album[:tracks].each do |track|
%tr{ class: (track.available == false ? "unavailable" : nil) } %tr{ class: (track[:available] == false ? "unavailable" : nil) }
%td.song-title= track.name %td.song-title= track[:name]
%td.song-duration %td.song-duration
.s-duration= (track.duration != '0:00' ? track.duration : '&nbsp;'.html_safe) .s-duration= (track[:duration] != '0:00' ? track[:duration] : '&nbsp;'.html_safe)
%span.label.success.s-add{ 'data-album-id' => album.id, 'data-id' => track.id }= t('player.add_one') %span.label.success.s-add{ 'data-id' => track[:id] }= t('player.add_one')

View File

@ -16,10 +16,10 @@ Beathaven::Application.routes.draw do
match 'track/report' => 'track#report_unavailable' match 'track/report' => 'track#report_unavailable'
match 'playlist/(:id)' => 'playlist#data' match 'playlist/(:id)' => 'playlist#data'
match 'playlist/lastfm-top50/(:id)' => 'playlist#lastfm_top50' match 'playlist/lastfm-top50/(:artist)' => 'playlist#lastfm_top50', constraints: { artist: /.*/ }
match 'settings' => 'user#settings' match 'settings' => 'user#settings'
match 'artist/autocomplete' => 'last_fm#autocomplete' match 'artist/autocomplete' => 'last_fm#autocomplete'
match 'artist/(:name)' => 'artist#data', constraints: { name: /.*/ } match 'artist/(:artist)' => 'artist#data', constraints: { artist: /.*/ }
end end