Skip to content

Commit 1621b68

Browse files
committed
Add option to choose belongs_to relationship with roles
1 parent d342a68 commit 1621b68

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lib/generators/authorization/install/install_generator.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ class InstallGenerator < Rails::Generators::Base
77

88
argument :name, type: :string, default: "User"
99
argument :attributes, type: :array, default: ['name:string'], banner: "field[:type] field[:type]"
10-
class_option :create_user, type: :boolean, default: false, desc: "Skips the creation of a new User model. Use if the model already exists."
10+
class_option :create_user, type: :boolean, default: false, desc: "Creates the defined User model with attributes given."
1111
class_option :commit, type: :boolean, default: false, desc: "Performs rake tasks such as migrate and seed."
12+
class_option :user_belongs_to_role, type: :boolean, default: false, desc: "Users have only one role, which can inherit others roles."
1213

1314
def self.next_migration_number dirname
1415
if ActiveRecord::Base.timestamped_migrations
@@ -19,21 +20,25 @@ def self.next_migration_number dirname
1920
end
2021

2122
def install_decl_auth
22-
habtm_table_name = "#{name.pluralize}" <= "Roles" ? "#{name.pluralize}Roles" : "Roles#{name.pluralize}"
23-
habtm_file_glob = "#{name.pluralize}" <= "Roles" ? 'db/migrate/*create_*_roles*' : 'db/migrate/*create_roles_*'
23+
habtm_table_name = "#{name.pluralize}" <= "Roles" ? "#{name.pluralize}Roles" : "Roles#{name.pluralize}" unless options[:user_belongs_to_role]
24+
habtm_file_glob = "#{name.pluralize}" <= "Roles" ? 'db/migrate/*create_*_roles*' : 'db/migrate/*create_roles_*' unless options[:user_belongs_to_role]
2425

2526
generate 'model', "#{name} #{attributes.join(' ')}" if options[:create_user]
2627
generate 'model', 'Role title:string'
2728

28-
generate 'migration', "Create#{habtm_table_name} #{name.downcase}:integer role:integer"
29-
gsub_file Dir.glob(habtm_file_glob).last, 'integer', 'references'
30-
inject_into_file Dir.glob(habtm_file_glob).last, ", id: false", before: ' do |t|'
29+
if options[:user_belongs_to_role]
30+
inject_into_file "app/models/#{name.singularize.downcase}.rb", " belongs_to :role\n", after: "ActiveRecord::Base\n"
31+
else
32+
generate 'migration', "Create#{habtm_table_name} #{name.downcase}:integer role:integer"
33+
gsub_file Dir.glob(habtm_file_glob).last, 'integer', 'references'
34+
inject_into_file Dir.glob(habtm_file_glob).last, ", id: false", before: ' do |t|'
35+
inject_into_file "app/models/role.rb", " has_and_belongs_to_many :#{name.downcase.pluralize}\n", after: "ActiveRecord::Base\n"
36+
inject_into_file "app/models/#{name.singularize.downcase}.rb", " has_and_belongs_to_many :roles\n", after: "ActiveRecord::Base\n"
37+
end
3138

3239
rake 'db:migrate' if options[:commit]
3340

34-
inject_into_file "app/models/role.rb", " has_and_belongs_to_many :#{name.downcase.pluralize}\n", after: "ActiveRecord::Base\n"
3541

36-
inject_into_file "app/models/#{name.singularize.downcase}.rb", " has_and_belongs_to_many :roles\n", after: "ActiveRecord::Base\n"
3742
inject_into_file "app/models/#{name.singularize.downcase}.rb", before: "\nend" do <<-'RUBY'
3843
3944

0 commit comments

Comments
 (0)