Skip to content

Commit

Permalink
Add after_install hook and generator
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhill committed Jul 19, 2017
1 parent feb71c2 commit 3939188
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
7.4.0
-----
* Add an after_authenticate job which will be run once the shop is authenticated. [[#431]](https://github.com/Shopify/shopify_app/pull/432)

7.3.0
-----
* Bump required omniauth-shopify-oauth2 version to 1.2.0.
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Table of Contents
* [**Managing Api Keys**](#managing-api-keys)
* [**WebhooksManager**](#webhooksmanager)
* [**ScripttagsManager**](#scripttagsmanager)
* [**AfterAuthenticate Job**](#afterauthenticate-job)
* [**ShopifyApp::SessionRepository**](#shopifyappsessionrepository)
* [**AuthenticatedController**](#authenticatedcontroller)
* [**AppProxyVerification**](#appproxyverification)
Expand Down Expand Up @@ -290,6 +291,31 @@ Scripttags are created in the same way as the Webhooks, with a background job wh

If `src` responds to `call` its return value will be used as the scripttag's source. It will be called on scripttag creation and deletion.

AfterAuthenticate Job
---------------------

If your app needs to perform specific actions after it is installed ShopifyApp can queue or run a job of your choosing (note that we already provide support for automatically creating Webhooks and Scripttags). To configure the after authenticate job update your initializer as follows:

```ruby
ShopifyApp.configure do |config|
config.add_after_authenticate_job = { job: Shopify::AfterAuthenticateJob }
end
```

If you need the job to run synchronously add the `inline` flag:

```ruby
ShopifyApp.configure do |config|
config.add_after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true }
end
```

We've also provided a generator which creates a skeleton job and updates the initializer for you:

```
bin/rails g shopify_app:add_after_authenticate_job
```

ShopifyApp::SessionRepository
-----------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rails/generators/base'

module ShopifyApp
module Generators
class AddAfterAuthenticateJobGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)

hook_for :test_framework, as: :job, in: :rails do |instance, generator|
instance.invoke generator, [ instance.send(:job_file_name) ]
end

def init_after_authenticate_config
initializer = load_initializer

after_authenticate_job_config = " config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }\n"

inject_into_file(
'config/initializers/shopify_app.rb',
after_authenticate_job_config,
before: 'end'
)

unless initializer.include?(after_authenticate_job_config)
shell.say "Error adding after_authneticate_job to config. Add this line manually: #{after_authenticate_job_config}", :red
end
end

def add_after_authenticate_job
template 'after_authenticate_job.rb', "app/jobs/#{job_file_name}_job.rb"
end

private

def load_initializer
File.read(File.join(destination_root, 'config/initializers/shopify_app.rb'))
end

def job_file_name
'shopify/after_authenticate'
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Shopify
class AfterAuthenticateJob < ActiveJob::Base
def perform(shop_domain:)
shop = Shop.find_by(shopify_domain: shop_domain)

shop.with_shopify_session do
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
config.secret = "<%= @secret %>"
config.scope = "<%= @scope %>"
config.embedded_app = <%= embedded_app? %>
config.after_authenticate_job = false
end
1 change: 1 addition & 0 deletions lib/shopify_app/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Configuration
alias_method :embedded_app?, :embedded_app
attr_accessor :webhooks
attr_accessor :scripttags
attr_accessor :after_authenticate_job

# customise ActiveJob queue names
attr_accessor :scripttags_manager_queue_name
Expand Down
12 changes: 12 additions & 0 deletions lib/shopify_app/sessions_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def callback
login_shop
install_webhooks
install_scripttags
perform_after_authenticate_job

redirect_to return_address
else
Expand Down Expand Up @@ -87,5 +88,16 @@ def return_address
session.delete(:return_to) || main_app.root_url
end

def perform_after_authenticate_job
config = ShopifyApp.configuration.after_authenticate_job

return unless config && config[:job].present?

if config[:inline] == true
config[:job].perform_now(shop_domain: session[:shopify_domain])
else
config[:job].perform_later(shop_domain: session[:shopify_domain])
end
end
end
end
2 changes: 1 addition & 1 deletion lib/shopify_app/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ShopifyApp
VERSION = '7.3.0'
VERSION = '7.4.0'
end
50 changes: 50 additions & 0 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require 'test_helper'

module Shopify
class AfterAuthenticateJob < ActiveJob::Base
def perform; end
end
end

module ShopifyApp
class SessionsControllerTest < ActionController::TestCase

Expand Down Expand Up @@ -140,6 +146,50 @@ class SessionsControllerTest < ActionController::TestCase
assert_equal 'Cerrar sesión', flash[:notice]
end

test "#callback calls #perform_after_authenticate_job and performs inline when inline is true" do
ShopifyApp.configure do |config|
config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true }
end

Shopify::AfterAuthenticateJob.expects(:perform_now)

mock_shopify_omniauth
get :callback, params: { shop: 'shop' }
end

test "#callback calls #perform_after_authenticate_job and performs asynchronous when inline isn't true" 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 doesn't call #perform_after_authenticate_job if job is nil" do
ShopifyApp.configure do |config|
config.after_authenticate_job = { job: nil, inline: false }
end

Shopify::AfterAuthenticateJob.expects(:perform_later).never

mock_shopify_omniauth
get :callback, params: { shop: 'shop' }
end

test "#callback calls #perform_after_authenticate_job and performs async if inline isn't present" do
ShopifyApp.configure do |config|
config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob }
end

Shopify::AfterAuthenticateJob.expects(:perform_later)

mock_shopify_omniauth
get :callback, params: { shop: 'shop' }
end

private

def mock_shopify_omniauth
Expand Down
30 changes: 30 additions & 0 deletions test/generators/add_after_authenticate_job_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'test_helper'
require 'generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator'

class AddAfterAuthenticateJobGeneratorTest < Rails::Generators::TestCase
tests ShopifyApp::Generators::AddAfterAuthenticateJobGenerator
destination File.expand_path("../tmp", File.dirname(__FILE__))

setup do
prepare_destination
end

test 'adds enable_after_authenticate_actions config' do
provide_existing_initializer_file

run_generator

assert_file "config/initializers/shopify_app.rb" do |config|
assert_match 'config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }', config
end
end

test "adds the after_authenticate job" do
provide_existing_initializer_file

run_generator

assert_directory "app/jobs/shopify"
assert_file "app/jobs/shopify/after_authenticate_job.rb"
end
end
1 change: 1 addition & 0 deletions test/generators/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class InstallGeneratorTest < Rails::Generators::TestCase
assert_match 'config.secret = "<secret>"', shopify_app
assert_match 'config.scope = "read_orders, read_products"', shopify_app
assert_match "config.embedded_app = true", shopify_app
assert_match "config.after_authenticate_job = false", shopify_app
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/shopify_app/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class ConfigurationTest < ActiveSupport::TestCase
test "configure" do
ShopifyApp.configure do |config|
config.embedded_app = true
config.after_authenticate_job = false
end

assert_equal true, ShopifyApp.configuration.embedded_app
assert_equal false, ShopifyApp.configuration.after_authenticate_job
end

test "defaults to myshopify_domain" do
Expand Down

0 comments on commit 3939188

Please sign in to comment.