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



152
153
154
155
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 152

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

#create_admin_userObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 86

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_routesObject



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

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



60
61
62
63
64
65
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 60

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



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

def run_alchemy_installer
  unless options[:skip_alchemy_installer]
    arguments =
      (
        if options[:auto_accept]
          %w[--skip-demo-files --skip --auto-accept]
        else
          []
        end
      )
    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



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

def run_spree_custom_user_generator
  if alchemy_devise_present? &&
       !options[:skip_spree_custom_user_generator]
    arguments =
      (
        if options[:auto_accept]
          %w[Alchemy::User --force]
        else
          ["Alchemy::User"]
        end
      )
    Spree::CustomUserGenerator.start(arguments)
    gsub_file "lib/spree/authentication_helpers.rb",
              /main_app\./,
              "Alchemy."
    rake("db:migrate", abort_on_failure: true)
  end
end

#set_root_routeObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/generators/alchemy/solidus/install/install_generator.rb', line 136

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")
    rake("alchemy:db:seed", abort_on_failure: true)
  end
end