@@ -9,26 +9,26 @@ class PersonBlocksController < ApplicationController # rubocop:todo Style/Docume
99 def index # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
1010 authorize PersonBlock
1111
12- # AC-2.11: I can search through my blocked users by name
12+ # AC-2.11: I can search through my blocked users by name and slug
1313 @blocked_people = helpers . current_person . blocked_people
1414 if params [ :search ] . present?
15- # Search by translated name using includes and references
15+ # Search by translated name and slug using includes and references
1616 # Apply policy scope to ensure only authorized people are searchable
1717 search_term = params [ :search ] . strip
1818 authorized_person_ids = policy_scope ( BetterTogether ::Person ) . pluck ( :id )
1919
2020 @blocked_people = @blocked_people . where ( id : authorized_person_ids )
2121 . includes ( :string_translations )
2222 . references ( :string_translations )
23- . where ( string_translations : { key : 'name' } )
23+ . where ( string_translations : { key : [ 'name' , 'slug' ] } )
2424 . where ( 'string_translations.value ILIKE ?' , "%#{ search_term } %" )
2525 . distinct
2626 end
2727
2828 # AC-2.12: I can see when I blocked each user (provide person_blocks for timestamp info)
2929 @person_blocks = helpers . current_person . person_blocks . includes ( :blocked )
3030 if params [ :search ] . present?
31- # Filter person_blocks by matching blocked person names
31+ # Filter person_blocks by matching blocked person names and slugs
3232 # Apply policy scope to ensure only authorized people are searchable
3333 search_term = params [ :search ] . strip
3434 authorized_person_ids = policy_scope ( BetterTogether ::Person ) . pluck ( :id )
@@ -37,7 +37,7 @@ def index # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
3737 . where ( better_together_people : { id : authorized_person_ids } )
3838 . includes ( blocked : :string_translations )
3939 . references ( :string_translations )
40- . where ( string_translations : { key : 'name' } )
40+ . where ( string_translations : { key : [ 'name' , 'slug' ] } )
4141 . where ( 'string_translations.value ILIKE ?' , "%#{ search_term } %" )
4242 . distinct
4343 end
@@ -56,15 +56,6 @@ def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
5656 @person_block = helpers . current_person . person_blocks . new ( person_block_params )
5757 # rubocop:enable Layout/IndentationWidth
5858
59- # AC-2.13: I can block a user by entering their username or email
60- if params [ :person_block ] [ :blocked_identifier ] . present? # rubocop:todo Layout/IndentationConsistency
61- target_person = BetterTogether ::Person . find_by ( identifier : params [ :person_block ] [ :blocked_identifier ] ) ||
62- # rubocop:todo Layout/LineLength
63- BetterTogether ::Person . joins ( :user ) . find_by ( better_together_users : { email : params [ :person_block ] [ :blocked_identifier ] } )
64- # rubocop:enable Layout/LineLength
65- @person_block . blocked = target_person if target_person
66- end
67-
6859 authorize @person_block # rubocop:todo Layout/IndentationConsistency
6960
7061 respond_to do |format | # rubocop:todo Layout/IndentationConsistency
@@ -85,6 +76,44 @@ def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
8576 end
8677 end
8778
79+ def search
80+ authorize PersonBlock
81+
82+ search_term = params [ :q ] . to_s . strip
83+ current_person = helpers . current_person
84+
85+ # Get all people that could potentially be blocked (policy scoped)
86+ blockable_people = policy_scope ( BetterTogether ::Person )
87+ . where . not ( id : current_person . id ) # Can't block yourself
88+ . where . not ( id : current_person . blocked_people . select ( :id ) ) # Already blocked
89+
90+ if search_term . present?
91+ # Use Mobility's i18n scope to search across translations for name and slug
92+ search_pattern = "%#{ search_term } %"
93+ blockable_people = blockable_people . i18n . where (
94+ 'mobility_string_translations.value ILIKE ?' ,
95+ search_pattern
96+ ) . where (
97+ mobility_string_translations : { key : [ 'name' , 'slug' ] }
98+ )
99+ end
100+
101+ # Limit results and format for slim-select
102+ people_data = blockable_people
103+ . limit ( 20 )
104+ . map do |person |
105+ {
106+ text : person . name , # This will be the display text
107+ value : person . id . to_s ,
108+ data : {
109+ slug : person . slug
110+ }
111+ }
112+ end
113+
114+ render json : people_data
115+ end
116+
88117 def destroy
89118 authorize @person_block
90119 @person_block . destroy
0 commit comments