project.rb 3.25 KB
Newer Older
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
1
2
3
4
5
require "grit"

class Project < ActiveRecord::Base
  belongs_to :owner, :class_name => "User"

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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13
  has_many :merge_requests, :dependent => :destroy
14
  has_many :issues,         :dependent => :destroy, :order => "position"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15
  has_many :milestones,     :dependent => :destroy
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
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
Aleksei Kvitinskii's avatar
Aleksei Kvitinskii committed
23

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
24
25
26
  attr_protected :private_flag, :owner_id

  scope :public_only, where(:private_flag => false)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
27
  scope :without_user, lambda { |user|  where("id not in (:ids)", :ids => user.projects.map(&:id) ) }
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
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
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

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
41
42
43
44
  def to_param
    code
  end

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

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
58
  def common_notes
gitlabhq's avatar
gitlabhq committed
59
    notes.where(:noteable_type => ["", nil]).inc_author_project
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
60
61
  end

62
63
  def build_commit_note(commit)
    notes.new(:noteable_id => commit.id, :noteable_type => "Commit")
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
64
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
65

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

  def commit_line_notes(commit)
Valery Sizov's avatar
Valery Sizov committed
71
    notes.where(:noteable_id => commit.id, :noteable_type => "Commit").where("line_code is not null")
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
72
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
73

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
74
75
76
77
78
79
80
81
  def public?
    !private_flag
  end

  def private?
    private_flag
  end

82
  def last_activity
83
    events.last || nil
gitlabhq's avatar
gitlabhq committed
84
85
86
  end

  def last_activity_date
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
87
88
89
90
91
    if events.last
      events.last.created_at
    else
      updated_at
    end
gitlabhq's avatar
gitlabhq committed
92
93
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
94
95
96
  def project_id
    self.id
  end
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
97
end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
116
#  wiki_enabled           :boolean         default(TRUE), not null
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
117
118
#