Class: MaterialAdminGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- MaterialAdminGenerator
- Includes:
- Rails::Generators::Actions
- Defined in:
- lib/generators/material_admin/material_admin_generator.rb
Constant Summary collapse
- GEM_NAME =
'rails_material_admin'
- GEM_PATH =
Gem.loaded_specs[GEM_NAME].full_gem_path
- JS_PKGS =
'datatables.net-bs5 datatables.net-responsive-bs5'\ ' expose-loader file-loader url-loader resolve-url-loader'\ ' @rails/ujs @rails/activestorage'\ ' stimulus turbolinks'\ ' jquery popper.js select2 @popperjs/core webpack-cli'
- ESLINT_JS_PKGS =
'eslint prettier eslint-plugin-prettier eslint-config-prettier'\ ' stylelint stylelint-config-prettier stylelint-config-recommended stylelint-scss'
- WEBPACKER_DIR_PREFIX =
TODO: customize dir prefix
'javascript'
Instance Method Summary collapse
- #add_gems ⇒ Object
- #at_end ⇒ Object
- #create_base_controller ⇒ Object
- #create_dashboard_controller ⇒ Object
- #create_dashboard_view ⇒ Object
- #create_helper ⇒ Object
- #create_js_manifest ⇒ Object
- #create_layout ⇒ Object
- #create_new_session_file ⇒ Object
- #create_partials ⇒ Object
- #create_sessions_controller ⇒ Object
- #create_style_manifest ⇒ Object
- #import_files ⇒ Object
- #init_db ⇒ Object
- #modify_webpacker_environment_file ⇒ Object
- #need_modify ⇒ Object
- #prompt_confit_route ⇒ Object
- #prompt_extract_css ⇒ Object
- #yarn_install ⇒ Object
Instance Method Details
#add_gems ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 26 def add_gems p 'Add gems...' gem 'slim' gem 'devise' gem 'kaminari' system('bundle install') end |
#at_end ⇒ Object
256 257 258 259 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 256 def at_end p '****************************************************' p '****************************************************' end |
#create_base_controller ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 141 def create_base_controller p "Create base controller'" create_file "#{Rails.root}/app/controllers/#{layout_name}/base_controller.rb" do <<~dashboard_controller # frozen_string_literal: true class #{layout_name.camelize}::BaseController < ActionController::Base layout '#{layout_name}' include DatatableDecorator before_action :authenticate_#{layout_name}! end dashboard_controller end end |
#create_dashboard_controller ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 159 def create_dashboard_controller p 'Create dashboard controller...' create_file "#{Rails.root}/app/controllers/#{layout_name}/dashboard_controller.rb" do <<~dashboard_controller # frozen_string_literal: true class #{layout_name.camelize}::DashboardController < #{layout_name.camelize}::BaseController def index end end dashboard_controller end end |
#create_dashboard_view ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 188 def create_dashboard_view p 'Create dashboard view...' create_file "#{Rails.root}/app/views/#{layout_name}/dashboard/index.html.slim" do <<~dashboard_page h1 Welcome come back🙂 dashboard_page end end |
#create_helper ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 71 def create_helper p "Create #{layout_name} layout helper..." create_file "#{Rails.root}/app/helpers/#{layout_name}_helper.rb" do <<~helper # frozen_string_literal: true module #{layout_name.camelize}Helper def body_id ctrl_name = controller_path.gsub(%r{^v\d+/}, '').tr('/', '-') [ctrl_name, action_name].map(&:dasherize).join('-') end def body_class controller_path.gsub(%r{^v\d+/}, '').tr('/', '-').dasherize end def body_data_controller @body_data_controller ||= controller_name.tr('_', '-').split('/').join('-') end end helper end end |
#create_js_manifest ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 96 def create_js_manifest p "Create #{layout_name} js manifest..." src = "#{GEM_PATH}/vendor/javascript/packs/admin.js" dest = "#{Rails.root}/app/#{WEBPACKER_DIR_PREFIX}/packs/#{layout_name}.js" FileUtils.cp src, dest end |
#create_layout ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 114 def create_layout p "Create #{layout_name} layout..." layout_html_path = "#{Rails.root}/app/views/layouts/#{layout_name}.html.erb" File.rename(layout_html_path, "#{['layout_name']}.html.slim") if File.exist?(layout_html_path) src = "#{GEM_PATH}/app/views/layouts/admin.html.slim" dest = "#{Rails.root}/app/views/layouts/#{layout_name}.html.slim" FileUtils.cp src, dest src = "#{GEM_PATH}/app/views/layouts/unauthorized.html.slim" dest = "#{Rails.root}/app/views/layouts/unauthorized.html.slim" FileUtils.cp src, dest end |
#create_new_session_file ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 198 def create_new_session_file p 'Create sign_in template...' dir = "#{Rails.root}/app/views/#{layout_name}/sessions/" FileUtils.mkdir_p(dir) unless File.directory?(dir) FileUtils.cp( "#{GEM_PATH}/app/views/#{layout_name}/sessions/new.html.slim", "#{Rails.root}/app/views/#{layout_name}/sessions/new.html.slim" ) end |
#create_partials ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 131 def create_partials p "Create #{layout_name} shared partials..." dest = "#{Rails.root}/app/views/#{layout_name}/shared" FileUtils.mkdir_p(dest) unless File.directory?(dest) FileUtils.copy_entry("#{GEM_PATH}/app/views/shared", dest) end |
#create_sessions_controller ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 174 def create_sessions_controller p "Create #{layout_name} sessions controller..." create_file "#{Rails.root}/app/controllers/#{layout_name}/sessions_controller.rb" do <<~sessions_controller # frozen_string_literal: true class Admin::SessionsController < Devise::SessionsController layout 'unauthorized' end sessions_controller end end |
#create_style_manifest ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 105 def create_style_manifest p "Create #{layout_name} style js manifest..." src = "#{GEM_PATH}/vendor/javascript/packs/admin_style.js" dest = "#{Rails.root}/app/#{WEBPACKER_DIR_PREFIX}/packs/#{layout_name}_style.js" FileUtils.cp src, dest end |
#import_files ⇒ Object
36 37 38 39 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 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 36 def import_files p 'Import material admin theme files...' webpacker_dir = "#{Rails.root}/app/#{WEBPACKER_DIR_PREFIX}" FileUtils.mkdir_p("#{webpacker_dir}/vendor") unless File.directory?("#{webpacker_dir}/vendor") FileUtils.copy_entry("#{GEM_PATH}/vendor/#{GEM_NAME}", "#{webpacker_dir}/vendor/#{GEM_NAME}/") # p 'Import package.json...' # FileUtils.cp "#{GEM_PATH}/package.json", "#{Rails.root}/package.json" p 'Import js source files...' FileUtils.copy_entry("#{GEM_PATH}/vendor/javascript", webpacker_dir) p 'Import datatable files...' FileUtils.cp( "#{GEM_PATH}/app/controllers/concerns/datatable_decorator.rb", "#{Rails.root}/app/controllers/concerns/datatable_decorator.rb" ) FileUtils.cp( "#{GEM_PATH}/app/helpers/datatables_helper.rb", "#{Rails.root}/app/helpers/datatables_helper.rb" ) end |
#init_db ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 211 def init_db p 'Init db...' system('rake db:drop db:create') p 'Init devise...' system('rails g devise:install') system("rails generate model #{layout_name}") system("rails generate devise #{layout_name}") system('rake db:migrate') end |
#modify_webpacker_environment_file ⇒ Object
223 224 225 226 227 228 229 230 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 223 def modify_webpacker_environment_file p 'Overwrite config webpack environment js file' src = "#{GEM_PATH}/config/webpack/environment.js" dest = "#{Rails.root}/config/webpack/environment.js" FileUtils.cp src, dest end |
#need_modify ⇒ Object
232 233 234 235 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 232 def need_modify p '****************************************************' p '****************************************************' end |
#prompt_confit_route ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 237 def prompt_confit_route puts "Need change the devise default route to:\n" routes = <<~routes devise_for :#{layout_name}, path: '#{layout_name}', controllers: { sessions: '#{layout_name}/sessions' } namespace :#{layout_name} do root 'dashboard#index' end routes puts routes end |
#prompt_extract_css ⇒ Object
251 252 253 254 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 251 def prompt_extract_css puts "Need set extract_css to true in webpacker.yml\n" p 'extract_css: true' end |
#yarn_install ⇒ Object
66 67 68 69 |
# File 'lib/generators/material_admin/material_admin_generator.rb', line 66 def yarn_install system("yarn add #{JS_PKGS}") system("yarn add #{ESLINT_JS_PKGS} --dev") end |