Skip to content

Commit

Permalink
Add expires_at columns to user model
Browse files Browse the repository at this point in the history
  • Loading branch information
gbzodek committed Nov 23, 2023
1 parent d08c7e4 commit c0d57ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserExpiresAtColumn < ActiveRecord::Migration[<%= rails_migration_version %>]
def change
add_column :users, :expires_at, :datetime
end
end
18 changes: 18 additions & 0 deletions lib/generators/shopify_app/user_model/user_model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ def create_scopes_storage_in_user_model
end
end

def create_expires_at_storage_in_user_model
expires_at_column_prompt = <<~PROMPT
It is highly recommended that apps record the User session expiry date. \
This will allow apps to check if the session has expired and re-authenticate \
without a first call to Shopify.
The following migration will add an `expires_at` column to the User model. \
Do you want to include this migration? [y/n]
PROMPT

if new_shopify_cli_app? || Rails.env.test? || yes?(expires_at_column_prompt)
migration_template(
"db/migrate/add_user_expires_at_column.erb",
"db/migrate/add_user_expires_at_column.rb",
)
end
end

def update_shopify_app_initializer
gsub_file("config/initializers/shopify_app.rb", "ShopifyApp::InMemoryUserSessionStore", "User")
end
Expand Down
12 changes: 11 additions & 1 deletion test/generators/user_model_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ class UserModelGeneratorTest < Rails::Generators::TestCase
end
end

test "create User with access_scopes migration with --new-shopify-cli-app flag provided" do
test "create expires_at migration for User model" do
run_generator
assert_migration "db/migrate/add_user_expires_at_column.rb" do |migration|
assert_match "add_column :users, :expires_at, :datetime", migration
end
end

test "create User with all migrations with --new-shopify-cli-app flag provided" do
Rails.env = "mock_environment"

run_generator ["--new-shopify-cli-app"]
Expand All @@ -44,6 +51,9 @@ class UserModelGeneratorTest < Rails::Generators::TestCase
assert_migration "db/migrate/add_user_access_scopes_column.rb" do |migration|
assert_match "add_column :users, :access_scopes, :string", migration
end
assert_migration "db/migrate/add_user_expires_at_column.rb" do |migration|
assert_match "add_column :users, :expires_at, :datetime", migration
end
end

test "updates the shopify_app initializer to use User to store session" do
Expand Down

0 comments on commit c0d57ab

Please sign in to comment.