From 30d954281fa6bc07303894a902c2917098b12ab7 Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Mon, 1 Oct 2012 11:33:12 -0400 Subject: [PATCH 1/7] refactoring --- app/contexts/bidding.rb | 87 ++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/app/contexts/bidding.rb b/app/contexts/bidding.rb index 5aba28e..b5a9f98 100644 --- a/app/contexts/bidding.rb +++ b/app/contexts/bidding.rb @@ -17,54 +17,73 @@ def self.buy user, bid_params, listener bidding.bid end - attr_reader :bidder, :biddable_auction, :bid_creator, :listener + attr_reader :bidder, :biddable, :validator, :request, :listener def initialize user, auction, bid_params, listener @bidder = user.extend Bidder - @biddable_auction = auction.extend BiddableAuction - @bid_creator = bid_params.extend BidCreator + @biddable = auction.extend Biddable + @validator = bid_params.extend Validator + @request = bid_params @listener = listener end def bid in_context do - bidder.create_bid + validator.validate + biddable.create_bid end + rescue InvalidRecordException => e listener.create_on_error e.errors + rescue ValidationException => e listener.create_on_error [e.message] end - module Bidder + module Validator include ContextAccessor - def create_bid - validate_bid - context.biddable_auction.create_bid - end - - def validate_bid + def validate validate_bidding_against_yourself - context.biddable_auction.validate_status - context.bid_creator.validate + validate_status + validate_presence + validate_against_last_bid + validate_against_buy_it_now end def validate_bidding_against_yourself - raise ValidationException, "Bidding against yourself is not allowed." if last_bidder? + raise ValidationException, "Bidding against yourself is not allowed." if context.bidder.last_bidder? end - def last_bidder? - context.biddable_auction.last_bidder == self + def validate_status + raise ValidationException, "Bidding on closed auction is not allowed." unless context.biddable.started? + end + + def validate_presence + raise ValidationException, errors.full_messages.join(", ") unless valid? + end + + def validate_against_last_bid + last_bid = context.biddable.last_bid + raise ValidationException, "The amount must be greater than the last bid." if last_bid && last_bid.amount >= amount + end + + def validate_against_buy_it_now + buy_it_now_price = context.biddable.buy_it_now_price + raise ValidationException, "Bid cannot exceed the buy it now price." if amount > buy_it_now_price end end - module BiddableAuction + module Bidder include ContextAccessor - def validate_status - raise ValidationException, "Bidding on closed auction is not allowed." unless started? + def last_bidder? + context.biddable.last_bidder == self end + end + + module Biddable + include ContextAccessor def last_bidder return nil unless last_bid @@ -72,7 +91,7 @@ def last_bidder end def create_bid - bid = make_bid(context.bidder, context.bid_creator.amount) + bid = make_bid(context.bidder, context.request.amount) if purchasing? bid close_auction @@ -104,32 +123,4 @@ def extend_auction extend_end_date_for EXTENDING_INTERVAL end end - - module BidCreator - include ContextAccessor - - def validate - validate_presence - validate_against_last_bid - validate_against_buy_it_now - end - - def validate_presence - raise ValidationException, validation_message unless valid? - end - - def validation_message - errors.full_messages.join(", ") - end - - def validate_against_last_bid - last_bid = context.biddable_auction.last_bid - raise ValidationException, "The amount must be greater than the last bid." if last_bid && last_bid.amount >= amount - end - - def validate_against_buy_it_now - buy_it_now_price = context.biddable_auction.buy_it_now_price - raise ValidationException, "Bid cannot exceed the buy it now price." if amount > buy_it_now_price - end - end end From 9d49e2604e2eacda5de2582f7e8c2bdde8dcb5db Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Mon, 1 Oct 2012 15:15:09 -0400 Subject: [PATCH 2/7] improves styling --- .../bootstrap_and_overrides.css.less | 20 +++---- app/models/user.rb | 3 + app/presenters/auction_presenter.rb | 24 +++++--- app/views/auctions/_bid.html.erb | 6 +- app/views/auctions/new.html.erb | 60 +++++++++++++------ app/views/auctions/show.html.erb | 34 ++++------- app/views/layouts/_navbar.html.erb | 6 +- spec/requests/authenticating_user_spec.rb | 1 + spec/requests/creating_auction_spec.rb | 8 +-- 9 files changed, 93 insertions(+), 69 deletions(-) diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index 0d6c5f6..54adaf9 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -2,6 +2,7 @@ body { padding-top: 60px; padding-left: 20px; + font-size: 13px; } @import "twitter/bootstrap/responsive"; @@ -42,16 +43,15 @@ select.datetime { width: 60px; } -.new_bid_params { - float: left; - padding-right: 20px; - - input[type=text]{ - width: 50px; - margin: 0px; - } -} - #bids { float: right; +} + +dl.dl-horizontal { + font-size: 17px; + + dt { + width: 200px; + margin-right: 20px; + } } \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 7d29d6f..babcf11 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,9 @@ class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation, :remember_me + validates :name, presence: true + validates :email, presence: true + def address Address.new(address_country, address_city, address_street, address_postal_code) end diff --git a/app/presenters/auction_presenter.rb b/app/presenters/auction_presenter.rb index dcd2cab..5228c93 100644 --- a/app/presenters/auction_presenter.rb +++ b/app/presenters/auction_presenter.rb @@ -5,7 +5,7 @@ class AuctionPresenter def_delegator :@item, :description, :item_description def_delegator :@seller, :name, :seller_name - def_delegators :@auction, :buy_it_now_price, :id, :created_at, :extendable, :end_date + def_delegators :@auction, :buy_it_now_price, :id, :created_at, :extendable def initialize auction, current_user, view_context @auction = auction @@ -18,21 +18,27 @@ def initialize auction, current_user, view_context @current_user = current_user end + def render_end_date + return "" unless @auction.end_date + h.content_tag :dl, class: "dl-horizontal" do + h.content_tag(:dt, "End Date") + + h.content_tag(:dd, @auction.end_date, id: "end-date") + end + end + def render_winner return "" unless @winner - h.content_tag :p do - h.content_tag(:b, "Winner") + - h.tag(:br) + - h.content_tag(:span, @winner.name, id: "winner") + h.content_tag :dl, class: "dl-horizontal" do + h.content_tag(:dt, "Winner") + + h.content_tag(:dd, @winner.name, id: "winner") end end def render_last_bid return "" unless @auction.last_bid - h.content_tag :p do - h.content_tag(:b, "Last Bid") + - h.tag(:br) + - @auction.last_bid.amount.to_s + h.content_tag :dl, class: "dl-horizontal" do + h.content_tag(:dt, "Last Bid") + + h.content_tag(:dd, @auction.last_bid.amount.to_s, id: "last-bid") end end diff --git a/app/views/auctions/_bid.html.erb b/app/views/auctions/_bid.html.erb index d737e6b..909c6d6 100644 --- a/app/views/auctions/_bid.html.erb +++ b/app/views/auctions/_bid.html.erb @@ -1,4 +1,6 @@ -<%= form_for BidParams.empty, url: auction_bids_path(auction_id) do |f| %> - <%= f.text_field :amount %> +<%= form_for BidParams.empty, url: auction_bids_path(auction_id), html:{class: "form-inline", style: 'float: left; margin-right: 20px;'} do |f| %> +
+ $<%= f.text_field :amount, class: 'input-small' %>.00 +
<%= f.submit "Bid", class: "btn" %> <% end %> \ No newline at end of file diff --git a/app/views/auctions/new.html.erb b/app/views/auctions/new.html.erb index 078daf9..93cdf50 100644 --- a/app/views/auctions/new.html.erb +++ b/app/views/auctions/new.html.erb @@ -1,26 +1,50 @@ -<% content_for :title, "Create an Auction" %> +<% content_for :title, "Sell Item" %> -

Create an Auction

+<%= form_for AuctionParams.empty, url: auctions_path, remote: true, html: {class: 'form-horizontal'} do |f| %> + Sell Item - + -<%= form_for AuctionParams.empty, url: auctions_path, remote: true do |f| %>
- <%= f.label :item_name %> - <%= f.text_field :item_name %> - - <%= f.label :item_description %> - <%= f.text_field :item_description %> - - <%= f.label :buy_it_now_price %> - <%= f.text_field :buy_it_now_price %> - - <%= f.label :extendable %> - <%= f.check_box :extendable %> +
+ <%= f.label :item_name, "Name", class: 'control-label' %> +
+ <%= f.text_field :item_name %> +
+
+ +
+ <%= f.label :item_description, "Description", class: 'control-label' %> +
+ <%= f.text_field :item_description %> +
+
+ +
+ <%= f.label :buy_it_now_price, "Buy it now price", class: 'control-label' %> +
+
+ $<%= f.text_field :buy_it_now_price, class: 'input-small' %>.00 +
+
+
+ +
+ <%= f.label :end_date, class: 'control-label' %> +
+ +
+
+ +
+
+ <%= f.label :extendable, class: "checkbox" do %> + <%= f.check_box :extendable %> Extendable + <% end %> +
+
- <%= f.label :end_date %> -
diff --git a/app/views/auctions/show.html.erb b/app/views/auctions/show.html.erb index 053c31c..ce3210a 100644 --- a/app/views/auctions/show.html.erb +++ b/app/views/auctions/show.html.erb @@ -6,36 +6,24 @@

<%= @auction.item_name %>

+<%= @auction.item_description %> -

- Description
- <%= @auction.item_description %> -

+
+
Seller
+
<%= @auction.seller_name %>
+
-

- Seller
- <%= @auction.seller_name %> -

+
+
Buy it now price
+
<%= @auction.buy_it_now_price %>
+
-

- Price
- <%= @auction.buy_it_now_price %> -

+<%= @auction.render_end_date %> -

- Extendable
- <%= @auction.extendable %> -

- -

- End date
- <%= @auction.end_date %> -

+<%= @auction.render_winner %> <%= @auction.render_last_bid %> -<%= @auction.render_winner %> -
<%= @auction.render_actions %>
diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index c1cccc1..c89b838 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -1,11 +1,11 @@