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

标签 ruby-on-rails shopify

我有一个 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/

相关文章:

javascript - 覆盖 js.erb 文件

ruby-on-rails - 我在 OSX 10.7.5 上收到 "no such file to load — openssl"错误

ruby-on-rails - last_login_at 不工作(空)w/Authlogic Magic Columns

mysql - rake 数据库出现问题 :migrate

php - 无法在 PHP 中访问或解析已解码的 json 结构

html - 在桌面上水平对齐图像,在移动设备上垂直对齐图像

ruby-on-rails - 使用 first_or_create 时 nilClass 的未定义方法 '+'

javascript - 尝试将 Shopify 的 Draggable 模块与 svelte 一起使用,但没有成功

javascript - Shopify API 从外部站点获取购物车

image - 在 Shopify 结帐页面上加载图像像素时出现 CSP 问题