closet page saving backend
This commit is contained in:
parent
d5641dddbb
commit
6ebacc99dd
7 changed files with 121 additions and 5 deletions
63
app/controllers/closet_pages_controller.rb
Normal file
63
app/controllers/closet_pages_controller.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
class ClosetPagesController < ApplicationController
|
||||
include ActionView::Helpers::TextHelper
|
||||
|
||||
before_filter :authenticate_user!, :build_closet_page
|
||||
|
||||
rescue_from ClosetPage::ParseError, :with => :on_parse_error
|
||||
|
||||
def create
|
||||
if params[:closet_page] && params[:closet_page][:source]
|
||||
@closet_page.source = params[:closet_page][:source]
|
||||
saved_counts = @closet_page.save_hangers!
|
||||
|
||||
any_created = saved_counts[:created] > 0
|
||||
any_updated = saved_counts[:updated] > 0
|
||||
if any_created || any_updated
|
||||
message = "Page #{@closet_page.index} saved! We "
|
||||
message << "added " + pluralize(saved_counts[:created], 'item') + " to your closet" if any_created
|
||||
message << " and " if any_created && any_updated
|
||||
message << "updated the count on " + pluralize(saved_counts[:updated], 'item') if any_updated
|
||||
message << ". "
|
||||
else
|
||||
message = "Success! We checked that page, and we already had all this data recorded. "
|
||||
end
|
||||
|
||||
unless @closet_page.unknown_item_names.empty?
|
||||
message << "We also found " +
|
||||
pluralize(@closet_page.unknown_item_names.size, 'item') +
|
||||
" we didn't recognize: " +
|
||||
@closet_page.unknown_item_names.to_sentence +
|
||||
". Please put each item on your pet and type its name in on the " +
|
||||
"home page so we can have a record of it. Thanks! "
|
||||
end
|
||||
|
||||
if @closet_page.last?
|
||||
message << "That was the last page of your Neopets closet."
|
||||
destination = user_closet_hangers_path(current_user)
|
||||
else
|
||||
message << "Let's move onto the next page!"
|
||||
destination = {:action => :new, :page => (@closet_page.index + 1)}
|
||||
end
|
||||
|
||||
flash[:success] = message
|
||||
redirect_to destination
|
||||
else
|
||||
redirect_to :action => :new
|
||||
end
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def build_closet_page
|
||||
@closet_page = ClosetPage.new(current_user)
|
||||
end
|
||||
|
||||
def on_parse_error
|
||||
flash[:alert] = "We had trouble reading your source code. Is it a valid HTML document? Make sure you pasted the computery-looking result of right-click > View Source, and not the pretty page itself."
|
||||
render :action => :new
|
||||
end
|
||||
end
|
||||
|
2
app/helpers/closet_pages_helper.rb
Normal file
2
app/helpers/closet_pages_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module ClosetPagesHelper
|
||||
end
|
|
@ -1,6 +1,9 @@
|
|||
require 'yaml'
|
||||
|
||||
class ClosetPage
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
|
||||
SELECTORS = {
|
||||
:items => "form[action=\"process_closet.phtml\"] tr[bgcolor!=silver][bgcolor!=\"#E4E4E4\"]",
|
||||
:item_thumbnail => "img",
|
||||
|
@ -11,18 +14,39 @@ class ClosetPage
|
|||
:selected => "option[selected]"
|
||||
}
|
||||
|
||||
attr_reader :hangers, :index, :total_pages, :unknown_item_names
|
||||
attr_reader :hangers, :index, :source, :total_pages, :unknown_item_names, :user
|
||||
|
||||
def initialize(user)
|
||||
raise ArgumentError, "Expected #{user.inspect} to be a User", caller unless user.is_a?(User)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def last?
|
||||
@index == @total_pages
|
||||
end
|
||||
|
||||
def persisted?
|
||||
false
|
||||
end
|
||||
|
||||
def save_hangers!
|
||||
@hangers.each(&:save!)
|
||||
counts = {:created => 0, :updated => 0}
|
||||
ClosetHanger.transaction do
|
||||
@hangers.each do |hanger|
|
||||
if hanger.new_record?
|
||||
counts[:created] += 1
|
||||
hanger.save!
|
||||
elsif hanger.changed?
|
||||
counts[:updated] += 1
|
||||
hanger.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
counts
|
||||
end
|
||||
|
||||
def source=(source)
|
||||
@source = source
|
||||
parse_source!(source)
|
||||
end
|
||||
|
||||
|
@ -42,7 +66,7 @@ class ClosetPage
|
|||
|
||||
page_selector = element(:page_select, doc)
|
||||
@total_pages = page_selector.children.size
|
||||
@index = element(:selected, page_selector)['value']
|
||||
@index = element(:selected, page_selector)['value'].to_i
|
||||
|
||||
items_data = {
|
||||
:id => {},
|
||||
|
@ -89,8 +113,7 @@ class ClosetPage
|
|||
@hangers = items.map do |item|
|
||||
data = items_data[:id].delete(item.id) ||
|
||||
items_data[:thumbnail_url].delete(item.thumbnail_url)
|
||||
hanger = @user.closet_hangers.build
|
||||
hanger.item = item
|
||||
hanger = @user.closet_hangers.find_or_initialize_by_item_id(item.id)
|
||||
hanger.quantity = data[:quantity]
|
||||
hanger
|
||||
end
|
||||
|
|
5
app/views/closet_pages/new.html.haml
Normal file
5
app/views/closet_pages/new.html.haml
Normal file
|
@ -0,0 +1,5 @@
|
|||
= form_for(@closet_page) do |f|
|
||||
= f.label :source
|
||||
= f.text_area :source
|
||||
= f.submit 'Add items to closet'
|
||||
|
|
@ -24,6 +24,8 @@ OpenneoImpressItems::Application.routes.draw do |map|
|
|||
resources :pet_attributes, :only => [:index]
|
||||
resources :swf_assets, :only => [:index, :show]
|
||||
|
||||
resources :closet_pages, :only => [:new, :create], :path => 'closet/pages'
|
||||
|
||||
match '/users/current-user/outfits' => 'outfits#index', :as => :current_user_outfits
|
||||
|
||||
match '/pets/load' => 'pets#load', :method => :post, :as => :load_pet
|
||||
|
@ -37,6 +39,7 @@ OpenneoImpressItems::Application.routes.draw do |map|
|
|||
resources :contributions, :only => [:index]
|
||||
resources :closet_hangers, :only => [:index], :path => 'closet'
|
||||
end
|
||||
|
||||
match 'users/top-contributors' => 'users#top_contributors', :as => :top_contributors
|
||||
match 'users/top_contributors' => redirect('/users/top-contributors')
|
||||
|
||||
|
|
5
spec/controllers/closet_pages_controller_spec.rb
Normal file
5
spec/controllers/closet_pages_controller_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe ClosetPagesController do
|
||||
|
||||
end
|
15
spec/helpers/closet_pages_helper_spec.rb
Normal file
15
spec/helpers/closet_pages_helper_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
require 'spec_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the ClosetPagesHelper. For example:
|
||||
#
|
||||
# describe ClosetPagesHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# helper.concat_strings("this","that").should == "this that"
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
describe ClosetPagesHelper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in a new issue