Class: Padrino::Generators::AdminApp
- Includes:
- Actions, Padrino::Generators::Admin::Actions, Thor::Actions
- Defined in:
- padrino-admin/lib/padrino-admin/generators/admin_app.rb
Overview
Defines the generator for creating a new admin app.
Class Method Summary collapse
-
.banner ⇒ Object
Defines the “banner” text for the CLI.
-
.source_root ⇒ Object
Define the source template root.
Instance Method Summary collapse
-
#create_admin ⇒ Object
Copies over the Padrino base admin application.
-
#source_paths ⇒ Object
Look for custom template files in a generators folder under the project root.
Methods included from Padrino::Generators::Admin::Actions
#add_project_module, #ext, #fetch_app_name, #orm, #remove_project_module, #supported_ext, #supported_orm
Methods included from Actions
#already_exists?, #app_skeleton, #apply_component_for, #apply_default_fields, #check_app_existence, #destination_root, #empty_directory_with_keep_file, #execute_component_setup, #fetch_app_name, #fetch_component_choice, #fetch_project_name, #in_app_root?, #include_component_module_for, #initializer, #inject_into_file, #insert_hook, #insert_into_gemfile, #insert_middleware, #invalid_fields, #keep_file, #middleware, #recognize_path, #require_contrib, #require_dependencies, #resolve_valid_choice, #retrieve_component_config, #run_bundler, #store_component_choice, #store_component_config, #test?, #tiny?, #valid_choice?, #valid_constant?, #validate_namespace
Class Method Details
.banner ⇒ Object
Defines the “banner” text for the CLI.
13 |
# File 'padrino-admin/lib/padrino-admin/generators/admin_app.rb', line 13 def self.; 'padrino-gen admin'; end |
.source_root ⇒ Object
Define the source template root
11 |
# File 'padrino-admin/lib/padrino-admin/generators/admin_app.rb', line 11 def self.source_root; __dir__; end |
Instance Method Details
#create_admin ⇒ Object
Copies over the Padrino base admin application.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'padrino-admin/lib/padrino-admin/generators/admin_app.rb', line 40 def create_admin self.destination_root = [:root] if in_app_root? unless supported_orm.include?(orm) say "<= At the moment, Padrino only supports #{supported_orm.join(' or ')}. Sorry!", :yellow raise SystemExit end tmp_ext = [:renderer] || fetch_component_choice(:renderer) unless supported_ext.include?(tmp_ext.to_sym) say "<= You are using '#{tmp_ext}' and for admin we only support '#{supported_ext.join(', ')}'. Please use #{supported_ext.map { |ext| "-e #{ext}" }.join(' or ')}", :yellow raise SystemExit end # Get the app's namespace. @app_name = fetch_app_name # setup admin app name @admin_name = [:admin_name].classify @admin_path = [:admin_name].underscore store_component_choice(:admin_renderer, tmp_ext) self.behavior = :revoke if [:destroy] empty_directory destination_root(@admin_path) # Setup Admin Model @model_name = [:admin_model].classify @model_singular = @model_name.underscore @model_plural = @model_singular.pluralize directory 'templates/app', destination_root(@admin_path) directory 'templates/assets', destination_root('public', @admin_path) template 'templates/app.rb.tt', destination_root("#{@admin_path}/app.rb") inject_into_file destination_root('config/apps.rb'), " Padrino.mount('\#{@app_name}::\#{@admin_name}', app_file: Padrino.root('\#{@admin_path}/app.rb')).to('/\#{@admin_path}')\n RUBY\n\n unless options[:destroy]\n insert_middleware 'ConnectionPoolManagement', @admin_path if %i[minirecord activerecord].include?(orm)\n insert_middleware 'IdentityMap', @admin_path if orm == :datamapper\n end\n\n params = [\n @model_singular, 'name:string', 'surname:string', 'email:string', 'crypted_password:string', 'role:string',\n \"-a=\#{options[:models_path]}\",\n \"-r=\#{options[:root]}\"\n ]\n params << '-s' if options[:skip_migration]\n params << '-d' if options[:destroy]\n\n Padrino::Generators::Model.start(params)\n column = Struct.new(:name, :type)\n columns = %i[id name surname email].map { |col| column.new(col) }\n column_fields = [\n { name: :name, field_type: :text_field },\n { name: :surname, field_type: :text_field },\n { name: :email, field_type: :text_field },\n { name: :password, field_type: :password_field },\n { name: :password_confirmation, field_type: :password_field },\n { name: :role, field_type: :text_field }\n ]\n\n unless options[:destroy]\n admin_app = Padrino::Generators::AdminPage.new([@model_singular], root: options[:root], destroy: options[:destroy], admin_model: @model_singular, admin_name: @admin_name)\n admin_app.default_orm = Padrino::Admin::Generators::Orm.new(@model_singular, orm, columns, column_fields)\n admin_app.invoke_all\n end\n\n # TODO: See this, there's something wrong it's not being applied properly or something because test_account_model_generator last test fails.\n template \"templates/account/\#{orm}.rb.tt\", destination_root('models', \"\#{@model_singular}.rb\"), force: true\n\n seed_destination = destination_root('db', 'seeds.rb')\n run \"mv \#{seed_destination} \#{destination_root('db/seeds.old')}\" if File.exist?(seed_destination)\n template 'templates/account/seeds.rb.tt', seed_destination\n\n empty_directory destination_root(\"\#{@admin_path}/controllers\")\n empty_directory destination_root(\"\#{@admin_path}/views\")\n empty_directory destination_root(\"\#{@admin_path}/views/base\")\n empty_directory destination_root(\"\#{@admin_path}/views/layouts\")\n empty_directory destination_root(\"\#{@admin_path}/views/sessions\")\n empty_directory destination_root(\"\#{@admin_path}/views/errors\")\n\n template \"templates/\#{ext}/app/base/index.\#{ext}.tt\", destination_root(\"\#{@admin_path}/views/base/index.\#{ext}\")\n template \"templates/\#{ext}/app/layouts/application.\#{ext}.tt\", destination_root(\"\#{@admin_path}/views/layouts/application.\#{ext}\")\n template \"templates/\#{ext}/app/layouts/error.\#{ext}.tt\", destination_root(\"\#{@admin_path}/views/layouts/error.\#{ext}\")\n template \"templates/\#{ext}/app/sessions/new.\#{ext}.tt\", destination_root(\"\#{@admin_path}/views/sessions/new.\#{ext}\")\n # Custom error.\n template \"templates/\#{ext}/app/errors/403.\#{ext}.tt\", destination_root(\"\#{@admin_path}/views/errors/403.\#{ext}\")\n template \"templates/\#{ext}/app/errors/404.\#{ext}.tt\", destination_root(\"\#{@admin_path}/views/errors/404.\#{ext}\")\n template \"templates/\#{ext}/app/errors/500.\#{ext}.tt\", destination_root(\"\#{@admin_path}/views/errors/500.\#{ext}\")\n\n unless options[:destroy]\n add_project_module @model_plural\n require_dependencies('bcrypt')\n end\n\n require_dependencies 'activesupport', version: '>= 3.1'\n\n # A nicer select box.\n # TODO FIXME This doesn't make much sense in here. Review.\n # gsub_file destination_root(\"admin/views/\#{@model_plural}/_form.\#{ext}\"), \"f.text_field :role, class: :text_field\", \"f.select :role, options: access_control.roles\"\n\n # Destroy account only if not logged in.\n gsub_file destination_root(\"\#{@admin_path}/controllers/\#{@model_plural}.rb\"), \"if \#{@model_singular}.destroy\", \"if \#{@model_singular} != current_account && \#{@model_singular}.destroy\"\n return if self.behavior == :revoke\n\n instructions = []\n instructions << \"Run 'bundle'\"\n if %i[activerecord datamapper sequel].include?(orm)\n instructions << \"Run 'bundle exec rake db:create' if you have not created a database yet\"\n instructions << \"Run 'bundle exec rake db:migrate'\"\n end\n instructions << \"Now repeat after me... 'ohm mani padme hum', 'ohm mani padme hum'... :)\" if orm == :ohm\n instructions << \"Run 'bundle exec rake db:seed'\"\n instructions << \"Visit the admin panel in the browser at '/\#{@admin_path}'\"\n instructions.map! { |i| \" \#{instructions.index(i) + 1}) \#{i}\" }\n\n say\n say '=' * 65, :green\n say 'The admin panel has been mounted! Next, follow these steps:', :green\n say '=' * 65, :green\n say instructions.join(\"\\n\")\n say '=' * 65, :green\n say\n else\n say 'You are not at the root of a Padrino application! (config/boot.rb not found)'\n end\nend\n".prepend("\n"), before: %r{^Padrino.mount.*\.to\('/'\)$} |
#source_paths ⇒ Object
Look for custom template files in a generators folder under the project root.
21 22 23 24 25 26 27 |
# File 'padrino-admin/lib/padrino-admin/generators/admin_app.rb', line 21 def source_paths if File.exist? destination_root('generators', 'templates') [destination_root('generators').to_s, __dir__] else [__dir__] end end |