Class: RolesGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- RolesGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/roles/roles_generator.rb
Overview
Class definition for the Rails Generator integrating Roles
Class Method Summary collapse
-
.next_migration_number(_path) ⇒ Object
Implement the required interface for Rails::Generators::Migration.
Instance Method Summary collapse
-
#copy_migrations ⇒ Object
Setup the database migrations.
-
#inject_routes ⇒ Object
The engine routes have to come after the devise routes so that /users/sign_in will work.
-
#inject_user_roles_behavior ⇒ Object
Add behaviors to the user model.
-
#rails3_attr_accessible ⇒ Object
If this gem is installed under Rails 3, an attr_accessible method is required for the Role model.
Class Method Details
.next_migration_number(_path) ⇒ Object
Implement the required interface for Rails::Generators::Migration. taken from github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
22 23 24 25 26 27 28 29 |
# 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 |
Instance Method Details
#copy_migrations ⇒ Object
Setup the database migrations
32 33 34 35 36 37 |
# 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 ⇒ Object
The engine routes have to come after the devise routes so that /users/sign_in will work
61 62 63 64 65 |
# 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 ⇒ Object
Add behaviors to the user model
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 ⇒ Object
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.
69 70 71 72 73 74 |
# 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 |