Skip to content

Commit

Permalink
Merge pull request #743 from Shopify/after-authenticate-job-string-co…
Browse files Browse the repository at this point in the history
…nstantize

Allow #after_authenticate_job to accept a string class name
  • Loading branch information
kmcphillips committed May 8, 2019
2 parents 5613559 + 3a52f78 commit 32af7ac
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/shopify_app/callback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/callback_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 32af7ac

Please sign in to comment.