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

#append_assetsObject



139
140
141
142
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 139

def append_assets
  append_file "vendor/assets/javascripts/alchemy/admin/all.js",
    "//= require alchemy/solidus/admin.js"
end

#create_admin_userObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 82

def create_admin_user
  if alchemy_devise_present? && !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

    # This is a bit strange, but without the double save this fails with a failed validation.
    Alchemy::User.create!(
      login: ,
      email: email,
      password: password,
      password_confirmation: password,
      alchemy_roles: 'admin'
    ).tap do |user|
      user.spree_roles = [Spree::Role.find_or_create_by!(name: 'admin')]
      user.save!
    end
  end
end

#inject_admin_tabObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 65

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 107

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



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

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

#run_alchemy_installerObject



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

def run_alchemy_installer
  unless options[:skip_alchemy_installer]
    arguments = options[:auto_accept] ? ['--skip-demo-files', '--force', '--auto-accept'] : []
    Alchemy::Generators::InstallGenerator.start(arguments)
    rake('railties:install:migrations', abort_on_failure: true)
    rake('db:migrate', abort_on_failure: true)
  end
end

#run_spree_custom_user_generatorObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 53

def run_spree_custom_user_generator
  if alchemy_devise_present? && !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', abort_on_failure: true)
  end
end

#set_root_routeObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 123

def set_root_route
  routes_file_path = Rails.root.join('config', 'routes.rb')
  if options[:auto_accept] || yes?("\nDo you want Alchemy to handle the root route '/'? (y/n)")
    sentinel = "Rails.application.routes.draw do\n"
    inject_into_file routes_file_path, {after: sentinel} do
      <<~ROOT_ROUTE
        \  # Let AlchemyCMS handle the root route
        \  root to: 'alchemy/pages#index'
      ROOT_ROUTE
    end
    copy_file('db/seeds/alchemy/pages.yml')
    append_file(Rails.root.join('db', 'seeds.rb'), "Alchemy::Seeder.seed!\n")
    rake('alchemy:db:seed', abort_on_failure: true)
  end
end