2011-08-02 19:42:56 -07:00
|
|
|
class NeopetsPagesController < ApplicationController
|
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
|
|
|
|
before_filter :authenticate_user!, :build_neopets_page
|
|
|
|
|
2013-06-25 23:40:02 -07:00
|
|
|
rescue_from ClosetPage::ParseError, with: :on_parse_error
|
2011-08-02 19:42:56 -07:00
|
|
|
|
|
|
|
def create
|
|
|
|
if @page_params && @page_params[:source]
|
|
|
|
@neopets_page.index = @page_params[:index]
|
2013-04-09 13:50:33 -07:00
|
|
|
@neopets_page.list_id = @page_params[:list_id]
|
2011-08-02 19:42:56 -07:00
|
|
|
@neopets_page.source = @page_params[:source]
|
|
|
|
|
2013-01-01 20:40:37 -08:00
|
|
|
messages = [t('neopets_pages.create.success',
|
2013-06-25 23:40:02 -07:00
|
|
|
index: @neopets_page.index)]
|
2011-08-02 19:42:56 -07:00
|
|
|
|
2013-01-01 20:40:37 -08:00
|
|
|
saved_counts = @neopets_page.save_hangers!
|
2011-08-02 19:42:56 -07:00
|
|
|
any_created = saved_counts[:created] > 0
|
|
|
|
any_updated = saved_counts[:updated] > 0
|
2013-01-01 20:40:37 -08:00
|
|
|
if any_created && any_updated
|
|
|
|
created_msg = t('neopets_pages.create.created_and_updated_hangers.created_msg',
|
2013-06-25 23:40:02 -07:00
|
|
|
count: saved_counts[:created])
|
2013-01-01 20:40:37 -08:00
|
|
|
updated_msg = t('neopets_pages.create.created_and_updated_hangers.updated_msg',
|
2013-06-25 23:40:02 -07:00
|
|
|
count: saved_counts[:updated])
|
2013-01-01 20:40:37 -08:00
|
|
|
messages << t('neopets_pages.create.created_and_updated_hangers.text',
|
2013-06-25 23:40:02 -07:00
|
|
|
created_msg: created_msg,
|
|
|
|
updated_msg: updated_msg)
|
2013-01-01 20:40:37 -08:00
|
|
|
elsif any_created
|
|
|
|
messages << t('neopets_pages.create.created_hangers',
|
2013-06-25 23:40:02 -07:00
|
|
|
count: saved_counts[:created])
|
2013-01-01 20:40:37 -08:00
|
|
|
elsif any_updated
|
|
|
|
messages << t('neopets_pages.create.updated_hangers',
|
2013-06-25 23:40:02 -07:00
|
|
|
count: saved_counts[:updated])
|
2013-01-01 20:40:37 -08:00
|
|
|
elsif @neopets_page.hangers.size > 1 # saw items, but at same quantities
|
|
|
|
messages << t('neopets_pages.create.no_changes')
|
|
|
|
else # no items recognized
|
|
|
|
messages << t('neopets_pages.create.no_data')
|
2011-08-02 19:42:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
unless @neopets_page.unknown_item_names.empty?
|
2013-01-01 20:40:37 -08:00
|
|
|
messages << t('neopets_pages.create.unknown_items',
|
2013-06-25 23:40:02 -07:00
|
|
|
item_names: @neopets_page.unknown_item_names.to_sentence,
|
|
|
|
count: @neopets_page.unknown_item_names.size)
|
2011-08-02 19:42:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if @neopets_page.last?
|
2013-06-25 23:40:02 -07:00
|
|
|
messages << t('neopets_pages.create.done', name: @neopets_page.name)
|
2011-08-02 19:42:56 -07:00
|
|
|
destination = user_closet_hangers_path(current_user)
|
|
|
|
else
|
2013-01-01 20:40:37 -08:00
|
|
|
messages << t('neopets_pages.create.next_page',
|
2013-06-25 23:40:02 -07:00
|
|
|
next_index: (@neopets_page.index + 1))
|
2013-04-09 13:50:33 -07:00
|
|
|
destination = {action: :new, index: @neopets_page.index + 1,
|
|
|
|
list_id: @neopets_page.list_id}
|
2011-08-02 19:42:56 -07:00
|
|
|
end
|
|
|
|
|
2013-01-01 20:40:37 -08:00
|
|
|
flash[:success] = messages.join(' ')
|
2011-08-02 19:42:56 -07:00
|
|
|
redirect_to destination
|
|
|
|
else
|
2013-06-25 23:40:02 -07:00
|
|
|
redirect_to action: :new
|
2011-08-02 19:42:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@neopets_page.index ||= 1
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
TYPES = {
|
|
|
|
'closet' => ClosetPage,
|
|
|
|
'sdb' => SafetyDepositPage
|
|
|
|
}
|
|
|
|
def build_neopets_page
|
|
|
|
type_class = TYPES[params[:type]]
|
|
|
|
|
|
|
|
@neopets_page = type_class.new(current_user)
|
|
|
|
@neopets_page.index = params[:index]
|
2013-04-09 13:50:33 -07:00
|
|
|
@neopets_page.list_id = params[:list_id]
|
2011-08-02 19:42:56 -07:00
|
|
|
@page_params = params[type_class.model_name.singular]
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_parse_error(e)
|
|
|
|
Rails.logger.info "Neopets page parse error: #{e.message}"
|
2013-01-01 20:40:37 -08:00
|
|
|
flash[:alert] = t('neopets_pages.create.parse_error')
|
2013-06-25 23:40:02 -07:00
|
|
|
render action: :new
|
2011-08-02 19:42:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|