ransack
ransack copied to clipboard
Make searches chainable using and/or
I'd like to add a feature for merging searches like search_parents.or(search_children). With #412 it becomes doable by setting up a shared context to handle the join aliases before building the search conditions:
shared_context = Ransack::Context.for(Person)
search_parents = Person.search({parent_name_eq: "A"}, context: shared_context)
search_children = Person.search({children_name_eq: "B"}, context: shared_context)
shared_conditions = [search_parents, search_children].map { |search|
Ransack::Visitor.new.accept(search.base)
}.reduce(&:or)
Person.joins(shared_context.join_sources).where(shared_conditions)
Currently the context needs to be passed around before initializing searches, because the initializer automatically calls build to populate the condition tree. This would need to change to evaluate lazily.
I unfortunately cannot use this at all. Ransack::Visitor.new.accept just returns nil.
+1
+1