【问题标题】:Shopify Rails App: Triggering create callback for a shop re-installShopify Rails 应用程序:触发商店重新安装的创建回调
【发布时间】:2015-12-31 02:23:48
【问题描述】:

我有一个 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方法里面是否合适。

我想知道我是否缺少一些简单的东西。基本上,如果 webhook 不存在,我想运行我的 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 页面)

【问题讨论】:

    标签: ruby-on-rails shopify


    【解决方案1】:

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


    参考文献

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多