<commit_before>module Ecm
  module Cms
    module DatabaseResolver
      # Include hook for class methods
      def self.included(base)
        base.extend(ClassMethods)
      end

      # class methods go here
      module ClassMethods
      end

      # instance methods go here
      def find_templates(name, prefix, partial, details)
        return [] unless resolve(partial)

        conditions = {
          pathname: assert_slashs(prefix.to_s),
          basename: normalize_basename(name),
          locale: normalize_array(details[:locale]).first,
          format: normalize_array(details[:formats]).first,
          handler: normalize_array(details[:handlers])
        }

        format = conditions.delete(:format)
        locale = conditions.delete(:locale)

        query  = template_class.constantize.where(conditions)

        # 2) Check for templates with the given format or format is nil
        query = query.where(["format = ? OR format = ''", format])

        # 3) Ensure templates with format come first
        query = query.order('format DESC')

        # 4) Check for templates with the given locale or locale is nil
        query = query.where(["locale = ? OR locale = ''", locale])

        # 5) Ensure templates with locale come first
        query = query.order('locale DESC')

        # 6) Now trigger the query passing on conditions to initialization<commit_msg>Fixed conditions for templates when locale or handler is null.<commit_after>module Ecm
  module Cms
    module DatabaseResolver
      # Include hook for class methods
      def self.included(base)
        base.extend(ClassMethods)
      end

      # class methods go here
      module ClassMethods
      end

      # instance methods go here
      def find_templates(name, prefix, partial, details)
        return [] unless resolve(partial)

        conditions = {
          pathname: assert_slashs(prefix.to_s),
          basename: normalize_basename(name),
          locale: normalize_array(details[:locale]).first,
          format: normalize_array(details[:formats]).first,
          handler: normalize_array(details[:handlers])
        }

        format = conditions.delete(:format)
        locale = conditions.delete(:locale)

        query  = template_class.constantize.where(conditions)

        # 2) Check for templates with the given format or format is nil
        query = query.where(["format = ? OR format = '' OR format IS NULL", format])

        # 3) Ensure templates with format come first
        query = query.order('format DESC')

        # 4) Check for templates with the given locale or locale is nil
        query = query.where(["locale = ? OR locale = '' OR locale IS NULL", locale])

        # 5) Ensure templates with locale come first
        query = query.order('locale DESC')

        # 6) Now trigger the query passing on conditions to initialization
{'fraction_non_alphanumeric': 0.08382608695652174, 'fraction_numerical': 0.0034782608695652175, 'mean_word_length': 2.5375153751537516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2625152', 'n_tokens_mistral': 740, 'n_tokens_neox': 701, 'n_words': 246}