Class: Spree::DummyGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/spree/dummy/dummy_generator.rb

Constant Summary collapse

SPREE_GEMS =
%w(spree_backend spree_frontend spree_api spree_emails).freeze
PASSTHROUGH_OPTIONS =
[
  :skip_active_record, :skip_javascript, :database, :javascript, :quiet, :pretend, :force, :skip
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



106
107
108
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 106

def database
  @database
end

#lib_nameObject (readonly)

Returns the value of attribute lib_name.



105
106
107
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 105

def lib_name
  @lib_name
end

Class Method Details

.source_pathsObject



14
15
16
17
18
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 14

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

Instance Method Details

#clean_upObject



20
21
22
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 20

def clean_up
  remove_directory_if_exists(dummy_path)
end

#generate_test_dummyObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 28

def generate_test_dummy
  # calling slice on a Thor::CoreExtensions::HashWithIndifferentAccess
  # object has been known to return nil
  opts = {}.merge(options).slice(*PASSTHROUGH_OPTIONS)
  opts[:database] = 'postgresql' if opts[:database].blank?
  opts[:force] = true
  opts[:skip_bundle] = true
  opts[:skip_gemfile] = true
  opts[:skip_git] = true
  opts[:skip_listen] = true
  opts[:skip_rc] = true
  opts[:skip_spring] = true
  opts[:skip_test] = true
  opts[:skip_bootsnap] = true

  puts 'Generating dummy Rails application...'
  invoke Rails::Generators::AppGenerator,
    [File.expand_path(dummy_path, destination_root)], opts
  inject_yaml_permitted_classes
end

#inject_content_security_policyObject



97
98
99
100
101
102
103
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 97

def inject_content_security_policy
  inside dummy_path do
    inject_into_file 'config/initializers/content_security_policy.rb', %Q[
  p.script_src  :self, :https, :unsafe_inline, :http, :unsafe_eval
    ], before: /^end/, verbose: true
  end
end

#test_dummy_cleanObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 80

def test_dummy_clean
  inside dummy_path do
    remove_file '.gitignore'
    remove_file 'doc'
    remove_file 'Gemfile'
    remove_file 'lib/tasks'
    remove_file 'app/assets/images/rails.png'
    remove_file 'app/assets/javascripts/application.js'
    remove_file 'public/index.html'
    remove_file 'public/robots.txt'
    remove_file 'README'
    remove_file 'test'
    remove_file 'vendor'
    remove_file 'spec'
  end
end

#test_dummy_configObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 49

def test_dummy_config
  @lib_name = options[:lib_name]
  @database = options[:database]

  template 'rails/database.yml', "#{dummy_path}/config/database.yml", force: true
  template 'rails/boot.rb', "#{dummy_path}/config/boot.rb", force: true
  template 'rails/application.rb', "#{dummy_path}/config/application.rb", force: true
  template 'rails/routes.rb', "#{dummy_path}/config/routes.rb", force: true
  template 'rails/test.rb', "#{dummy_path}/config/environments/test.rb", force: true
  template 'rails/script/rails', "#{dummy_path}/spec/dummy/script/rails", force: true
  template 'initializers/devise.rb', "#{dummy_path}/config/initializers/devise.rb", force: true

  if lib_name == 'spree/backend'
    template 'package.json', "#{dummy_path}/package.json", force: true
  end
end

#test_dummy_inject_extension_requirementsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/spree/dummy/dummy_generator.rb', line 66

def test_dummy_inject_extension_requirements
  if DummyGeneratorHelper.inject_extension_requirements
    SPREE_GEMS.each do |gem|
      begin
        require "#{gem}"
        inside dummy_path do
          inject_require_for(gem)
        end
      rescue StandardError, LoadError
      end
    end
  end
end