简体   繁体   English

Shopify Rails App:触发为商店重新安装创建回调

[英]Shopify Rails App: Triggering create callback for a shop re-install

I have a Shopify app that runs a callback when a new Shop is created. 我有一个Shopify应用程序,在创建新商店时运行回调。 I just discovered a bug that when the app is uninstalled, then re-insalled, the callback isn't run because the shop isn't actually created again (I don't delete shops from my DB on uninstall). 我刚刚发现了一个错误,当应用程序被卸载,然后重新安装时,回调没有运行,因为商店实际上没有再次创建(我不会在卸载时从我的数据库中删除商店)。

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 see if shop.installed = false, and then if it's false, I can init_webhooks. 我可以运行检查以查看shop.installed = false,然后如果它是假的,我可以init_webhooks。 But I'm just not sure where I should be putting this logic. 但我只是不确定我应该把这个逻辑放在哪里。 I don't know if it's appropriate to put inside the store or retrieve methods. 我不知道放入商店或检索方法是否合适。

I'm wondering if there's something simple that I'm missing. 我想知道是否有一些我想念的简单。 Basically I want to run my init_webhooks if the webhooks don't exist. 基本上我想运行我的init_webhooks,如果webhooks不存在。

EDIT: I tried the below solution of refactoring my callbacks into their own method whereby I could check to see if app is installed and then, if not, run the methods I want on new installs: 编辑:我尝试了以下解决方案,将我的回调重构为他们自己的方法,我可以检查是否安装了应用程序然后,如果没有,运行我想要的新安装方法:

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

This seems to be working fine for brand new installs, but on a re-install, the user doesn't seem to authenticate (keeps redirecting to the /login page after entering shopify url)< 这似乎适用于全新的安装,但在重新安装时,用户似乎没有进行身份验证(在进入shopify网址后继续重定向到/ login页面)<

You can put this check inside an Initializer so when the app is started, it only checks once and does any necessary setup before the rest of the app loads or begins to take requests. 您可以将此检查放在初始化程序中,这样当应用程序启动时,它只检查一次,并在应用程序的其余部分加载或开始接收请求之前进行任何必要的设置。


References 参考

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM