1
0
Fork 0
forked from OpenNeo/impress
impress/app/controllers/neopets_page_import_tasks_controller.rb

85 lines
2.8 KiB
Ruby
Raw Permalink Normal View History

2015-07-27 10:25:24 -07:00
class NeopetsPageImportTasksController < ApplicationController
include ActionView::Helpers::TextHelper
before_action :authenticate_user!
2015-07-27 10:25:24 -07:00
before_action :require_source, only: [:create]
2015-07-27 10:25:24 -07:00
rescue_from NeopetsPage::ParseError, with: :on_parse_error
2015-07-27 16:36:13 -07:00
rescue_from NeopetsPage::TypeNotFound, with: :not_found
2015-07-27 10:25:24 -07:00
def create
2015-07-27 16:32:59 -07:00
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index].to_i, params[:neopets_page][:source])
2015-07-27 10:25:24 -07:00
@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[:notice] = messages.join(' ')
2015-07-27 10:25:24 -07:00
redirect_to destination
end
def new
2015-07-27 16:32:59 -07:00
neopets_page = NeopetsPage.new(params[:page_type], params[:expected_index].to_i, nil)
2015-07-27 10:25:24 -07:00
@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)
2015-07-27 10:25:24 -07:00
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