From a402368c36c680e630bd2e60e9db342276c14ae1 Mon Sep 17 00:00:00 2001 From: gpongelli Date: Thu, 27 Nov 2014 18:13:22 +0100 Subject: [PATCH] Update install_generator.rb Test generator to avoid "Could not find generator authorization:install" error. --- .../install/install_generator.rb | 138 +++++++++--------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/lib/generators/authorization/install/install_generator.rb b/lib/generators/authorization/install/install_generator.rb index f96eea9b..a821ae56 100644 --- a/lib/generators/authorization/install/install_generator.rb +++ b/lib/generators/authorization/install/install_generator.rb @@ -1,77 +1,79 @@ require 'rails/generators' module Authorization - class InstallGenerator < Rails::Generators::Base - - include Rails::Generators::Migration - source_root File.expand_path('../templates', __FILE__) - - argument :name, type: :string, default: "User" - argument :attributes, type: :array, default: ['name:string'], banner: "field[:type] field[:type]" - class_option :create_user, type: :boolean, default: false, desc: "Creates the defined User model with attributes given." - class_option :commit, type: :boolean, default: false, desc: "Performs rake tasks such as migrate and seed." - class_option :user_belongs_to_role, type: :boolean, default: false, desc: "Users have only one role, which can inherit others roles." - - def self.next_migration_number dirname - if ActiveRecord::Base.timestamped_migrations - Time.now.utc.strftime("%Y%m%d%H%M%S") - else - "%.3d" % (current_migration_number(dirname) + 1) + class Generators + class InstallGenerator < Rails::Generators::Base + + include Rails::Generators::Migration + source_root File.expand_path('../templates', __FILE__) + + argument :name, type: :string, default: "User" + argument :attributes, type: :array, default: ['name:string'], banner: "field[:type] field[:type]" + class_option :create_user, type: :boolean, default: false, desc: "Creates the defined User model with attributes given." + class_option :commit, type: :boolean, default: false, desc: "Performs rake tasks such as migrate and seed." + class_option :user_belongs_to_role, type: :boolean, default: false, desc: "Users have only one role, which can inherit others roles." + + def self.next_migration_number dirname + if ActiveRecord::Base.timestamped_migrations + Time.now.utc.strftime("%Y%m%d%H%M%S") + else + "%.3d" % (current_migration_number(dirname) + 1) + end end + + def install_decl_auth + habtm_table_name = "#{name.pluralize}" <= "Roles" ? "#{name.pluralize}Roles" : "Roles#{name.pluralize}" unless options[:user_belongs_to_role] + habtm_file_glob = "#{name.pluralize}" <= "Roles" ? 'db/migrate/*create_*_roles*' : 'db/migrate/*create_roles_*' unless options[:user_belongs_to_role] + + generate 'model', "#{name} #{attributes.join(' ')}" if options[:create_user] + generate 'model', 'Role title:string' + + if options[:user_belongs_to_role] + inject_into_file "app/models/#{name.singularize.downcase}.rb", " belongs_to :role\n", after: "ActiveRecord::Base\n" + generate 'migration', "AddRoleIdTo#{name.camelcase} role_id:integer" + else + generate 'migration', "Create#{habtm_table_name} #{name.downcase}:integer role:integer" + gsub_file Dir.glob(habtm_file_glob).last, 'integer', 'references' + inject_into_file Dir.glob(habtm_file_glob).last, ", id: false", before: ' do |t|' + inject_into_file "app/models/role.rb", " has_and_belongs_to_many :#{name.downcase.pluralize}\n", after: "ActiveRecord::Base\n" + inject_into_file "app/models/#{name.singularize.downcase}.rb", " has_and_belongs_to_many :roles\n", after: "ActiveRecord::Base\n" + end + + rake 'db:migrate' if options[:commit] + + if options[:user_belongs_to_role] + inject_into_file "app/models/#{name.singularize.downcase}.rb", before: "\nend" do <<-'RUBY' + + + def role_symbols + [role.title.to_sym] end - - def install_decl_auth - habtm_table_name = "#{name.pluralize}" <= "Roles" ? "#{name.pluralize}Roles" : "Roles#{name.pluralize}" unless options[:user_belongs_to_role] - habtm_file_glob = "#{name.pluralize}" <= "Roles" ? 'db/migrate/*create_*_roles*' : 'db/migrate/*create_roles_*' unless options[:user_belongs_to_role] - - generate 'model', "#{name} #{attributes.join(' ')}" if options[:create_user] - generate 'model', 'Role title:string' - - if options[:user_belongs_to_role] - inject_into_file "app/models/#{name.singularize.downcase}.rb", " belongs_to :role\n", after: "ActiveRecord::Base\n" - generate 'migration', "AddRoleIdTo#{name.camelcase} role_id:integer" - else - generate 'migration', "Create#{habtm_table_name} #{name.downcase}:integer role:integer" - gsub_file Dir.glob(habtm_file_glob).last, 'integer', 'references' - inject_into_file Dir.glob(habtm_file_glob).last, ", id: false", before: ' do |t|' - inject_into_file "app/models/role.rb", " has_and_belongs_to_many :#{name.downcase.pluralize}\n", after: "ActiveRecord::Base\n" - inject_into_file "app/models/#{name.singularize.downcase}.rb", " has_and_belongs_to_many :roles\n", after: "ActiveRecord::Base\n" - end - - rake 'db:migrate' if options[:commit] - - if options[:user_belongs_to_role] - inject_into_file "app/models/#{name.singularize.downcase}.rb", before: "\nend" do <<-'RUBY' - - - def role_symbols - [role.title.to_sym] - end - RUBY + RUBY + end + else + inject_into_file "app/models/#{name.singularize.downcase}.rb", before: "\nend" do <<-'RUBY' + + + def role_symbols + (roles || []).map {|r| r.title.to_sym} + end + RUBY + end end - else - inject_into_file "app/models/#{name.singularize.downcase}.rb", before: "\nend" do <<-'RUBY' - - - def role_symbols - (roles || []).map {|r| r.title.to_sym} - end - RUBY + + inject_into_file 'db/seeds.rb', after: ".first)\n" do <<-'RUBY' + + roles = Role.create([ + {title: 'admin'}, + {title: 'user'} + ]) if Role.count == 0 + RUBY end + + rake 'db:seed' if options[:commit] + + generate 'authorization:rules' + puts "Please run `rake db:migrate` and `rake db:seed` to finish installing." unless options[:commit] end - - inject_into_file 'db/seeds.rb', after: ".first)\n" do <<-'RUBY' - -roles = Role.create([ - {title: 'admin'}, - {title: 'user'} -]) if Role.count == 0 -RUBY - end - - rake 'db:seed' if options[:commit] - - generate 'authorization:rules' - puts "Please run `rake db:migrate` and `rake db:seed` to finish installing." unless options[:commit] end end -end \ No newline at end of file +end