From c445da2b4dc5097fcfddf5e40d74c2965573fc1c Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 27 Feb 2009 09:55:41 +0000 Subject: [PATCH] Added date_available_from to products, updated conditions in product.find_products_for_sale model, updated product maintenance forms. --- app/models/product.rb | 2 +- app/views/products/edit.html.erb | 4 +++ app/views/products/index.html.erb | 7 +++++ app/views/products/new.html.erb | 4 +++ app/views/products/show.html.erb | 4 +++ db/migrate/20090202163608_add_test_data.rb | 4 +-- ...2213_add_date_avilable_until_to_product.rb | 10 ++++++ .../20090226212307_add_more_test_data.rb | 31 +++++++++++++++++++ db/schema.rb | 15 +++++++-- 9 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20090226212213_add_date_avilable_until_to_product.rb create mode 100644 db/migrate/20090226212307_add_more_test_data.rb diff --git a/app/models/product.rb b/app/models/product.rb index 9cc9488..27fbdab 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -9,7 +9,7 @@ class Product < ActiveRecord::Base def self.find_products_for_sale find(:all, - :conditions => "date_available <= now()", + :conditions => "date_available <= now() and (date_available_until is null or date_available_until >= now())", :order => :title) end diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb index 18efd55..ce912cc 100644 --- a/app/views/products/edit.html.erb +++ b/app/views/products/edit.html.erb @@ -23,6 +23,10 @@ <%= f.label :date_available %>
<%= f.date_select :date_available, :order => [:day, :month, :year] %>

+

+ <%= f.label :date_available_until %>
+ <%= f.date_select :date_available_until, :order => [:day, :month, :year], :include_blank => true %> +

<%= f.submit "Update" %>

diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index 3e068e6..1bed039 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -18,6 +18,13 @@ <% else %>
Available from <%= product.date_available %>
<% end %> + <% if not product.date_available_until.nil? %> + <% if product.date_available_until.future? %> +
Available until <%= product.date_available_until %>
+ <% else %> +
Unavailable since <%= product.date_available_until %>
+ <% end %> + <% end %> diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb index 9eac9ae..339feb8 100644 --- a/app/views/products/new.html.erb +++ b/app/views/products/new.html.erb @@ -23,6 +23,10 @@ <%= f.label :date_available %>
<%= f.date_select :date_available, :order => [:day, :month, :year] %>

+

+ <%= f.label :date_available_until %>
+ <%= f.date_select :date_available_until, :order => [:day, :month, :year], :include_blank => true %> +

<%= f.submit "Create" %>

diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb index fb3631e..5fc752f 100644 --- a/app/views/products/show.html.erb +++ b/app/views/products/show.html.erb @@ -23,6 +23,10 @@ <%=h @product.date_available %>

+

+ Available until: + <%=h @product.date_available_until %> +

<%= link_to 'Edit', edit_product_path(@product) %> | <%= link_to 'Back', products_path %> diff --git a/db/migrate/20090202163608_add_test_data.rb b/db/migrate/20090202163608_add_test_data.rb index 35a0a7a..0bf4e82 100644 --- a/db/migrate/20090202163608_add_test_data.rb +++ b/db/migrate/20090202163608_add_test_data.rb @@ -29,7 +29,7 @@ class AddTestData < ActiveRecord::Migration

}, :image_url => '/images/daisy.gif', - :price => 5.00, + :price => 4.99, :date_available => Time.utc(2011, 7, 1) ) @@ -57,7 +57,7 @@ class AddTestData < ActiveRecord::Migration

}, :image_url => '/images/can-of-air.jpg', - :price => 5000.00, + :price => 4999.95, :date_available => Time.utc(2009, 1, 1) ) end diff --git a/db/migrate/20090226212213_add_date_avilable_until_to_product.rb b/db/migrate/20090226212213_add_date_avilable_until_to_product.rb new file mode 100644 index 0000000..bfc90e5 --- /dev/null +++ b/db/migrate/20090226212213_add_date_avilable_until_to_product.rb @@ -0,0 +1,10 @@ +class AddDateAvilableUntilToProduct < ActiveRecord::Migration + def self.up + add_column :products, :date_available_until, :date + Product.reset_column_information + end + + def self.down + remove_column :products, :date_available_until + end +end diff --git a/db/migrate/20090226212307_add_more_test_data.rb b/db/migrate/20090226212307_add_more_test_data.rb new file mode 100644 index 0000000..d91721b --- /dev/null +++ b/db/migrate/20090226212307_add_more_test_data.rb @@ -0,0 +1,31 @@ +class AddMoreTestData < ActiveRecord::Migration + def self.up + Product.create(:title => 'Rainbow', + :description => + %{

What could be better than a beautiful rainbow to brighten up your + day? Experience the wonder of light and water in all its glory. +

+

Due to metreological variations, the rainbow you receive may + differ slightly from the one shown here. +

+

(Terminal gold not included.) +

+ }, + :image_url => '/images/rainbow.jpg', + :price => 17.99, + :date_available => Time.utc(2008, 12, 31), + :date_available_until => Time.utc(2009, 1, 1) +# :available_until => 'fred' +# :au => Time.utc(2008, 12, 31) + ) + + box = Product.find(3) + box.update_attribute(:date_available_until, Time.utc(2010, 7, 1)) +# box.update_attribute(:available_until, 'tom') +# box.update_attribute(:au, Time.utc(2010, 7, 1)) + end + + def self.down + Product.delete_all(["title = ?", 'Rainbow']) + end +end diff --git a/db/schema.rb b/db/schema.rb index dc43da5..d08e763 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20090202163608) do +ActiveRecord::Schema.define(:version => 20090226212307) do create_table "products", :force => true do |t| t.string "title" @@ -17,8 +17,19 @@ ActiveRecord::Schema.define(:version => 20090202163608) do t.string "image_url" t.datetime "created_at" t.datetime "updated_at" - t.decimal "price", :precision => 8, :scale => 2, :default => 0.0 + t.decimal "price", :precision => 8, :scale => 2, :default => 0.0 t.date "date_available" + t.date "date_available_until" end + create_table "sessions", :force => true do |t| + t.string "session_id", :null => false + t.text "data" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" + add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" + end -- 2.34.1