gpt4 book ai didi

ruby-on-rails - Shopify Rails 应用程序 : Triggering create callback for a shop re-install

转载 作者:行者123 更新时间:2023-12-04 21:38:15 27 4
gpt4 key购买 nike

我有一个 Shopify 应用程序,它在创建新商店时运行回调。我刚刚发现了一个错误,当应用程序被卸载,然后重新安装时,回调不会运行,因为商店实际上没有再次创建(我不会在卸载时从我的数据库中删除商店)。

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,然后如果它是 false,我可以 init_webhooks。但我只是不确定我应该把这个逻辑放在哪里。不知道放在 store 里面或者retrieve 方法是否合适。

我想知道是否有一些简单的东西我错过了。基本上,如果 webhooks 不存在,我想运行我的 init_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 后继续重定向到/login 页面)<

最佳答案

您可以将此检查放在 Initializer 中,这样当应用程序启动时,它只检查一次并在应用程序的其余部分加载或开始接受请求之前进行任何必要的设置。

引用文献

http://guides.rubyonrails.org/configuring.html#using-initializer-files

关于ruby-on-rails - Shopify Rails 应用程序 : Triggering create callback for a shop re-install,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32914553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com