2011-07-12 17:03:04 -07:00
|
|
|
class ClosetHangersController < ApplicationController
|
2011-10-10 19:43:46 -07:00
|
|
|
before_filter :authorize_user!, :only => [:destroy, :create, :update, :update_quantities, :petpage]
|
2011-10-31 15:13:36 -07:00
|
|
|
before_filter :find_item, :only => [:create, :update_quantities]
|
2011-10-10 19:43:46 -07:00
|
|
|
before_filter :find_user, :only => [:index, :petpage, :update_quantities]
|
2011-07-15 20:14:26 -07:00
|
|
|
|
|
|
|
def destroy
|
2011-10-31 15:13:36 -07:00
|
|
|
@closet_hanger = current_user.closet_hangers.find params[:id]
|
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
|
2011-07-30 16:47:04 -07:00
|
|
|
@public_perspective = params.has_key?(:public) || !user_is?(@user)
|
|
|
|
|
2011-07-30 23:48:16 -07:00
|
|
|
find_closet_hangers!
|
2011-07-30 20:03:43 -07:00
|
|
|
|
2011-07-30 21:19:28 -07:00
|
|
|
if @public_perspective && user_signed_in?
|
2011-07-30 20:03:43 -07:00
|
|
|
items = []
|
|
|
|
@closet_lists_by_owned.each do |owned, lists|
|
|
|
|
lists.each do |list|
|
|
|
|
list.hangers.each { |hanger| items << hanger.item }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@unlisted_closet_hangers_by_owned.each do |owned, hangers|
|
|
|
|
hangers.each { |hanger| items << hanger.item }
|
|
|
|
end
|
|
|
|
|
|
|
|
current_user.assign_closeted_to_items!(items)
|
|
|
|
end
|
2011-07-12 17:03:04 -07:00
|
|
|
end
|
2011-07-14 09:50:24 -07:00
|
|
|
|
2011-07-30 23:48:16 -07:00
|
|
|
def petpage
|
|
|
|
@public_perspective = true
|
|
|
|
find_closet_hangers!
|
|
|
|
end
|
|
|
|
|
2011-10-10 19:43:46 -07:00
|
|
|
def create
|
|
|
|
@closet_hanger = current_user.closet_hangers.build(params[:closet_hanger])
|
|
|
|
@closet_hanger.item = @item
|
|
|
|
|
|
|
|
if @closet_hanger.save
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
message = "Success! You #{@closet_hanger.verb(:you)} #{@closet_hanger.quantity} "
|
|
|
|
message << ((@closet_hanger.quantity > 1) ? @item.name.pluralize : @item.name)
|
|
|
|
message << " in the \"#{@closet_hanger.list.name}\" list" if @closet_hanger.list
|
|
|
|
flash[:success] = "#{message}."
|
|
|
|
redirect_back!(@item)
|
|
|
|
}
|
|
|
|
|
|
|
|
format.json { render :json => true }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:alert] = "We couldn't save how many of this item you #{@closet_hanger.verb(:you)}: #{@closet_hanger.errors.full_messages.to_sentence}"
|
|
|
|
redirect_back!(@item)
|
|
|
|
}
|
|
|
|
|
|
|
|
format.json { render :json => {:errors => @closet_hanger.errors.full_messages}, :status => :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-15 13:15:57 -07:00
|
|
|
def update
|
2011-10-10 19:43:46 -07:00
|
|
|
@closet_hanger = current_user.closet_hangers.find(params[:id])
|
2011-07-15 13:15:57 -07:00
|
|
|
@closet_hanger.attributes = params[:closet_hanger]
|
2011-10-10 19:43:46 -07:00
|
|
|
@item = @closet_hanger.item
|
2011-07-15 13:15:57 -07:00
|
|
|
|
|
|
|
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-10-10 19:43:46 -07:00
|
|
|
|
|
|
|
def update_quantities
|
|
|
|
begin
|
|
|
|
ClosetHanger.transaction do
|
|
|
|
params[:quantity].each do |key, quantity|
|
|
|
|
ClosetHanger.set_quantity!(quantity, :user_id => @user.id,
|
|
|
|
:item_id => @item.id, :key => key)
|
|
|
|
end
|
|
|
|
flash[:success] = "Successfully saved how many #{@item.name} you own and want."
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
flash[:alert] = "We couldn't save those quantities. #{e.message}"
|
|
|
|
end
|
|
|
|
redirect_to @item
|
|
|
|
end
|
2011-07-15 13:15:57 -07:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-07-15 20:14:26 -07:00
|
|
|
def find_item
|
|
|
|
@item = Item.find params[:item_id]
|
|
|
|
end
|
|
|
|
|
2011-07-30 23:48:16 -07:00
|
|
|
def find_user
|
2011-07-31 15:45:53 -07:00
|
|
|
if params[:user_id]
|
|
|
|
@user = User.find params[:user_id]
|
2011-07-31 19:17:59 -07:00
|
|
|
elsif user_signed_in?
|
2011-07-31 15:45:53 -07:00
|
|
|
redirect_to user_closet_hangers_path(current_user)
|
2011-07-31 19:17:59 -07:00
|
|
|
else
|
|
|
|
redirect_to login_path(:return_to => request.fullpath)
|
2011-07-31 15:45:53 -07:00
|
|
|
end
|
2011-07-30 23:48:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_closet_hangers!
|
|
|
|
@perspective_user = current_user unless @public_perspective
|
|
|
|
|
|
|
|
@closet_lists_by_owned = @user.closet_lists.
|
|
|
|
alphabetical.includes(:hangers => :item)
|
|
|
|
unless @perspective_user == @user
|
|
|
|
# If we run this when the user matches, we'll end up with effectively:
|
|
|
|
# WHERE belongs_to_user AND (is_public OR belongs_to_user)
|
|
|
|
# and it's a bit silly to put the SQL server through a condition that's
|
|
|
|
# always true.
|
|
|
|
@closet_lists_by_owned = @closet_lists_by_owned.visible_to(@perspective_user)
|
|
|
|
end
|
|
|
|
@closet_lists_by_owned = @closet_lists_by_owned.group_by(&:hangers_owned)
|
|
|
|
|
|
|
|
visible_groups = @user.closet_hangers_groups_visible_to(@perspective_user)
|
|
|
|
unless visible_groups.empty?
|
|
|
|
@unlisted_closet_hangers_by_owned = @user.closet_hangers.unlisted.
|
|
|
|
owned_before_wanted.alphabetical_by_item_name.includes(:item).
|
|
|
|
where(:owned => [visible_groups]).group_by(&:owned)
|
|
|
|
else
|
|
|
|
@unlisted_closet_hangers_by_owned = {}
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|