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

Filtering Field::HasMany #1988

Open
vijuarez opened this issue May 19, 2021 · 10 comments
Open

Filtering Field::HasMany #1988

vijuarez opened this issue May 19, 2021 · 10 comments
Labels
fields new fields, displaying and editing data fields-context

Comments

@vijuarez
Copy link

  • What would you like to be able to do? Can you provide some examples?

See, I've got a lot of users with different characteristics on my app. For example, they all have a role, which is an enum. Some models have references to users, but some of them have restrictions related to roles. For example, you can't select an user who's a guess to be the editor on a post. I've got all of that figured out via validations, but it would be nice to have it reflected on the interface. As if, when I start editing the editors attribute on a certain post, only users with the role author or admin appear as options.

  • How could we go about implementing that?

Passing some kind of filter (maybe a function) to the field on user_dashboard.rb would be nice.

  • Can you think of other approaches to the problem?

I could just filter it out during rendering (modifying the partial for Field::HasMany), but that's not nice to do if I want to do it on a lot of dashboards with many different conditions. A minimal example on how to do this would be extremely useful to me.

@szabcsee
Copy link

I also had to go with modifying the partial.

@pablobm
Copy link
Collaborator

pablobm commented Jun 10, 2021

Yeah, I don't think there's a way to do this at the moment. Perhaps it would be a good idea to add a new option to the field that allows for this. It would probably have to modify HasMany#candidate_resources to use this new option. This is the concerned code:

def candidate_resources
if options.key?(:includes)
includes = options.fetch(:includes)
associated_class.includes(*includes).all
else
associated_class.all
end
end

Perhaps someone would like to volunteer a PR?

@vijuarez
Copy link
Author

I prepared a very rudimentary implementation here, but it's not that thought out. It would be probably better to pass scopes rather than plain where instructions, and I don't know how to filter with model methods. It's a start though.

https://github.com/vijuarez/administrate/blob/fd157d238f7f0049afac18edba1be22655c9cf3e/lib/administrate/field/has_many.rb#L85-L100

@pablobm
Copy link
Collaborator

pablobm commented Jun 17, 2021

Yup, that's the sort of thing we would need. I think it could be done better if it accepted a call-able instead, like a lambda. This is what we already do for Select and BelongsTo. For example:

def collection
values = options.fetch(:collection, [])
if values.respond_to? :call
return values.arity.positive? ? values.call(self) : values.call
end
@collection ||= values
end

benoror added a commit to benoror/administrate that referenced this issue Sep 21, 2022
@benoror
Copy link

benoror commented Sep 21, 2022

Hi! Stumbled upon this issue while needing filtering on HasMany as well.

Based on current implementation for BelongsTo, would it make sense to emulate it in HasMany?:

main...benoror:administrate:patch-1

@benoror
Copy link

benoror commented Sep 22, 2022

Found this extension that can achieve the same thing: https://github.com/XPBytes/administrate-field-scoped_has_many

@pablobm pablobm added fields new fields, displaying and editing data fields-context labels Apr 20, 2023
@9mm
Copy link

9mm commented May 12, 2023

it would be great to just get this in here because that extension doesnt provide support for nice many-to-many fields etc, its just an awful html multiselect which is impossible to use for large options

@benoror
Copy link

benoror commented May 12, 2023

@9mm
Copy link

9mm commented May 12, 2023

Awesome, thanks!

@benoror
Copy link

benoror commented Jul 11, 2023

I ended up disliking the Dropdown component used in XPBytes/administrate-field-scoped_has_many due to usability reasons.

I tried coming back to using the OG HasMany implementation from Administrate and the before-mentioned https://github.com/thoughtbot/administrate/blob/main/docs/guides/scoping_has_many_relations.md guide, but faced issues with :source param in a :through relationship, similarly as exposed here: #681

I ended up MonkeyPatching to support a scope option, inspired by the [https://github.com/XPBytes/administrate-field-scoped_has_many/blob/4f13967cb405b94829ae75a22f100d29f2743b2c/lib/administrate/field/scoped_has_many.rb#L24C1-L29C1](very same XPBytes overriden implementation):

module Administrate
  module Field
    class HasMany < Associative
      private

      def candidate_resources
        puts options[:scope]
        scope = options[:scope] ? options[:scope].call(self) : associated_class.all
        scope = scope.includes(*options.fetch(:includes)) if options.key?(:includes)

        scope
      end
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fields new fields, displaying and editing data fields-context
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants