Sha256: f339c64ec123589ae255e2703622482f99856a99796a68be82adb17b6128ba56

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require 'hobo_fields'

module GitModels
  module Branch
    extend ActiveSupport::Concern

    included do
      fields do
        git_updated_at :datetime, null: false
        name :text, limit: 1024, null: false
        timestamps
      end

      validates :name, uniqueness: { scope: :repository, message: 'Branch names must be unique within each repository' }

      belongs_to :author, class_name: 'User', inverse_of: :branches, required: true
      belongs_to :repository, class_name: 'Repository', inverse_of: :branches, required: true

      scope :branches_not_updated_since, lambda { |checked_at_date| where('branches.updated_at < ?', checked_at_date) }

      scope :from_repository, lambda { |repository_name|
        joins(:repository).where('repositories.name = ?', repository_name)
      }

      scope :with_name, lambda { |name| where(name: name) }

      def to_s
        name
      end

      def =~(other)
        name =~ other
      end

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

    class_methods do
      def create_from_git_data!(branch_data)
        repository = ::Repository.create!(branch_data.repository_name)
        branch = where(repository: repository, name: branch_data.name).first_or_initialize
        branch.git_updated_at = branch_data.last_modified_date
        branch.updated_at = Time.current # force updated time
        u = ::User.create_from_git_data!(branch_data)
        branch.author = u
        branch.save!
        branch
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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