Skip to content
Snippets Groups Projects
with_automatism.rb 1.31 KiB
Newer Older
module Communication::Website::Menu::WithAutomatism
  extend ActiveSupport::Concern

Arnaud Levy's avatar
Arnaud Levy committed
  included do 
    scope :automatic, -> { where(automatic: true) }
  end
Arnaud Levy's avatar
Arnaud Levy committed
  def generate_automatically
    begin
      suspend_git_sync
      clear_items
      create_items
    ensure
      reactivate_git_sync
    end
  end

  def stop_automatism!
    update_column :automatic, false
  end
Arnaud Levy's avatar
Arnaud Levy committed

  protected
Arnaud Levy's avatar
Arnaud Levy committed
  def suspend_git_sync
    # TODO
  end

  def clear_items
    # TODO est-ce bien de ne pas gérer les dépendances avec un destroy? Effet de bord?
    items.delete_all
  end

  def create_items
    home = website.pages.root.first
    create_items_for_children_of(home)
  end

  def create_items_for_children_of(page, parent_item = nil)
    page.children.ordered.each do |child_page|
      next if child_page.default_menu_identifier != identifier
      create_item_for(child_page, parent_item)
    end
  end

  def create_item_for(page, parent_item = nil)
    item = items.create kind: :page,
                        about: page,
                        title: page.title,
                        position: page.position,
                        website: website,
                        university: university,
                        parent: parent_item
    create_items_for_children_of(page, item)
  end

  def reactivate_git_sync
    # TODO
  end