Skip to content

Commit

Permalink
fix nil log resource in development seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed May 1, 2024
1 parent e9be306 commit 9a051e0
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions db/seeds/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,21 @@ def srand(range) = rand < 0.9 ? brand(range.begin..(range.begin + range.end * 0.
route.required_parts.reduce({}) { _1.merge(_2 => SecureRandom.uuid) },
)

resource = route.requirements[:controller].classify.split('::').last.safe_constantize
# Select a random record
resource = if klass = route.requirements[:controller].classify.split('::').last.safe_constantize
next unless klass < ActiveRecord::Base

scope = klass < Accountable ? klass.where(account:) : klass.all
record = scope.offset((rand() * scope.count).floor) # random row
.limit(1)
.take

record
end

environment = resource.try(:environment)
admin = account.admins.for_environment(environment, strict: true).sample
requestor = if resource.respond_to?(:role) && rand(0..1).zero?
admin = account.admins.for_environment(environment, strict: true).take
requestor = if rand(0..1).zero? && resource.respond_to?(:role)
resource
else
admin
Expand All @@ -316,15 +327,17 @@ def srand(range) = rand < 0.9 ? brand(range.begin..(range.begin + range.end * 0.
account:,
)

event_log = EventLog.create!(
event_type_id: event_types.sample,
idempotency_key: SecureRandom.hex,
whodunnit: requestor,
environment:,
resource:,
request_log:,
account:,
)
unless resource.nil?
EventLog.create!(
event_type_id: event_types.sample,
idempotency_key: SecureRandom.hex,
whodunnit: requestor,
environment:,
resource:,
request_log:,
account:,
)
end
end
end
rescue ActiveRecord::RecordNotSaved => e
Expand Down

0 comments on commit 9a051e0

Please sign in to comment.