diff --git a/README.md b/README.md index 3f2d8b72c..17f338c43 100644 --- a/README.md +++ b/README.md @@ -347,10 +347,12 @@ If your app needs to perform specific actions after the user is authenticated su ```ruby ShopifyApp.configure do |config| - config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob } + config.after_authenticate_job = { job: "Shopify::AfterAuthenticateJob" } end ``` +The job can be configured as either a class or a class name string. + If you need the job to run synchronously add the `inline` flag: ```ruby diff --git a/app/controllers/shopify_app/callback_controller.rb b/app/controllers/shopify_app/callback_controller.rb index 70dfbc2b4..2f9aec72e 100644 --- a/app/controllers/shopify_app/callback_controller.rb +++ b/app/controllers/shopify_app/callback_controller.rb @@ -86,10 +86,13 @@ def perform_after_authenticate_job return unless config && config[:job].present? + job = config[:job] + job = job.constantize if job.is_a?(String) + if config[:inline] == true - config[:job].perform_now(shop_domain: session[:shopify_domain]) + job.perform_now(shop_domain: session[:shopify_domain]) else - config[:job].perform_later(shop_domain: session[:shopify_domain]) + job.perform_later(shop_domain: session[:shopify_domain]) end end end diff --git a/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb b/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb index 3e7f7a9c3..1586baf06 100644 --- a/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +++ b/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb @@ -14,7 +14,7 @@ def init_after_authenticate_config after_authenticate_job_config = " config.after_authenticate_job = "\ - "{ job: Shopify::AfterAuthenticateJob, inline: false }\n" + "{ job: \"Shopify::AfterAuthenticateJob\", inline: false }\n" inject_into_file( 'config/initializers/shopify_app.rb', diff --git a/test/controllers/callback_controller_test.rb b/test/controllers/callback_controller_test.rb index 0706d0148..580a04c07 100644 --- a/test/controllers/callback_controller_test.rb +++ b/test/controllers/callback_controller_test.rb @@ -124,6 +124,29 @@ class CallbackControllerTest < ActionController::TestCase get :callback, params: { shop: 'shop' } end + test "#callback calls #perform_after_authenticate_job constantizes from a string to a class" do + ShopifyApp.configure do |config| + config.after_authenticate_job = { job: "Shopify::AfterAuthenticateJob", inline: false } + end + + Shopify::AfterAuthenticateJob.expects(:perform_later) + + mock_shopify_omniauth + get :callback, params: { shop: 'shop' } + end + + test "#callback calls #perform_after_authenticate_job raises if the string is not a valid job class" do + ShopifyApp.configure do |config| + config.after_authenticate_job = { job: "InvalidJobClassThatDoesNotExist", inline: false } + end + + mock_shopify_omniauth + + assert_raise NameError do + get :callback, params: { shop: 'shop' } + end + end + private def mock_shopify_omniauth diff --git a/test/generators/add_after_authenticate_job_generator_test.rb b/test/generators/add_after_authenticate_job_generator_test.rb index 15c997954..89a081a4a 100644 --- a/test/generators/add_after_authenticate_job_generator_test.rb +++ b/test/generators/add_after_authenticate_job_generator_test.rb @@ -15,7 +15,7 @@ class AddAfterAuthenticateJobGeneratorTest < Rails::Generators::TestCase run_generator assert_file "config/initializers/shopify_app.rb" do |config| - assert_match 'config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }', config + assert_match 'config.after_authenticate_job = { job: "Shopify::AfterAuthenticateJob", inline: false }', config end end