basic hanger interface from item

This commit is contained in:
Emi Matchu 2011-07-14 12:50:24 -04:00
parent 46698d4297
commit fab612950a
9 changed files with 115 additions and 4 deletions

View file

@ -1,7 +1,50 @@
class ClosetHangersController < ApplicationController class ClosetHangersController < ApplicationController
before_filter :find_item, :only => [:create, :update]
def create
@closet_hanger = new_hanger
save_hanger!
end
def update
begin
@closet_hanger = @item.closet_hangers.find(params[:id])
@closet_hanger.attributes = params[:closet_hanger]
rescue ActiveRecord::RecordNotFound
# Since updating a hanger is really just changing an item quantity, if
# for some reason this hanger doesn't exist (like if user left a tab
# open), we can still create a new hanger and do the job the user wants
@closet_hanger = new_hanger
end
save_hanger!
end
def index def index
@user = User.find params[:user_id] @user = User.find params[:user_id]
@closet_hangers = @user.closet_hangers.alphabetical_by_item_name.includes(:item) @closet_hangers = @user.closet_hangers.alphabetical_by_item_name.includes(:item)
end end
protected
def find_item
@item = Item.find params[:item_id]
end
def new_hanger
current_user.closet_hangers.find_or_initialize_by_item_id(@item.id, params[:closet_hanger])
end
def save_hanger!
if @closet_hanger.quantity == 0
@closet_hanger.destroy
flash[:success] = "Success! You do not own #{@item.name}."
elsif @closet_hanger.save
flash[:success] = "Success! You own #{@closet_hanger.quantity} #{@item.name.pluralize}."
else
flash[:alert] = "We couldn't save how many of this item you own: #{@closet_hanger.errors.full_messages.to_sentence}"
end
redirect_to @item
end
end end

View file

@ -39,6 +39,10 @@ class ItemsController < ApplicationController
def show def show
@item = Item.find params[:id] @item = Item.find params[:id]
if user_signed_in?
@hanger = current_user.closet_hangers.find_or_initialize_by_item_id(@item.id)
@hanger.quantity ||= 0
end
end end
def needed def needed

View file

@ -2,6 +2,12 @@ class ClosetHanger < ActiveRecord::Base
belongs_to :item belongs_to :item
belongs_to :user belongs_to :user
attr_accessible :quantity
validates :item_id, :uniqueness => {:scope => :user_id}
validates :quantity, :numericality => {:greater_than => 0}
validates_presence_of :item, :user
scope :alphabetical_by_item_name, joins(:item).order(Item.arel_table[:name]) scope :alphabetical_by_item_name, joins(:item).order(Item.arel_table[:name])
end end

View file

@ -76,7 +76,7 @@ $container_width: 800px
input, button, select, label input, button, select, label
cursor: pointer cursor: pointer
input[type=text], input[type=password], input[type=search], select input[type=text], input[type=password], input[type=search], input[type=number], select
+border-radius(3px) +border-radius(3px)
background: #fff background: #fff
border: 1px solid $input-border-color border: 1px solid $input-border-color

View file

@ -62,3 +62,23 @@ body.items-show
.nc-icon .nc-icon
height: 16px height: 16px
width: 16px width: 16px
#closet-hanger-form
border: 1px solid $module-border-color
float: right
font-size: 85%
padding: 1em .5em .5em
width: 18em
label
display: block
font-weight: bold
input[type=number]
width: 4em
a
display: block
margin-top: .5em
text-align: right

View file

@ -10,8 +10,14 @@
= nc_icon_for(@item) = nc_icon_for(@item)
- unless @item.rarity.blank? - unless @item.rarity.blank?
== Rarity: #{@item.rarity_index} (#{@item.rarity}) == Rarity: #{@item.rarity_index} (#{@item.rarity})
%a.button{:href => neoitems_url_for(@item)} NeoItems = link_to 'NeoItems', neoitems_url_for(@item), :class => 'button'
- if @hanger
= form_for([@item, @hanger], :html => {:id => 'closet-hanger-form'}) do |f|
= f.label :quantity, "How many of these do you own?"
= f.number_field :quantity, :min => 0, :required => true
= f.submit "Save"
= link_to 'Your Items &rarr;'.html_safe, user_closet_hangers_path(current_user)
%p= @item.description %p= @item.description
#item-zones #item-zones

View file

@ -19,6 +19,8 @@ OpenneoImpressItems::Application.routes.draw do |map|
collection do collection do
get :needed get :needed
end end
resources :closet_hangers, :only => [:create, :update]
end end
resources :outfits, :only => [:show, :create, :update, :destroy] resources :outfits, :only => [:show, :create, :update, :destroy]
resources :pet_attributes, :only => [:index] resources :pet_attributes, :only => [:index]

View file

@ -0,0 +1,7 @@
class SetClosetHangersQuantityDefaultToZero < ActiveRecord::Migration
def self.up
end
def self.down
end
end

View file

@ -97,7 +97,7 @@ input, button, select, label {
} }
/* line 79, ../../../app/stylesheets/_layout.sass */ /* line 79, ../../../app/stylesheets/_layout.sass */
input[type=text], body.pets-bulk #bulk-pets-form textarea, input[type=password], input[type=search], select { input[type=text], body.pets-bulk #bulk-pets-form textarea, input[type=password], input[type=search], input[type=number], select {
-moz-border-radius: 3px; -moz-border-radius: 3px;
-webkit-border-radius: 3px; -webkit-border-radius: 3px;
background: white; background: white;
@ -106,7 +106,7 @@ input[type=text], body.pets-bulk #bulk-pets-form textarea, input[type=password],
padding: 0.25em; padding: 0.25em;
} }
/* line 85, ../../../app/stylesheets/_layout.sass */ /* line 85, ../../../app/stylesheets/_layout.sass */
input[type=text]:focus, body.pets-bulk #bulk-pets-form textarea:focus, input[type=text]:active, body.pets-bulk #bulk-pets-form textarea:active, input[type=password]:focus, input[type=password]:active, input[type=search]:focus, input[type=search]:active, select:focus, select:active { input[type=text]:focus, body.pets-bulk #bulk-pets-form textarea:focus, input[type=text]:active, body.pets-bulk #bulk-pets-form textarea:active, input[type=password]:focus, input[type=password]:active, input[type=search]:focus, input[type=search]:active, input[type=number]:focus, input[type=number]:active, select:focus, select:active {
color: inherit; color: inherit;
} }
@ -952,6 +952,29 @@ body.items-show .nc-icon {
height: 16px; height: 16px;
width: 16px; width: 16px;
} }
/* line 66, ../../../app/stylesheets/items/_show.sass */
body.items-show #closet-hanger-form {
border: 1px solid #006600;
float: right;
font-size: 85%;
padding: 1em 0.5em 0.5em;
width: 18em;
}
/* line 73, ../../../app/stylesheets/items/_show.sass */
body.items-show #closet-hanger-form label {
display: block;
font-weight: bold;
}
/* line 77, ../../../app/stylesheets/items/_show.sass */
body.items-show #closet-hanger-form input[type=number] {
width: 4em;
}
/* line 80, ../../../app/stylesheets/items/_show.sass */
body.items-show #closet-hanger-form a {
display: block;
margin-top: 0.5em;
text-align: right;
}
@import url(../shared/jquery.jgrowl.css); @import url(../shared/jquery.jgrowl.css);
/* line 112, ../../../app/stylesheets/outfits/_edit.sass */ /* line 112, ../../../app/stylesheets/outfits/_edit.sass */