1+ # frozen_string_literal: true
2+
13namespace :better_together do
24 namespace :qa do
35 namespace :rich_text do
@@ -20,58 +22,15 @@ namespace :better_together do
2022 next unless links . any?
2123
2224 links . each_with_index do |link , index |
23- begin
24- uri = URI . parse ( link )
25-
26- internal_link = uri . host == platform_uri . host
27- link_type = determine_link_type ( uri , internal_link )
28-
29- if uri . host . nil? && uri . scheme . nil?
30- invalid_type = if uri . path
31- 'path'
32- elsif link . include? ( 'mailto' )
33- 'email'
34- elsif link . include? ( 'tel' )
35- 'phone'
36- else
37- 'undetermined'
38- end
39-
40- invalid_rich_text_links << {
41- rich_text_id : rt . id ,
42- rich_text_record_id : rt . record_id ,
43- rich_text_record_type : rt . record_type ,
44- locale : rt . locale ,
45- position : index , # Track the first position for clarity
46-
47- link_attributes : {
48- url : link ,
49- link_type : "invalid:#{ invalid_type } " ,
50- valid_link : false ,
51- error_message : 'No host or scheme. Needs review.'
52- }
53- }
25+ uri = URI . parse ( link )
5426
55- next
56- end
27+ internal_link = uri . host == platform_uri . host
28+ link_type = determine_link_type ( uri , internal_link )
5729
58- valid_rich_text_links << {
59- rich_text_id : rt . id ,
60- rich_text_record_id : rt . record_id ,
61- rich_text_record_type : rt . record_type ,
62- locale : rt . locale ,
63- position : index , # Track the first position for clarity
64-
65- link_attributes : {
66- url : link ,
67- host : uri . host ,
68- link_type : link_type ,
69- valid_link : true ,
70- external : !internal_link
71- }
72- }
73- rescue URI ::InvalidURIError => e
74- invalid_type = if link . include? ( 'mailto' )
30+ if uri . host . nil? && uri . scheme . nil?
31+ invalid_type = if uri . path
32+ 'path'
33+ elsif link . include? ( 'mailto' )
7534 'email'
7635 elsif link . include? ( 'tel' )
7736 'phone'
@@ -90,25 +49,75 @@ namespace :better_together do
9049 url : link ,
9150 link_type : "invalid:#{ invalid_type } " ,
9251 valid_link : false ,
93- error_message : e . message ,
52+ error_message : 'No host or scheme. Needs review.'
9453 }
9554 }
55+
56+ next
9657 end
58+
59+ valid_rich_text_links << {
60+ rich_text_id : rt . id ,
61+ rich_text_record_id : rt . record_id ,
62+ rich_text_record_type : rt . record_type ,
63+ locale : rt . locale ,
64+ position : index , # Track the first position for clarity
65+
66+ link_attributes : {
67+ url : link ,
68+ host : uri . host ,
69+ link_type : link_type ,
70+ valid_link : true ,
71+ external : !internal_link
72+ }
73+ }
74+ rescue URI ::InvalidURIError => e
75+ invalid_type = if link . include? ( 'mailto' )
76+ 'email'
77+ elsif link . include? ( 'tel' )
78+ 'phone'
79+ else
80+ 'undetermined'
81+ end
82+
83+ invalid_rich_text_links << {
84+ rich_text_id : rt . id ,
85+ rich_text_record_id : rt . record_id ,
86+ rich_text_record_type : rt . record_type ,
87+ locale : rt . locale ,
88+ position : index , # Track the first position for clarity
89+
90+ link_attributes : {
91+ url : link ,
92+ link_type : "invalid:#{ invalid_type } " ,
93+ valid_link : false ,
94+ error_message : e . message
95+ }
96+ }
9797 end
9898 end
9999
100100 # Upsert valid and invalid links
101- BetterTogether ::Metrics ::RichTextLink . upsert_all ( valid_rich_text_links , unique_by : %i[ rich_text_id position locale ] ) if valid_rich_text_links . any?
102- BetterTogether ::Metrics ::RichTextLink . upsert_all ( invalid_rich_text_links , unique_by : %i[ rich_text_id position locale ] ) if invalid_rich_text_links . any?
101+ if valid_rich_text_links . any?
102+ BetterTogether ::Metrics ::RichTextLink . upsert_all ( valid_rich_text_links ,
103+ unique_by : %i[ rich_text_id position
104+ locale ] )
105+ end
106+ if invalid_rich_text_links . any?
107+ BetterTogether ::Metrics ::RichTextLink . upsert_all ( invalid_rich_text_links ,
108+ unique_by : %i[ rich_text_id position
109+ locale ] )
110+ end
103111
104112 puts "Valid links processed: #{ valid_rich_text_links . size } "
105113 puts "Invalid links processed: #{ invalid_rich_text_links . size } "
106114 end
107115
108116 desc 'checks rich text links and returns their status code'
109117 task check : :environment do
110- internal_queue_job = BetterTogether ::Metrics ::RichTextInternalLinkCheckerQueueJob . new
111- external_queue_job = BetterTogether ::Metrics ::RichTextExternalLinkCheckerQueueJob . new ; byebug
118+ BetterTogether ::Metrics ::RichTextInternalLinkCheckerQueueJob . new
119+ BetterTogether ::Metrics ::RichTextExternalLinkCheckerQueueJob . new
120+ byebug
112121 end
113122
114123 def determine_link_type ( uri , internal_link )
0 commit comments