@@ -9,12 +9,14 @@ class RepositoryGitExtra < ActiveRecord::Base
9
9
]
10
10
11
11
DISABLED = 0
12
- HTTP = 1
13
- HTTPS = 2
14
- BOTH = 3
12
+ HTTP = 3
13
+ HTTPS = 1
14
+ BOTH = 2
15
+
16
+ ALLOWED_URLS = %w[ ssh http https go git git_annex ]
15
17
16
18
## Attributes
17
- attr_accessible :git_http , :git_daemon , :git_notify , :git_annex , :default_branch , :protected_branch , :public_repo , :key
19
+ attr_accessible :git_http , :git_daemon , :git_notify , :git_annex , :default_branch , :protected_branch , :public_repo , :key , :urls_order
18
20
19
21
## Relations
20
22
belongs_to :repository
@@ -25,8 +27,14 @@ class RepositoryGitExtra < ActiveRecord::Base
25
27
validates :default_branch , presence : true
26
28
validates :key , presence : true
27
29
30
+ validate :validate_urls_order
31
+
32
+ ## Serializations
33
+ serialize :urls_order , Array
34
+
28
35
## Callbacks
29
- after_save :check_if_default_branch_changed
36
+ before_save :check_urls_order_consistency
37
+ after_save :check_if_default_branch_changed
30
38
31
39
## Virtual attribute
32
40
attr_accessor :default_branch_has_changed
@@ -41,6 +49,13 @@ def default_branch_has_changed?
41
49
private
42
50
43
51
52
+ def validate_urls_order
53
+ urls_order . each do |url |
54
+ errors . add ( :urls_order , :invalid ) unless ALLOWED_URLS . include? ( url )
55
+ end
56
+ end
57
+
58
+
44
59
# This is Rails method : <attribute>_changed?
45
60
# However, the value is cleared before passing the object to the controller.
46
61
# We need to save it in virtual attribute to trigger Gitolite resync if changed.
@@ -53,4 +68,57 @@ def check_if_default_branch_changed
53
68
end
54
69
end
55
70
71
+
72
+ def check_urls_order_consistency
73
+ check_ssh_url
74
+ check_git_http_urls
75
+ check_go_url
76
+ check_git_url
77
+ end
78
+
79
+
80
+ # SSH url should always be present in urls_order Array
81
+ #
82
+ def check_ssh_url
83
+ add_url ( 'ssh' )
84
+ end
85
+
86
+
87
+ def check_git_http_urls
88
+ case git_http
89
+ when DISABLED
90
+ remove_url ( 'http' )
91
+ remove_url ( 'https' )
92
+ when HTTP
93
+ add_url ( 'http' )
94
+ remove_url ( 'https' )
95
+ when HTTPS
96
+ add_url ( 'https' )
97
+ remove_url ( 'http' )
98
+ when BOTH
99
+ add_url ( 'http' )
100
+ add_url ( 'https' )
101
+ end
102
+ end
103
+
104
+
105
+ def check_go_url
106
+ repository . go_access_available? ? add_url ( 'go' ) : remove_url ( 'go' )
107
+ end
108
+
109
+
110
+ def check_git_url
111
+ git_daemon? ? add_url ( 'git' ) : remove_url ( 'git' )
112
+ end
113
+
114
+
115
+ def remove_url ( url )
116
+ self . urls_order . delete ( url )
117
+ end
118
+
119
+
120
+ def add_url ( url )
121
+ self . urls_order . push ( url ) . uniq!
122
+ end
123
+
56
124
end
0 commit comments