Create sample import after user is created

This commit is contained in:
Eugene Burmakin 2025-03-12 20:26:53 +01:00
parent 51618ec191
commit 4a6c5bf97c
3 changed files with 2630 additions and 0 deletions

View file

@ -16,6 +16,7 @@ class User < ApplicationRecord
has_many :trips, dependent: :destroy
after_create :create_api_key
after_create :import_sample_points
after_commit :activate, on: :create, if: -> { DawarichSettings.self_hosted? }
before_save :sanitize_input
@ -116,4 +117,22 @@ class User < ApplicationRecord
settings['photoprism_url']&.gsub!(%r{/+\z}, '')
settings.try(:[], 'maps')&.try(:[], 'url')&.strip!
end
def import_sample_points
return unless Rails.env.development? ||
Rails.env.production? ||
(Rails.env.test? && ENV['IMPORT_SAMPLE_POINTS'])
raw_data = Hash.from_xml(
File.read(Rails.root.join('lib/assets/sample_points.gpx'))
)
import = imports.create(
name: 'DELETE_ME_this_is_a_demo_import_DELETE_ME',
source: 'gpx',
raw_data:
)
ImportJob.perform_later(id, import.id)
end
end

2589
lib/assets/sample_points.gpx Normal file

File diff suppressed because it is too large Load diff

View file

@ -55,6 +55,28 @@ RSpec.describe User, type: :model do
end
end
end
describe '#import_sample_points' do
before do
stub_const('IMPORT_SAMPLE_POINTS', true)
end
it 'creates a sample import and enqueues an import job' do
expect do
user = create(:user)
# Explicitly call the method since the callback is disabled in test env
user.send(:import_sample_points)
# Check if an import was created with the expected name
expect(user.imports.count).to eq(1)
expect(user.imports.first.name).to eq('DELETE_ME_this_is_a_demo_import_DELETE_ME')
expect(user.imports.first.source).to eq('gpx')
# Check if the ImportJob was enqueued with correct parameters
expect(ImportJob).to have_been_enqueued.with(user.id, user.imports.first.id)
end.to change(Import, :count).by(1)
end
end
end
describe 'methods' do