11# frozen_string_literal: true
22
33module BetterTogether
4- class PersonBlocksController < ApplicationController # rubocop:todo Style/Documentation
4+ class PersonBlocksController < ApplicationController # rubocop:todo Style/Documentation, Metrics/ClassLength
55 before_action :authenticate_user!
66 before_action :set_person_block , only : :destroy
77 after_action :verify_authorized
@@ -20,7 +20,7 @@ def index # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
2020 @blocked_people = @blocked_people . where ( id : authorized_person_ids )
2121 . includes ( :string_translations )
2222 . references ( :string_translations )
23- . where ( string_translations : { key : [ ' name' , ' slug' ] } )
23+ . where ( string_translations : { key : %w[ name slug ] } )
2424 . where ( 'string_translations.value ILIKE ?' , "%#{ search_term } %" )
2525 . distinct
2626 end
@@ -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' , ' slug' ] } )
40+ . where ( string_translations : { key : %w[ name slug ] } )
4141 . where ( 'string_translations.value ILIKE ?' , "%#{ search_term } %" )
4242 . distinct
4343 end
@@ -78,39 +78,11 @@ def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
7878
7979 def search
8080 authorize PersonBlock
81-
81+
8282 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-
83+ blockable_people = find_blockable_people ( search_term )
84+ people_data = format_people_for_select ( blockable_people )
85+
11486 render json : people_data
11587 end
11688
@@ -146,5 +118,42 @@ def set_person_block
146118 def person_block_params
147119 params . require ( :person_block ) . permit ( :blocked_id )
148120 end
121+
122+ def find_blockable_people ( search_term )
123+ current_person = helpers . current_person
124+ blockable_people = base_blockable_people_scope ( current_person )
125+
126+ return blockable_people unless search_term . present?
127+
128+ filter_by_search_term ( blockable_people , search_term )
129+ end
130+
131+ def base_blockable_people_scope ( current_person )
132+ policy_scope ( BetterTogether ::Person )
133+ . where . not ( id : current_person . id ) # Can't block yourself
134+ . where . not ( id : current_person . blocked_people . select ( :id ) ) # Already blocked
135+ end
136+
137+ def filter_by_search_term ( scope , search_term )
138+ search_pattern = "%#{ search_term } %"
139+ scope . i18n . where (
140+ 'mobility_string_translations.value ILIKE ?' ,
141+ search_pattern
142+ ) . where (
143+ mobility_string_translations : { key : %w[ name slug ] }
144+ )
145+ end
146+
147+ def format_people_for_select ( people )
148+ people . limit ( 20 ) . map do |person |
149+ {
150+ text : person . name , # This will be the display text
151+ value : person . id . to_s ,
152+ data : {
153+ slug : person . slug
154+ }
155+ }
156+ end
157+ end
149158 end
150159end
0 commit comments