@@ -57,67 +57,65 @@ def process_rich_text(rt)
5757 return [ 0 , 0 ] if links . empty?
5858
5959 links . each_with_index do |link , index |
60- begin
61- uri = parse_uri ( link )
62- if uri . nil?
60+ uri = parse_uri ( link )
61+ if uri . nil?
62+ create_invalid ( rt , index , link , 'undetermined' )
63+ invalid_count += 1
64+ next
65+ end
66+
67+ canonical_host = uri . host
68+ if canonical_host . nil? && uri . scheme . nil?
69+ if link . start_with? ( '/' )
70+ canonical_host = rt_platform_host
71+ else
6372 create_invalid ( rt , index , link , 'undetermined' )
6473 invalid_count += 1
6574 next
6675 end
76+ end
6777
68- canonical_host = uri . host
69- if canonical_host . nil? && uri . scheme . nil?
70- if link . start_with? ( '/' )
71- canonical_host = rt_platform_host
72- else
73- create_invalid ( rt , index , link , 'undetermined' )
74- invalid_count += 1
75- next
76- end
77- end
78-
79- bt_link = BetterTogether ::Content ::Link . find_or_initialize_by ( url : link )
80- bt_link . host ||= canonical_host
81- bt_link . scheme ||= uri . scheme
82- bt_link . external = ( canonical_host . present? && ( rt_platform_host != canonical_host ) )
83- bt_link . save! if bt_link . changed?
84-
85- # Persist the RichTextLink depending on the schema available.
86- if BetterTogether ::Metrics ::RichTextLink . column_names . include? ( 'link_id' )
87- attrs = {
88- link_id : bt_link . id ,
89- rich_text_id : rt . id ,
90- rich_text_record_id : rt . record_id ,
91- rich_text_record_type : rt . record_type ,
92- position : index ,
93- locale : rt . locale
94- }
95-
96- BetterTogether ::Metrics ::RichTextLink . find_or_create_by! ( attrs )
97- else
98- # Fallback schema: metrics rich_text_links store URL and metadata inline
99- # Some legacy schemas use column names (for example `valid`) that clash
100- # with Active Record method names. To avoid DangerousAttributeError we
101- # perform a raw INSERT unless a record already exists for this
102- # (rich_text_id, url) tuple.
103- model = BetterTogether ::Metrics ::RichTextLink
104- unless model . where ( rich_text_id : rt . id , url : link ) . exists?
105- conn = model . connection
106- table = model . table_name
107- now = Time . current
108- cols = %w[ rich_text_id url link_type external valid host error_message created_at updated_at ]
109- vals = [ rt . id , link , bt_link . link_type || 'website' , bt_link . external || false ,
110- bt_link . valid_link || false , bt_link . host , bt_link . error_message , now , now ]
111- sql = "INSERT INTO #{ table } (#{ cols . join ( ',' ) } ) VALUES (#{ vals . map { |v | conn . quote ( v ) } . join ( ',' ) } )"
112- conn . execute ( sql )
113- end
78+ bt_link = BetterTogether ::Content ::Link . find_or_initialize_by ( url : link )
79+ bt_link . host ||= canonical_host
80+ bt_link . scheme ||= uri . scheme
81+ bt_link . external = ( canonical_host . present? && ( rt_platform_host != canonical_host ) )
82+ bt_link . save! if bt_link . changed?
83+
84+ # Persist the RichTextLink depending on the schema available.
85+ if BetterTogether ::Metrics ::RichTextLink . column_names . include? ( 'link_id' )
86+ attrs = {
87+ link_id : bt_link . id ,
88+ rich_text_id : rt . id ,
89+ rich_text_record_id : rt . record_id ,
90+ rich_text_record_type : rt . record_type ,
91+ position : index ,
92+ locale : rt . locale
93+ }
94+
95+ BetterTogether ::Metrics ::RichTextLink . find_or_create_by! ( attrs )
96+ else
97+ # Fallback schema: metrics rich_text_links store URL and metadata inline
98+ # Some legacy schemas use column names (for example `valid`) that clash
99+ # with Active Record method names. To avoid DangerousAttributeError we
100+ # perform a raw INSERT unless a record already exists for this
101+ # (rich_text_id, url) tuple.
102+ model = BetterTogether ::Metrics ::RichTextLink
103+ unless model . where ( rich_text_id : rt . id , url : link ) . exists?
104+ conn = model . connection
105+ table = model . table_name
106+ now = Time . current
107+ cols = %w[ rich_text_id url link_type external valid host error_message created_at updated_at ]
108+ vals = [ rt . id , link , bt_link . link_type || 'website' , bt_link . external || false ,
109+ bt_link . valid_link || false , bt_link . host , bt_link . error_message , now , now ]
110+ sql = "INSERT INTO #{ table } (#{ cols . join ( ',' ) } ) VALUES (#{ vals . map { |v | conn . quote ( v ) } . join ( ',' ) } )"
111+ conn . execute ( sql )
114112 end
115-
116- valid_count += 1
117- rescue URI ::InvalidURIError
118- create_invalid ( rt , index , link , 'invalid_uri' )
119- invalid_count += 1
120113 end
114+
115+ valid_count += 1
116+ rescue URI ::InvalidURIError
117+ create_invalid ( rt , index , link , 'invalid_uri' )
118+ invalid_count += 1
121119 end
122120
123121 [ valid_count , invalid_count ]
0 commit comments