project.rb 3.2 KB
Newer Older
G
gitlabhq 已提交
1 2 3
require "grit"

class Project < ActiveRecord::Base
G
gitlabhq 已提交
4 5
  belongs_to :owner, :class_name => "User"

R
randx 已提交
6 7 8 9 10
  does "project/validations"
  does "project/repository"
  does "project/permissions"
  does "project/hooks"

11 12
  has_many :users,          :through => :users_projects
  has_many :events,         :dependent => :destroy
D
Dmitriy Zaporozhets 已提交
13
  has_many :merge_requests, :dependent => :destroy
14
  has_many :issues,         :dependent => :destroy, :order => "position"
D
Dmitriy Zaporozhets 已提交
15
  has_many :milestones,     :dependent => :destroy
G
gitlabhq 已提交
16
  has_many :users_projects, :dependent => :destroy
17 18 19 20 21
  has_many :notes,          :dependent => :destroy
  has_many :snippets,       :dependent => :destroy
  has_many :deploy_keys,    :dependent => :destroy, :foreign_key => "project_id", :class_name => "Key"
  has_many :web_hooks,      :dependent => :destroy
  has_many :wikis,          :dependent => :destroy
22
  has_many :protected_branches, :dependent => :destroy
A
Aleksei Kvitinskii 已提交
23

V
Valera Sizov 已提交
24
  attr_protected :private_flag, :owner_id
G
gitlabhq 已提交
25 26

  scope :public_only, where(:private_flag => false)
D
Dmitriy Zaporozhets 已提交
27
  scope :without_user, lambda { |user|  where("id not in (:ids)", :ids => user.projects.map(&:id) ) }
G
gitlabhq 已提交
28

29 30 31
  def self.active
    joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
  end
32 33

  def self.access_options
D
Dmitriy Zaporozhets 已提交
34
    UsersProject.access_roles
35 36
  end

37 38 39 40
  def self.search query
    where("name like :query or code like :query or path like :query", :query => "%#{query}%")
  end

G
gitlabhq 已提交
41 42 43 44
  def to_param
    code
  end

A
Ariejan de Vroom 已提交
45 46 47 48
  def web_url
    [GIT_HOST['host'], code].join("/")
  end

49 50 51 52 53
  def team_member_by_name_or_email(email = nil, name = nil)
    user = users.where("email like ? or name like ?", email, name).first
    users_projects.find_by_user_id(user.id) if user
  end

54 55 56 57
  def team_member_by_id(user_id)
    users_projects.find_by_user_id(user_id)
  end

G
gitlabhq 已提交
58
  def common_notes
G
gitlabhq 已提交
59
    notes.where(:noteable_type => ["", nil]).inc_author_project
G
gitlabhq 已提交
60 61
  end

62 63
  def build_commit_note(commit)
    notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
G
gitlabhq 已提交
64
  end
N
Nihad Abbasov 已提交
65

66
  def commit_notes(commit)
D
Dmitriy Zaporozhets 已提交
67 68 69 70
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit", :line_code => nil)
  end

  def commit_line_notes(commit)
V
Valery Sizov 已提交
71
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit").where("line_code is not null")
G
gitlabhq 已提交
72
  end
N
Nihad Abbasov 已提交
73

G
gitlabhq 已提交
74 75 76 77 78 79 80 81
  def public?
    !private_flag
  end

  def private?
    private_flag
  end

N
Nihad Abbasov 已提交
82
  def last_activity
83
    events.last || nil
G
gitlabhq 已提交
84 85 86
  end

  def last_activity_date
D
Dmitriy Zaporozhets 已提交
87 88
    if events.last
      events.last.created_at
89
    else
D
Dmitriy Zaporozhets 已提交
90
      updated_at
91
    end
D
Dmitriy Zaporozhets 已提交
92
  end
93

D
Dmitriy Zaporozhets 已提交
94 95 96
  def project_id
    self.id
  end
G
gitlabhq 已提交
97
end
D
Dmitriy Zaporozhets 已提交
98

G
gitlabhq 已提交
99 100 101 102
# == Schema Information
#
# Table name: projects
#
103 104 105 106 107 108 109 110 111 112 113 114 115
#  id                     :integer         not null, primary key
#  name                   :string(255)
#  path                   :string(255)
#  description            :text
#  created_at             :datetime
#  updated_at             :datetime
#  private_flag           :boolean         default(TRUE), not null
#  code                   :string(255)
#  owner_id               :integer
#  default_branch         :string(255)     default("master"), not null
#  issues_enabled         :boolean         default(TRUE), not null
#  wall_enabled           :boolean         default(TRUE), not null
#  merge_requests_enabled :boolean         default(TRUE), not null
D
Dmitriy Zaporozhets 已提交
116
#  wiki_enabled           :boolean         default(TRUE), not null
G
gitlabhq 已提交
117 118
#