Use new "strong parameters" from rails4
This commit is contained in:
parent
1b5055d0a6
commit
4c6adc6927
2
Gemfile
2
Gemfile
@ -6,8 +6,6 @@ gem 'haml', '~> 4.0'
|
|||||||
gem 'bcrypt', '~> 3.0'
|
gem 'bcrypt', '~> 3.0'
|
||||||
gem 'rabl', '~> 0.6'
|
gem 'rabl', '~> 0.6'
|
||||||
|
|
||||||
gem 'protected_attributes', '~> 1.0'
|
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
gem 'rspec-rails', '~> 2.6'
|
gem 'rspec-rails', '~> 2.6'
|
||||||
gem 'spork', '~> 1.0rc'
|
gem 'spork', '~> 1.0rc'
|
||||||
|
@ -7,8 +7,14 @@ module API
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@playlist = current_user.playlists.build(params[:playlist].slice(:name))
|
@playlist = current_user.playlists.build(playlist_params)
|
||||||
@playlist.save
|
@playlist.save
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def playlist_params
|
||||||
|
params.require(:playlist).permit(:name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,7 +8,7 @@ class PlaylistsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@playlist = current_user.playlists.build(params[:playlist])
|
@playlist = current_user.playlists.build(playlist_params)
|
||||||
if @playlist.save
|
if @playlist.save
|
||||||
redirect_to action: 'index'
|
redirect_to action: 'index'
|
||||||
else
|
else
|
||||||
@ -22,10 +22,16 @@ class PlaylistsController < ApplicationController
|
|||||||
|
|
||||||
def update
|
def update
|
||||||
@playlist = Playlist.find(params[:id])
|
@playlist = Playlist.find(params[:id])
|
||||||
if @playlist.update_attributes params[:playlist]
|
if @playlist.update_attributes playlist_params
|
||||||
redirect_to action: 'index'
|
redirect_to action: 'index'
|
||||||
else
|
else
|
||||||
render action: 'edit'
|
render action: 'edit'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def playlist_params
|
||||||
|
params.require(:playlist).permit(:name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,11 +8,17 @@ class TracksController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@track = Track.new params[:track]
|
@track = Track.new track_params
|
||||||
if @track.save
|
if @track.save
|
||||||
redirect_to @track
|
redirect_to @track
|
||||||
else
|
else
|
||||||
render :new
|
render :new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def track_params
|
||||||
|
params.require(:track).permit(:name, :file)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,7 +6,7 @@ class UsersController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@user = User.new(params[:user])
|
@user = User.new(user_params)
|
||||||
if !@user.save
|
if !@user.save
|
||||||
render :new
|
render :new
|
||||||
else
|
else
|
||||||
@ -14,4 +14,10 @@ class UsersController < ApplicationController
|
|||||||
redirect_to :root
|
redirect_to :root
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def user_params
|
||||||
|
params.require(:user).permit(:email, :password, :password_confirmation)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
class Playlist < ActiveRecord::Base
|
class Playlist < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
attr_accessible :name
|
|
||||||
|
|
||||||
validates_presence_of :user
|
validates_presence_of :user
|
||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
end
|
end
|
||||||
|
@ -3,8 +3,6 @@ require 'fileutils'
|
|||||||
class Sound < ActiveRecord::Base
|
class Sound < ActiveRecord::Base
|
||||||
belongs_to :track
|
belongs_to :track
|
||||||
|
|
||||||
attr_accessible :file
|
|
||||||
|
|
||||||
validates_presence_of :sha256
|
validates_presence_of :sha256
|
||||||
validates_presence_of :mime_type
|
validates_presence_of :mime_type
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
class Track < ActiveRecord::Base
|
class Track < ActiveRecord::Base
|
||||||
has_many :sounds
|
has_many :sounds
|
||||||
|
|
||||||
attr_accessible :name, :file
|
|
||||||
|
|
||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
|
|
||||||
def file=(file)
|
def file=(file)
|
||||||
|
@ -4,7 +4,6 @@ class User < ActiveRecord::Base
|
|||||||
include BCrypt
|
include BCrypt
|
||||||
|
|
||||||
attr_reader :password
|
attr_reader :password
|
||||||
attr_accessible :email, :password, :password_confirmation
|
|
||||||
|
|
||||||
has_many :playlists
|
has_many :playlists
|
||||||
|
|
||||||
|
@ -1,28 +1,34 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe UsersController do
|
describe UsersController do
|
||||||
|
let(:attributes) { FactoryGirl.attributes_for(:user) }
|
||||||
|
|
||||||
describe 'GET new' do
|
describe 'GET new' do
|
||||||
it 'assigns a new user as @user' do
|
it 'assigns a new user as @user' do
|
||||||
get :new
|
get :new
|
||||||
expect(assigns[:user]).to be_a_new(User)
|
expect(assigns[:user]).to be_a_new User
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'POST create' do
|
describe 'POST create' do
|
||||||
|
def do_post(params = {})
|
||||||
|
post :create, user: attributes.merge(params)
|
||||||
|
end
|
||||||
|
|
||||||
context 'whith valid params' do
|
context 'whith valid params' do
|
||||||
it 'creates a new user' do
|
it 'creates a new user' do
|
||||||
expect {
|
expect {
|
||||||
post :create, user: FactoryGirl.attributes_for(:user)
|
do_post
|
||||||
}.to change(User, :count).by(1)
|
}.to change(User, :count).by(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'signs the user in' do
|
it 'signs the user in' do
|
||||||
post :create, user: FactoryGirl.attributes_for(:user)
|
do_post
|
||||||
expect(controller.current_user).not_to be_nil
|
expect(controller.current_user).not_to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects to the home page' do
|
it 'redirects to the home page' do
|
||||||
post :create, user: FactoryGirl.attributes_for(:user)
|
do_post
|
||||||
expect(response).to redirect_to :root
|
expect(response).to redirect_to :root
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -31,12 +37,12 @@ describe UsersController do
|
|||||||
before { allow_any_instance_of(User).to receive(:save) { false } }
|
before { allow_any_instance_of(User).to receive(:save) { false } }
|
||||||
|
|
||||||
it 'assigns the user as @user' do
|
it 'assigns the user as @user' do
|
||||||
post :create, user: {}
|
do_post
|
||||||
expect(assigns[:user]).to be_a_new User
|
expect(assigns[:user]).to be_a_new User
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'renders the new template' do
|
it 'renders the new template' do
|
||||||
post :create, user: {}
|
do_post
|
||||||
expect(response).to render_template 'new'
|
expect(response).to render_template 'new'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,5 +8,4 @@ describe Playlist do
|
|||||||
it { should belong_to :user }
|
it { should belong_to :user }
|
||||||
it { should validate_presence_of :user }
|
it { should validate_presence_of :user }
|
||||||
it { should validate_presence_of :name }
|
it { should validate_presence_of :name }
|
||||||
it { should_not allow_mass_assignment_of :user }
|
|
||||||
end
|
end
|
||||||
|
@ -8,7 +8,6 @@ describe Track do
|
|||||||
it { should be_valid }
|
it { should be_valid }
|
||||||
it { should have_many :sounds }
|
it { should have_many :sounds }
|
||||||
it { should validate_presence_of :name }
|
it { should validate_presence_of :name }
|
||||||
it { should_not allow_mass_assignment_of :sounds }
|
|
||||||
|
|
||||||
context 'with a file' do
|
context 'with a file' do
|
||||||
before { track.file = file }
|
before { track.file = file }
|
||||||
|
@ -9,7 +9,6 @@ describe User do
|
|||||||
it { should validate_presence_of :email }
|
it { should validate_presence_of :email }
|
||||||
it { should validate_presence_of :password }
|
it { should validate_presence_of :password }
|
||||||
it { should validate_presence_of :password_hash }
|
it { should validate_presence_of :password_hash }
|
||||||
it { should_not allow_mass_assignment_of :password_hash }
|
|
||||||
|
|
||||||
context 'when a user with the same email address already exists' do
|
context 'when a user with the same email address already exists' do
|
||||||
let(:old_user) { FactoryGirl.create(:user, email: 'unique@example.net') }
|
let(:old_user) { FactoryGirl.create(:user, email: 'unique@example.net') }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user