<commit_before># encoding utf-8

require_relative './carto_json_serializer'
require_relative '../../controllers/carto/api/legend_presenter'
require_dependency 'carto/legend_definition_validator'

module Carto
  class Legend < ActiveRecord::Base
    self.inheritance_column = :_type

    belongs_to :layer, class_name: Carto::Layer

    VALID_LEGEND_TYPES = %(html category bubble choropleth custom).freeze

    serialize :definition, ::Carto::CartoJsonSerializer

    validates :definition, carto_json_symbolizer: true
    validates :type, :layer, presence: true
    validates :type, inclusion: { in: VALID_LEGEND_TYPES }, allow_nil: true

    validate :on_data_layer,
             :under_max_legends_per_layer,
             :validate_definition_schema

    before_validation :ensure_definition

    after_commit :force_notify_layer_change

    def definition
      super_definition = super

      super_definition.with_indifferent_access if super_definition
    end

    private

    def ensure_definition
      self.definition ||= Hash.new
    end

    def on_data_layer
      unless layer.data_layer?
        errors.add(:layer, "'#{layer.kind}' layers can't have legends")
      end
    end

    MAX_LEGENDS_PER_LAYER = 2

    def under_max_legends_per_layer
      other_legends = layer.legends.select { |legend| legend.id != id }

      unless other_legends.count < MAX_LEGENDS_PER_LAYER
        errors.add(:layer, 'Maximum number of legends per layer reached')
      end
    end

    def validate_definition_schema
      validator = Carto::LegendDefinitionValidator.new(type, definition)
      definition_errors = validator.errors

      if definition_errors.any?<commit_msg>revert since it was only was 1 layer of symbolization<commit_after># encoding utf-8

require_relative './carto_json_serializer'
require_relative '../../controllers/carto/api/legend_presenter'
require_dependency 'carto/legend_definition_validator'

module Carto
  class Legend < ActiveRecord::Base
    self.inheritance_column = :_type

    belongs_to :layer, class_name: Carto::Layer

    VALID_LEGEND_TYPES = %(html category bubble choropleth custom).freeze

    serialize :definition, ::Carto::CartoJsonSerializer

    validates :definition, carto_json_symbolizer: true
    validates :type, :layer, presence: true
    validates :type, inclusion: { in: VALID_LEGEND_TYPES }, allow_nil: true

    validate :on_data_layer,
             :under_max_legends_per_layer,
             :validate_definition_schema

    before_validation :ensure_definition

    after_commit :force_notify_layer_change

    private

    def ensure_definition
      self.definition ||= Hash.new
    end

    def on_data_layer
      unless layer.data_layer?
        errors.add(:layer, "'#{layer.kind}' layers can't have legends")
      end
    end

    MAX_LEGENDS_PER_LAYER = 2

    def under_max_legends_per_layer
      other_legends = layer.legends.select { |legend| legend.id != id }

      unless other_legends.count < MAX_LEGENDS_PER_LAYER
        errors.add(:layer, 'Maximum number of legends per layer reached')
      end
    end

    def validate_definition_schema
      validator = Carto::LegendDefinitionValidator.new(type, definition)
      definition_errors = validator.errors

      if definition_errors.any?
{'fraction_non_alphanumeric': 0.07393828292086771, 'fraction_numerical': 0.0015276504735716467, 'mean_word_length': 3.611267605633803, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5493735', 'n_tokens_mistral': 1007, 'n_tokens_neox': 957, 'n_words': 220}