2011-07-12 17:03:04 -07:00
|
|
|
class ClosetHangersController < ApplicationController
|
2011-07-15 20:39:19 -07:00
|
|
|
before_filter :authorize_user!, :only => [:destroy, :create, :update]
|
|
|
|
before_filter :find_item, :only => [:destroy, :create, :update]
|
2011-07-15 20:14:26 -07:00
|
|
|
|
|
|
|
def destroy
|
2011-07-22 14:55:05 -07:00
|
|
|
raise ActiveRecord::RecordNotFound unless params[:closet_hanger]
|
|
|
|
@closet_hanger = current_user.closet_hangers.find_by_item_id_and_owned!(@item.id, owned)
|
2011-07-15 20:14:26 -07:00
|
|
|
@closet_hanger.destroy
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_after_destroy! }
|
2011-07-15 20:39:19 -07:00
|
|
|
format.json { render :json => true }
|
2011-07-15 20:14:26 -07:00
|
|
|
end
|
|
|
|
end
|
2011-07-14 09:50:24 -07:00
|
|
|
|
2011-07-12 17:03:04 -07:00
|
|
|
def index
|
|
|
|
@user = User.find params[:user_id]
|
2011-07-29 07:52:04 -07:00
|
|
|
@closet_lists_by_owned = @user.closet_lists.alphabetical.
|
|
|
|
includes(:hangers => :item).group_by(&:hangers_owned)
|
|
|
|
@unlisted_closet_hangers_by_owned = @user.closet_hangers.unlisted.
|
|
|
|
owned_before_wanted.alphabetical_by_item_name.includes(:item).
|
|
|
|
group_by(&:owned)
|
2011-07-20 09:39:18 -07:00
|
|
|
@public_perspective = params.has_key?(:public) || !user_is?(@user)
|
2011-07-12 17:03:04 -07:00
|
|
|
end
|
2011-07-14 09:50:24 -07:00
|
|
|
|
2011-07-15 13:15:57 -07:00
|
|
|
# Since the user does not care about the idea of a hanger, but rather the
|
|
|
|
# quantity of an item they own, the user would expect a create form to work
|
|
|
|
# even after the record already exists, and an update form to work even after
|
|
|
|
# the record is deleted. So, create and update are aliased, and both find
|
2011-07-15 20:14:26 -07:00
|
|
|
# the record if it exists or create a new one if it does not. They will even
|
|
|
|
# delete the record if quantity is zero.
|
2011-07-15 13:15:57 -07:00
|
|
|
#
|
|
|
|
# This is kinda a violation of REST. It's not worth breaking user
|
|
|
|
# expectations, though, and I can't really think of a genuinely RESTful way
|
|
|
|
# to pull this off.
|
|
|
|
def update
|
2011-07-22 12:31:23 -07:00
|
|
|
@closet_hanger = current_user.closet_hangers.find_or_initialize_by_item_id_and_owned(@item.id, owned)
|
2011-07-15 13:15:57 -07:00
|
|
|
@closet_hanger.attributes = params[:closet_hanger]
|
|
|
|
|
|
|
|
unless @closet_hanger.quantity == 0 # save the hanger, new record or not
|
|
|
|
if @closet_hanger.save
|
2011-07-15 19:52:53 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2011-07-29 08:25:17 -07:00
|
|
|
message = "Success! You #{@closet_hanger.verb(:you)} #{@closet_hanger.quantity} "
|
|
|
|
message << ((@closet_hanger.quantity > 1) ? @item.name.pluralize : @item.name)
|
2011-07-29 07:52:04 -07:00
|
|
|
message << " in the \"#{@closet_hanger.list.name}\" list" if @closet_hanger.list
|
|
|
|
flash[:success] = "#{message}."
|
2011-07-20 12:16:22 -07:00
|
|
|
redirect_back!(@item)
|
2011-07-15 19:52:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
format.json { render :json => true }
|
|
|
|
end
|
2011-07-15 13:15:57 -07:00
|
|
|
else
|
2011-07-15 19:52:53 -07:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2011-07-22 12:31:23 -07:00
|
|
|
flash[:alert] = "We couldn't save how many of this item you #{@closet_hanger.verb(:you)}: #{@closet_hanger.errors.full_messages.to_sentence}"
|
2011-07-20 12:16:22 -07:00
|
|
|
redirect_back!(@item)
|
2011-07-15 19:52:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
format.json { render :json => {:errors => @closet_hanger.errors.full_messages}, :status => :unprocessable_entity }
|
|
|
|
end
|
2011-07-15 13:15:57 -07:00
|
|
|
end
|
|
|
|
else # delete the hanger since the user doesn't want it
|
2011-07-14 09:50:24 -07:00
|
|
|
@closet_hanger.destroy
|
2011-07-15 19:52:53 -07:00
|
|
|
respond_to do |format|
|
2011-07-15 20:14:26 -07:00
|
|
|
format.html { redirect_after_destroy! }
|
2011-07-14 09:50:24 -07:00
|
|
|
|
2011-07-15 19:52:53 -07:00
|
|
|
format.json { render :json => true }
|
|
|
|
end
|
|
|
|
end
|
2011-07-14 09:50:24 -07:00
|
|
|
end
|
2011-07-15 13:15:57 -07:00
|
|
|
|
|
|
|
alias_method :create, :update
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-07-15 20:14:26 -07:00
|
|
|
def find_item
|
|
|
|
@item = Item.find params[:item_id]
|
|
|
|
end
|
|
|
|
|
2011-07-22 14:55:05 -07:00
|
|
|
def owned
|
|
|
|
owned = true
|
|
|
|
if params[:closet_hanger]
|
|
|
|
owned = case params[:closet_hanger][:owned]
|
|
|
|
when 'true', '1' then true
|
|
|
|
when 'false', '0' then false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-15 20:14:26 -07:00
|
|
|
def redirect_after_destroy!
|
2011-07-22 12:31:23 -07:00
|
|
|
flash[:success] = "Success! You do not #{@closet_hanger.verb(:you)} #{@item.name}."
|
2011-07-20 12:16:22 -07:00
|
|
|
redirect_back!(@item)
|
2011-07-15 19:52:53 -07:00
|
|
|
end
|
2011-07-12 17:03:04 -07:00
|
|
|
end
|
|
|
|
|