-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproject_alias.rb
43 lines (38 loc) · 1.6 KB
/
project_alias.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
class ProjectAlias < ActiveRecord::Base
belongs_to :project
if defined? ChiliProject
IDENTIFIER_RE = /\A(?!\d+$)[a-z0-9\-_]*\z/
else
IDENTIFIER_RE = /\A(?!\d+$)[a-z0-9\-]*\z/
end
validates_presence_of :project, :alias
validates_uniqueness_of :alias
validates_length_of :alias, :in => Project.const_defined?(:IDENTIFIER_MAX_LENGTH) ? 1..Project::IDENTIFIER_MAX_LENGTH : 1..20
validates_format_of :alias, :with => IDENTIFIER_RE
validates_exclusion_of :alias, :in => %w(new)
def validate
if self.alias == self.project.identifier
errors.add(:alias, :same_as_identifier)
elsif Project.find_all_by_identifier(self.alias).any?
errors.add(:alias, :identifier_exists)
end
end
def rename!
if Project.connection.update("UPDATE #{Project.table_name} " +
"SET identifier = '#{self.alias}' " +
"WHERE id = #{project.id}") > 0
if Project.connection.update("UPDATE #{ProjectAlias.table_name} " +
"SET alias = '#{project.identifier}', undeletable = #{!self.undeletable} " +
"WHERE id = #{id}") > 0
true
else
Project.connection.update("UPDATE #{Project.table_name} " +
"SET identifier = '#{project.identifier}' " +
"WHERE id = #{project.id}")
false
end
else
false
end
end
end