Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

extract validity period and use as a scope

Klaus Zanders0bc9074a8bee
diff --git a/app/models/enterprise_token.rb b/app/models/enterprise_token.rb
index 1f1fc763c5e..e92d44607f2 100644
--- a/app/models/enterprise_token.rb
+++ b/app/models/enterprise_token.rb
@@ -97,6 +97,7 @@ class EnterpriseToken < ApplicationRecord
def set_active_tokens
EnterpriseToken
+ .active
.order(Arel.sql("created_at DESC"))
.to_a
.reject(&:expired?)
@@ -121,9 +122,18 @@ class EnterpriseToken < ApplicationRecord
validate :valid_token_object
validate :valid_domain
+ before_validation :extract_validity_from_token
before_save :clear_current_tokens_cache
before_destroy :clear_current_tokens_cache
+ scope :active, ->(date = Date.current) {
+ where(<<~SQL.squish, date: date)
+ (valid_from IS NULL OR valid_from <= :date)
+ AND
+ (valid_until IS NULL OR valid_until >= :date)
+ SQL
+ }
+
delegate :will_expire?,
:subscriber,
:mail,
@@ -193,4 +203,13 @@ class EnterpriseToken < ApplicationRecord
def valid_domain
errors.add :domain, :invalid if invalid_domain?
end
+
+ def extract_validity_from_token
+ return unless token_object
+
+ self.valid_from = token_object.starts_at
+ self.valid_until = if token_object.will_expire?
+ token_object.expires_at.next_day(token_object.reprieve_days.to_i)
+ end
+ end
end
diff --git a/db/migrate/20250610111413_add_validity_period_to_enterprise_token.rb b/db/migrate/20250610111413_add_validity_period_to_enterprise_token.rb
new file mode 100644
index 00000000000..5634b94844c
--- /dev/null
+++ b/db/migrate/20250610111413_add_validity_period_to_enterprise_token.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+class AddValidityPeriodToEnterpriseToken < ActiveRecord::Migration[8.0]
+ def change
+ change_table :enterprise_tokens, bulk: true do |table|
+ table.column :valid_from, :date
+ table.column :valid_until, :date
+
+ table.index %i[valid_from valid_until]
+ end
+ end
+end