Skip to content

Commit

Permalink
Merge pull request #26 from divins/feature/add_alarm_to_items
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
divins committed Jul 26, 2011
2 parents 22a7dd5 + c3ff567 commit dbc0399
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
10 changes: 6 additions & 4 deletions app/assets/stylesheets/layouts/items.css.sass
Expand Up @@ -47,11 +47,13 @@
a a
margin-right: 15px margin-right: 15px
.short_description .short_description
width: 40% width: 39%
.desired_stock .desired_stock
width: 10% width: 7%
.alarm
width: 7%
.actual_stock .actual_stock
width: 10% width: 7%
font-weight: bold font-weight: bold
.ok .ok
color: green color: green
Expand Down Expand Up @@ -99,7 +101,7 @@
overflow: hidden overflow: hidden
+inline-block +inline-block
width: 600px width: 600px
height: 250px height: 300px
float: left float: left
background-color: #222 background-color: #222
text-align: center text-align: center
Expand Down
17 changes: 15 additions & 2 deletions app/models/item.rb
@@ -1,11 +1,15 @@
class Item < ActiveRecord::Base class Item < ActiveRecord::Base
belongs_to :user belongs_to :user


before_validation :default_values

validates :short_description, presence: true validates :short_description, presence: true
validates :desired_stock, presence: true, numericality: {greater_than_or_equal_to: 0} 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 :actual_stock, presence: true, numericality: {greater_than_or_equal_to: 0}
validates :category, presence: true validates :category, presence: true
validate :check_category validate :check_category
validate :check_alarm


def plus_one! def plus_one!
self.actual_stock = self.actual_stock+1 self.actual_stock = self.actual_stock+1
Expand All @@ -22,8 +26,7 @@ def category_ordered
order('category DESC') order('category DESC')
end end
def stock_ordered def stock_ordered
# order('(actual_stock/desired_stock) ASC') order('actual_stock ASC, alarm DESC')
order('actual_stock ASC, desired_stock DESC')
end end
def short_description_ordered def short_description_ordered
order('short_description ASC') order('short_description ASC')
Expand All @@ -32,7 +35,17 @@ def short_description_ordered


private private


def default_values
self.desired_stock ||= 0
self.alarm ||= 0
self.actual_stock ||= 0
end

def check_category def check_category
errors.add :category, :invalid_category unless Categories.all.include? self.category errors.add :category, :invalid_category unless Categories.all.include? self.category
end end

def check_alarm
errors.add :alarm, :invalid_alarm unless self.desired_stock >= self.alarm
end
end end
1 change: 1 addition & 0 deletions app/views/user/items/_form.html.slim
Expand Up @@ -3,6 +3,7 @@
= form.input :category, collection: @categories, include_blank: t('categories.blank') = form.input :category, collection: @categories, include_blank: t('categories.blank')
= form.input :short_description = form.input :short_description
= form.input :desired_stock = form.input :desired_stock
= form.input :alarm
= form.input :actual_stock = form.input :actual_stock
.submit .submit
= form.submit id: 'new_item_commit' = form.submit id: 'new_item_commit'
5 changes: 3 additions & 2 deletions app/views/user/items/_item.html.slim
@@ -1,7 +1,7 @@
-if @current_category != item.category -if @current_category != item.category
-@current_category = item.category -@current_category = item.category
tr class="#{item.category}_row category_row" tr class="#{item.category}_row category_row"
td colspan=5 td colspan=6
=t("categories.#{item.category}") =t("categories.#{item.category}")
tr id=dom_id(item) class=item.category tr id=dom_id(item) class=item.category
td.actions td.actions
Expand All @@ -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') = 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.short_description = item.short_description
td.desired_stock = item.desired_stock td.desired_stock = item.desired_stock
td.alarm = item.alarm
td.actual_stock td.actual_stock
-if(item.desired_stock <= item.actual_stock) -if(item.desired_stock <= item.actual_stock)
.ok = item.actual_stock .ok = item.actual_stock
-elsif(item.actual_stock == 0) -elsif(item.actual_stock <= item.alarm)
.outta = item.actual_stock .outta = item.actual_stock
-else -else
.need = item.actual_stock .need = item.actual_stock
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20110725184900_add_alarm_to_items.rb
@@ -0,0 +1,5 @@
class AddAlarmToItems < ActiveRecord::Migration
def change
add_column :items, :alarm, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # 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| create_table "items", :force => true do |t|
t.integer "user_id" t.integer "user_id"
Expand All @@ -20,6 +20,7 @@
t.integer "actual_stock" t.integer "actual_stock"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.integer "alarm"
end end


create_table "users", :force => true do |t| create_table "users", :force => true do |t|
Expand Down
1 change: 1 addition & 0 deletions test/blueprints.rb
Expand Up @@ -13,5 +13,6 @@
category { "meat" } category { "meat" }
short_description { "Item #{sn}" } short_description { "Item #{sn}" }
desired_stock { rand(4) } desired_stock { rand(4) }
alarm { 0 }
actual_stock { rand(4) } actual_stock { rand(4) }
end end
6 changes: 4 additions & 2 deletions test/unit/models/item_test.rb
Expand Up @@ -26,15 +26,17 @@
end end
it "is not valid without desired stock" do it "is not valid without desired stock" do
@item.desired_stock = nil @item.desired_stock = nil
@item.valid?.must_equal false @item.valid?
@item.desired_stock.must_equal 0
end end
it "is not valid with subzero desired stock" do it "is not valid with subzero desired stock" do
@item.desired_stock = -1 @item.desired_stock = -1
@item.valid?.must_equal false @item.valid?.must_equal false
end end
it "is not valid without actual stock" do it "is not valid without actual stock" do
@item.actual_stock = nil @item.actual_stock = nil
@item.valid?.must_equal false @item.valid?
@item.actual_stock.must_equal 0
end end
it "is not valid with subzero actual stock" do it "is not valid with subzero actual stock" do
@item.actual_stock = -1 @item.actual_stock = -1
Expand Down

0 comments on commit dbc0399

Please sign in to comment.