Class: Superuser::InitGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/superuser/init_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_base_routeObject



49
50
51
52
53
54
55
56
# File 'lib/generators/superuser/init_generator.rb', line 49

def add_base_route
  path = File.join(destination_root, 'config/routes.rb')
  file_content = File.read(path)
  # if namespace for :superuser don't exists then create it
  unless file_content.include? 'namespace :superuser do'
    route "\tnamespace :superuser do\n\t\troot to: 'dashboard#index'\n\tend\n"
  end
end

#add_entry_to_webpack_native_configObject

in case of user is using webpack_native gem then add an entry pointing to superuser “application” javascript file



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
# File 'lib/generators/superuser/init_generator.rb', line 63

def add_entry_to_webpack_native_config
  if for_webpack_native

    webpack_config_file = "#{Rails.root}/app/webpack_native/webpack.config.js"

    entry_line = "\n\t\t\tsuperuser: './src/javascripts/superuser.js',"

    path = File.join(destination_root, 'app/webpack_native/webpack.config.js')
    file_content = File.read(path)

    if file_content.include? 'entry: {'
      inject_into_file webpack_config_file, entry_line, :after => 'entry: {'
      puts separator_line
      note = "Restart rails server (in case it's running) for updates to take place in webpack.config.js"
      puts "\e[33m#{note}\e[0m"
      puts separator_line
    else
      puts separator_line
      puts "You need to add the following entry to your webpack.config.js, i.e:\n\n"
      entry = "entry: { \n  superuser: './src/javascripts/superuser/application.js',\n  // ...\n}"
      puts "\e[32m#{entry}\e[0m"
      note = "\nNote: do not forget to restart your server after that!"
      puts "\e[33m#{note}\e[0m"
      puts separator_line
    end
  end
end

#generate_base_controllerObject



31
32
33
34
35
36
# File 'lib/generators/superuser/init_generator.rb', line 31

def generate_base_controller
  superuser_base_controller = "app/controllers/superuser/base_controller.rb"
  if !File.exist?(superuser_base_controller)
    copy_file "base_controller.rb", superuser_base_controller
  end
end

#generate_css_fileObject



6
7
8
9
10
11
# File 'lib/generators/superuser/init_generator.rb', line 6

def generate_css_file
  superuser_css_file = "#{stylesheets_folder()}/superuser.scss"
  if !File.exist?(superuser_css_file)
    copy_file "superuser_base.scss", superuser_css_file
  end
end

#generate_dashboard_controllerObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/superuser/init_generator.rb', line 38

def generate_dashboard_controller
  superuser_dashboard_controller = "app/controllers/superuser/dashboard_controller.rb"
  if !File.exist?(superuser_dashboard_controller)
    copy_file "dashboard_controller.rb", superuser_dashboard_controller

    copy_file "views/dashboard_index.html.erb", "app/views/superuser/dashboard/index.html.erb"

    add_layout_links 'app/views/layouts/superuser/application.html.erb', search = '<div class="sidebar_dashboard_link">', "<%= link_to 'dashboard', [:superuser, :root] %>"
  end
end

#generate_js_fileObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/superuser/init_generator.rb', line 13

def generate_js_file
  superuser_js_file = "#{javascripts_folder()}/superuser.js"
  if !File.exist?(superuser_js_file)
    if for_webpack_native
      copy_file "webpack_native_base.js", superuser_js_file
    else
  	  copy_file "superuser_base.js", superuser_js_file
    end
  end
end

#generate_layoutObject



24
25
26
27
28
29
# File 'lib/generators/superuser/init_generator.rb', line 24

def generate_layout
  superuser_layout_file = "app/views/layouts/superuser/application.html.erb"
  if !File.exist?(superuser_layout_file)
    template "views/layouts/application.html.erb", superuser_layout_file
  end
end

#generate_search_formObject



58
59
60
# File 'lib/generators/superuser/init_generator.rb', line 58

def generate_search_form
  template "views/_search.html.erb", "app/views/shared/superuser/_search.html.erb"
end