-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathrepository_post_receive_url.rb
63 lines (47 loc) · 1.33 KB
/
repository_post_receive_url.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
require 'uri'
class RepositoryPostReceiveUrl < ActiveRecord::Base
include Redmine::SafeAttributes
## Attributes
safe_attributes 'url', 'mode', 'active', 'use_triggers', 'triggers', 'split_payloads'
## Relations
belongs_to :repository
## Validations
validates :repository_id, presence: true
# Only allow HTTP(s) format
validates :url, presence: true,
uniqueness: { case_sensitive: false, scope: :repository_id },
format: { with: URI::DEFAULT_PARSER.make_regexp(%w[http https]) }
validates :mode, presence: true, inclusion: { in: %i[github get post] }
## Serializations
serialize :triggers, type: Array
## Scopes
scope :active, -> { where active: true }
scope :inactive, -> { where active: false }
scope :sorted, -> { order :url }
## Callbacks
before_validation :strip_whitespace
before_validation :remove_blank_triggers
def mode
self[:mode].to_sym
end
def mode=(value)
self[:mode] = value.to_s
end
def github_mode?
mode == :github
end
private
# Strip leading and trailing whitespace
def strip_whitespace
self.url = begin
url.strip
rescue StandardError
''
end
end
# Remove blank entries in triggers
def remove_blank_triggers
self.triggers = triggers.select(&:present?)
end
end