Class: Kowl::ConfigGenerator

Inherits:
Generators::Base show all
Defined in:
lib/kowl/generators/config_generator.rb

Instance Method Summary collapse

Methods inherited from Generators::Base

default_source_root, source_paths

Methods included from Docker

#alpine_docker_dependencies, #app_js_volumes, #app_volumes, #db_volumes, #debian_database_dependencies, #debian_docker_dependencies, #docker_app_command, #docker_compose_database_string, #docker_databases, #docker_depends_on, #docker_port_watcher, #docker_redis_service, #docker_sidekiq_service, #docker_variables, #docker_volumes, #docker_webpacker_service, #dockerfile_database_args, #dockerfile_migration_snip, #js_volumes, #mysql_volumes, #postgresql_volumes, #redis_volumes

Methods included from Actions

#add_extension_routes, #add_package, #append_to_file, #database_route, #dev_config, #dup_file, #file_exists?, #mailer_gems, #mailer_route, #mk_dir, #move_file, #pry_gems, #rails_cmd, #remove_dir, #remove_file, #remove_gem, #replace_string_in_file, #robocop_test_engine, #sidekiq_route, #template_linter_gems

Instance Method Details

#add_filtered_parametersObject

Add additional parameters to be filtered from logs



155
156
157
# File 'lib/kowl/generators/config_generator.rb', line 155

def add_filtered_parameters
  append_to_file('config/initializers/filter_parameter_logging.rb', 'Rails.application.config.filter_parameters += %i[email ip]')
end

#application_configObject

Generates additional config the appliations config/application.rb file based on the current options (test_engine, template_engine, etc.)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/kowl/generators/config_generator.rb', line 100

def application_config
  # Moves generating of default stylesheets and set sthe default timezone
  # => add configuration to config/application.rb
  # => https://apidock.com/rails/Rails/Generators
  sidekiq_config = (options[:skip_sidekiq] ? '' : 'config.active_job.queue_adapter = :sidekiq')
  cache_storage_config = (options[:skip_sidekiq] ? 'config.cache_store = :mem_cache_store' : "config.cache_storage = :redis_cache_store, { driver: :hiredis, url: ENV.fetch('REDIS_URL'){ 'redis://localhost:6379/0' } }")
  app_config = <<-CONFIG
    # Set the applications timezone
    config.time_zone = 'UTC'
    config.active_record.default_timezone = :local
    #{sidekiq_config}
    #{cache_storage_config}

    #{options[:skip_docker] ? '' : "# If you change this to use imagemagick, you'll need to update your Dockerfile as well"}
    # => https://api.rubyonrails.org/classes/ActiveStorage/Variant.html
    config.active_storage.variant_processor = :vips
  CONFIG
  insert_into_file 'config/application.rb', optimize_indentation(app_config, 4), after: /\s?config.load_defaults\s(\d\S\d)\n/
end

#copy_autoprefixerObject

Setup autoprefixer basic configuration



45
46
47
# File 'lib/kowl/generators/config_generator.rb', line 45

def copy_autoprefixer
  copy_file '../autoprefixer.yml', 'config/autoprefixer.yml', force: true
end

#copy_devise_configObject

Generate application initializer for devise



39
40
41
42
# File 'lib/kowl/generators/config_generator.rb', line 39

def copy_devise_config
  # Encrypt devise passwords using Argon for better password security
  copy_file 'devise_argon2.rb', 'config/initializers/devise_argon2.rb' unless options[:noauth]
end

#copy_faker_configObject

Generate application initializer for faker/applicatiton tests



50
51
52
# File 'lib/kowl/generators/config_generator.rb', line 50

def copy_faker_config
  copy_file('faker.rb', 'config/initializers/faker.rb', force: true) unless options[:skip_tests]
end

#copy_geocoder_configObject

Generate application initializer for geocoder



55
56
57
58
59
60
# File 'lib/kowl/generators/config_generator.rb', line 55

def copy_geocoder_config
  return nil if options[:noauth]

  copy_file('geocoder.rb', 'config/initializers/geocoder.rb')
  GeoDB.perform(Rails.root)
end

#copy_initializersObject

Generate several application initializers to cleanup to make the application easier to manage



63
64
65
66
67
68
# File 'lib/kowl/generators/config_generator.rb', line 63

def copy_initializers
  files = %w[bullet logging lograge oj sass]
  files.push('slim') if options['template_engine'] == 'slim'

  files.map { |file| copy_file "#{file}.rb", "config/initializers/#{file}.rb" }
end

#copy_lockbox_configObject

If wanting to encrypt database attributes, generate lockbox initializer



142
143
144
145
146
# File 'lib/kowl/generators/config_generator.rb', line 142

def copy_lockbox_config
  # Lockbox will be used to secure sensative data attributes throughout the application
  # => https://github.com/ankane/lockbox
  copy_file('lockbox.rb', 'config/initializers/lockbox.rb') if options[:encrypt]
end

#copy_logstop_configObject

Generate a logstop application initializer To filter out sensative parameteres before they are passed to the applications logs



150
151
152
# File 'lib/kowl/generators/config_generator.rb', line 150

def copy_logstop_config
  copy_file 'logstop.rb', 'config/initializers/logstop.rb'
end

#copy_pagy_configObject

Generatet initializer for pagy if pagination will be used in the application



77
78
79
# File 'lib/kowl/generators/config_generator.rb', line 77

def copy_pagy_config
  template('pagy.rb.tt', 'config/initializers/pagy.rb', force: true) unless options[:skip_pagination]
end

#copy_rack_attackObject

Generate initializers for rack_attack depending on the applications requirements (devise, sidekiq, etc)



71
72
73
74
# File 'lib/kowl/generators/config_generator.rb', line 71

def copy_rack_attack
  # it's a template because it has routes that may not be used if --noauth is specified
  template('rack_attack.rb.tt', 'config/initializers/rack_attack.rb')
end

#create_version_fileObject

Generaet version file used for docker application versioning and deployment



82
83
84
# File 'lib/kowl/generators/config_generator.rb', line 82

def create_version_file
  create_file('.version', '0.0.1')
end

#generate_generators_configObject

Generate initializer for the applications generator config



121
122
123
# File 'lib/kowl/generators/config_generator.rb', line 121

def generate_generators_config
  template 'generators.rb.tt', 'config/initializers/generators.rb', force: true
end

#set_production_loglevelObject

This sets the production log_level to info, by default all environments have a log_level of debug. And this can be dangerous when your application logs are collecting user params and showing Mailers within the logs guides.rubyonrails.org/debugging_rails_applications.html#log-levels



94
95
96
97
# File 'lib/kowl/generators/config_generator.rb', line 94

def set_production_loglevel
  inject_into_file('config/environments/production.rb', '  #   ', before: "  config.log_level = :debug\n")
  inject_into_file('config/environments/production.rb', "\n  config.log_level = :info\n", after: 'config.log_level = :debug')
end

#setup_corsObject

Setup a basic CORS policy for application JS interactions



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/kowl/generators/config_generator.rb', line 160

def setup_cors
  cors_str = <<~CORS
    Rails.application.config.content_security_policy do |policy|
      if Rails.env.development?
        policy.script_src :self, :https, :unsafe_eval

        policy.connect_src :self, :https, 'http://localhost:3000', 'ws://localhost:3000', 'http://localhost:3000', 'ws://localhost:3000'
      else
        policy.script_src :self, :https
      end
    end
  CORS
  append_to_file('config/initializers/content_security_policy.rb', cors_str)
end

#setup_erdObject

Run the ERD generator, so the application will generate a new ERB when migrations are added/modified



131
132
133
134
135
# File 'lib/kowl/generators/config_generator.rb', line 131

def setup_erd
  return nil if options[:skip_erd]

  generate('erd:install')
end

#setup_middleware_configObject

Generate initializer for application middleware



126
127
128
# File 'lib/kowl/generators/config_generator.rb', line 126

def setup_middleware_config
  copy_file 'middleware.rb', 'config/initializers/middleware.rb', force: true
end

#setup_simpleformObject

Generate simpleform config early, so rails generators don’t output a ton of simpleform garbage everytime a generator is ran



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kowl/generators/config_generator.rb', line 25

def setup_simpleform
  return unless options[:simpleform]

  case options[:framework]
  when 'bootstrap'
    generate 'simple_form:install', '--bootstrap', '--force'
  when 'semantic'
    copy_file 'simpleform/semantic.rb', 'config/initializers/simple_form.rb', force: true
  else
    generate 'simple_form:install', '--force'
  end
end

#setup_slowpokeObject

Setup Rack::Timeout using slowpoke, in order to kill slow requests



87
88
89
# File 'lib/kowl/generators/config_generator.rb', line 87

def setup_slowpoke
  generate('slowpoke:install')
end