class RolesGenerator

Class definition for the Rails Generator integrating Roles

Public Class Methods

next_migration_number(_path) click to toggle source

Implement the required interface for Rails::Generators::Migration. taken from github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb

# File lib/generators/roles/roles_generator.rb, line 22
def self.next_migration_number(_path)
  if @prev_migration_nr
    @prev_migration_nr += 1
  else
    @prev_migration_nr = Time.now.utc.strftime('%Y%m%d%H%M%S').to_i
  end
  @prev_migration_nr.to_s
end

Public Instance Methods

copy_migrations() click to toggle source

Setup the database migrations

# File lib/generators/roles/roles_generator.rb, line 32
def copy_migrations
  # Can't get this any more DRY, because we need this order.
  %w[user_roles.rb].each do |f|
    better_migration_template f
  end
end
inject_routes() click to toggle source

The engine routes have to come after the devise routes so that /users/sign_in will work

# File lib/generators/roles/roles_generator.rb, line 61
def inject_routes
  routing_code = "mount Hydra::RoleManagement::Engine => '/'"
  sentinel = /devise_for :users(.*)$/
  inject_into_file 'config/routes.rb', "\n  #{routing_code}\n", after: sentinel, verbose: false
end
inject_user_roles_behavior() click to toggle source

Add behaviors to the user model

# File lib/generators/roles/roles_generator.rb, line 40
def inject_user_roles_behavior
  file_path = "app/models/#{model_name.underscore}.rb"
  if File.exist?(file_path)
    place_marker = if File.read(file_path).match?(/include Hydra::User/)
                     /include Hydra::User/
                   elsif File.read(file_path).match?(/include Blacklight::User/)
                     /include Blacklight::User/
                   end
    if place_marker
      code = "\n  # Connects this user object to Role-management behaviors.\n" \
             "  include Hydra::RoleManagement::UserRoles\n\n"
      inject_into_file file_path, code, after: place_marker
    else
      Rails.logger.error "     \e[31mFailure\e[0m  Hydra::User is not included in #{file_path}.  Add 'include Hydra::User' and rerun."
    end
  else
    Rails.logger.error "     \e[31mFailure\e[0m  hydra-role-management requires a user object. This generators assumes that the model is defined in the file #{file_path}, which does not exist.  If you used a different name, please re-run the generator and provide that name as an argument. Such as \b  rails -g roles client"
  end
end
rails3_attr_accessible() click to toggle source

If this gem is installed under Rails 3, an attr_accessible method is required for the Role model. This file will be added to config/initializers and the correct code will be added to the model at runtime.

# File lib/generators/roles/roles_generator.rb, line 69
def rails3_attr_accessible
  return if ActionController.const_defined? :StrongParameters

  Rails.logger.info 'Role model will include attr_accessible :name because you are installing this gem in a Rails 3 app.'
  copy_file 'hydra_role_management_rails3.rb', 'config/initializers/hydra_role_management_rails3.rb'
end

Private Instance Methods

better_migration_template(file) click to toggle source
# File lib/generators/roles/roles_generator.rb, line 78
def better_migration_template(file)
  sleep 1 # ensure scripts have different time stamps
  migration_template "migrations/#{file}", "db/migrate/#{file}"
rescue StandardError
  Rails.logger.error "  \e[1m\e[34mMigrations\e[0m  " + $ERROR_INFO.message
end