# Terminal rails active_storage:set up rails g scaffold nations title flag:attachment rails g controller nations/imports rails g job nations/import rails g job International locations::CreateRecord bundle add solid_queue rails g solid_queue:set up bundle exec rake solid_queue:begin # needs to be added to the Procfile.dev
# Gemfile gem "image_processing", "~> 1.2" gem "solid_queue", "~> 0.3.4"
# config/routes.rb assets :nations namespace :nations do useful resource :imports, solely: [:new, :create] finish
# app/controllers/nations/imports_controller.rb
class International locations::ImportsController < ApplicationController
def new
finish
def create
file = params[:file]
blob = ActiveStorage::Blob.create_and_upload!(
io: file,
filename: file.original_filename,
content_type: file.content_type
)
International locations::ImportJob.perform_later(blob.signed_id)
redirect_to countries_path, discover: "International locations are being imported..."
finish
finish
# app/views/nations/imports/new.html.erb
<%= form_with url: countries_imports_path do |f| %>
<div class="mb-3">
<%= f.label :file, class: "form-label" %>
<%= f.file_field :file, class: "form-control" %>
</div>
<%= f.submit "Import", class: "btn btn-primary" %>
<% finish %>
# app/views/welcome/index.html.erb <%= link_to "Import International locations", new_countries_imports_path %>
# app/jobs/nations/import_job.rb
require "csv"
class International locations::ImportJob < ApplicationJob
queue_as :default
def carry out(blob_signed_id)
blob = ActiveStorage::Blob.find_signed(blob_signed_id)
blob.obtain do |information|
encoded_data = information.force_encoding("ASCII-8BIT").encode("UTF-8", invalid: :exchange, undef: :exchange, exchange: "?")
CSV.parse(encoded_data, headers: true) do |row|
International locations::CreateRecordJob.perform_later(
title: row["Country"],
flag_url: row["Flag URL"]
)
finish
finish
guarantee
blob.purge_later
finish
finish
# app/jobs/nations/create_record_job.rb
require "open-uri"
class International locations::CreateRecordJob < ApplicationJob
queue_as :default
def carry out(title:, flag_url:)
nation = Nation.find_or_initialize_by(title: title)
nation.flag.connect(
io: URI.open(flag_url),
filename: File.basename(URI.parse(flag_url).path)
)
nation.save!
finish
finish
# config/utility.rb config.active_job.queue_adapter = :solid_queue
# config/solid_queue.yml
default: &default
dispatchers:
- polling_interval: 1
batch_size: 500
employees:
- queues: "*"
threads: 1
processes: 1
polling_interval: 0.1
improvement:
<<: *default
check:
<<: *default
manufacturing:
<<: *default
# app/views/nations/index.html.erb <%= image_tag nation.flag.variant(resize_to_limit: [30, 30]) if nation.flag.hooked up? && nation.flag.picture? %>

