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 18, 2017
1 parent feb71c2 commit 89e5a21
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exit
config[:inline].present?
config[:job].present?
config
next
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
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 89e5a21

Please sign in to comment.