Class: Infold::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#check_devise_installedObject



7
8
9
10
11
# File 'lib/generators/infold/install_generator.rb', line 7

def check_devise_installed
  initializer_file =
    File.join(Rails.root, "config", "initializers", "devise.rb")
  @devise_installed = File.exist?(initializer_file)
end

#copy_app_filesObject



62
63
64
65
66
67
68
69
# File 'lib/generators/infold/install_generator.rb', line 62

def copy_app_files
  template_path = File.expand_path("templates/install", __dir__)
  Dir.glob("#{template_path}/app/**/*").each do |item|
    next if FileTest.directory?(item) || item.end_with?('admin.scss')
    dist_path = item.to_s.gsub(template_path.to_s, Rails.root.to_s)
    template item, dist_path, encoding: :utf8, ask: true
  end
end

#copy_locale_filesObject



76
77
78
79
80
81
82
# File 'lib/generators/infold/install_generator.rb', line 76

def copy_locale_files
  template_path = File.expand_path("templates/install/config/locales", __dir__)
  Dir.glob("#{template_path}/*") do |item|
    dist_path = Rails.root.join("config/locales", item.split('/').last)
    template item, dist_path, encoding: :utf8, ask: true
  end
end

#create_devise_userObject



24
25
26
# File 'lib/generators/infold/install_generator.rb', line 24

def create_devise_user
  invoke "devise", ["admin_User"]
end

#delete_devise_routesObject



28
29
30
31
# File 'lib/generators/infold/install_generator.rb', line 28

def delete_devise_routes
  routes_file = File.join(destination_root, "config", "routes.rb")
  gsub_file routes_file, /devise_for :admin_users.*$/, ""
end

#edit_devise_configObject



33
34
35
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
# File 'lib/generators/infold/install_generator.rb', line 33

def edit_devise_config
  config_file = File.join(destination_root, "config", "initializers", "devise.rb")
  gsub_file config_file, /# config.scoped_views = false$/, "config.scoped_views = true"
  unless @devise_installed
    gsub_file config_file, /# config.parent_controller = 'DeviseController'$/, "config.parent_controller = 'Admin::TurboDeviseController'"
    gsub_file config_file, "# config.navigational_formats = ['*/*', :html]", "config.navigational_formats = ['*/*', :html, :turbo_stream]"
    gsub_file config_file, "# config.warden do |manager|", "config.warden do |manager|\n    manager.failure_app = TurboFailureApp\n  end"
    code = <<-CODE.gsub(/^\s+/, '')
      # Turbo doesn't work with devise by default.
      # Keep tabs on https://github.com/heartcombo/devise/issues/5446 for a possible fix
      # Fix from https://gorails.com/episodes/devise-hotwire-turbo
      class TurboFailureApp < Devise::FailureApp
      [TAB]def respond
      [TAB][TAB]if request_format == :turbo_stream
      [TAB][TAB][TAB]redirect
      [TAB][TAB]else
      [TAB][TAB][TAB]super
      [TAB][TAB]end
      [TAB]end
      
      [TAB]def skip_format?
      [TAB][TAB]%w(html turbo_stream */*).include? request_format.to_s
      [TAB]end
      end
    CODE
    inject_into_file(config_file, before: "Devise.setup do |config|") { code.gsub('[TAB]', '  ') }
  end
end

#edit_routes_fileObject



71
72
73
74
# File 'lib/generators/infold/install_generator.rb', line 71

def edit_routes_file
  template "config/routes/admin.rb", "config/routes/admin.rb", ask: true
  route "draw(:admin)"
end

#install_admin_usersObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/infold/install_generator.rb', line 84

def install_admin_users
  template "config/infold/admin_user.yml", "config/infold/admin_user.yml", ask: true
  generate 'infold:scaffold', 'admin_user'

  route_file = Rails.root.join('config/routes/admin.rb')
  route = "resources :admin_users"
  in_file = File.readlines(route_file).grep(/^\s+#{route}$/)
  if in_file.blank?
    inject_into_file route_file, after: "namespace 'admin' do" do
      "\n  #{route}"
    end
  end
end

#install_deviseObject



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

def install_devise
  require "devise"

  if @devise_installed
    log :generate, "No need to install devise, already done."
  else
    log :generate, "devise:install"
    invoke "devise:install"
  end
end