I have a Shopify app that launches a callback when creating a new store. I just found an error that when the application is uninstalled and then re-locked, the callback does not start because the store is not actually created (I do not delete stores from my database when uninstalling).
class Shop < ActiveRecord::Base include ShopifyApp::Shop after_create :init_webhooks def self.store(session) shop = Shop.where(:shopify_domain => session.url).first_or_create({ shopify_domain: session.url, :shopify_token => session.token, :installed => true}) shop.id end def self.retrieve(id) shop = Shop.where(:id => id).first if shop ShopifyAPI::Session.new(shop.shopify_domain, shop.shopify_token) else nil end end
I could run a check to check if shop.installed = false, and then if it is false, I can set init_webhooks. But I'm just not sure where I should put this logic. I do not know if it is appropriate to put in a store or retrieve methods.
I am wondering if there is something simple that I am missing. Basically I want to run init_webhooks if the web hosts do not exist.
EDIT: I tried the following solution to refactor my callbacks into my own method so that I can check if the application is installed, and then if not, run the methods that I want with the new installations:
def self.retrieve(id) shop = Shop.where(:id = id).first if shop shop.boot ShopifyAPI::Session.new(shop.shopify_domain, shop.shopify_token) else nil end end def boot if !installed shopify_session init_webhooks self.installed = true self.save! end end
It seems to work fine for new versions, but when reinstalling, the user does not seem to authenticate (continues to redirect to the / login page after entering urify url) <
source share