Skip to content

Fix topics filtering by provider #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Oli0li
Copy link
Collaborator

@Oli0li Oli0li commented Apr 13, 2025

What Issue Does This PR Cover, If Any?

Resolves #136

What Changed? And Why Did It Change?

It redefines the scope of Topics shown on the Topics page to ensure we only display Topics from the currently selected provider.
This fixes a bug that, for Admin users, caused all Topics to be displayed regardless of the Provider, even though the name of the first Provider's was displayed at the top and gave the impression that the Topics were already filtered by Provider.

How Has This Been Tested?

With system tests (spec/system/topic_search_spec.rb) and by hand

Please Provide Screenshots

Changing.provider.as.Admin.mp4

Additional Comments

After making this change, I saw some tests were failing because they are set up in a way where users who are Contributors have no Providers. So I made it so that Contributors can still access the "Topics" page even if they don't have any Providers, in case that's something that may happen when we first add users, but that means they wouldn't be able to see any Topics or create new ones. We could display a message such as "You haven't been added to a provider yet" on the Topics page to clarify the situation, or we could redirect them to the dashboard if they don't have a provider. I wasn't sure what the desired behaviour was, please let me know and I can make the changes.

@Oli0li Oli0li force-pushed the fix-topics-sorted-by-provider branch 2 times, most recently from 2800332 to 0f6687d Compare April 13, 2025 12:56
@Oli0li Oli0li requested a review from dcollie2 April 13, 2025 12:59
@Oli0li Oli0li force-pushed the fix-topics-sorted-by-provider branch from 0f6687d to e969ece Compare April 14, 2025 18:37
@dmitrytrager
Copy link
Collaborator

@Oli0li please fix conflicts

@Oli0li Oli0li force-pushed the fix-topics-sorted-by-provider branch from e969ece to 7c95492 Compare April 16, 2025 21:52
@Oli0li
Copy link
Collaborator Author

Oli0li commented Apr 16, 2025

@Oli0li please fix conflicts

Thank you @dmitrytrager! I just resolved the conflict and added a commit to fix a warning I noticed in the tests.

@Oli0li Oli0li requested a review from dmitrytrager April 16, 2025 21:57
else
Current.user.topics
end.includes(:language)
@scope ||= current_provider&.topics&.includes(:language) || Topic.none
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of having Topic.none here.
What if we also define null object for provider and return it if there is no current provider.
We can define topics association for this null object and will return Topic.none there. Using this approach we will have just current_provider.topics.includes(:language) without &s. Wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @dmitrytrager! I hadn't used this pattern before, it's very interesting. I added a new commit to add this NullProvider, please let me know what you think.

Oli0li added 4 commits April 19, 2025 15:35
For Admins as for Contributors, we want to only display the topics that
belong to the first provider they are entitled to see, as this provider's
name is displayed by default on the index page. Users are then able to
see topics from other providers they are entitled to check by selecting
them from the dropdown menu.

The provider_id param is not part of the search form, it is part of the
provider form in the choose_provider partial that allows us to set the
current_provider_id cookie in SettingsController, so it wasn't being
passed. We can ensure we see only relevant topics by redefining #scope,
which will change according to the presence of current_provider_id cookie,
instead of having a wider scope and then trying to check the params.
I think this method is currently unnecessary, as we are only showing
topics scoped by provider. But if we wanted something like this, I would
like to suggest using an association:

has_many :topics, through: :providers"

or, if we want to keep this as a method, change:

Topic.where(provider_id: providers.pluck(:id))

to:

Topic.where(provider_id: providers)

as it would allow us to get the topics in 1 query instead of having 1
query for plucking the providers ids and then another for getting the topics.
This change fixes the "searching by tags" tests by addressing this
warning:

 "warning: Locator Language:#<Language id: 448, name: "Basic", created_at:
 "2025-04-16 21:45:50.059603000 +0000", updated_at: "2025-04-16
 21:45:50.059603000 +0000"> for selector :option must be an instance of
 String or Symbol or Integer. This will raise an error in a future version
 of Capybara."
This would be in case a user does not have a provider set up yet.
@Oli0li Oli0li force-pushed the fix-topics-sorted-by-provider branch from 974c6b4 to 00d0c84 Compare April 19, 2025 14:35
@Oli0li Oli0li requested a review from dmitrytrager April 19, 2025 14:38
Copy link
Collaborator

@dmitrytrager dmitrytrager left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Topics not filtering by provider for admin
2 participants