Class: Alchemy::Solidus::InstallGenerator

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

Constant Summary collapse

SPREE_MOUNT_REGEXP =
/mount\sSpree::Core::Engine.*$/

Instance Method Summary collapse

Instance Method Details

#copy_alchemy_initializerObject



56
57
58
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 56

def copy_alchemy_initializer
  template "alchemy.rb.tt", "config/initializers/alchemy.rb"
end

#create_admin_userObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 77

def create_admin_user
  if Kernel.const_defined?('Alchemy::Devise') && !options[:skip_alchemy_user_generator] && Alchemy::User.count.zero?
     = ENV.fetch('ALCHEMY_ADMIN_USER_LOGIN', 'admin')
    email = ENV.fetch('ALCHEMY_ADMIN_USER_EMAIL', '[email protected]')
    password = ENV.fetch('ALCHEMY_ADMIN_USER_PASSWORD', 'test1234')
    unless options[:auto_accept]
       = ask("\nEnter the username for the admin user", default: )
      email = ask("Enter the email for the admin user", default: email)
      password = ask("Enter the password for the admin user", default: password)
    end

    Alchemy::User.create!(
      login: ,
      email: email,
      password: password,
      password_confirmation: password,
      alchemy_roles: 'admin',
      spree_roles: [Spree::Role.find_or_create_by!(name: 'admin')]
    )
  end
end

#inject_admin_tabObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 60

def inject_admin_tab
  inject_into_file 'config/initializers/spree.rb', {after: "Spree::Backend::Config.configure do |config|\n"} do
    <<~ADMIN_TAB
      \  # AlchemyCMS admin tabs
      \  config.menu_items << config.class::MenuItem.new(
      \    [:pages, :sites, :languages, :tags, :users, :pictures, :attachments],
      \    'copy',
      \    label: :cms,
      \    condition: -> { can?(:index, :alchemy_admin_dashboard) },
      \    partial: 'spree/admin/shared/alchemy_sub_menu',
      \    url: '/admin/pages',
      \    match_path: '/pages'
      \  )
    ADMIN_TAB
  end
end

#inject_routesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 99

def inject_routes
  routes_file_path = Rails.root.join('config', 'routes.rb')
  mountpoint = '/'
  unless options[:auto_accept]
    mountpoint = ask("\nAt which path do you want to mount AlchemyCMS at?", default: mountpoint)
  end
  if File.read(routes_file_path).match SPREE_MOUNT_REGEXP
    sentinel = SPREE_MOUNT_REGEXP
  else
    sentinel = "Rails.application.routes.draw do\n"
  end
  inject_into_file routes_file_path, {after: sentinel} do
    "\n  mount Alchemy::Engine, at: '/#{mountpoint.chomp('/')}'\n"
  end
end

#run_alchemy_devise_installerObject



37
38
39
40
41
42
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 37

def run_alchemy_devise_installer
  if Kernel.const_defined?('Alchemy::Devise') && !options[:skip_alchemy_devise_installer]
    arguments = options[:auto_accept] ? ['--force'] : []
    Alchemy::Devise::Generators::InstallGenerator.start(arguments)
  end
end

#run_alchemy_installerObject



30
31
32
33
34
35
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 30

def run_alchemy_installer
  unless options[:skip_alchemy_installer]
    arguments = options[:auto_accept] ? ['--skip-demo-files', '--force'] : []
    Alchemy::Generators::InstallGenerator.start(arguments)
  end
end

#run_spree_custom_user_generatorObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 44

def run_spree_custom_user_generator
  if Kernel.const_defined?('Alchemy::Devise') && !options[:skip_spree_custom_user_generator]
    arguments = options[:auto_accept] ? ['Alchemy::User', '--force'] : ['Alchemy::User']
    Spree::CustomUserGenerator.start(arguments)
    gsub_file 'lib/spree/authentication_helpers.rb', /main_app\./, 'Alchemy.'
    if SolidusSupport.solidus_gem_version < Gem::Version.new('2.5.0')
      gsub_file 'config/initializers/spree.rb', /Spree\.user_class.?=.?.+$/, 'Spree.user_class = "Alchemy::User"'
    end
    rake 'db:migrate'
  end
end

#set_root_routeObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 115

def set_root_route
  routes_file_path = Rails.root.join('config', 'routes.rb')
  if options[:auto_accept] || ask("\nDo you want Alchemy to handle the root route ('/')?", default: true)
    if File.read(routes_file_path).match SPREE_MOUNT_REGEXP
      sentinel = SPREE_MOUNT_REGEXP
    else
      sentinel = "Rails.application.routes.draw do\n"
    end
    inject_into_file routes_file_path, {after: sentinel} do
      <<~ROOT_ROUTE
        \n
        \  # Let AlchemyCMS handle the root route
        \  Spree::Core::Engine.routes.draw do
        \    root to: '/alchemy/pages#index'
        \  end
      ROOT_ROUTE
    end
  end
end