From 019303031ba2b9168a02920e0d6d257a81100744 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 17 Aug 2013 12:07:04 -0400 Subject: [PATCH] choose list when importing from pets --- app/controllers/neopets_users_controller.rb | 1 + app/models/neopets_user.rb | 31 +++++++++++++++++++-- app/views/neopets_users/new.html.haml | 1 + 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/controllers/neopets_users_controller.rb b/app/controllers/neopets_users_controller.rb index 265ab4f3..d9bf8e83 100644 --- a/app/controllers/neopets_users_controller.rb +++ b/app/controllers/neopets_users_controller.rb @@ -9,6 +9,7 @@ class NeopetsUsersController < ApplicationController def create @neopets_user.username = params[:neopets_user][:username] + @neopets_user.list_id = params[:neopets_user][:list_id] @neopets_user.load! @neopets_user.save_hangers! diff --git a/app/models/neopets_user.rb b/app/models/neopets_user.rb index b26e0764..27a8bada 100644 --- a/app/models/neopets_user.rb +++ b/app/models/neopets_user.rb @@ -5,12 +5,27 @@ class NeopetsUser extend ActiveModel::Naming attr_accessor :username - attr_reader :hangers + attr_reader :hangers, :list_id def initialize(app_user) @app_user = app_user end + def list_id=(list_id) + # TODO: DRY up with ClosetPage + @list_id = list_id + if list_id == 'true' + @closet_list = nil + @hangers_owned = true + elsif list_id == 'false' + @closet_list = nil + @hangers_owned = false + elsif list_id.present? + @closet_list = @app_user.closet_lists.find(list_id) + @hangers_owned = @closet_list.hangers_owned? + end + end + def load! user = Neopets::User.new(@username) @@ -23,19 +38,29 @@ class NeopetsUser pets = pets.map { |pet| Pet.find_or_initialize_by_name(pet.name) } items = pets.each(&:load!).map(&:items).flatten item_ids = items.map(&:id) - existing_hanger_item_ids = @app_user.closet_hangers.select(:item_id).where(:item_id => item_ids).map(&:item_id) item_quantities = {} items.each do |i| item_quantities[i] ||= 0 item_quantities[i] += 1 end + # TODO: DRY up with ClosetPage + # We don't want to insert duplicate hangers of what a user owns if they + # already have it in another list (e.g. imports to Items You Own *after* + # curating their Up For Trade list), so we check for the hanger's presence + # in *all* items the user owns or wants (whichever is appropriate for this + # request). + hangers_scope = @app_user.closet_hangers.where(owned: @hangers_owned) + existing_hanger_item_ids = hangers_scope.select(:item_id). + where(item_id: item_ids).map(&:item_id) + @hangers = [] item_quantities.each do |item, quantity| next if existing_hanger_item_ids.include?(item.id) - hanger = @app_user.closet_hangers.build + hanger = hangers_scope.build hanger.item = item hanger.quantity = quantity + hanger.list = @closet_list @hangers << hanger end end diff --git a/app/views/neopets_users/new.html.haml b/app/views/neopets_users/new.html.haml index 9fc52c6e..c6a43cd4 100644 --- a/app/views/neopets_users/new.html.haml +++ b/app/views/neopets_users/new.html.haml @@ -7,5 +7,6 @@ = f.label :username, t('.username_label') = f.text_field :username + = f.select :list_id, neopets_page_list_options(current_user) = f.submit t('.submit')