From 8b226998a4faaaa9ce0a9da55d07f2440227ac1c Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sat, 2 May 2015 00:24:30 +0000 Subject: [PATCH] Refactor controllers --- app/controllers/api/playlists_controller.rb | 7 ++++++- app/controllers/api/sounds_controller.rb | 11 +++++++++-- app/controllers/api/tracks_controller.rb | 8 ++++++++ app/controllers/playlists_controller.rb | 17 ++++++++++------- app/controllers/sessions_controller.rb | 2 +- app/controllers/sounds_controller.rb | 12 ++++++++++-- app/controllers/tracks_controller.rb | 10 ++++++---- app/controllers/users_controller.rb | 1 + 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/playlists_controller.rb b/app/controllers/api/playlists_controller.rb index 4466bad..806a706 100644 --- a/app/controllers/api/playlists_controller.rb +++ b/app/controllers/api/playlists_controller.rb @@ -1,11 +1,12 @@ module API class PlaylistsController < ApplicationController + before_action :set_playlist, only: :show + def index @playlists = Playlist.all end def show - @playlist = Playlist.find(params[:id]) end def create @@ -16,6 +17,10 @@ module API private + def set_playlist + @playlist = Playlist.find(params[:id]) + end + def playlist_params params.require(:playlist).permit :name end diff --git a/app/controllers/api/sounds_controller.rb b/app/controllers/api/sounds_controller.rb index b7b9178..67efd3a 100644 --- a/app/controllers/api/sounds_controller.rb +++ b/app/controllers/api/sounds_controller.rb @@ -1,9 +1,16 @@ module API class SoundsController < ApplicationController - # FIXME: add some tests! + before_action :set_sound, only: :show + def show - sound = Sound.find(params[:id]) send_file sound.path, type: sound.mime_type end + + + private + + def set_sound + @sound = Sound.find(params[:id]) + end end end diff --git a/app/controllers/api/tracks_controller.rb b/app/controllers/api/tracks_controller.rb index a8cbd0e..4eb51ec 100644 --- a/app/controllers/api/tracks_controller.rb +++ b/app/controllers/api/tracks_controller.rb @@ -1,10 +1,18 @@ module API class TracksController < ApplicationController + before_action :set_track, only: :show + def index @tracks = Track.all end def show + end + + + private + + def set_track @track = Track.find(params[:id]) end end diff --git a/app/controllers/playlists_controller.rb b/app/controllers/playlists_controller.rb index 9fdcc36..9799764 100644 --- a/app/controllers/playlists_controller.rb +++ b/app/controllers/playlists_controller.rb @@ -1,4 +1,6 @@ class PlaylistsController < ApplicationController + before_filter :set_playlist, only: %i[edit update] + def index @playlists = Playlist.all end @@ -11,29 +13,30 @@ class PlaylistsController < ApplicationController @playlist = current_user.playlists.build playlist_params if @playlist.save - redirect_to action: 'index' + redirect_to action: :index else - render action: 'new' + render :new end end def edit - @playlist = Playlist.find(params[:id]) end def update - @playlist = Playlist.find(params[:id]) - if @playlist.update_attributes playlist_params - redirect_to action: 'index' + redirect_to action: :index else - render action: 'edit' + render :edit end end private + def set_playlist + @playlist = Playlist.find(params[:id]) + end + def playlist_params params.require(:playlist).permit :name end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 07dbe53..877b045 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -8,7 +8,7 @@ class SessionsController < ApplicationController self.current_user = user redirect_to :root else - render 'new' + render :new end end diff --git a/app/controllers/sounds_controller.rb b/app/controllers/sounds_controller.rb index 5a9b70d..a84aac1 100644 --- a/app/controllers/sounds_controller.rb +++ b/app/controllers/sounds_controller.rb @@ -1,6 +1,14 @@ class SoundsController < ApplicationController + before_filter :set_sound, only: :show + def show - sound = Sound.find(params[:id]) - send_file sound.path, type: sound.mime_type + send_file @sound.path, type: @sound.mime_type + end + + + private + + def set_sound + @sound = Sound.find(params[:id]) end end diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index 95fee5c..ec8dbfc 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -1,10 +1,11 @@ class TracksController < ApplicationController + before_filter :set_track, only: %i[show edit update] + def index @tracks = Track.all end def show - @track = Track.find(params[:id]) end def new @@ -12,7 +13,6 @@ class TracksController < ApplicationController end def edit - @track = Track.find(params[:id]) end def create @@ -26,8 +26,6 @@ class TracksController < ApplicationController end def update - @track = Track.find(params[:id]) - if @track.update_attributes track_params redirect_to action: :index else @@ -38,6 +36,10 @@ class TracksController < ApplicationController private + def set_track + @track = Track.find(params[:id]) + end + def track_params params.require(:track).permit %i[name file] end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ed0d0b5..ad81a6f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -16,6 +16,7 @@ class UsersController < ApplicationController end end + private def user_params