Sha256: 1c31b50e1155a48c5187cbc11824fbf14e267610851118c134d4f9f483294486

Contents?: true

Size: 962 Bytes

Versions: 1

Compression:

Stored size: 962 Bytes

Contents

# frozen_string_literal: true

require 'hobo_fields'

module GitModels
  module Commit
    extend ActiveSupport::Concern

    included do
      fields do
        sha :text, limit: 40, null: false
        message :text, limit: 1024, null: false
        timestamps
      end

      validates :sha, uniqueness: { message: 'SHAs must be globally unique' }
      validates :sha, format: { without: /[0]{40}/ }

      belongs_to :author, class_name: 'User', inverse_of: :commits, required: true

      def short_sha
        sha[0, 7]
      end

      def to_s
        sha
      end

      def <=>(other)
        sha <=> other.sha
      end
    end

    class_methods do
      def create_from_git_commit!(git_commit)
        commit = where(sha: git_commit.sha).first_or_initialize
        commit.message = git_commit.message.truncate(1024)
        commit.author = ::User.create_from_git_data!(git_commit)
        commit.save!
        commit
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git_models-1.2.0 app/models/concerns/commit.rb