Not having much luck in searching the docs for the...
# avo-2
b
Not having much luck in searching the docs for the best solution for this πŸ˜“ Anyone know of the best way to filter records (like on the index pages) based on the current_user? For example, I have multiple users (that have a company_id) and I want the logged in users to only see the
Product.where(company_id: current_user.company_id
on their resource index page. Is using Pundit the way to do that? Going to keep looking through docs since I know it's somewhere there 😊
l
I think pundit scopes will work very well for that scenario
t
What about accessing the
current_user
object inside Avo Filter objects?
Use case would be having
options
for a Select unique to a User's records on some table.
That way instead of selecting the Site Categories for a Blog Article, the User could create their own Categories and file the Post under that, but other users wouldn't see every other user's Post Categories.
So I could have "Cool Stuff" and "New Stuff" but you might have "Q2 Release Info" and "Q4 Release Info"
l
You can access the current user through the context object
b
Thanks for the guidance again Adrian. I thought using Pundit was going to be too much but it was as simple as just scoping it out properly for all my models / resources:
Copy code
class Scope < Scope
  def resolve
    scope.where(company_id: user.company_id)
  end
end
t
The context is always nil in the filter.
l
Hmm
But have you configured it in the initializer?
And how are you valling it in the filter?
t
Copy code
╰─$ cat config/initializers/avo.rb
Avo.configure do |config|
  config.set_context do
    {
      current_user: current_user,
      params: request.params
    }
  end
Copy code
12: def default
 => 13:   binding.pry
    14: end

[1] pry(#<ProfileUsersFilter>)> ls
ActiveSupport::ToJsonWithActiveSupportEncoder#methods: to_json
ActiveSupport::Dependencies::RequireDependency#methods: require_dependency
Filter#methods:
  applied_filters  applied_or_default_value  apply_query  component  component=  component?  default=  default?  empty_message  empty_message=  empty_message?  id  name  name=  name?  params  template  template=  template?
Profile Users#methods: apply  default  options
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  pry_instance
[2] pry(#<ProfileUsersFilter>)> context
NameError: undefined local variable or method `context' for #<ProfileUsersFilter:0x00007fe860fe7ea0>
from (pry):1:in `default'
[3] pry(#<ProfileUsersFilter>)>
There is no context in a filter.
It's an uninstantiated variable.
Context works everywhere else, as expected.
But, not at all in Filters.
l
It’s a global variable and tou call it with ::Avo::App.context
t
I missed that part.
Sure enough, it's there.
Thanks!
l
For sure!
t
> You can access the context data with
::Avo::App.context
object. There it was, sitting right under the instructions I had no issues following right up until that point. Ha!
l
It happens to me too
t
hehe
l
In some classes I delegate the context to avo::app
t
Yep, I guess that's why
context
outright works in some places, and not in others. Working as intended πŸ‘ πŸ‘
l
I’m not yet sure the delegation is best, but it’s definitely annoying that it’s not the same everywhere
t
minor nitpicking at this point if it works πŸ™‚
7 Views