impress/app/controllers/neopets_page_import_tasks_controller.rb
Matchu 4f564db785 Upgrade to Rails 5.2.8.1
Some important little upgrades but mostly straightforward!

Note that there's still a known issue where item searches crash, I was hoping that this was a bug in Rails 4.2 that would be fixed on upgading to 5, but nope, oh well!

Also uhh I just got a bit silly and didn't actually mean to go all the way to 5.2 in one go, I had meant to start at 5.0… but tbh the 5.1 and 5.2 changes seem small, and this seems to be working, so. Yeah ok let's roll!
2023-08-02 16:05:09 -07:00

84 lines
2.8 KiB
Ruby

class NeopetsPageImportTasksController < ApplicationController
include ActionView::Helpers::TextHelper
before_action :authenticate_user!
before_action :require_source, only: [:create]
rescue_from NeopetsPage::ParseError, with: :on_parse_error
rescue_from NeopetsPage::TypeNotFound, with: :not_found
def create
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index].to_i, params[:neopets_page][:source])
@import_task = neopets_page.build_import_task(current_user, params[:neopets_page_import_task][:list_id])
messages = [tt('success', index: neopets_page.index)]
results = @import_task.save
any_created = results[:counts][:created] > 0
any_updated = results[:counts][:updated] > 0
any_unchanged = results[:counts][:unchanged] > 0
if any_created && any_updated
created_msg = tt('created_and_updated_hangers.created_msg',
count: results[:counts][:created])
updated_msg = tt('created_and_updated_hangers.updated_msg',
count: results[:counts][:updated])
messages << tt('created_and_updated_hangers.text',
created_msg: created_msg,
updated_msg: updated_msg)
elsif any_created
messages << tt('created_hangers',
count: results[:counts][:created])
elsif any_updated
messages << tt('updated_hangers',
count: results[:counts][:updated])
elsif any_unchanged
messages << tt('no_changes')
else
messages << tt('no_data')
end
unless results[:unknown_item_names].empty?
messages << tt('unknown_items',
item_names: results[:unknown_item_names].to_sentence,
count: results[:unknown_item_names].size)
end
if neopets_page.last?
messages << tt('done', name: neopets_page.name)
destination = user_closet_hangers_path(current_user)
else
messages << tt('next_page',
next_index: (neopets_page.index + 1))
destination = new_neopets_page_import_task_path(
expected_index: neopets_page.index + 1, list_id: @import_task.list_id)
end
flash[:success] = messages.join(' ')
redirect_to destination
end
def new
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index].to_i, nil)
@import_task = neopets_page.build_import_task(current_user, params[:list_id])
end
def tt(key, params={})
t("neopets_page_import_tasks.create.#{key}", params)
end
def require_source
redirect_to(action: :new) if params[:neopets_page][:source].empty?
end
protected
def on_parse_error(e)
Rails.logger.info "Neopets page parse error: #{e.message}"
flash[:alert] = tt('parse_error')
render action: :new
end
end