Permalink
Browse files

Created roles. Associate default role (student) when creating user.

  • Loading branch information...
1 parent a76f29d commit 24b253f9748a9acded4f5f410e7fd56bfbb517fa @ugisozols ugisozols committed Dec 30, 2010
View
@@ -0,0 +1,3 @@
+class Role < ActiveRecord::Base
+ has_and_belongs_to_many :users
+end
View
@@ -7,5 +7,12 @@ class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
- has_one :role
+ has_and_belongs_to_many :roles
+
+ after_create :assign_default_role
+
+ private
+ def assign_default_role
+ self.roles << Role.where(:title => "student").first
+ end
end
@@ -0,0 +1,15 @@
+class CreateRoles < ActiveRecord::Migration
+ def self.up
+ create_table :roles do |t|
+ t.string :title
+ t.timestamps
+ end
+
+ puts "Adding default role (student) to table ..."
+ Role.create!(:title => "student")
+ end
+
+ def self.down
+ drop_table :roles
+ end
+end
@@ -0,0 +1,12 @@
+class CreateRolesUsers < ActiveRecord::Migration
+ def self.up
+ create_table :roles_users, :id => false do |t|
+ t.integer :role_id
+ t.integer :user_id
+ end
+ end
+
+ def self.down
+ drop_table :roles_users
+ end
+end
View
@@ -10,7 +10,18 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20101212030012) do
+ActiveRecord::Schema.define(:version => 20101230060611) do
+
+ create_table "roles", :force => true do |t|
+ t.string "title"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "roles_users", :id => false, :force => true do |t|
+ t.integer "role_id"
+ t.integer "user_id"
+ end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
View
@@ -2,16 +2,32 @@
describe User do
before(:each) do
+ # in dev or production this role would be created by migration
+ Role.create!(:title => "student")
+
@user = User.create! :email => "test@test.com", :password => '123abc'
@user2 = User.create "Test2"
end
+=begin
+ # TODO: remove this
describe "A Role" do
it "should be associated with a user" do
User.new.should respond_to(:role)
pending "needs to be associated and validated with a default role"
end
end
+=end
+
+ context "A Role" do
+ it "have a roles association" do
+ User.new.should respond_to(:roles)
+ end
+
+ it "assigns default role of student when creating user" do
+ @user.roles.first.title.should == "student"
+ end
+ end
context "when an user is created it has to have an e-mail address" do

0 comments on commit 24b253f

Please sign in to comment.