diff --git a/app/controllers/api/application_controller.rb b/app/controllers/api/application_controller.rb index 60e876d..d8a77e7 100644 --- a/app/controllers/api/application_controller.rb +++ b/app/controllers/api/application_controller.rb @@ -1,7 +1,7 @@ module API class ApplicationController < ::ApplicationController skip_before_filter :verify_authenticity_token - skip_before_filter :authenticate!, only: [:cor_preflight] + skip_before_filter :authenticate!, only: :cor_preflight before_filter :cor_filter diff --git a/app/controllers/api/playlists_controller.rb b/app/controllers/api/playlists_controller.rb index 8f83019..b3a576a 100644 --- a/app/controllers/api/playlists_controller.rb +++ b/app/controllers/api/playlists_controller.rb @@ -5,14 +5,15 @@ module API end def create - @playlist = current_user.playlists.build(playlist_params) + @playlist = current_user.playlists.build playlist_params @playlist.save end + private def playlist_params - params.require(:playlist).permit(:name) + params.require(:playlist).permit :name end end end diff --git a/app/controllers/api/sessions_controller.rb b/app/controllers/api/sessions_controller.rb index eaad78a..2877932 100644 --- a/app/controllers/api/sessions_controller.rb +++ b/app/controllers/api/sessions_controller.rb @@ -1,11 +1,11 @@ module API class SessionsController < ApplicationController - skip_before_filter :authenticate!, only: [:create] + skip_before_filter :authenticate!, only: :create def create user = User.find_by_email(params[:session][:email]) - unless user.try(:authenticate?, params[:session][:password]) + unless user.try :authenticate?, params[:session][:password] return render json: '', status: :not_found end diff --git a/app/controllers/api/sounds_controller.rb b/app/controllers/api/sounds_controller.rb index b5f3321..b7b9178 100644 --- a/app/controllers/api/sounds_controller.rb +++ b/app/controllers/api/sounds_controller.rb @@ -2,7 +2,7 @@ module API class SoundsController < ApplicationController # FIXME: add some tests! def show - sound = Sound.find params[:id] + sound = Sound.find(params[:id]) send_file sound.path, type: sound.mime_type end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5b08613..ae6546f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base before_filter :authenticate! - def current_user=(user) + def current_user= user session[:user_id] = user.id end @@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base @current_user ||= User.find(session[:user_id]) if session[:user_id] end + protected def authenticate! diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 872e743..e8ea6df 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,6 +1,6 @@ class HomeController < ApplicationController def index - @playlists = Playlist.all - @tracks = Track.latest + @playlists = Playlist.all + @tracks = Track.latest end end diff --git a/app/controllers/playlists_controller.rb b/app/controllers/playlists_controller.rb index ef39ded..9fdcc36 100644 --- a/app/controllers/playlists_controller.rb +++ b/app/controllers/playlists_controller.rb @@ -8,7 +8,8 @@ class PlaylistsController < ApplicationController end def create - @playlist = current_user.playlists.build(playlist_params) + @playlist = current_user.playlists.build playlist_params + if @playlist.save redirect_to action: 'index' else @@ -22,6 +23,7 @@ class PlaylistsController < ApplicationController def update @playlist = Playlist.find(params[:id]) + if @playlist.update_attributes playlist_params redirect_to action: 'index' else @@ -29,9 +31,10 @@ class PlaylistsController < ApplicationController end end + private def playlist_params - params.require(:playlist).permit(:name) + params.require(:playlist).permit :name end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index ba207cb..ccc72d5 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,9 +1,10 @@ class SessionsController < ApplicationController - skip_before_filter :authenticate!, only: [:new, :create] + skip_before_filter :authenticate!, only: %i[new create] def create user = User.find_by_email(params[:session][:email]) - if user.try(:authenticate?, params[:session][:password]) + + if user.try :authenticate?, params[:session][:password] self.current_user = user redirect_to :root else diff --git a/app/controllers/sounds_controller.rb b/app/controllers/sounds_controller.rb index c047256..5a9b70d 100644 --- a/app/controllers/sounds_controller.rb +++ b/app/controllers/sounds_controller.rb @@ -1,6 +1,6 @@ class SoundsController < ApplicationController def show - sound = Sound.find params[:id] + sound = Sound.find(params[:id]) send_file sound.path, type: sound.mime_type end end diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index b9b3b4e..a53ac74 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -1,6 +1,6 @@ class TracksController < ApplicationController def show - @track = Track.find params[:id] + @track = Track.find(params[:id]) end def new @@ -8,7 +8,8 @@ class TracksController < ApplicationController end def create - @track = Track.new track_params + @track = Track.new(track_params) + if @track.save redirect_to @track else @@ -19,6 +20,6 @@ class TracksController < ApplicationController private def track_params - params.require(:track).permit(:name, :file) + params.require(:track).permit %i[name file] end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ce251ce..ed0d0b5 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,5 @@ class UsersController < ApplicationController - skip_before_filter :authenticate!, only: [:new, :create] + skip_before_filter :authenticate!, only: %i[new create] def new @user = User.new @@ -7,6 +7,7 @@ class UsersController < ApplicationController def create @user = User.new(user_params) + if !@user.save render :new else @@ -18,6 +19,6 @@ class UsersController < ApplicationController private def user_params - params.require(:user).permit(:email, :password, :password_confirmation) + params.require(:user).permit %i[email password password_confirmation] end end diff --git a/app/models/sound.rb b/app/models/sound.rb index 6dfd0d6..7ddfec6 100644 --- a/app/models/sound.rb +++ b/app/models/sound.rb @@ -10,7 +10,7 @@ class Sound < ActiveRecord::Base "#{Rails.configuration.sounds_path}/#{sha256}" end - def file=(file) + def file= file self.sha256 = Digest::SHA256.file(file.path).hexdigest FileUtils.cp file.path, path self.mime_type = file.content_type diff --git a/app/models/track.rb b/app/models/track.rb index 46c81dd..2fb9274 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -3,8 +3,8 @@ class Track < ActiveRecord::Base validates_presence_of :name - def file=(file) - sounds.build(file: file) + def file= file + sounds.build file: file end def sound diff --git a/app/models/user.rb b/app/models/user.rb index 81b8ad2..1b6a416 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,12 +15,12 @@ class User < ActiveRecord::Base confirmation: true validates_presence_of :password_hash - def password=(plain_password) + def password= plain_password @password = plain_password self.password_hash = Password.create(plain_password) end - def authenticate?(password) + def authenticate? password Password.new(password_hash) == password end end diff --git a/app/views/sessions/new.html.haml b/app/views/sessions/new.html.haml index fbffef9..5e994e3 100644 --- a/app/views/sessions/new.html.haml +++ b/app/views/sessions/new.html.haml @@ -1,4 +1,4 @@ -= form_for(:session, url: sessions_path) do |f| += form_for :session, url: sessions_path do |f| %table %tbody %tr diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml index 0280580..4ac7f44 100644 --- a/app/views/users/new.html.haml +++ b/app/views/users/new.html.haml @@ -1,4 +1,4 @@ -= form_for(@user, url: users_path) do |f| += form_for @user, url: users_path do |f| - if @user.errors.any? %ul - @user.errors.full_messages.each do |m|