Skip to content
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

Extend this gem to use multiple resources? #200

Open
raderj89 opened this issue Apr 23, 2019 · 2 comments
Open

Extend this gem to use multiple resources? #200

raderj89 opened this issue Apr 23, 2019 · 2 comments

Comments

@raderj89
Copy link

raderj89 commented Apr 23, 2019

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:

    module ClassMethods
      def acts_as_scopable(*resources) # not doing anything with this yet, but could be used for validation
        default_scope lambda {
          if ActsAsScopable.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.id
            end

            where(query_criteria)
          else
            all
          end
        }
      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.

@emilebosch
Copy link

@raderj89 Did u ever find a good solution for this? I'd like this too for tenants that are bi-directional. I.e invoice.

Class Invoice < AR
  acts_as_tenant :buyer
  acts_as_tenant :supplier
end

So when logging in as a supplier you can see the invoices received and as buyer the invoices sent.

@jairovm
Copy link

jairovm commented Mar 31, 2021

This is what I ended up doing in order to scope my tables by multiple tenants rails/rails#40720 (comment)

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

No branches or pull requests

4 participants