开发者

Building a Rails3 app with Shopify API. Where to place the session filter?

I'm working on a Rails app that uses the Shopify API via the Shopify_app Gem. To make a long story short in order to make calls to the Shopify API the following code must appear in the controller of the calling class.

 around_filter :shopify_sess开发者_JAVA百科ion, :except => 'welcome'

  def welcome
    current_host = "#{request.host}#{':' + request.port.to_s if request.port != 80}"
    @callback_url = "http://#{current_host}/login/finalize"
  end

Now, since I have multiple controllers in my app it hardly seems appropriate to place the above code in each. I tried to remove the code from each controller and place it solely in the application controller but that didn't work out. Any guidance as to why I might be experiencing these problems would be greatly appreciated. As a bonus if you could tell me how to get my controller tests in RSpec working without commenting out the above code in the controllers I would be especially grateful.

Thanks for your time


It makes sense for you to define the around filter in each controller as you're defining which methods of that controller to apply the filter to. The Application Controller knows nothing of the classes below it, so putting the definition there makes no sense.

I recommend setting up FakeWeb or something similar to fake out any external calls made by the API using testing. To avoid the shopify_session method from redirecting you to the login screen, make sure to set session[:shopify] to an instance of ShopifyAPI::Session in your test setup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜