From 5d928c486f625428cb354566b2581bf4391c014a Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Sat, 12 Mar 2022 00:01:40 -0500 Subject: [PATCH] Getter convenience method for tokens --- app/models/customer_token.rb | 18 ++++++++++++++++++ lib/issue_patch.rb | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) 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