1
0
Fork 0

Mass artist controller refactoring

This commit is contained in:
Gregory Eremin 2011-10-20 15:01:57 +04:00
parent 62d7d378bd
commit 73ce491479
9 changed files with 83 additions and 131 deletions

View File

@ -53,9 +53,13 @@ class window.Player
autoplay = false autoplay = false
initial_count = $('.playlist li').length initial_count = $('.playlist li').length
for item in tracks for item in tracks
len = parseInt(item.length, 10)
m = Math.floor(len / 60)
s = len - Math.floor(len / 60) * 60
duration = m + ':' + (if s < 10 then '0' else '') + s
$('.playlist').append ' $('.playlist').append '
<li id="i' +Math.round(Math.random() * 999999)+ '" data-id="'+item.id+'"> <li id="i' +Math.round(Math.random() * 999999)+ '" data-id="'+item.id+'">
<div class="song-duration">' +item.duration+ '</div> <div class="song-duration">' +duration+ '</div>
<div class="remove">remove</div> <div class="remove">remove</div>
<div class="artist-name">' +item.artist+ '</div> <div class="artist-name">' +item.artist+ '</div>
<div class="song-title">' +item.name+ '</div> <div class="song-title">' +item.name+ '</div>

View File

@ -20,8 +20,8 @@ class window.Search
_ajax.setArchor '/artist/' +name+ '/' _ajax.setArchor '/artist/' +name+ '/'
_page.print data.html _page.print data.html
if _session.getUser().id if _session.getUser().id
if data.albums? if data.artist.albums?
for album in data.albums for album in data.artist.albums
_player.albums[album.id] = album _player.albums[album.id] = album
$('.button-container').show() $('.button-container').show()
_search.hideSpinner() _search.hideSpinner()

View File

@ -15,12 +15,11 @@ class ApplicationController < ActionController::Base
render :partial => 'about' render :partial => 'about'
end end
protected protected
def authorize def authorize
unless Vkontakte.check(params) unless Vkontakte.check(params)
render :json => { :status => 'login failed' } render :json => { :status => 'login failed' }
false
end end
end end
@ -40,4 +39,8 @@ class ApplicationController < ActionController::Base
end end
cookies[:beathaven_sid] cookies[:beathaven_sid]
end end
def render_compact_partial partial_name
(render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '')
end
end end

View File

@ -2,121 +2,52 @@ require 'open-uri'
require 'musicbrainz' require 'musicbrainz'
class ArtistController < ApplicationController class ArtistController < ApplicationController
@@default_album_types = [:album, :soundtrack]
def data def data
@data = {
:status => '',
:correct_name => '',
:html => ''
}
@loading = false
# Bad params # Bad params
if params[:name].nil? or params[:name].length == 0 if params[:name].nil? or params[:name].empty?
@data[:status] = 'loading_failed'; return render :json => { :status => 'loading_failed' }
render :json => @data
return
end end
# Searching for artist # Searching for artist
name = params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '') artist_name = get_artist_name_from_query
@artist = Artist.find_by_name(name) @artist = Artist.find_by_name(artist_name, include: {artist_links: {}, albums: {tracks: {}}})
# Artist not found # Artist not found in DB
unless @artist unless @artist
results = MusicBrainz::Artist.search(name) results = MusicBrainz::Artist.search(artist_name)
if results.empty? if results.empty?
@data[:status] = 'not_found' return render :json => { :status => 'not_found' }
render :json => @data end
elsif results[0][:name] != name and (results[0][:name].downcase == name.downcase or results[0][:name].downcase == 'the '+ name.downcase) best_match = results[0][:name]
@data[:status] = 'corrected' if best_match != artist_name and (best_match.downcase == artist_name.downcase or best_match.downcase == 'the '+ artist_name.downcase)
@data[:correct_name] = results[0][:name] return render :json => { :status => 'corrected', :correct_name => best_match }
render :json => @data elsif results[0][:name] == artist_name
elsif results[0][:name] == name queue_loading(artist_name, results[0][:mbid])
# Saving artist and queueing job
@artist = Artist.new
@artist.name = name
@artist.mbid = results[0][:mbid]
@artist.status = 0
@artist.save
Delayed::Job.enqueue LoadArtistJob.new(name)
# Rendering loading info
@artist = { :artist => @artist, :albums => [] } @artist = { :artist => @artist, :albums => [] }
@loading = true @loading = true
@data[:status] = 'loading' return render :json => { :status => 'loading', :html => render_compact_partial(:page) }
@data[:html] = (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '')
render :json => @data
else else
@suggestions = results.take(5) @suggestions = results.take(5)
@data[:status] = 'suggestions' return render :json => { :status => 'suggestions', :html => render_compact_partial(:suggestions) }
@data[:html] = (render_to_string :partial => 'suggestions').gsub(/\n\s+/, '').gsub(/\n/, '')
render :json => @data
end
return
end
if @artist and @artist.status == 2
@data[:status] = 'fail'
@data[:html] = (render_to_string :partial => 'fail').gsub(/\n\s+/, '').gsub(/\n/, '')
render :json => @data
return
end
if @artist.status == 1
@data[:status] = 'ok'
else
@data[:status] = 'loading'
@loading = true
end
@data[:albums], @data[:html] = buildArtistHTML(@artist)
render :json => @data
end
def buildArtistHTML artist
@artist[:artist] = {
id: artist.id,
name: artist.name,
desc: ActionController::Base.helpers.strip_tags(artist.desc),
pic: artist.pic_url,
urls: artist.artist_links
}
media_types = @@default_album_types
session = Session.find_by_key(session_key)
unless session.nil?
media_types = session.user.music
end
@artist[:albums] = []
artist.albums.each do |album|
if true # media_types.include? album.album_type.downcase.to_sym and album.status == 1
tmp_album = {:id => album.id, :name => album.name, :year => album.year, :pic => album.pic_url, :tracks => []}
album.tracks.each do |track|
tmp_track = {id: track.id, name: track.name, live: track.live, acoustic: track.acoustic}
tmp_track[:length] = track.length unless track.length.nil?
tmp_track[:duration] = formatTrackDuration(track.length)
tmp_track[:position] = track.position.to_s 36
# tmp_track[:mbid] = track.mbid
# (track.bonus == 0 ? album_tracks : bonus_tracks) << tmp_track
tmp_album[:tracks] << tmp_track
end
# tmp_album[:tracks] = {album: album_tracks, bonus: bonus_tracks}
@artist[:albums] << tmp_album unless tmp_album[:tracks].empty?
end end
end end
return @artist[:albums], (render_to_string :partial => 'page').gsub(/\n\s+/, '').gsub(/\n/, '') # Artist loading failed
if @artist.status == 2
return render :json => { :status => 'fail', :html => render_compact_partial(:fail) }
end
render json: { status: @artist.status_str, artist: @artist, html: render_compact_partial(:page) }, include: {albums: {include: {tracks: {}}}}
end end
def formatTrackDuration length private
if length
time = length # (length / 1000).round def get_artist_name_from_query
time_m = (time / 60).floor params[:name].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
time_s = time - time_m * 60 end
time_m.to_s + ':' + (time_s < 10 ? '0' : '') + time_s.to_s
else def queue_loading artist_name, mbid
'0:00' @artist = Artist.create( :name => artist_name, :mbid => mbid, :status => 0 )
end Delayed::Job.enqueue(LoadArtistJob.new(artist_name))
end end
end end

View File

@ -8,8 +8,7 @@ class TrackController < ApplicationController
if track.length == nil or track.length.to_i == 0 and params[:length].to_i > 0 if track.length == nil or track.length.to_i == 0 and params[:length].to_i > 0
track.length = params[:length].to_i track.length = params[:length].to_i
track.save track.save
render :json => { :result => :success } return render :json => { :result => :success }
return
end end
end end
end end
@ -21,8 +20,7 @@ class TrackController < ApplicationController
unless track.nil? or track.available unless track.nil? or track.available
track.available = true track.available = true
track.save track.save
render :json => { :status => :success } return render :json => { :status => :success }
return
end end
render :json => { :status => :failed } render :json => { :status => :failed }
end end
@ -32,8 +30,7 @@ class TrackController < ApplicationController
unless track.nil? unless track.nil?
# Saving track file # Saving track file
vote(params[:track_id], params[:owner_id], params[:audio_id], -1) vote(params[:track_id], params[:owner_id], params[:audio_id], -1)
render :json => { :status => :success } return render :json => { :status => :success }
return
end end
render :json => { :status => :failed } render :json => { :status => :failed }
end end

View File

@ -5,10 +5,7 @@ class UserController < ApplicationController
@res = {:status => 'hello', :newbie => false} @res = {:status => 'hello', :newbie => false}
user = User.find_by_vkid(params[:mid]) user = User.find_by_vkid(params[:mid])
if user.nil? if user.nil?
user = User.new user = User.new(:vkid => params[:mid], :lang => 'ru').save
user.vkid = params[:mid]
user.lang = 'ru'
user.save
@res[:newbie] = true @res[:newbie] = true
end end
@ -17,8 +14,7 @@ class UserController < ApplicationController
session.key = session_key session.key = session_key
session.save session.save
@res[:ok_reload] = true @res[:ok_reload] = true
render :json => @res return render :json => @res
return
end end
@res[:user] = { @res[:user] = {
@ -52,7 +48,6 @@ class UserController < ApplicationController
if update_params.include? :show if update_params.include? :show
update_params[:show] = update_params[:show].map{ |k, v| k.to_sym } update_params[:show] = update_params[:show].map{ |k, v| k.to_sym }
end end
params[:params].each do |k, v| params[:params].each do |k, v|
user[k] = v user[k] = v
end end

View File

@ -3,4 +3,14 @@ class Artist < ActiveRecord::Base
has_many :artist_links, :dependent => :destroy has_many :artist_links, :dependent => :destroy
has_many :artist_aliases, :dependent => :destroy has_many :artist_aliases, :dependent => :destroy
has_many :tracks, :through => :track_artists has_many :tracks, :through => :track_artists
after_initialize :prepare_description
def status_str
%w(loading ok fail)[self.status]
end
def prepare_description
self.desc.gsub! /[\[<].*?[\]>]/, ''
end
end end

View File

@ -2,4 +2,16 @@ class Track < ActiveRecord::Base
belongs_to :album belongs_to :album
has_many :track_artists has_many :track_artists
has_many :artists, :through => :track_artists has_many :artists, :through => :track_artists
@duration
def duration
if self.length
time = self.length
time_m = (time / 60).floor
time_s = time - time_m * 60
time_m.to_s + ':' + (time_s < 10 ? '0' : '') + time_s.to_s
else
'0:00'
end
end
end end

View File

@ -1,32 +1,32 @@
- if @loading - if @artist.status == 0
.alert-message.warning .alert-message.warning
%p= I18n.t 'search.loading' %p= I18n.t 'search.loading'
.row.artist-info .row.artist-info
.span4.columns.pic .span4.columns.pic
= image_tag @artist[:artist][:pic] unless @artist[:artist][:pic].nil? = image_tag @artist.pic_url unless @artist.pic_url.nil?
.span7.columns.desc .span7.columns.desc
%h2= @artist[:artist][:name] %h2= @artist.name
= @artist[:artist][:desc] unless @artist[:artist][:desc].nil? = @artist.desc unless @artist.desc.nil?
- unless @artist[:artist][:urls].nil? - unless @artist.artist_links.empty?
.service-icons .service-icons
- @artist[:artist][:urls].each do |service| - @artist.artist_links.each do |service|
- if ['wikipedia', 'microblog', 'official_homepage', 'social_network'].include?service.service - if ['wikipedia', 'microblog', 'official_homepage', 'social_network'].include?service.service
%a.foreign-link{ :href => service.url, :target => '_blank' } %a.foreign-link{ :href => service.url, :target => '_blank' }
= image_tag 'services/'+service.service+(service.service == 'official_homepage' ? '.png' : '.ico') = image_tag 'services/'+service.service+(service.service == 'official_homepage' ? '.png' : '.ico')
- @artist[:albums].each do |album| - @artist.albums.each do |album|
.row.album .row.album
.span4.columns.art .span4.columns.art
%img{ :src => album[:pic] } %img{ :src => album.pic_url }
.button-container .button-container
%a.btn.add-album{ :'data-album-id' => album[:id] }= I18n.t 'player.add' %a.btn.add-album{ :'data-album-id' => album.id }= I18n.t 'player.add'
.span7.columns.tracks .span7.columns.tracks
%h3{ :'data-album-id' => album[:id] }= album[:name] + " (" + album[:year].to_s + ")" %h3{ :'data-album-id' => album.id }= album.name + " (" + album.year.to_s + ")"
%table.zebra-striped.tracklist %table.zebra-striped.tracklist
- album[:tracks].each do |track| - album.tracks.each do |track|
%tr %tr
%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)
.s-add{ :'data-album-id' => album[:id], :'data-id' => track[:id] }= I18n.t 'player.add_one' .s-add{ :'data-album-id' => album.id, :'data-id' => track.id }= I18n.t 'player.add_one'