chee@ibm4linux:~/workspace/crm4test$ script/plugin install git://github.com/timcharper/role_requirement.git
Result
Initialized empty Git repository in /home/chee/workspace/crm4test/vendor/plugins/role_requirement/.git/
remote: Counting objects: 32, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 32 (delta 3), reused 10 (delta 0)
Unpacking objects: 100% (32/32), done.
From git://github.com/timcharper/role_requirement
* branch HEAD -> FETCH_HEAD
Step 2 - Run the generator
chee@ibm4linux:~/workspace/crm4test$ script/generate roles Role User
Generating Role against User
Added the following to the top of app/models/user.rb:
# ---------------------------------------
# The following code has been generated by role_requirement.
# You may wish to modify it to suit your need
has_and_belongs_to_many :roles
# has_role? simply needs to return true or false whether a user has a role or not.
# It may be a good idea to have "admin" roles return true always
def has_role?(role_in_question)
@_list ||= self.roles.collect(&:name)
return true if @_list.include?("admin")
(@_list.include?(role_in_question.to_s) )
end
# ---------------------------------------
Added ApplicationController include to /home/chee/workspace/crm4test/app/controllers/application_controller.rb
Added RoleRequirement include to /home/chee/workspace/crm4test/app/controllers/application_controller.rb
create test/fixtures/roles.yml
create app/models/role.rb
create lib/role_requirement_system.rb
create lib/role_requirement_test_helper.rb
create lib/hijacker.rb
exists db/migrate
create db/migrate/20090831042625_create_roles.rb
Step 3 - rake db:migrate
chee@ibm4linux:~/workspace/crm4test$ rake db:migrate
(in /home/chee/workspace/crm4test)
== CreateRoles: migrating ====================================================
-- create_table("roles")
-> 0.0037s
-- create_table("roles_users", {:id=>false})
-> 0.0027s
-- add_index("roles_users", "role_id")
-> 0.0122s
-- add_index("roles_users", "user_id")
-> 0.0016s
== CreateRoles: migrated (0.0229s) ===========================================
What does the Generators do ?
- Only if many roles are used:
- Generates habtm table, creates role.rb with the habtm declaration. Adds declaration in user.rb (scans the code for "class User < ActiveRecord::Base", and puts the new code right after it.
- Creates an admin user in users.yml, with a role named admin in roles.yml, including a fixture to demonstrate how to relate roles to users in roles_users.yml
- Modify the user.rb (or corresponding user model) file, add the instance method has_role?
- Generates RoleRequirementSystem against for the corresponding user model.
- Generates a migration to make the necessary database changes
- Scans ApplicationController, inserts the lines "include AuthenticatedSystem", and "include RoleRequirementSystem", if not already included.
- Scans test_helper.rb and adds "includes RoleRequirementTestHelpers", if not already included.
No comments:
Post a Comment