<commit_before>class LoadCmsContentSetService < ApplicationService
  attr_reader :convention, :content_set, :content_set_name

  validates_presence_of :convention, :content_set_name
  validate :ensure_content_set_exists
  validate :ensure_no_conflicting_pages
  validate :ensure_no_conflicting_partials
  validate :ensure_no_conflicting_layouts

  def initialize(convention:, content_set_name:)
    @convention = convention
    @content_set_name = content_set_name
    @content_set = CmsContentSet.new(name: content_set_name)
  end

  private

  def inner_call
    load_content_for_subdir('layouts') do |cms_layout|
      @convention.update!(default_layout: cms_layout) if cms_layout.name == 'Default'
    end

    load_content_for_subdir('pages') do |page|
      @convention.update!(root_page: page) if page.name == 'root'
    end

    load_content_for_subdir('partials')

    success
  end

  def load_content_for_subdir(subdir, &block)
    content_set.all_template_paths_with_names(subdir).each do |path, name|
      content, attrs = content_set.template_content(path)
      model = association_for_subdir(subdir).create!(name: name, content: content, **attrs)

      block.call(model) if block
    end
  end

  def association_for_subdir(subdir)
    case subdir
    when 'layouts' then @convention.cms_layouts
    when 'pages' then @convention.pages
    when 'partials' then @convention.cms_partials
    else raise "Unknown content type: #{subdir}"
    end
  end

  def ensure_content_set_exists
    errors.add(:base, "No content set found at #{content_set.root_path}") unless Dir.exist?(content_set.root_path)
  end

  def ensure_no_conflicting_pages
    ensure_no_conflicting_content('pages', convention.pages.pluck(:name))

    if content_set.all_template_names('pages').include?('root') && convention.root_page
      errors.add(:base, "#{convention.name} already has a root page")
    end
  end

  def ensure_no_conflicting_partials
    ensure_no_conflicting_content('partials', convention.cms_partials.pluck(:name))
  end

  def ensure_no_conflicting_layouts
    ensure_no_conflicting_content('layouts', convention.cms_layouts.pluck(:name))

    if content_set.all_template_names('layouts').include?('Default') && convention.default_layout
      errors.add(:base, "#{convention.name} already has a default layout")
    end
  end

  def ensure_no_conflicting_content(subdir, content_identifiers)
    content_identifiers = Set.new(content_identifiers)

    content_set.all_template_paths_with_names(subdir).each do |_path, name|
      if content_identifiers.include?(name)
        errors.add(:base, "A #{subdir.singularize} named #{name} already exists in #{convention.name}")
      end
    end
  end
end<commit_msg>more DRY<commit_after>class LoadCmsContentSetService < ApplicationService
  attr_reader :convention, :content_set, :content_set_name

  validates_presence_of :convention, :content_set_name
  validate :ensure_content_set_exists
  validate :ensure_no_conflicting_pages
  validate :ensure_no_conflicting_partials
  validate :ensure_no_conflicting_layouts

  def initialize(convention:, content_set_name:)
    @convention = convention
    @content_set_name = content_set_name
    @content_set = CmsContentSet.new(name: content_set_name)
  end

  private

  def inner_call
    load_content_for_subdir('layouts') do |cms_layout|
      @convention.update!(default_layout: cms_layout) if cms_layout.name == 'Default'
    end

    load_content_for_subdir('pages') do |page|
      @convention.update!(root_page: page) if page.name == 'root'
    end

    load_content_for_subdir('partials')

    success
  end

  def load_content_for_subdir(subdir, &block)
    content_set.all_template_paths_with_names(subdir).each do |path, name|
      content, attrs = content_set.template_content(path)
      model = association_for_subdir(subdir).create!(name: name, content: content, **attrs)

      block.call(model) if block
    end
  end

  def association_for_subdir(subdir)
    case subdir
    when 'layouts' then @convention.cms_layouts
    when 'pages' then @convention.pages
    when 'partials' then @convention.cms_partials
    else raise "Unknown content type: #{subdir}"
    end
  end

  def ensure_content_set_exists
    errors.add(:base, "No content set found at #{content_set.root_path}") unless Dir.exist?(content_set.root_path)
  end

  def ensure_no_conflicting_pages
    ensure_no_conflicting_content(
      'pages',
      convention.pages.pluck(:name),
      (
        convention.root_page ?
        { 'root' => 'root page' } :
        {}
      )
    )
  end

  def ensure_no_conflicting_partials
    ensure_no_conflicting_content('partials', convention.cms_partials.pluck(:name))
  end

  def ensure_no_conflicting_layouts
    ensure_no_conflicting_content(
      'layouts',
      convention.cms_layouts.pluck(:name),
      (
        convention.default_layout ?
        { 'Default' => 'default layout' } :
        {}
      )
    )
  end

  def ensure_no_conflicting_content(subdir, content_identifiers, taken_special_identifiers = {})
    content_identifiers = Set.new(content_identifiers)

    content_set.all_template_names(subdir).each do |name|
      if taken_special_identifiers[name]
        errors.add(:base, "#{convention.name} already has a #{taken_special_identifiers[name]}")
      end

      if content_identifiers.include?(name)
        errors.add(:base, "A #{subdir.singularize} named #{name} already exists in #{convention.name}")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09264652348016017, 'fraction_numerical': 0.0, 'mean_word_length': 4.324612403100775, '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': '517999', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1771, 'n_words': 339}