Skip to content

Commit

Permalink
User#indexでユーザーのキーワード検索をできるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamu-kazu222 committed Feb 15, 2025
1 parent 7cb22ac commit 1b0708f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
7 changes: 4 additions & 3 deletions app/models/concerns/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ def columns_for_keyword_search(*column_names)
private

def params_for_keyword_search(searched_values = {})
return {} if searched_values[:words].blank?
return {} if searched_values[:word].blank?

groupings = searched_values[:words].map do |word|
groupings = searched_values[:word].split(/[[:blank:]]+/).map do |word|
if word.start_with?('user:')
create_parameter_for_search_user_id(word.delete_prefix('user:'))
else
{ _join_column_names => word }
end
end
{ combinator: 'and', groupings: }

{ combinator: 'or', groupings: groupings }
end

def word_to_groupings(word)
Expand Down
8 changes: 2 additions & 6 deletions app/models/search_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def initialize(word:, users: nil, target: nil, require_retire_user: false)

def search
validated_search_word = validate_search_word
return User.none if validated_search_word.blank?

searched_user = @users ? @users.search_by_keywords(words: [validated_search_word]) : User.search_by_keywords({ words: [validated_search_word] })
searched_user = @users ? @users.merge(User.search_by_keywords(word: validated_search_word)) : User.search_by_keywords(word: validated_search_word)

if @target == 'retired'
searched_user.unscope(where: :retired_on).retired
Expand All @@ -24,11 +22,9 @@ def search
end

def validate_search_word
return '' if @word.nil?

stripped_word = @word.strip
if stripped_word.match?(/^[\w-]+$/)
stripped_word.length >= 3 ? stripped_word : nil
stripped_word if stripped_word.length >= 3
elsif stripped_word.length >= 2
stripped_word
end
Expand Down

0 comments on commit 1b0708f

Please sign in to comment.