Class: Spree::Authentication::CustomGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/spree/authentication/custom/custom_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_pathsObject



8
9
10
11
12
# File 'lib/generators/spree/authentication/custom/custom_generator.rb', line 8

def self.source_paths
  paths = superclass.source_paths
  paths << File.expand_path('templates', __dir__)
  paths.flatten
end

Instance Method Details

#generateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/spree/authentication/custom/custom_generator.rb', line 14

def generate
  template 'authentication_helpers.rb.tt', 'lib/spree/authentication_helpers.rb'

  file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
  send(file_action, 'config/initializers/spree.rb') do
    %Q{
      Rails.application.config.to_prepare do
        require_dependency 'spree/authentication_helpers'
      end\n}
  end

  user_class_file = Rails.root.join('app', 'models', "#{Spree.user_class.name.underscore}.rb")

  if File.exist?(user_class_file)
    inject_into_file user_class_file, after: "class #{Spree.user_class.name} < ApplicationRecord\n" do
      <<-RUBY
    # Spree modules
    include Spree::UserAddress
    include Spree::UserMethods
    include Spree::UserPaymentSource
      RUBY
    end
    say "Successfully added Spree user modules into #{user_class_file}"
  else
    say "Could not locate user model file at #{user_class_file}. Please add these lines manually:", :red
    say <<~RUBY
      # Spree modules
      include Spree::UserAddress
      include Spree::UserMethods
      include Spree::UserPaymentSource
    RUBY
  end
end