From 85af53417bf788777fa4ddbb154d6bdf14139b2b Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 22 Jul 2011 15:31:23 -0400 Subject: [PATCH] distinguish between owning and wanting an item --- app/controllers/closet_hangers_controller.rb | 13 +++++---- app/controllers/items_controller.rb | 7 +++-- app/models/closet_hanger.rb | 11 +++++-- app/stylesheets/items/_show.sass | 18 +++++------- app/views/items/show.html.haml | 29 ++++++++++--------- ...10722180616_add_owned_to_closet_hangers.rb | 10 +++++++ db/schema.rb | 5 ++-- public/stylesheets/compiled/screen.css | 26 ++++++++--------- 8 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 db/migrate/20110722180616_add_owned_to_closet_hangers.rb diff --git a/app/controllers/closet_hangers_controller.rb b/app/controllers/closet_hangers_controller.rb index fdc53c07..a082025e 100644 --- a/app/controllers/closet_hangers_controller.rb +++ b/app/controllers/closet_hangers_controller.rb @@ -13,7 +13,7 @@ class ClosetHangersController < ApplicationController def index @user = User.find params[:user_id] - @closet_hangers = @user.closet_hangers.alphabetical_by_item_name.includes(:item) + @closet_hangers = @user.closet_hangers.owned_before_wanted.alphabetical_by_item_name.includes(:item) @public_perspective = params.has_key?(:public) || !user_is?(@user) end @@ -28,14 +28,17 @@ class ClosetHangersController < ApplicationController # expectations, though, and I can't really think of a genuinely RESTful way # to pull this off. def update - @closet_hanger = current_user.closet_hangers.find_or_initialize_by_item_id(@item.id) + owned = (params[:closet_hanger][:owned] == 'true') if params[:closet_hanger] + owned ||= true + + @closet_hanger = current_user.closet_hangers.find_or_initialize_by_item_id_and_owned(@item.id, owned) @closet_hanger.attributes = params[:closet_hanger] unless @closet_hanger.quantity == 0 # save the hanger, new record or not if @closet_hanger.save respond_to do |format| format.html { - flash[:success] = "Success! You own #{@closet_hanger.quantity} #{@item.name.pluralize}." + flash[:success] = "Success! You #{@closet_hanger.verb(:you)} #{@closet_hanger.quantity} #{@item.name.pluralize}." redirect_back!(@item) } @@ -44,7 +47,7 @@ class ClosetHangersController < ApplicationController else respond_to do |format| format.html { - flash[:alert] = "We couldn't save how many of this item you own: #{@closet_hanger.errors.full_messages.to_sentence}" + 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) } @@ -74,7 +77,7 @@ class ClosetHangersController < ApplicationController end def redirect_after_destroy! - flash[:success] = "Success! You do not own #{@item.name}." + flash[:success] = "Success! You do not #{@closet_hanger.verb(:you)} #{@item.name}." redirect_back!(@item) end end diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 37006fd2..aac96188 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -40,8 +40,11 @@ class ItemsController < ApplicationController def show @item = Item.find params[:id] if user_signed_in? - @hanger = current_user.closet_hangers.find_or_initialize_by_item_id(@item.id) - @hanger.quantity ||= 1 + @hangers = [true, false].map do |owned| + hanger = current_user.closet_hangers.find_or_initialize_by_item_id_and_owned(@item.id, owned) + hanger.quantity ||= 1 + hanger + end end end diff --git a/app/models/closet_hanger.rb b/app/models/closet_hanger.rb index 4556c605..8ffb94c7 100644 --- a/app/models/closet_hanger.rb +++ b/app/models/closet_hanger.rb @@ -2,12 +2,19 @@ class ClosetHanger < ActiveRecord::Base belongs_to :item belongs_to :user - attr_accessible :quantity + attr_accessible :owned, :quantity - validates :item_id, :uniqueness => {:scope => :user_id} + validates :item_id, :uniqueness => {:scope => [:user_id, :owned]} validates :quantity, :numericality => {:greater_than => 0} validates_presence_of :item, :user scope :alphabetical_by_item_name, joins(:item).order(Item.arel_table[:name]) + scope :owned_before_wanted, order(arel_table[:owned].desc) + + def verb(subject=:someone) + base = (owned?) ? 'own' : 'want' + base + 's' if subject != :you + base + end end diff --git a/app/stylesheets/items/_show.sass b/app/stylesheets/items/_show.sass index 7bc4d7d6..9f98674a 100644 --- a/app/stylesheets/items/_show.sass +++ b/app/stylesheets/items/_show.sass @@ -63,25 +63,23 @@ body.items-show height: 16px width: 16px - #closet-hanger-form + #closet-hangers border: 1px solid $module-border-color float: right font-size: 85% - padding: 1em .5em .5em + padding: 1em width: 18em label, header display: block font-weight: bold + header + font-size: 125% + + form + padding: .5em 0 + input[type=number] width: 4em - #closet-hanger-form-your-items - display: block - margin-top: .5em - text-align: right - - p - font-family: $main-font - diff --git a/app/views/items/show.html.haml b/app/views/items/show.html.haml index 201982d5..2cb6576c 100644 --- a/app/views/items/show.html.haml +++ b/app/views/items/show.html.haml @@ -12,19 +12,22 @@ == Rarity: #{@item.rarity_index} (#{@item.rarity}) = link_to 'NeoItems', neoitems_url_for(@item), :class => 'button' - - if @hanger - = form_for(@hanger, :url => user_item_closet_hanger_path(current_user, @item), :html => {:id => 'closet-hanger-form'}) do |f| - - if @hanger.new_record? - %header Do you own this item? - %p - We can help you track your closet. - = f.hidden_field :quantity - = f.submit "I own this item!" - - else - = 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 →'.html_safe, user_closet_hangers_path(current_user), :id => 'closet-hanger-form-your-items' + - if @hangers + #closet-hangers + %header + Track this in + = link_to 'Your Items', user_closet_hangers_path(current_user) + - @hangers.each do |hanger| + = form_for(hanger, :url => user_item_closet_hanger_path(current_user, @item)) do |f| + - if hanger.new_record? + = f.hidden_field :quantity + = f.hidden_field :owned + = f.submit "I #{hanger.verb(:you)} this item!" + - else + = f.hidden_field :owned + = f.label :quantity, "How many of these do you #{hanger.verb(:you)}?" + = f.number_field :quantity, :min => 0, :required => true + = f.submit "Save" %p= @item.description #item-zones diff --git a/db/migrate/20110722180616_add_owned_to_closet_hangers.rb b/db/migrate/20110722180616_add_owned_to_closet_hangers.rb new file mode 100644 index 00000000..faa4d4cf --- /dev/null +++ b/db/migrate/20110722180616_add_owned_to_closet_hangers.rb @@ -0,0 +1,10 @@ +class AddOwnedToClosetHangers < ActiveRecord::Migration + def self.up + add_column :closet_hangers, :owned, :boolean, :null => false, :default => true + end + + def self.down + remove_column :closet_hangers, :owned + end +end + diff --git a/db/schema.rb b/db/schema.rb index 6b144a06..05893ef1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110720183722) do +ActiveRecord::Schema.define(:version => 20110722180616) do create_table "auth_servers", :force => true do |t| t.string "short_name", :limit => 10, :null => false @@ -26,6 +26,7 @@ ActiveRecord::Schema.define(:version => 20110720183722) do t.integer "quantity" t.datetime "created_at" t.datetime "updated_at" + t.boolean "owned", :default => true, :null => false end create_table "contributions", :force => true do |t| @@ -177,6 +178,7 @@ ActiveRecord::Schema.define(:version => 20110720183722) do t.boolean "forum_admin", :default => false, :null => false t.boolean "forum_moderator" t.boolean "image_mode_tester", :default => false, :null => false + t.text "closet_description", :null => false t.string "neopets_username" end @@ -188,4 +190,3 @@ ActiveRecord::Schema.define(:version => 20110720183722) do end end - diff --git a/public/stylesheets/compiled/screen.css b/public/stylesheets/compiled/screen.css index 9a3d13da..1b9c0a33 100644 --- a/public/stylesheets/compiled/screen.css +++ b/public/stylesheets/compiled/screen.css @@ -620,7 +620,7 @@ body.closet_hangers-index #closet-hangers-contact { } /* line 38, ../../../app/stylesheets/closet_hangers/_index.sass */ body.closet_hangers-index #closet-hangers-contact a { - background-image: url('/images/neomail.png?1311187553'); + background-image: url('/images/neomail.png?1311362308'); background-position: left center; background-repeat: no-repeat; padding-left: 18px; @@ -1202,31 +1202,29 @@ body.items-show .nc-icon { width: 16px; } /* line 66, ../../../app/stylesheets/items/_show.sass */ -body.items-show #closet-hanger-form { +body.items-show #closet-hangers { border: 1px solid #006600; float: right; font-size: 85%; - padding: 1em 0.5em 0.5em; + padding: 1em; width: 18em; } /* line 73, ../../../app/stylesheets/items/_show.sass */ -body.items-show #closet-hanger-form label, body.items-show #closet-hanger-form header { +body.items-show #closet-hangers label, body.items-show #closet-hangers header { display: block; font-weight: bold; } /* line 77, ../../../app/stylesheets/items/_show.sass */ -body.items-show #closet-hanger-form input[type=number] { - width: 4em; +body.items-show #closet-hangers header { + font-size: 125%; } /* line 80, ../../../app/stylesheets/items/_show.sass */ -body.items-show #closet-hanger-form #closet-hanger-form-your-items { - display: block; - margin-top: 0.5em; - text-align: right; +body.items-show #closet-hangers form { + padding: 0.5em 0; } -/* line 85, ../../../app/stylesheets/items/_show.sass */ -body.items-show #closet-hanger-form p { - font-family: "Droid Sans", Helvetica, Arial, Verdana, sans-serif; +/* line 83, ../../../app/stylesheets/items/_show.sass */ +body.items-show #closet-hangers input[type=number] { + width: 4em; } @import url(../shared/jquery.jgrowl.css); @@ -1807,7 +1805,7 @@ body.outfits-edit .object:hover ul, body.outfits-edit .object:hover .object-info } /* line 418, ../../../app/stylesheets/outfits/_edit.sass */ body.outfits-edit .nc-icon { - background: url('/images/nc.png?1310666180') no-repeat; + background: url('/images/nc.png?1311362308') no-repeat; height: 16px; position: absolute; right: 16px;