@@ -7,8 +7,9 @@ class InstallGenerator < Rails::Generators::Base
7
7
8
8
argument :name , type : :string , default : "User"
9
9
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 ."
11
11
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."
12
13
13
14
def self . next_migration_number dirname
14
15
if ActiveRecord ::Base . timestamped_migrations
@@ -19,21 +20,25 @@ def self.next_migration_number dirname
19
20
end
20
21
21
22
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 ]
24
25
25
26
generate 'model' , "#{ name } #{ attributes . join ( ' ' ) } " if options [ :create_user ]
26
27
generate 'model' , 'Role title:string'
27
28
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
31
38
32
39
rake 'db:migrate' if options [ :commit ]
33
40
34
- inject_into_file "app/models/role.rb" , " has_and_belongs_to_many :#{ name . downcase . pluralize } \n " , after : "ActiveRecord::Base\n "
35
41
36
- inject_into_file "app/models/#{ name . singularize . downcase } .rb" , " has_and_belongs_to_many :roles\n " , after : "ActiveRecord::Base\n "
37
42
inject_into_file "app/models/#{ name . singularize . downcase } .rb" , before : "\n end" do <<-'RUBY'
38
43
39
44
0 commit comments