Add a rules controller

This commit is contained in:
Dan Milne
2025-11-04 09:47:11 +11:00
parent 5ff166613e
commit c72d83acda
14 changed files with 272 additions and 42 deletions

View File

@@ -6,11 +6,11 @@ class Current < ActiveSupport::CurrentAttributes
attribute :project
attribute :ip
def self.baffle_host
def baffle_host
@baffle_host || ENV.fetch("BAFFLE_HOST", "localhost:3000")
end
def self.baffle_internal_host
def baffle_internal_host
@baffle_internal_host || ENV.fetch("BAFFLE_INTERNAL_HOST", nil)
end
end

View File

@@ -43,15 +43,16 @@ class Project < ApplicationRecord
end
def dsn
host = Current.baffle_host || "localhost:3000"
host = Current.baffle_host || ENV.fetch("BAFFLE_HOST", "localhost:3000")
protocol = host.include?("localhost") ? "http" : "https"
"#{protocol}://#{public_key}@#{host}/#{slug}"
end
def internal_dsn
return nil unless Current.baffle_internal_host.present?
internal_host = Current.baffle_internal_host || ENV.fetch("BAFFLE_INTERNAL_HOST", nil)
return nil unless internal_host.present?
host = Current.baffle_internal_host
host = internal_host
protocol = "http" # Internal connections use HTTP
"#{protocol}://#{public_key}@#{host}/#{slug}"
end

View File

@@ -62,8 +62,15 @@ class Rule < ApplicationRecord
end
# Class method to get latest version (for sync cursor)
# Returns microsecond Unix timestamp for efficient machine comparison
def self.latest_version
maximum(:updated_at)&.iso8601(6) || Time.current.iso8601(6)
max_time = maximum(:updated_at)
if max_time
# Convert to microseconds since epoch
(max_time.to_f * 1_000_000).to_i
else
(Time.current.to_f * 1_000_000).to_i
end
end
# Disable rule (soft delete)