comparison plugins/redmine_checkout/lib/checkout/protocol.rb @ 1117:b4b72f1eb644 redmine-2.2-integration

Moved all the plugins from the vendor folder to the application root folder.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Tue, 08 Jan 2013 12:32:05 +0000
parents vendor/plugins/redmine_checkout/lib/checkout/protocol.rb@020926a36823
children 522f2aff1f07
comparison
equal deleted inserted replaced
1116:bb32da3bea34 1117:b4b72f1eb644
1 module Checkout
2 class <<self
3 def awesome?
4 # Yes, this plugin is awesome!
5 true
6 end
7 end
8
9 class Protocol
10 attr_accessor :protocol, :regex, :regex_replacement, :access, :repository
11 attr_writer :default, :command, :fixed_url, :append_path
12
13
14 def initialize(args={})
15 args = args.dup
16
17 @protocol = args.delete :protocol
18 @command = args.delete :command # optional, if not set the default from the repo is used
19
20 # either a fixed url
21 @fixed_url = args.delete :fixed_url
22
23 # or a regex
24 @regex = args.delete :regex
25 @regex_replacement = args.delete :regex_replacement
26
27 @access = args.delete :access
28 @append_path = args.delete :append_path
29 @default = args.delete :is_default
30
31 @repository = args.delete :repository
32 end
33
34 def full_command(path = "")
35 cmd = ""
36 if repository.checkout_display_command?
37 cmd = self.command.present? ? self.command.strip + " " : ""
38 end
39 cmd + URI.escape(self.url(path))
40 end
41
42 def default?
43 @default.to_i > 0
44 end
45
46 def command
47 @command || self.repository && self.repository.checkout_default_command || ""
48 end
49
50 def append_path?
51 @append_path.to_i > 0
52 end
53
54 def access_rw(user)
55 # reduces the three available access levels 'read+write', 'read-only' and 'permission'
56 # to 'read+write' and 'read-only' and nil (not allowed)
57
58 @access_rw ||= {}
59 return @access_rw[user] if @access_rw.key? user
60 @access_rw[user] = case access
61 when 'permission'
62 case
63 when user.allowed_to?(:commit_access, repository.project) && user.allowed_to?(:browse_repository, repository.project)
64 'read+write'
65 when user.allowed_to?(:browse_repository, repository.project)
66 'read-only'
67 else
68 nil
69 end
70 else
71 @access
72 end
73 end
74
75 def access_label(user)
76 case access_rw(user)
77 when 'read+write': :label_access_read_write
78 when 'read-only': :label_access_read_only
79 end
80 end
81
82 def fixed_url
83 @fixed_url.present? ? @fixed_url : begin
84 if (regex.blank? || regex_replacement.blank?)
85 repository.url
86 else
87 repository.url.gsub(Regexp.new(regex), regex_replacement)
88 end
89 end
90 rescue RegexpError
91 repository.url || ""
92 end
93
94 def url(path = "")
95 return "" unless repository
96
97 url = fixed_url.sub(/\/+$/, "")
98 if repository.allow_subtree_checkout? && self.append_path? && path.present?
99 url = "#{url}/#{path}"
100 end
101
102 if repository.checkout_display_login?
103 begin
104 uri = URI.parse url
105 (uri.user = repository.login) if repository.login
106 (uri.password = repository.password) if (repository.checkout_display_login == 'password' && repository.login && repository.password)
107 url = uri.to_s
108 rescue URI::InvalidURIError
109 end
110 end
111 url
112 end
113 end
114 end