Class: Kowl::ActionGenerator

Inherits:
Generators::Base show all
Defined in:
lib/kowl/generators/action_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_trix_stylesheetsObject

If using javascript this adds trix’s import to the applications SCSS file



67
68
69
70
71
# File 'lib/kowl/generators/action_generator.rb', line 67

def add_trix_stylesheets
  return '' if options[:skip_javascript]

  append_to_file('app/assets/stylesheets/application.scss', "\n@import 'trix/dist/trix';")
end

#copy_simpleform_inputsObject

Because simpleform hasn’t Actiontext input types yet, attribute type needs to be added in order for an input type to properly load

> github.com/plataformatec/simple_form/issues/1638



17
18
19
20
21
22
23
# File 'lib/kowl/generators/action_generator.rb', line 17

def copy_simpleform_inputs
  return nil unless options[:simpleform]

  # If using simpleform tthis allows you to use rich_text_area input type (trix)
  # ie: `= f.rich_text_area :body`
  directory('app/inputs', 'app/inputs')
end

#correct_oracle_indexesObject

Correct issue with application index if using Oracle for the applications database This is because Oracle can only have an index of 30 characters long So oracle is being used we need to correct the migrations to remove the indexes with huge names



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kowl/generators/action_generator.rb', line 44

def correct_oracle_indexes
  return nil unless options[:database] == 'oracle'

  # Fix - index_active_storage_attachments_uniqueness
  Dir.glob('db/migrate/**_create_active_storage_tables.active_storage.rb').select do |migration_file|
    gsub_file migration_file, 't.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true', 't.index [ :record_type, :record_id, :name, :blob_id ], name: "index_activestorage", unique: true'
  end
  Dir.glob('db/migrate/**_create_action_text_tables.action_text.rb').select do |migration_file|
    gsub_file migration_file, 't.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true', 't.index [ :record_type, :record_id, :name ], name: "index_actiontext", unique: true'
  end
end

#correct_sqlserver_indexesObject

Correct issue with the applications index if using SQLserver for the application database This is because the active_record migration flagges an error for foreign_key constraints for using blob_ids as the index



58
59
60
61
62
63
64
# File 'lib/kowl/generators/action_generator.rb', line 58

def correct_sqlserver_indexes
  return nil unless options[:database] == 'sqlserver'

  Dir.glob('db/migrate/**_create_active_storage_tables.active_storage.rb').select do |migration_file|
    gsub_file migration_file, 't.foreign_key :active_storage_blobs, column: :blob_id', '# t.foreign_key :active_storage_blobs, column: :blob_id'
  end
end

#limit_file_upload_typesObject

Add default ActiveStorage FileType and FileSize limits for uploading through trix (this can easily be changed)

Limit trix mimetyps to jpeg and png
Limit max filesizes to 2MB


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kowl/generators/action_generator.rb', line 76

def limit_file_upload_types
  return nil if options[:skip_javascript]

  trix_action_storage = <<~TRIX_STORAGE
    window.addEventListener('trix-file-accept', function(event) {
      const acceptedTypes = ['image/jpg', 'image/jpeg', 'image/png']
      const maxFileSize = (1024 * 1024) * 2 // 2MB
      // Limit trix file upload types to the mimetypes listed below
      if (!acceptedTypes.includes(event.file.type)) {
        event.preventDefault()
        alert('Only support attachment of jpeg or png files')
      }
      // Limit trix max filesize limits to 2MB
      if (event.file.size > maxFileSize) {
        event.preventDefault()
        alert('Only support of attachment files upto size 2MB!')
      }
    })
  TRIX_STORAGE
  append_to_file('app/javascript/packs/application.js', "\n\n#{trix_action_storage}")
end

#setup_action_textObject

Used to beign setting up action_text based on if options is specified or not



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

def setup_action_text
  # If javascript is being skipped, you'll only want to generate the migrations.
  # => But if you're skipping javascript, you'll need to add actiontext javascript components yourself
  if options[:skip_javascript]
    rake 'action_text:copy_migrations'
  else
    rake 'action_text:install'
  end
end

#setup_active_storageObject

Setup and create ActiveStorage migrations



37
38
39
# File 'lib/kowl/generators/action_generator.rb', line 37

def setup_active_storage
  rake 'active_storage:install'
end