Skip to content
Browse files

tweaking checkin checkout actions; fixing the tag gen

  • Loading branch information...
1 parent eae6513 commit 9dd03a98ebc5405fda4486d052ed7e4068637a7e @chaserx chaserx committed Jul 7, 2012
Showing with 28 additions and 5 deletions.
  1. +5 −0 app/controllers/items_controller.rb
  2. +11 −0 app/helpers/items_helper.rb
  3. +9 −5 app/models/item.rb
  4. +3 −0 app/views/items/show.html.erb
View
5 app/controllers/items_controller.rb
@@ -81,12 +81,16 @@ def destroy
end
end
+ # TODO(chase): this should probably be one action that changes the state
+ # of the item.
+
def check_in
@item = Item.find(params[:id])
if @item.checkedout?
if @item.update_attributes(checkedout: false, time_checked_in: Time.now,
checkedin: true, checked_out_by: '',
time_checked_out: nil)
+ redirect_to @item, success: 'Item successfully checked in'
else
redirect_to @item, error: 'Item could not be checked in'
end
@@ -101,6 +105,7 @@ def check_out
if @item.update_attributes(checkedout: true, time_checked_in: '',
checkedin: false, checked_out_by: current_user,
time_checked_out: Time.now)
+ redirect_to @item, success: 'Item successfully checked in'
else
redirect_to @item, error: 'Item could not be checked out'
end
View
11 app/helpers/items_helper.rb
@@ -1,2 +1,13 @@
module ItemsHelper
+ def check_in_out_link_helper(item)
+ if item.checkedin?
+ link_to 'Check Out', check_out_item_path(item), class: 'btn btn-small',
+ method: :put
+ elsif item.checkedout?
+ link_to 'Check In', check_in_item_path(item), class: 'btn btn-small',
+ method: :put
+ else
+ "ack! unknown item status"
+ end
+ end
end
View
14 app/models/item.rb
@@ -1,12 +1,12 @@
class Item < ActiveRecord::Base
- attr_accessible :time_checked_in, :time_checked_out, :checked_out_by, :date_collected, :name, :tag_number, :type, :desc
+ attr_accessible :time_checked_in, :time_checked_out, :checked_out_by,
+ :date_collected, :name, :tag_number, :type, :desc,
+ :checkedin, :checkedout
- validates :tag_number, uniqueness: true, allow_nil: true
-
- before_save :assign_tag_number
+ before_create :assign_tag_number
def assign_tag_number
- if self.tag_number.nil?
+ if self.tag_number.nil? || self.tag_number.empty?
self.tag_number = create_random_tag
else
return
@@ -18,4 +18,8 @@ def create_random_tag
("%d%d" % [rand(100), Time.now.to_i]).to_i.to_s(36)
end
+ def lookup_by_tagnumber(tag)
+ where(tag_number: tag).first
+ end
+
end
View
3 app/views/items/show.html.erb
@@ -41,6 +41,9 @@
<b>Tag number:</b>
<%= @item.tag_number %>
</p>
+<p>
+ <%= check_in_out_link_helper(@item) %>
+</p>
<%= link_to 'Edit', edit_item_path(@item) %> |

0 comments on commit 9dd03a9

Please sign in to comment.
Something went wrong with that request. Please try again.