Skip to content

Commit

Permalink
Merge pull request #378 from openstax/fix_multiple_filters
Browse files Browse the repository at this point in the history
Search improvements
  • Loading branch information
Dantemss authored Aug 9, 2022
2 parents 0a5eeeb + 0448ac8 commit 0876f91
Show file tree
Hide file tree
Showing 4 changed files with 372 additions and 56 deletions.
92 changes: 70 additions & 22 deletions app/routines/search_exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ def exec(params = {}, options = {})
sanitized_tags = to_string_array(tag).map(&:downcase)
next @items = @items.none if sanitized_tags.empty?

@items = @items.joins(:tags).where(tags: { name: sanitized_tags })
@items = @items.where(
ExerciseTag.joins(:tag).where(
'"exercise_tags"."exercise_id" = "exercises"."id"'
).where(tag: { name: sanitized_tags }).arel.exists
)
end
end

Expand Down Expand Up @@ -189,11 +193,16 @@ def exec(params = {}, options = {})
sn = to_string_array(name, append_wildcard: true)
next @items = @items.none if sn.empty?

@items = @items.joins(publication: { authors: { user: :account } }).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn)))
@items = @items.joins(:publication).where(
Author.joins(user: :account).where(
'"authors"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists
)
end
end

Expand All @@ -202,11 +211,16 @@ def exec(params = {}, options = {})
sn = to_string_array(name, append_wildcard: true)
next @items = @items.none if sn.empty?

@items = @items.joins(publication: { copyright_holders: { user: :account } }).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn)))
@items = @items.joins(:publication).where(
CopyrightHolder.joins(user: :account).where(
'"copyright_holders"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists
)
end
end

Expand All @@ -215,17 +229,51 @@ def exec(params = {}, options = {})
sn = to_string_array(name, append_wildcard: true)
next @items = @items.none if sn.empty?

@items = @items.joins(
publication: { authors: { user: :account }, copyright_holders: { user: :account } }
).where(
acct_author[:username].matches_any(sn)
.or(acct_author[:first_name].matches_any(sn))
.or(acct_author[:last_name].matches_any(sn))
.or(acct_author[:full_name].matches_any(sn))
.or(acct_copyright[:username].matches_any(sn))
.or(acct_copyright[:first_name].matches_any(sn))
.or(acct_copyright[:last_name].matches_any(sn))
.or(acct_copyright[:full_name].matches_any(sn)))
@items = @items.joins(:publication).where(
Author.joins(user: :account).where(
'"authors"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists.or(
CopyrightHolder.joins(user: :account).where(
'"copyright_holders"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists
)
)
end
end

with.keyword :format do |formats|
formats.each do |format|
sanitized_formats = to_string_array(format).map(&:downcase)
next @items = @items.none if sanitized_formats.empty?

@items = @items.where(
Question.joins(stems: :stylings).where(
'"questions"."exercise_id" = "exercises"."id"'
).where(stems: { stylings: { style: sanitized_formats } }).arel.exists
)
end
end

with.keyword :solutions_are_public do |saps|
saps.each do |sap|
sanitized_saps = to_string_array(sap).map do |str|
ActiveModel::Type::Boolean.new.cast(str)
end
next @items = @items.none if sanitized_saps.empty?

@items = @items.joins(publication: :publication_group).where(
publication: { publication_group: { solutions_are_public: sanitized_saps } }
)
end
end
end
Expand Down
79 changes: 57 additions & 22 deletions app/routines/search_vocab_terms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ def exec(params = {}, options = {})
sanitized_tags = to_string_array(tag).map(&:downcase)
next @items = @items.none if sanitized_tags.empty?

@items = @items.joins(:tags).where(tags: { name: sanitized_tags })
@items = @items.where(
VocabTermTag.joins(:tag).where(
'"vocab_term_tags"."vocab_term_id" = "vocab_terms"."id"'
).where(tag: { name: sanitized_tags }).arel.exists
)
end
end

Expand Down Expand Up @@ -198,11 +202,16 @@ def exec(params = {}, options = {})
sn = to_string_array(name, append_wildcard: true)
next @items = @items.none if sn.empty?

@items = @items.joins(publication: { authors: { user: :account } }).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn)))
@items = @items.joins(:publication).where(
Author.joins(user: :account).where(
'"authors"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists
)
end
end

Expand All @@ -211,11 +220,16 @@ def exec(params = {}, options = {})
sn = to_string_array(name, append_wildcard: true)
next @items = @items.none if sn.empty?

@items = @items.joins(publication: { copyright_holders: { user: :account } }).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn)))
@items = @items.joins(:publication).where(
CopyrightHolder.joins(user: :account).where(
'"copyright_holders"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists
)
end
end

Expand All @@ -224,17 +238,38 @@ def exec(params = {}, options = {})
sn = to_string_array(name, append_wildcard: true)
next @items = @items.none if sn.empty?

@items = @items.joins(
publication: { authors: { user: :account }, copyright_holders: { user: :account } }
).where(
acct_author[:username].matches_any(sn)
.or(acct_author[:first_name].matches_any(sn))
.or(acct_author[:last_name].matches_any(sn))
.or(acct_author[:full_name].matches_any(sn))
.or(acct_copyright[:username].matches_any(sn))
.or(acct_copyright[:first_name].matches_any(sn))
.or(acct_copyright[:last_name].matches_any(sn))
.or(acct_copyright[:full_name].matches_any(sn)))
@items = @items.joins(:publication).where(
Author.joins(user: :account).where(
'"authors"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists.or(
CopyrightHolder.joins(user: :account).where(
'"copyright_holders"."publication_id" = "publication"."id"'
).where(
acct[:username].matches_any(sn)
.or(acct[:first_name].matches_any(sn))
.or(acct[:last_name].matches_any(sn))
.or(acct[:full_name].matches_any(sn))
).arel.exists
)
)
end
end

with.keyword :solutions_are_public do |saps|
saps.each do |sap|
sanitized_saps = to_string_array(sap).map do |str|
ActiveModel::Type::Boolean.new.cast(str)
end
next @items = @items.none if sanitized_saps.empty?

@items = @items.joins(publication: :publication_group).where(
publication: { publication_group: { solutions_are_public: sanitized_saps } }
)
end
end
end
Expand Down
Loading

0 comments on commit 0876f91

Please sign in to comment.