繁体   English   中英

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

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

我有一个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,然后如果它是假的,我可以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网址后继续重定向到/ login页面)<

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


参考

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

暂无
暂无

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

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