Skip to content

Commit

Permalink
ユーザーネーム(最終更新者)で検索できるよ user:komagataのような検索ワードでも絞り込めるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamu-kazu222 committed Nov 28, 2024
1 parent 74a7384 commit a9e3493
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/models/searcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ def self.result_for_all(words)
.reject { |type| type == :users }
.flat_map do |type|
model = model(type)
next [] unless model.column_names.include?('user_id')
model.where(user_id: user.id)
next [] unless model.column_names.include?('user_id') || model.column_names.include?('last_updated_user_id')
if type == :practices
model.where("last_updated_user_id = ?", user.id)
else
model.where(user_id: user.id)
end
end
.uniq
.select { |result| words.all? { |word| result_matches_keyword?(result, word) } }
Expand All @@ -110,7 +114,11 @@ def self.result_for(type, words, commentable_type: nil)
user = User.find_by(login_name: username)
return [] unless user

results = model(type).where(user_id: user.id)
results = if type == :practices
model(type).where("user_id = ? OR last_updated_user_id = ?", user.id, user.id)
else
model(type).where(user_id: user.id)
end
else
results = if commentable?(type)
model(type).search_by_keywords(words:, commentable_type:) +
Expand All @@ -127,9 +135,13 @@ def self.result_matches_keyword?(result, word)

if word.match(/^user:(\w+)$/)
username = Regexp.last_match(1)
return result.user&.login_name == username
if result.is_a?(Practice)
user = User.find_by(id: result.last_updated_user_id)
return user&.login_name == username
else
return result.user&.login_name == username
end
end

searchable_fields = [result.try(:title), result.try(:body), result.try(:description)]
searchable_fields.any? { |field| field.to_s.include?(word) }
end
Expand Down

0 comments on commit a9e3493

Please sign in to comment.