Permalink
Browse files

add random tag generator

  • Loading branch information...
1 parent 1f29fa1 commit 8bfb1a0b6470db0d1ed5190f0933f963cb46904b @chaserx chaserx committed Jul 7, 2012
Showing with 18 additions and 0 deletions.
  1. +18 −0 app/models/item.rb
View
18 app/models/item.rb
@@ -1,3 +1,21 @@
class Item < ActiveRecord::Base
attr_accessible :checked_in, :checked_out, :checked_out_by, :date_collected, :name, :tag_number
+
+ validates :tag_number, uniqueness: true
+
+ before_save :assign_tag_number
+
+ def assign_tag_number
+ if self.tag_number.empty?
+ self.tag_number = create_random_tag
+ else
+ return
+ end
+ end
+
+ def create_random_tag
+ # => "57kie8g"
+ ("%d%d" % [rand(100), Time.now.to_i]).to_i.to_s(36)
+ end
+
end

0 comments on commit 8bfb1a0

Please sign in to comment.