diff --git a/assets/coffee/wyrm.coffee b/assets/coffee/wyrm.coffee index 4bd577a..036764d 100644 --- a/assets/coffee/wyrm.coffee +++ b/assets/coffee/wyrm.coffee @@ -1,41 +1,31 @@ $(document).ready(() -> - $('.channel-path').click((e) -> - newPath = prompt('New channel directory path', $(this).find('span').text()) - if newPath - payload = - directory_path: newPath - channelId = $('#channel-id').val() - $.post('/channel/' + channelId + '/edit/directory_path', payload, (data) -> - if data == 'success' - $('.channel-path span').text(newPath) - ) - ) + $('.channel-path').click(updateAttribute) - $('.video-status').click((e) -> - newStatus = prompt('New video status', $(this).find('span').text()) - if newStatus - newStatus = newStatus.toLowerCase() - payload = - status: newStatus - videoId = $('#video-id').val() - $.post('/video/' + videoId + '/edit/status', payload, (data) -> - if data == 'success' - $('.video-status span').text(capitalizeWord(newStatus)) - ) - ) + $('.video-status').click(updateAttribute) - $('.video-serial').click((e) -> - newSerial = prompt('New video serial number', $(this).find('span').text()) - if newSerial - payload = - serial: newSerial - videoId = $('#video-id').val() - $.post('/video/' + videoId + '/edit/serial', payload, (data) -> - if data == 'success' - $('.video-serial span').text(newSerial) - ) - ) + $('.video-serial').click(updateAttribute) ) +updateAttribute = (e) -> + elem = $(this) + type = elem.data('type') + attr = elem.data('attribute') + newValue = prompt('Enter new ' + type + ' ' + attr + ':', elem.find('span').text()) + if newValue + if attr == 'status' + newValue = newValue.toLowerCase() + payload = + value: newValue + objectId = $('#' + type + '-id').val() + url = '/' + type + '/' + objectId + '/edit/' + attr + $.post(url, payload, (data) -> + if data == 'success' + if attr == 'status' + newValue = capitalizeWord(newValue) + elem.find('span').text(newValue) + ) + else + console.log('User canceled attribute "' + attr + '" update.') + capitalizeWord = (str) -> return str.charAt(0).toUpperCase() + str.slice(1) diff --git a/lib/routes/channel.rb b/lib/routes/channel.rb index f5af0c8..33dbfad 100644 --- a/lib/routes/channel.rb +++ b/lib/routes/channel.rb @@ -79,14 +79,14 @@ namespace '/channel' do attrToEdit = params[:attr] if attrToEdit == 'directory_path' - File.rename(channel.directory_path, params[attrToEdit]) + File.rename(channel.directory_path, params[:value]) channel.videos.each do |v| - video_path = v.directory_path.sub(channel.directory_path, params[attrToEdit]) + video_path = v.directory_path.sub(channel.directory_path, params[:value]) v.update(directory_path: video_path) end end - channel[attrToEdit.to_sym] = params[attrToEdit] + channel[attrToEdit.to_sym] = params[:value] channel.save() return "success" diff --git a/lib/routes/video.rb b/lib/routes/video.rb index 00652f6..49ffe5e 100644 --- a/lib/routes/video.rb +++ b/lib/routes/video.rb @@ -111,7 +111,15 @@ namespace '/video' do video = Video.where(id: params[:video_id]).first() attrToEdit = params[:attr] - video[attrToEdit.to_sym] = params[attrToEdit] + # if we update the video's serial, we need to also update the directory path + if attrToEdit == 'serial' + old_path = video.directory_path + new_path = video.directory_path.sub("##{video.serial}", "##{params[:value]}") + File.rename(old_path, new_path) + video[:directory_path] = new_path + end + + video[attrToEdit.to_sym] = params[:value] video.save() return "success" diff --git a/views/channel/view.erb b/views/channel/view.erb index 0d2ddaf..82b0ba4 100644 --- a/views/channel/view.erb +++ b/views/channel/view.erb @@ -16,7 +16,7 @@ Edit Delete -
+

<%= channel.directory_path %>

diff --git a/views/video/view.erb b/views/video/view.erb index fc31694..062e66b 100644 --- a/views/video/view.erb +++ b/views/video/view.erb @@ -18,10 +18,10 @@ -
+

Serial: <%= serialize(video.serial) %>

-
+

Status: <%= video.status.capitalize() %>