0 Replies - 3082 Views - Last Post: 07 October 2012 - 01:27 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

run rake tast from within a rake task

Posted 07 October 2012 - 01:27 PM

To seed my database, I use rake tasks in lue of seeds.rb since seeds.rb was depricated before I learned how to use it. Anyway, in my database population script I build a bunch of users, and now I setup their roles all in one rake task (db:populate)...

(sample_data.rake)
namespace :db do
  desc "Fill database with sample data"
  task :populate => :environment do
    Rake::Task['db:reset'].invoke    # yikes!  This line is dangerous!
    
    make_roles
    make_users
    
    make_newsblast
    make_news_posts
    make_bio_records
    setup_legacy_tables_data
  end
end
.
.
.




but now I'm thinking that since I'm setting up roles, maybe that should be done in it's own rake task... once that doesn't involve dropping the entier database and starting anew... That way, when ever I decide some new employee should get eleveated privledges, I just add the desired role to their User table and run my script. That sounds like a pretty awesome idea right?


(reset_permissions.rake)
namespace :db do
  desc "Setup the Roles database to provision permissions properly"
  task :permissions => :environment do

    delete_old_permissions
    make_roles
    assign_roles

  end
end


def delete_old_permissions
  Role.delete_all
  Right.delete_all
  Grant.delete_all
  Assignment.delete_all
end


def make_roles
  @communications_role = Role.create!(:name => 'communications')
  @communications_role.rights.create!(:resource => "users", :operation => "READ")
  @communications_role.rights.create!(:resource => "news_posts", :operation => "READ")
  @communications_role.rights.create!(:resource => "news_posts", :operation => "UPDATE")
  @communications_role.rights.create!(:resource => "news_posts", :operation => "CREATE")
  @communications_role.rights.create!(:resource => "news_posts", :operation => "DELETE")
  @communications_role.rights.create!(:resource => "news_blasts", :operation => "READ")
  @communications_role.rights.create!(:resource => "news_blasts", :operation => "UPDATE")
  @communications_role.rights.create!(:resource => "news_blasts", :operation => "CREATE")
  @communications_role.rights.create!(:resource => "news_blasts", :operation => "DELETE")
  @communications_role.rights.create!(:resource => "bio_records", :operation => "READ")
  @communications_role.rights.create!(:resource => "bio_records", :operation => "UPDATE")
  @communications_role.rights.create!(:resource => "bio_records", :operation => "CREATE")
  @communications_role.rights.create!(:resource => "bio_records", :operation => "DELETE")
  
  
  @management_role = Role.create!(:name => 'management')
  @management_role.rights.create!(:resource => "users", :operation => "READ")
  @management_role.rights.create!(:resource => "users", :operation => "UPDATE")
  @management_role.rights.create!(:resource => "users", :operation => "CREATE")
  @management_role.rights.create!(:resource => "users", :operation => "DELETE")
  
  
  @customer_role = Role.create!(:name => 'customer')
  @customer_role.rights.create!(:resource => "users", :operation => "READ")
end

def assign_roles
  emp1 = User.where(:email => "carolyn@excelsiorcarpetone.com").first
  emp2 = User.where(:email => "ari@excelsiorcarpetone.com").first
  emp3 = User.where(:email => "jean@excelsiorcarpetone.com").first
  emp4 = User.where(:email => "barrie@excelsiorcarpetone.com").first
  
  emp1.roles << @management_role
  emp1.roles << @communications_role
  
  emp2.roles << @communications_role
  emp3.roles << @communications_role
  emp4.roles << @communications_role
end




So how could I call 'reset_permissions.rake' from within 'sample_data.rake' so I would be able to run just 'reset_permissions' on the production system and 'sample_data.rake' to rebuild the entire database for development to begin on a new machine?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1