I have a multitenancy app and I'm setting the current tenancy like this:
class ApplicationController < ActionController::Base
around_filter :scope_current_tenancy
def scope_current_tenancy
Tenancy.current_id = current_tenancy.id if request.subdomain != 'www'
yield
ensure
Tenancy.current_id = nil
end
end
Then in my user model I have a default_scope
defined to access only to users within my tenancy:
class Postulant < ActiveRecord::Base
default_scope ->{ where("enlistments.tenancy_id = ?", Tenancy.current_id).includes(:enlistments).references(:enlistments) }
This works so far, but now using devise_invitable
and trying to accept an invitation I'm receiving a Filter chain halted as :resource_from_invitation_token rendered or redirected
message. The problem is because my scope_current_tenancy
filter is being executed after resource_from_invitation_token
, so resource
is not loading correctly.
class Devise::InvitationsController < DeviseController
prepend_before_filter :resource_from_invitation_token, :only => [:edit, :destroy]
def resource_from_invitation_token
# Here 'resource_class' is my Postulant model, so when I call
# 'find_by_invitation_token' applies the defined default_scope
# which doesn't word without 'scope_current_tenancy'
unless params[:invitation_token] && self.resource = resource_class.find_by_invitation_token(params[:invitation_token], true)
set_flash_message(:alert, :invitation_token_invalid)
redirect_to after_sign_out_path_for(resource_name)
end
end
end
So my question is, is there a way to run :scope_current_tenancy
before than :resource_from_invitation_token
?
I've tried to change around_filter :scope_current_tenancy
to prepend_around_filter :scope_current_tenancy
but I had no luck. Any thoughts?