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

View File

@ -1,19 +1,19 @@
class ArtistController < ApplicationController
def data
# Bad params
if params[:name].nil? or params[:name].empty?
return render json: { status: 'loading_failed' }
if params[:artist].nil? or params[:artist].empty?
return render json: { status: 'loading_failed' }, status: 404
end
# Searching for artist
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
unless @artist
results = MusicBrainz::Artist.search(artist_name)
if results.empty?
return render json: { status: 'not_found' }
return render json: { status: 'not_found' }, status: 404
end
best_match = results[0][:sort_name]
if best_match != artist_name and similar_names(artist_name, best_match)
@ -43,10 +43,6 @@ class ArtistController < ApplicationController
end
private
def get_artist_name_from_query
params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
end
def queue_loading artist_name, mbid
@artist = Artist.create( name: artist_name, mbid: mbid, status: 0 )

View File

@ -5,7 +5,7 @@ class PlaylistController < ApplicationController
end
def lastfm_top50
artist = Artist.find_by_id(params[:id])
artist = Artist.find_by_name(get_artist_name_from_query)
return if artist.nil?
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
{
name: name,
original_name: original_name,
url_name: name.gsub(" ", "+"),
pic_url: pic_url,
desc: desc,
albums: albums.map(&:serialize)
albums: albums.map(&:serialize),
artist_links: artist_links.map(&:serialize)
}
end

View File

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

View File

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