diff --git a/app/models/customer_token.rb b/app/models/customer_token.rb index 62c28ab..8c82dec 100644 --- a/app/models/customer_token.rb +++ b/app/models/customer_token.rb @@ -23,5 +23,23 @@ class CustomerToken < ActiveRecord::Base def remove_expired_tokens CustomerToken.where("expires_at < ?", Time.now).destroy_all end + + def expired? + self.expires_at < Time.now + end + + # Getter convenience method for tokens + def self.get_token(issue) + # reuse existing tokens + token = find_by_issue_id issue.id + unless token.nil? + return token unless token.expired? + # remove expired tokens + token.destroy + end + + # TODO add setting in pluging settings page + return create(:expires_at => Time.now + 1.month, :issue_id => issue.id) + end end diff --git a/lib/issue_patch.rb b/lib/issue_patch.rb index 8e4c75b..b7c1ecd 100644 --- a/lib/issue_patch.rb +++ b/lib/issue_patch.rb @@ -99,7 +99,7 @@ module IssuePatch # Create a shareable link for a customer def share_token - CustomerToken.create(:expires_at => Time.now + 1.month, :issue_id => id) + CustomerToken.get_token self end end