Sha256: 91983d1d80f7d0df377ac89d89b73610ae859885be089083ec2aec45ff4f0054

Contents?: true

Size: 899 Bytes

Versions: 9

Compression:

Stored size: 899 Bytes

Contents

module BelongsToCommit
  extend ActiveSupport::Concern

  included do
    belongs_to :commit
    before_validation :identify_commit, on: :create
    validates :sha, presence: {message: "must refer to a commit"}
  end

  module ClassMethods
    def find_by_sha!(sha)
      find_by_sha(sha) || raise(ActiveRecord::RecordNotFound)
    end

    def find_by_sha(sha)
      with_sha_like(sha).first if sha
    end

    def with_sha_like(sha)
      where(["sha LIKE ?", "#{sha.strip}%"])
    end
  end

private

  def identify_commit
    return unless project && sha
    self.commit = project.find_commit_by_sha(sha)
    self.sha = commit.sha if commit
  rescue Houston::Adapters::VersionControl::InvalidShaError
    Rails.logger.warn "\e[31m[#{self.class.name.underscore}] Unable to identify commit\e[0m"
    Rails.logger.warn "#{$!.class}: #{$!.message}\n#{$!.backtrace.join("\n  ")}"
    nil
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
houston-core-0.8.0.pre app/concerns/belongs_to_commit.rb
houston-core-0.7.0 app/concerns/belongs_to_commit.rb
houston-core-0.7.0.beta4 app/concerns/belongs_to_commit.rb
houston-core-0.7.0.beta3 app/concerns/belongs_to_commit.rb
houston-core-0.7.0.beta2 app/concerns/belongs_to_commit.rb
houston-core-0.7.0.beta app/concerns/belongs_to_commit.rb
houston-core-0.6.3 app/concerns/belongs_to_commit.rb
houston-core-0.6.2 app/concerns/belongs_to_commit.rb
houston-core-0.6.1 app/concerns/belongs_to_commit.rb