public
Description: The kick ass (non-commercial) home for musicians and their music
Homepage: http://alonetone.com
Clone URL: git://github.com/sudara/alonetone.git
sudara (author)
Mon May 12 16:19:10 -0700 2008
commit  46932449792418926d8655590ecaa2981b59e431
tree    60047bef1bfd9c313fe98c9b7791f66860f9d86d
parent  71a8a123b06a4ace4d0912fb44b90c74efa2af17
alonetone / app / controllers / playlists_controller.rb
100755 191 lines (162 sloc) 5.765 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class PlaylistsController < ApplicationController
  
  before_filter :find_user
  before_filter :find_playlists, :except => [:index, :new, :create, :sort]
  before_filter :login_required, :except => [:index, :show]
 
  before_filter :find_tracks, :only => [:show, :edit]
  
  rescue_from ActiveRecord::RecordNotFound, :with => :not_found
  rescue_from NoMethodError, :with => :user_not_found
  
  # GET /playlists
  # GET /playlists.xml
  def index
    if logged_in? && (current_user.id.to_s == @user.id.to_s || current_user.admin?)
      @all_playlists = @user.playlists.include_private
    else
      @all_playlists = @user.playlists.public
    end
    if present?(@all_playlists)
      # TODO: fugly array work
      split = @all_playlists.in_groups_of((@all_playlists.size.to_f/2).round)
      @playlists_left, @playlists_right = split[0].try(:compact), split[1].try(:compact)
      @page_title = "#{@user.name}'s albums and playlists"
      respond_to do |format|
         format.html # index.html.erb
         format.xml { render :xml => @playlists }
      end
    else
      redirect_to user_path(@user)
    end
  end
 
  def sort
    redirect_to user_home_url(@user) unless logged_in? && (@user.id.to_s == current_user.id.to_s) || admin?
    respond_to do |format|
      format.html { @playlists = @user.playlists.include_private.find(:all) }
      format.js do
        params["playlist"].each_with_index do |id, position|
          Playlist.update(id, :position => position)
        end
        render :nothing => true
      end
    end
  end
  
  # GET /playlists/1
  # GET /playlists/1.xml
  def show
    @page_title = "\"#{@playlist.title}\" by #{@user.name}"
    respond_to do |format|
      format.html # show.html.erb
      format.xml
      format.rss
    end
  end
 
  # GET /playlists/new
  # GET /playlists/new.xml
  def new
    @playlist = @user.playlists.build(:private => true)
    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @playlist }
    end
  end
 
  # GET /playlists/1/edit
  def edit
    # allow them to add their own assets
    # TODO: this is bad form, should be relocated to assets/index and listens/index
    @assets = @user.assets.paginate(:all, :limit => 10, :per_page => 10, :order => 'created_at DESC', :page => params[:uploads_page])
    @listens = @user.listens.paginate(:all, :limit => 10, :order => 'listens.created_at DESC', :per_page => 10, :page => params[:listens_page])
    respond_to do |format|
      format.html
      format.js do
        render :partial => 'your_stuff.html.erb' if params[:uploads_page]
        render :partial => 'your_listens.html.erb' if params[:listens_page]
      end
    end
  end
 
  def add_track
    @track = @playlist.tracks.create(:asset => Asset.find(params[:asset_id].split("_")[1]))
    respond_to do |format|
      format.js
    end
  rescue ActiveRecord::RecordNotFound, NoMethodError
    return head(:bad_request)
  end
  
  def attach_pic
    @pic = @playlist.build_pic(params[:pic])
    if @pic.save
      flash[:notice] = 'Cover art updated!'
    else
      flash[:error] = 'Whups, make sure you choose a valid jpg, gif, or png file!'
    end
    redirect_to edit_user_playlist_path(@user, @playlist)
  end
  
  
  
  def remove_track
    @track = @playlist.tracks.find(params[:track_id])
    if @track && @track.destroy
      respond_to do |format|
        format.js {return head(:ok); render :nothing => true}
      end
    else
      render :nothing => true
    end
  rescue ActiveRecord::RecordNotFound
    head(:bad_request)
  end
  
  def sort_tracks
    # get the params for this playlist
    params["track"].each_with_index do |id, position|
      Track.update(id, :position => position)
    end
    render :nothing => true
  end
 
  # POST /playlists
  # POST /playlists.xml
  def create
    @playlist = @user.playlists.build(params[:playlist])
 
    respond_to do |format|
      if @playlist.save
        flash[:notice] = 'Great, go ahead and add some tracks'
        format.html { redirect_to edit_user_playlist_path(@user, @playlist) }
        format.xml { render :xml => @playlist, :status => :created, :location => @playlist }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @playlist.errors, :status => :unprocessable_entity }
      end
    end
  end
 
 
 
  # PUT /playlists/1
  # PUT /playlists/1.xml
  def update
 
    respond_to do |format|
      if @playlist.update_attributes(params[:playlist])
        flash[:notice] = 'Playlist was successfully updated.'
        format.html { redirect_to edit_user_playlist_path(@user,@playlist) }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @playlist.errors, :status => :unprocessable_entity }
      end
    end
  end
 
  # DELETE /playlists/1
  # DELETE /playlists/1.xml
  def destroy
    @playlist.destroy
 
    respond_to do |format|
      format.html { redirect_to(user_playlists_url(@user)) }
      format.xml { head :ok }
    end
  end
  
  
  protected
  def not_found
    flash[:error] = "We didn't find that playlist from #{@user.name}, sorry, but try these others" and redirect_to user_playlists_path(@user)
  end
    
  def authorized?
    (!%w(destroy admin edit update remove_track attach_pic sort_tracks add_track set_playlist_description set_playlist_title).include?(action_name)) || (@playlist.user_id.to_s == current_user.id.to_s) || admin?
  end
  
  def find_playlists
    @playlist = @user.playlists.find_by_permalink(params[:permalink] || params[:id])
    @playlist = @user.playlists.find(params[:id]) if !@playlist && params[:id]
  end
  
  def find_tracks
    @tracks = @playlist.tracks
  end
  
end