You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was working on a project recently that uses this gem. An additional request came up where the client wanted certain models to be automatically scoped by location in addition to the tenant. So for example, say a Company (which was the tenant) has many Locations and Customers. If a Customer gives a Feedback at a Location, or if they buy a Product available at that Location, the resources should be scoped to that Location.
Instead of updating many controller methods to be aware of locations, I realized I could take a similar approach as this gem does and store the selected location in RequestStore and provide a default_scope very similar to what's provided here. Here's what my naive implementation looked like:
moduleClassMethodsdefacts_as_scopable(*resources)# not doing anything with this yet, but could be used for validationdefault_scopelambda{ifActsAsScopable.current_resource_scopes.present?query_criteria=ActsAsScopable.current_resource_scopes.each_with_object({})do |record,criteria|
criteria["#{record.class.name.downcase}_id".to_sym]=record.idendwhere(query_criteria)elseallend}end
In the base controller, I set the resource scopes, which in this case were the current_tenant and current_location.
I'm not sure if this departs too much from the spirit of this gem and should be implemented in a different gem or if it would be valuable to make this gem able to scope models by as many resources as someone might want.
The text was updated successfully, but these errors were encountered:
I was working on a project recently that uses this gem. An additional request came up where the client wanted certain models to be automatically scoped by location in addition to the tenant. So for example, say a Company (which was the tenant) has many Locations and Customers. If a Customer gives a Feedback at a Location, or if they buy a Product available at that Location, the resources should be scoped to that Location.
Instead of updating many controller methods to be aware of locations, I realized I could take a similar approach as this gem does and store the selected location in RequestStore and provide a
default_scope
very similar to what's provided here. Here's what my naive implementation looked like:In the base controller, I set the resource scopes, which in this case were the
current_tenant
andcurrent_location
.I'm not sure if this departs too much from the spirit of this gem and should be implemented in a different gem or if it would be valuable to make this gem able to scope models by as many resources as someone might want.
The text was updated successfully, but these errors were encountered: