module TenancyMixin
  extend ActiveSupport::Concern

  included do
    before_validation :set_tenant
  end

  module ClassMethods
    def scope_by_tenant?
      true
    end

    def accessible_tenant_ids(user_or_group, strategy)
      tenant = user_or_group.try(:current_tenant)
      return [] unless tenant

      tenant.accessible_tenant_ids(strategy)
    end
  end

  def set_tenant
    # Priority
    #   Owning group
    #   Current user
    #   Parent EMS
    #   Root tenant
    self.tenant_id ||= if respond_to?(:miq_group) && miq_group
                         miq_group.tenant_id
                       elsif User.current_tenant
                         User.current_tenant.id
                       elsif respond_to?(:ext_management_system) && ext_management_system
                         ext_management_system.tenant_id
                       else
                         Tenant.root_tenant.try(:id)
                       end
  end
end

module TenancyMixin
  extend ActiveSupport::Concern

  included do
    before_validation :set_tenant
  end

  module ClassMethods
    def scope_by_tenant?
      true
    end

    def accessible_tenant_ids(user_or_group, strategy)
      tenant = user_or_group.try(:current_tenant)
      return [] unless tenant

      tenant.accessible_tenant_ids(strategy)
    end
  end

  def set_tenant
    # Priority
    #   Owning group
    #   Current user
    #   Parent EMS
    #   Root tenant
    self.tenant_id ||= if respond_to?(:miq_group) && miq_group.try(:tenant_id)
                         miq_group.tenant_id
                       elsif User.current_tenant.try(:id)
                         User.current_tenant.id
                       elsif respond_to?(:ext_management_system) && ext_management_system.try(:tenant_id)
                         ext_management_system.tenant_id
                       else
                         Tenant.root_tenant.try(:id)
                       end
  end
end

Handle case of nil tenant id on group, user, and ems.
Handle case of nil tenant id on group, user, and ems.

If the group, user, ems is nil (handled already) or if their tenant id
is nil(fixed in this commit), we need to fall through to the next option.

Owning group -> current user -> parent ems -> root tenant

Fixes some API specs where a Vmware::InfraManager wasn't falling through
to the last case, getting the default tenant, because the instance
responded to ext_management_system (returning self) but the instance
didn't have a tenant_id value.
