Skip to content

Make searches chainable using and/or #417

@avit

Description

@avit

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions