I’m developing a Rails application that integrates with Shopify using their API through the shopify_app gem. I need to include specific session handling code in my controllers to make API calls work properly.
Here’s the code I have to add:
around_action :shopify_session, except: [:index]
def index
host_name = "#{request.host}#{':' + request.port.to_s unless request.port == 80}"
@redirect_url = "http://#{host_name}/auth/callback"
end
The problem is I have several controllers and copying this code everywhere feels wrong. I attempted moving it to ApplicationController but that approach failed. Can someone explain why this might not work when placed in the parent controller? Also, if anyone knows how to make RSpec controller tests pass without having to comment out this session filter code, that would be amazing.
Had a similar headache with shopify_app integration about 6 months ago. The issue with putting the around_action in ApplicationController is likely because it affects every single action across your entire app, including auth-related ones that shouldn’t have the session wrapper. This creates circular dependencies where the session handler tries to redirect before authentication is complete. What worked for me was creating a base controller specifically for Shopify-integrated controllers. Something like ShopifyBaseController < ApplicationController with your session handling, then inherit from that instead. This keeps the session logic isolated to controllers that actually need it. Regarding RSpec tests, you can stub the shopify_session method in your test setup or use skip_around_action in a test-specific controller setup. The key is mocking the Shopify session state so your tests don’t try to make actual API calls during testing.