<commit_before># frozen_string_literal: true

module Folio
  class MenuItem < ApplicationRecord
    # Relations
    has_ancestry orphan_strategy: :adopt
    belongs_to :menu, touch: true, required: true
    belongs_to :target, optional: true, polymorphic: true

    # Scopes
    scope :ordered, -> { order(position: :asc) }

    # Validations
    validate :validate_target_and_menu_locales
    validate :validate_menu_allowed_types

    private

      def validate_target_and_menu_locales
        if target && target.locale && target.locale != menu.locale
          errors.add(:target, :invalid)
        end
      end

      def validate_menu_allowed_types
        if menu.class.allowed_menu_item_classes.exclude?(self.class)
          errors.add(:type, :invalid)
        end
      end
  end
end

# == Schema Information
#
# Table name: folio_menu_items
#
#  id          :integer          not null, primary key
#  menu_id     :integer
#  type        :string
#  ancestry    :string
#  title       :string
#  rails_path  :string
#  position    :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null<commit_msg>force require menu_item subclasses in dev env<commit_after># frozen_string_literal: true

module Folio
  class MenuItem < ApplicationRecord
    # Relations
    has_ancestry orphan_strategy: :adopt
    belongs_to :menu, touch: true, required: true
    belongs_to :target, optional: true, polymorphic: true

    # Scopes
    scope :ordered, -> { order(position: :asc) }

    # Validations
    validate :validate_target_and_menu_locales
    validate :validate_menu_allowed_types

    private

      def validate_target_and_menu_locales
        if target && target.locale && target.locale != menu.locale
          errors.add(:target, :invalid)
        end
      end

      def validate_menu_allowed_types
        if menu.class.allowed_menu_item_classes.exclude?(self.class)
          errors.add(:type, :invalid)
        end
      end
  end
end

if Rails.env.development?
  Dir["#{Folio::Engine.root}/app/models/folio/menu_item/*.rb",
      'app/models/menu_item/*.rb'].each do |file|
    require_dependency file
  end
end

# == Schema Information
#
# Table name: folio_menu_items
#
#  id          :integer          not null, primary key
#  menu_id     :integer
#  type        :string
#  ancestry    :string
#  title       :string
#  rails_path  :string
#  position    :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
{'fraction_non_alphanumeric': 0.08193445243804956, 'fraction_numerical': 0.0, 'mean_word_length': 2.691740412979351, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6619215', 'n_tokens_mistral': 774, 'n_tokens_neox': 751, 'n_words': 189}