Sha256: 209862102475b527585c6ab57b86c2edefcdc1e4b11be352e435d666217635ef

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'uri'
require 'core_ext/hash/compact'
require 'active_record'

class Repository < ActiveRecord::Base
  autoload :ServiceHook, 'travis/model/repository/service_hook'

  include ServiceHook

  has_many :requests, :dependent => :delete_all
  has_many :builds, :dependent => :delete_all do
    def last_status_on(params)
      last_finished_on_branch(params[:branch]).try(:matrix_status, params)
    end
  end

  has_one :last_build,   :class_name => 'Build', :order => 'id DESC', :conditions => { :state  => ['started', 'finished']  }
  has_one :last_success, :class_name => 'Build', :order => 'id DESC', :conditions => { :status => 0 }
  has_one :last_failure, :class_name => 'Build', :order => 'id DESC', :conditions => { :status => 1 }

  validates :name,       :presence => true, :uniqueness => { :scope => :owner_name }
  validates :owner_name, :presence => true

  class << self
    def timeline
      where(arel_table[:last_build_started_at].not_eq(nil)).order(arel_table[:last_build_started_at].desc)
    end

    def recent
      limit(20)
    end

    def by_owner_name(owner_name)
      where(:owner_name => owner_name)
    end

    def by_slug(slug)
      where(:owner_name => slug.split('/').first, :name => slug.split('/').last)
    end

    def search(query)
      where('repositories.name ~* ? OR repositories.owner_name ~* ?', query, query)
    end

    def find_by(params)
      if id = params[:id] || params[:repository_id]
        self.find(id)
      else
        self.where(params.slice(:name, :owner_name)).first
      end
    end

    def active_by_name
      Hash[select([:active, :name]).map { |repository| [repository.name, repository.active] }]
    end
  end

  def last_build_status(params = {})
    params = params.symbolize_keys.slice(*Build.matrix_keys_for(params))
    params.blank? ? read_attribute(:last_build_status) : builds.last_status_on(params)
  end

  def slug
    @slug ||= [owner_name, name].join('/')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
travis-core-0.0.1 lib/travis/model/repository.rb