forked from bofh/fetsite
AutoCommit Mit Jun 10 17:03:04 CEST 2015
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
class Key < ActiveRecord::Base
|
||||
attr_accessible :expire, :is_valid, :type, :user_id
|
||||
attr_accessible :expire, :is_valid, :typ,
|
||||
belongs_to :parent, :polymorphic => true
|
||||
belongs_to :user
|
||||
before_create :create_unique_identifier
|
||||
@@ -8,4 +8,27 @@ class Key < ActiveRecord::Base
|
||||
self.uuid = SecureRandom.hex(10) # or whatever you chose like UUID tools
|
||||
end while self.class.exists?(:uuid => uuid)
|
||||
end
|
||||
def self.find_or_create(user, typ, parent=nil)
|
||||
if parent.nil? || !parent
|
||||
kk=Key.where(user_id: user.id, typ: typ, is_valid: true).first
|
||||
if kk.nil?
|
||||
kk=Key.new
|
||||
kk.user=user
|
||||
kk.typ = typ
|
||||
kk.is_valid = true
|
||||
end
|
||||
else
|
||||
kk=Key.where(user_id: user.id, typ: typ, is_valid: true, parent_type: parent.class.to_s, parent_id: parent.id).first
|
||||
|
||||
if kk.nil?
|
||||
kk=Key.new
|
||||
kk.user=user
|
||||
kk.typ = typ
|
||||
kk.is_valid = true
|
||||
kk.parent=parent
|
||||
|
||||
end
|
||||
end
|
||||
kk
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user