2015-10-02 29 views
5

我有一個Shopify應用程序,在創建新商店時運行回調。我剛剛發現了一個錯誤,當應用程序被卸載並重新安裝時,回調不會運行,因爲商店實際上並未再次創建(我不會在卸載時從我的數據庫中刪除商店)。Shopify Rails應用程序:觸發爲商店創建回調重新安裝

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 

我可以運行檢查,看看shop.installed = false,然後如果它是假的,我可以init_webhooks。但我不確定我應該把這個邏輯放在哪裏。我不知道是否適合放入商店或檢索方法。

我想知道是否有簡單的東西,我錯過了。基本上我想運行我的init_webhooks,如果webhooks不存在。

編輯:我嘗試重構我的回調到自己的方法如下解決方案,由此我可以檢查是否已安裝應用程序,然後,如果沒有,跑我希望在新的安裝方法:

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 

這似乎是全新的安裝正常工作,但在重新安裝,用戶似乎並沒有進行身份驗證(輸入shopify url後保持重定向到/登錄頁面)<

回答

相關問題