<commit_before>module Settings
  class Dmptemplate < RailsSettings::SettingObject

    VALID_FONT_FACES = [
      'Arial, Helvetica, Sans-Serif',
      '"Times New Roman", Times, Serif'
    ]

    VALID_FONT_SIZE_RANGE = (8..14)
    VALID_MARGIN_RANGE = (5..25)

    VALID_ADMIN_FIELDS = %i(
      project_name project_identifier grant_title principal_investigator
      project_data_contact project_description funder institution
    )

    DEFAULT_SETTINGS = {
      formatting: {
        margin: { # in millimeters
          top:    20,
          bottom: 20,
          left:   20,
          right:  20
        },
        font_face: VALID_FONT_FACES.first,
        font_size: 12 # pt
      },
      max_pages: 3,
      fields: {
        admin: VALID_ADMIN_FIELDS,
        questions: :all
      },
      title: ""
    }

    validate do
      formatting = value['formatting']
      max_pages  = value['max_pages']
      fields     = value['fields']

      if formatting.present?
        errs = []
        default_formatting = DEFAULT_SETTINGS[:formatting]

        unless (default_formatting.keys - formatting.keys).empty?
          errs << :missing_key
        else
          unless formatting[:margin].is_a?(Hash)
            errs << :invalid_margin
          else
            errs << :negative_margin if formatting[:margin].any? {|k,v| v.to_i < 0 }
            errs << :unknown_margin unless (formatting[:margin].keys - default_formatting[:margin].keys).empty?
            errs << :invalid_margin unless formatting[:margin].all? {|k,v| VALID_MARGIN_RANGE.member?(v) }
          end

          errs << :invalid_font_size unless VALID_FONT_SIZE_RANGE.member?(formatting[:font_size])
          errs << :invalid_font_face unless VALID_FONT_FACES.member?(formatting[:font_face])
          errs << :unknown_key unless (formatting.keys - default_formatting.keys).empty?
        end

        errs.map do |key|
          errors.add(:formatting, I18n.t("helpers.settings.plans.errors.#{key}"))
        end

      end

      if max_pages.present? && (!max_pages.is_a?(Integer) || max_pages <= 0)
        errors.add(:max_pages, I18n.t('helpers.settings.plans.errors.invalid_max_pages'))
      end
    end

    before_validation do
      self.formatting[:font_size] = self.formatting[:font_size].to_i if self.formatting[:font_size].present?
      self.formatting[:margin].each do |key, val|
        self.formatting[:margin][key] = val.to_i
      end

      self.fields.each do |key, val|
        if val.is_a?(Hash)
          val = key == :questions ? val.keys.map {|k| k.to_s.to_i } : val.keys
        end

        self.fields[key] = val<commit_msg>Bug fix for app/models/settings/dmptemplate.rb. When running test/unit/dmptemplate_test.rb test_not_setting_margin_should_not_be_valid, the test fails with error:<commit_after>module Settings
  class Dmptemplate < RailsSettings::SettingObject

    VALID_FONT_FACES = [
      'Arial, Helvetica, Sans-Serif',
      '"Times New Roman", Times, Serif'
    ]

    VALID_FONT_SIZE_RANGE = (8..14)
    VALID_MARGIN_RANGE = (5..25)

    VALID_ADMIN_FIELDS = %i(
      project_name project_identifier grant_title principal_investigator
      project_data_contact project_description funder institution
    )

    DEFAULT_SETTINGS = {
      formatting: {
        margin: { # in millimeters
          top:    20,
          bottom: 20,
          left:   20,
          right:  20
        },
        font_face: VALID_FONT_FACES.first,
        font_size: 12 # pt
      },
      max_pages: 3,
      fields: {
        admin: VALID_ADMIN_FIELDS,
        questions: :all
      },
      title: ""
    }

    validate do
      formatting = value['formatting']
      max_pages  = value['max_pages']
      fields     = value['fields']

      if formatting.present?
        errs = []
        default_formatting = DEFAULT_SETTINGS[:formatting]

        unless (default_formatting.keys - formatting.keys).empty?
          errs << :missing_key
        else
          unless formatting[:margin].is_a?(Hash)
            errs << :invalid_margin
          else
            errs << :negative_margin if formatting[:margin].any? {|k,v| v.to_i < 0 }
            errs << :unknown_margin unless (formatting[:margin].keys - default_formatting[:margin].keys).empty?
            errs << :invalid_margin unless formatting[:margin].all? {|k,v| VALID_MARGIN_RANGE.member?(v) }
          end

          errs << :invalid_font_size unless VALID_FONT_SIZE_RANGE.member?(formatting[:font_size])
          errs << :invalid_font_face unless VALID_FONT_FACES.member?(formatting[:font_face])
          errs << :unknown_key unless (formatting.keys - default_formatting.keys).empty?
        end

        errs.map do |key|
          errors.add(:formatting, I18n.t("helpers.settings.plans.errors.#{key}"))
        end

      end

      if max_pages.present? && (!max_pages.is_a?(Integer) || max_pages <= 0)
        errors.add(:max_pages, I18n.t('helpers.settings.plans.errors.invalid_max_pages'))
      end
    end

    before_validation do
      self.formatting[:font_size] = self.formatting[:font_size].to_i if self.formatting[:font_size].present?
      unless self.formatting[:margin].nil?
        self.formatting[:margin].each do |key, val|
          self.formatting[:margin][key] = val.to_i
        end
      end

      self.fields.each do |key, val|
        if val.is_a?(Hash)
          val = key == :questions ? val.keys.map {|k| k.to_s.to_i } : val.keys
        end

        self.fields[key] = val
{'fraction_non_alphanumeric': 0.11333819241982507, 'fraction_numerical': 0.008381924198250729, 'mean_word_length': 2.4050868486352357, 'pattern_counts': {'":': 0, '<': 41, '<?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': '4803714', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1740, 'n_words': 326}