Use repository committerships in TransferProjectOwnershipCommand
[gitorious:mainline.git] / app / presenters / repository_presenter.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2012-2013 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18 require "presenters/project_presenter"
19
20 # The presenter is intended to decouple the view and the model. This will enable
21 # us to refactor the underlying model without changing any views, remove view
22 # specific logic from the model, and provide a clear, explicit contract between
23 # the view and the model.
24 class RepositoryPresenter
25   attr_reader :repository
26   private :repository
27
28   def self.load(collection)
29     collection.map { |repository| RepositoryPresenter.new(repository) }
30   end
31
32   def initialize(repository)
33     @repository = repository
34   end
35
36   def git; repository.git; end
37   def id; repository.id; end
38   def name; repository.name; end
39   def gitdir; repository.gitdir; end
40   def to_param; repository.to_param; end
41   def path_segment; repository.path_segment; end
42   def full_repository_path; repository.full_repository_path; end
43   def head_candidate_name; repository.head_candidate_name; end
44   def disk_usage; repository.disk_usage; end
45   def ready?; repository.ready?; end
46   def committerships; repository.repository_committerships.committerships; end
47   def has_commits?; repository.has_commits?; end
48   def owner; repository.owner; end
49   def user; repository.user; end
50   def private?; repository.private?; end
51   def public?; repository.public?; end
52   def errors; repository.errors; end
53   def description; repository.description; end
54   def head; repository.head; end
55   def deny_force_pushing; repository.deny_force_pushing; end
56   def merge_requests_enabled; repository.merge_requests_enabled; end
57   def owner_id; repository.owner_id; end
58   def to_key; repository.to_key; end
59   def url_path; repository.url_path; end
60   def content_memberships; repository.content_memberships; end
61   def parent; repository.parent && RepositoryPresenter.new(repository.parent); end
62   def is_a?(thing); thing == Repository; end
63   def internal?; repository.internal?; end
64
65   def has_group_clones?
66     group_clones.any?
67   end
68
69   def has_user_clones?
70     user_clones.any?
71   end
72
73   def group_clone_count
74     group_clones.count
75   end
76
77   def user_clone_count
78     user_clones.count
79   end
80
81   def user_clones
82     @user_clones ||= repository.clones.by_users.fresh.map { |repo| self.class.new(repo) }
83   end
84
85   def owner_to_param_with_prefix
86     repository.owner.to_param_with_prefix
87   end
88
89   def user_to_param_with_prefix
90     repository.user.to_param_with_prefix
91   end
92
93   def owned_by_group?
94     repository.owned_by_group?
95   end
96
97   def short_created_at
98     repository.created_at.to_s(:short)
99   end
100
101   def long_created_at
102     repository.created_at.to_s(:long)
103   end
104
105   def user_committers
106     repository.repository_committerships.users
107   end
108
109   def group_committers
110     repository.repository_committerships.groups
111   end
112
113   def group_clones
114     @group_clones ||= repository.clones.by_groups.fresh.map { |repo| self.class.new(repo) }
115   end
116
117   def open_merge_request_count
118     repository.open_merge_requests.count
119   end
120
121   def self.model_name
122     Repository.model_name
123   end
124
125   def slug
126     "#{project.slug}/#{name}"
127   end
128
129   def project
130     @project ||= ProjectPresenter.new(@repository.project)
131   end
132 end