Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Commit

Permalink
made set_role only accept saved roles
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed Dec 4, 2013
1 parent 97ef476 commit 1911e3e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
validates :name, uniqueness: true
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def User.encrypt(token)
end

def set_role(role)
if Role.find(role.id)
self.roles << role
end


Expand Down
11 changes: 9 additions & 2 deletions spec/models/role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
describe "it should show a users roles" do
let(:user_id) do
user = FactoryGirl.create(:user)
user.roles << role
user.set_role(role)
user.save
user.id
end

specify { expect(User.find(user_id).roles).to eq [role] }
specify { expect(role.users).to eq [User.find(user_id)] }
end
end

describe "creating a role with a name that already exists" do
let(:duplicate_role) {Role.new(name: role.name)}
specify { expect(duplicate_role).not_to be_valid }
end

end

1 change: 1 addition & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it { should respond_to(:email) }
it { should respond_to(:encrypted_password)}
it { should respond_to(:remember_token)}
it { should respond_to(:roles) }

it { should be_valid }

Expand Down

0 comments on commit 1911e3e

Please sign in to comment.