Skip to content
Snippets Groups Projects
git_file.rb 2.86 KiB
Newer Older
Arnaud Levy's avatar
Arnaud Levy committed
# == Schema Information
#
# Table name: communication_website_git_files
#
#  id            :uuid             not null, primary key
#  about_type    :string           not null, indexed => [about_id]
#  previous_path :string
#  previous_sha  :string
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#  about_id      :uuid             not null, indexed => [about_type]
#  website_id    :uuid             not null, indexed
Arnaud Levy's avatar
Arnaud Levy committed
#
# Indexes
#
#  index_communication_website_git_files_on_website_id  (website_id)
#  index_communication_website_github_files_on_about    (about_type,about_id)
#
# Foreign Keys
#
Arnaud Levy's avatar
Arnaud Levy committed
#  fk_rails_8505d649e8  (website_id => communication_websites.id)
Arnaud Levy's avatar
Arnaud Levy committed
#
class Communication::Website::GitFile < ApplicationRecord
  belongs_to :website, class_name: 'Communication::Website'
  belongs_to :about, polymorphic: true

  attr_accessor :will_be_destroyed

  def self.sync(website, object, destroy: false)
    # Handle optional before-sync process
    object.before_git_sync
    git_file = where(website: website, about: object).first_or_create
    git_file.will_be_destroyed = destroy
Arnaud Levy's avatar
Arnaud Levy committed
    website.git_repository.add_git_file git_file
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def should_create?
Arnaud Levy's avatar
Arnaud Levy committed
    !should_destroy? &&
Arnaud Levy's avatar
Arnaud Levy committed
    !exists_on_git? &&
Arnaud Levy's avatar
Arnaud Levy committed
    (
      !synchronized_with_git? ||
      previous_path.nil? ||
      previous_sha.nil?
    )
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def should_update?
Arnaud Levy's avatar
Arnaud Levy committed
    !should_destroy? &&
    (
Arnaud Levy's avatar
Arnaud Levy committed
      different_path? ||
      different_sha?
Arnaud Levy's avatar
Arnaud Levy committed
    )
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def should_destroy?
Arnaud Levy's avatar
Arnaud Levy committed
    will_be_destroyed ||
    path.nil?
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def path
    @path ||= about.git_path(website)&.gsub(/\/+/, '/')
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def to_s
Arnaud Levy's avatar
Arnaud Levy committed
    @to_s ||= ApplicationController.render(
      template: template_static,
Arnaud Levy's avatar
Arnaud Levy committed
      layout: false,
Arnaud Levy's avatar
Arnaud Levy committed
      assigns: {
pabois's avatar
pabois committed
        about: about,
Arnaud Levy's avatar
Arnaud Levy committed
        website: website
      }
Arnaud Levy's avatar
Arnaud Levy committed
    )
  end

  protected

  def template_static
Arnaud Levy's avatar
Arnaud Levy committed
    if about.respond_to? :template_static
      about.template_static
    else
      "admin/#{about.class.name.underscore.pluralize}/static"
    end
  # Real sha on the git repo
Arnaud Levy's avatar
Arnaud Levy committed
  def git_sha_for(path)
    website.git_repository.git_sha path
  end

  def previous_git_sha
    @previous_git_sha ||= git_sha_for(previous_path)
  end

  def git_sha
Arnaud Levy's avatar
Arnaud Levy committed
    @git_sha ||= git_sha_for(path)
  end

  # Based on content, with the provider's algorithm (sha1 or sha256)
  def computed_sha
    @computed_sha ||= website.git_repository.computed_sha to_s
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def exists_on_git?
Arnaud Levy's avatar
Arnaud Levy committed
    previous_git_sha.present? || # The file exists where it was last time
    (
      previous_path.nil? && # Never saved in the database
      git_sha.present?      # but it exists in the git repo
    )
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def synchronized_with_git?
Arnaud Levy's avatar
Arnaud Levy committed
    exists_on_git? && # File exists
    previous_path == path && # at the same place
    previous_git_sha == previous_sha # with the same content
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def different_path?
    previous_path != path
  end

  def different_sha?
    previous_sha != computed_sha
Arnaud Levy's avatar
Arnaud Levy committed
  end
Arnaud Levy's avatar
Arnaud Levy committed
end