We've moved discussions to Discord

Scaffolds that only show for impersonal accounts

Jake Smith
Hey Chris Oliver , I could have sworn someone or myself already asked this, but I couldn't find it anywhere:

Is there a preferred way of limiting a scaffolded feature to only be available to impersonal accounts?  I'd like to offer group chat to those who are logged into one of their impersonal accounts.  There really isn't a reason for that to show up when you are logged into your personal account if you tied Channels to Accounts.

Beyond not showing it in the navbar, would I need to nest my channel resources inside of accounts in routes or something?

Jake Smith
I came back to this, and I was wondering if anyone else could think of anything else I should do besides the following:

app/views/shared/_left_nav.html.erb:
<%= nav_link_to "Channels", channels_path, class: 'nav-link' unless current_account.personal? %>

app/controllers/channels_controller.rb:
class ChannelsController < ApplicationController
  ...
  before_action :require_impersonal_account

... def require_impersonal_account return unless current_account.personal? flash[:alert] = 'This feature is not supported for personal accounts' redirect_back fallback_location: root_path end end
Chris Oliver
That should work and is what I would do. 👍
Jake Smith
Thanks Chris Oliver !

Jake Smith
Chris Oliver I'm having a bit of trouble getting my controller tests to pass.  This is related to another forum post where we asked about setting the current_account in a test for integration tests, and I can't get switch_account to work:

class ChannelsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @channel = channels(:one)
    @account = @channel.account
    @user = @account.owner
    sign_in @user
    switch_account(@account)
  end

  ...
end

My channel fixtures both point their account to company who has personal: false.

In my ChannelController, I still have:

class ChannelsController < ApplicationController
  before_action :authenticate_user!
  before_action :require_impersonal_account

  ...

  private

  ...

  def require_impersonal_account
    return unless current_account.personal?

    flash[:alert] = 'Channels are not supported for personal accounts'
    redirect_back fallback_location: root_path
  end
end

Unfortunately, current_account points to the personal account of the signed in user.  I also tried setting Current.account = @account in my tests, but it still seems to not take effect.  Any ideas?
Jake Smith
It looks like if I put script_name: "/#{@account.id}" on all of my path helpers in my channels_controller_test, I can get the tests to pass.  With this, I don't need to call switch_account or manually set the account other than with script_name
Notifications
You’re not receiving notifications from this thread.