Monday, August 31, 2009

Rails Plugins : How to Install role_requirement ?

Step 1 - Grab role_requirement form github.com

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:

Welcome to Rails.. Rails... Rails !

In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.

As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam

CCH
10th June 2010, 19:42