Skip to content

Commit b9e2722

Browse files
committed
feat: Add UUID support for user references
* Adds `--uuid` flag to install generator * Creates `Authtrail::Generators::InstallGenerator#user_reference` helper method for migration template * Creates tests for UUID and default ID type options * Updates the README with instructions for the UUID argument
1 parent e68c0b4 commit b9e2722

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ rails generate authtrail:install --encryption=none
3535
rails db:migrate
3636
```
3737

38+
If your application uses UUID for user IDs, add the `--uuid` option:
39+
40+
```sh
41+
rails generate authtrail:install --encryption=[lockbox|activerecord|none] --uuid
42+
rails db:migrate
43+
```
44+
3845
To enable geocoding, see the [Geocoding section](#geocoding).
3946

4047
## How It Works

lib/generators/authtrail/install_generator.rb

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class InstallGenerator < Rails::Generators::Base
77
source_root File.join(__dir__, "templates")
88

99
class_option :encryption, type: :string, required: true
10+
class_option :uuid, type: :boolean, default: false, desc: 'Use UUID for user_id'
1011

1112
def copy_migration
1213
encryption # ensure valid
@@ -78,6 +79,14 @@ def mysql?
7879
def adapter
7980
ActiveRecord::Base.connection_db_config.adapter.to_s
8081
end
82+
83+
def user_reference
84+
if options[:uuid]
85+
't.references :user, type: :uuid, polymorphic: true'
86+
else
87+
't.references :user, polymorphic: true'
88+
end
89+
end
8190
end
8291
end
8392
end

lib/generators/authtrail/templates/login_activities_migration.rb.tt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
66
<%= identity_column %>
77
t.boolean :success
88
t.string :failure_reason
9-
t.references :user, polymorphic: true
9+
<%= user_reference %>
1010
t.string :context
1111
<%= ip_column %>
1212
t.text :user_agent

test/install_generator_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@ def test_encryption_none
2828
assert_file "app/models/login_activity.rb", /LoginActivity < ApplicationRecord/
2929
assert_migration "db/migrate/create_login_activities.rb", /t.string :identity, index: true/
3030
end
31+
32+
def test_uuid_option
33+
run_generator ['--encryption=none', '--uuid']
34+
assert_migration 'db/migrate/create_login_activities.rb', /t.references :user, type: :uuid, polymorphic: true/
35+
end
36+
37+
def test_default_id_type
38+
run_generator ['--encryption=none']
39+
assert_migration 'db/migrate/create_login_activities.rb', /t.references :user, polymorphic: true/
40+
end
3141
end

0 commit comments

Comments
 (0)