Permalink
Browse files

Merge pull request #26 from divins/feature/add_alarm_to_items

Closes #17
  • Loading branch information...
2 parents 22a7dd5 + c3ff567 commit dbc0399245301bf15b4a8d4cf9d966b09aa3391c @divins committed Jul 26, 2011
@@ -47,11 +47,13 @@
a
margin-right: 15px
.short_description
- width: 40%
+ width: 39%
.desired_stock
- width: 10%
+ width: 7%
+ .alarm
+ width: 7%
.actual_stock
- width: 10%
+ width: 7%
font-weight: bold
.ok
color: green
@@ -99,7 +101,7 @@
overflow: hidden
+inline-block
width: 600px
- height: 250px
+ height: 300px
float: left
background-color: #222
text-align: center
View
@@ -1,11 +1,15 @@
class Item < ActiveRecord::Base
belongs_to :user
+ before_validation :default_values
+
validates :short_description, presence: true
validates :desired_stock, presence: true, numericality: {greater_than_or_equal_to: 0}
+ validates :alarm, presence: true, numericality: {greater_than_or_equal_to: 0}
validates :actual_stock, presence: true, numericality: {greater_than_or_equal_to: 0}
validates :category, presence: true
validate :check_category
+ validate :check_alarm
def plus_one!
self.actual_stock = self.actual_stock+1
@@ -22,8 +26,7 @@ def category_ordered
order('category DESC')
end
def stock_ordered
- # order('(actual_stock/desired_stock) ASC')
- order('actual_stock ASC, desired_stock DESC')
+ order('actual_stock ASC, alarm DESC')
end
def short_description_ordered
order('short_description ASC')
@@ -32,7 +35,17 @@ def short_description_ordered
private
+ def default_values
+ self.desired_stock ||= 0
+ self.alarm ||= 0
+ self.actual_stock ||= 0
+ end
+
def check_category
errors.add :category, :invalid_category unless Categories.all.include? self.category
end
+
+ def check_alarm
+ errors.add :alarm, :invalid_alarm unless self.desired_stock >= self.alarm
+ end
end
@@ -3,6 +3,7 @@
= form.input :category, collection: @categories, include_blank: t('categories.blank')
= form.input :short_description
= form.input :desired_stock
+ = form.input :alarm
= form.input :actual_stock
.submit
= form.submit id: 'new_item_commit'
@@ -1,7 +1,7 @@
-if @current_category != item.category
-@current_category = item.category
tr class="#{item.category}_row category_row"
- td colspan=5
+ td colspan=6
=t("categories.#{item.category}")
tr id=dom_id(item) class=item.category
td.actions
@@ -10,10 +10,11 @@ tr id=dom_id(item) class=item.category
= link_to image_tag("delete.png", alt: t('.actions.delete'), size: '18x18'), user_item_path(item), method: :delete, confirm: t('.confirmations.delete')
td.short_description = item.short_description
td.desired_stock = item.desired_stock
+ td.alarm = item.alarm
td.actual_stock
-if(item.desired_stock <= item.actual_stock)
.ok = item.actual_stock
- -elsif(item.actual_stock == 0)
+ -elsif(item.actual_stock <= item.alarm)
.outta = item.actual_stock
-else
.need = item.actual_stock
@@ -0,0 +1,5 @@
+class AddAlarmToItems < ActiveRecord::Migration
+ def change
+ add_column :items, :alarm, :integer
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110715172801) do
+ActiveRecord::Schema.define(:version => 20110725184900) do
create_table "items", :force => true do |t|
t.integer "user_id"
@@ -20,6 +20,7 @@
t.integer "actual_stock"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "alarm"
end
create_table "users", :force => true do |t|
View
@@ -13,5 +13,6 @@
category { "meat" }
short_description { "Item #{sn}" }
desired_stock { rand(4) }
+ alarm { 0 }
actual_stock { rand(4) }
end
@@ -26,15 +26,17 @@
end
it "is not valid without desired stock" do
@item.desired_stock = nil
- @item.valid?.must_equal false
+ @item.valid?
+ @item.desired_stock.must_equal 0
end
it "is not valid with subzero desired stock" do
@item.desired_stock = -1
@item.valid?.must_equal false
end
it "is not valid without actual stock" do
@item.actual_stock = nil
- @item.valid?.must_equal false
+ @item.valid?
+ @item.actual_stock.must_equal 0
end
it "is not valid with subzero actual stock" do
@item.actual_stock = -1

0 comments on commit dbc0399

Please sign in to comment.