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 Mar 17 09:24:38 -0700 2008
commit  4cd5571b8b7b04d82ed535357fb6aa25380de0b3
tree    a6304f5141de495490bafb74101f6a025219311c
parent  f606d65e61011c86ae327775f8714677492bd11f
alonetone / app / controllers / playlists_controller.rb
100755 162 lines (132 sloc) 4.252 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
class PlaylistsController < ApplicationController
  
  before_filter :find_user
  before_filter :find_playlists, :except => [:index, :new, :create]
  before_filter :login_required, :except => [:index, :show]
 
  before_filter :find_tracks, :only => [:show, :edit]
  in_place_edit_for :playlist, :description
  in_place_edit_for :playlist, :title
  
  
  # GET /playlists
  # GET /playlists.xml
  def index
    @playlists = @user.playlists.find(:all)
 
    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @playlists }
    end
  end
 
  # GET /playlists/1
  # GET /playlists/1.xml
  def show
    @page_title = "\"#{@playlist.title}\" by #{@user.name} on alonetone"
    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
    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
    @assets = @user.assets.paginate(:all, :limit => 10, :per_page => 10, :order => 'created_at DESC', :page => params[:page])
    @listens = @user.listens.find(:all, :limit => 10, :order => 'listens.created_at DESC')
    respond_to do |format|
      format.html
      format.js do
        render :update do |page|
            page.replace 'your_stuff_box', :partial => "your_stuff"
        end
      end
    end
  end
 
  
  def add_track
    @track = @playlist.tracks.create(:asset_id => params[:asset_id].split("_")[1]) if params[:asset_id]
    respond_to do |format|
      format.html { render :layout => false }
      format.js
    end
  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.destroy
      respond_to do |format|
        format.js
      end
    else
      render :nothing => true
    end
  end
  
  def sort_tracks
    # get the params for this playlist
    params["tracks"].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] = 'Playlist was successfully created.'
        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(@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 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