Skip to content

Commit

Permalink
add activity filter to licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Oct 23, 2024
1 parent 906dfe7 commit c62923b
Show file tree
Hide file tree
Showing 3 changed files with 643 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/controllers/api/v1/licenses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class LicensesController < Api::V1::BaseController
has_scope(:expires, type: :hash, only: :index) { |c, s, v|
s.expires(**v.symbolize_keys.slice(:within, :in, :before, :after))
}
has_scope(:activity, type: :hash, only: :index) { |c, s, v|
s.activity(**v.symbolize_keys.slice(:inside, :outside, :before, :after))
}
has_scope :suspended
has_scope :expiring
has_scope :expired
Expand Down
30 changes: 30 additions & 0 deletions app/models/license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,36 @@ class License < ApplicationRecord
SQL
}

scope :activity, -> (inside: nil, outside: nil, before: nil, after: nil) {
begin
case
when inside.present?
s = inside.to_s.match?(/\A\d+\z/) ? "PT#{inside.to_s}S".upcase : "P#{inside.to_s.delete_prefix('P').upcase}"
d = ActiveSupport::Duration.parse(s)

active(d.ago)
when outside.present?
s = outside.to_s.match?(/\A\d+\z/) ? "PT#{outside.to_s}S".upcase : "P#{outside.to_s.delete_prefix('P').upcase}"
d = ActiveSupport::Duration.parse(s)

inactive(d.ago)
when before.present?
t = before.to_s.match?(/\A\d+\z/) ? Time.at(before.to_i) : before.to_time

inactive(t)
when after.present?
t = after.to_s.match?(/\A\d+\z/) ? Time.at(after.to_i) : after.to_time

active(t)
else
none
end
rescue ActiveSupport::Duration::ISO8601Parser::ParsingError,
ArgumentError # to_time raises for invalid input
none
end
}

scope :suspended, -> (status = true) { where suspended: ActiveRecord::Type::Boolean.new.cast(status) }
scope :assigned, -> (status = true) {
if ActiveRecord::Type::Boolean.new.cast(status)
Expand Down
Loading

0 comments on commit c62923b

Please sign in to comment.