@@ -92,8 +92,12 @@ def position # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
9292 respond_to do |format |
9393 format . html { redirect_to request . referer || checklist_path ( @checklist ) , notice : t ( 'flash.generic.updated' ) }
9494 format . turbo_stream do
95- render turbo_stream : turbo_stream . replace ( helpers . dom_id ( @checklist , :checklist_items ) ,
96- partial : 'better_together/checklist_items/checklist_item' , collection : @checklist . checklist_items . with_translations , as : :checklist_item )
95+ render turbo_stream : turbo_stream . replace (
96+ helpers . dom_id ( @checklist , :checklist_items ) ,
97+ partial : 'better_together/checklist_items/checklist_item' ,
98+ collection : @checklist . checklist_items . with_translations ,
99+ as : :checklist_item
100+ )
97101 end
98102 end
99103 end
@@ -119,8 +123,12 @@ def reorder # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
119123 respond_to do |format |
120124 format . json { head :no_content }
121125 format . turbo_stream do
122- render turbo_stream : turbo_stream . replace ( helpers . dom_id ( @checklist , :checklist_items ) ,
123- partial : 'better_together/checklist_items/checklist_item' , collection : @checklist . checklist_items . with_translations , as : :checklist_item )
126+ render turbo_stream : turbo_stream . replace (
127+ helpers . dom_id ( @checklist , :checklist_items ) ,
128+ partial : 'better_together/checklist_items/checklist_item' ,
129+ collection : @checklist . checklist_items . with_translations ,
130+ as : :checklist_item
131+ )
124132 end
125133 end
126134 end
@@ -129,40 +137,36 @@ def reorder # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
129137
130138 def set_checklist
131139 key = params [ :checklist_id ] || params [ :id ]
132-
133140 @checklist = nil
134141 if key . present?
135- # Try direct id/identifier lookup first (fast)
136- @checklist = BetterTogether ::Checklist . where ( id : key ) . or ( BetterTogether ::Checklist . where ( identifier : key ) ) . first
137-
138- # Fallbacks to mirror FriendlyResourceController behaviour: try translated slug lookups
139- if @checklist . nil?
140- begin
141- # Try Mobility translation lookup across locales
142- translation = Mobility ::Backends ::ActiveRecord ::KeyValue ::StringTranslation . where (
143- translatable_type : 'BetterTogether::Checklist' ,
144- key : 'slug' ,
145- value : key
146- ) . includes ( :translatable ) . last
147-
148- @checklist ||= translation &.translatable
149- rescue StandardError
150- # ignore DB/translation lookup errors and continue to friendly_id fallback
151- end
152- end
153-
154- if @checklist . nil?
155- begin
156- @checklist = BetterTogether ::Checklist . friendly . find ( key )
157- rescue StandardError
158- @checklist ||= BetterTogether ::Checklist . find_by ( id : key )
159- end
160- end
142+ @checklist = find_by_id_or_identifier ( key ) || find_by_translation_slug ( key ) || find_by_friendly_or_id ( key )
161143 end
162144
163145 raise ActiveRecord ::RecordNotFound unless @checklist
164146 end
165147
148+ def find_by_id_or_identifier ( key )
149+ BetterTogether ::Checklist . where ( id : key ) . or ( BetterTogether ::Checklist . where ( identifier : key ) ) . first
150+ end
151+
152+ def find_by_translation_slug ( key )
153+ translation = Mobility ::Backends ::ActiveRecord ::KeyValue ::StringTranslation . where (
154+ translatable_type : 'BetterTogether::Checklist' ,
155+ key : 'slug' ,
156+ value : key
157+ ) . includes ( :translatable ) . last
158+
159+ translation &.translatable
160+ rescue StandardError
161+ nil
162+ end
163+
164+ def find_by_friendly_or_id ( key )
165+ BetterTogether ::Checklist . friendly . find ( key )
166+ rescue StandardError
167+ BetterTogether ::Checklist . find_by ( id : key )
168+ end
169+
166170 def checklist_item
167171 @checklist_item = set_resource_instance
168172 end
0 commit comments