4

I've this legacy Spree application with 2 decorated controllers with inherited callbacks :

Spree::Api::BaseController.class_eval do

  before_action :load_api_key_user, only: [:index, :show, :jstree]

  before_action :override_params, only: [:create, :new]

  before_action do
    api_key_user = Spree.user_class.by_store(
        current_store
    ).find_by(spree_api_key: api_key.to_s)
    if api_key_user && (api_key_user.admin? || api_key_user.manager?)
      load_api_key_user
    end
  end
  before_action :load_user
  before_action :authenticate_user
  before_action :load_user_roles
  ...

and

Spree::Api::V1::OrdersController.class_eval do
  # Allow non authenticated user to perform some actions..
  before_action :load_api_key_user, only: [:create, :apply_coupon_code]
  before_action :load_user
  # only owners of the order are allowed to handle order.
  before_action :authorize_for_order, except: [:create, :index, :apply_coupon_code]
  before_action :authenticate_user
  before_action :load_user_roles

The filter order chain differs from different machine with the same ruby version (ruby 2.2.1p85) and same gem versions which produce unexpected behaviour. I check the order with this command :

Spree::Api::V1::OrdersController._process_action_callbacks.map{|c| "#{c.kind}:#{c.filter}"}

And the result is different with the same configuration.

Local machine:["before:set_user_language", "before:set_version", "before:set_guest_token", "before:set_content_type", "before:set_xhr_redirected_to", "before:set_request_method_cookie", "after:abort_xdomain_redirect", "before:override_params", "before:70133939841780", "before:search_ransack_by_store", "before:search_ransack_by_region", "before:find_order", "before:load_api_key_user", "before:load_user", "before:authorize_for_order", "before:authenticate_user", "before:load_user_roles", "before:remove_email_from_params_if_nil", "before:70133903534220", "before:70133903532720", "before:70133931867720", "before:70133931866900"]

Remote machine:["before:set_user_language", "before:set_version", "before:set_guest_token", "before:set_content_type", "before:set_xhr_redirected_to", "before:set_request_method_cookie", "after:abort_xdomain_redirect", "before:find_order", "before:authorize_for_order", "before:remove_email_from_params_if_nil", "before:46966032393140", "before:46966032349400", "before:46966032346700", "before:46966032344600", "before:load_api_key_user", "before:override_params", "before:46966007219340", "before:load_user", "before:authenticate_user", "before:load_user_roles", "before:search_ransack_by_store", "before:search_ransack_by_region"]

So I have the same ruby version and the same gem set. What could else explain this change ?

Renaud Kern
  • 1,116
  • 10
  • 25

2 Answers2

4

Function sets are same and orders of them is different do you mean?

ActionController callback filter chain is not consistent.

I met this kind of thing when I was developing MFC project.

At that time, When I run as a debug mode, there was no err and when run as real mode, there was RUNTIME error and I fixed this error.

The RUNTIME error was because of function orders.

Event function OnCreate and OnResizeWindow caught my attention.

debug mode: OnCreate() is called before OnResizeWindow()
real mode:  OnCreate() is called after  OnResizeWindow()

I thought the reason is interrupting system.

Then about your project: On Remote machine, there will be more interruptions than on Local machine.

To fix your error, you have to build function ordering system on your own code.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
-2

["before:set_user_language", "before:set_version", "before:set_guest_token", "before:set_content_type", "before:set_xhr_redirected_to", "before:set_request_method_cookie", "after:abort_xdomain_redirect", "before:find_order", "before:authorize_for_order", "before:remove_email_from_params_if_nil", "before:46966032393140", "before:46966032349400", "before:46966032346700", "before:46966032344600", "before:load_api_key_user", "before:override_params", "before:46966007219340", "before:load_user", "before:authenticate_user", "before:load_user_roles", "before:search_ransack_by_store", "before:search_ransack_by_region"]