From a7d1ea2cdf075454b96447551148c0abe4c4fc7e Mon Sep 17 00:00:00 2001 From: Paul Mesnilgrente Date: Wed, 17 Jul 2024 21:27:46 +0200 Subject: [PATCH] refactor: the parser method was too complicated to read --- app/models/abstract_job.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/app/models/abstract_job.rb b/app/models/abstract_job.rb index 79f80fe..602b099 100644 --- a/app/models/abstract_job.rb +++ b/app/models/abstract_job.rb @@ -71,20 +71,33 @@ def enqueue def parser return @parser if @parser.present? + if version_id.present? - @parser = ParserVersion.find(version_id, params: { parser_id: }) + @parser = find_parser_from_version_id elsif environment.present? && !preview? - version = ParserVersion.find(:one, from: :current, params: { parser_id:, environment: }) - version.parser_id = parser_id - self.version_id = version.id if version.present? - @parser = version + @parser = find_parser_from_current else - parser = Parser.find(parser_id) - parser.content = parser_code if parser_code.present? - @parser = parser + @parser = find_parser_from_parser_id end end + def find_parser_from_version_id + ParserVersion.find(version_id, params: { parser_id: }) + end + + def find_parser_from_current + version = ParserVersion.find(:one, from: :current, params: { parser_id:, environment: }) + version.parser_id = parser_id + self.version_id = version.id if version.present? + version + end + + def find_parser_from_parser_id + parser = Parser.find(parser_id) + parser.content = parser_code if parser_code.present? + parser + end + def required_enrichments parser.enrichment_definitions(environment).dup.keep_if do |_name, options| options[:required_for_active_record]