| Class | Team |
| In: |
app/models/team.rb
|
| Parent: | ActiveRecord::Base |
# File app/models/team.rb, line 43
43: def Team.find_all_by_name_like(name, limit = 100)
44: name_like = "%#{name}%"
45: Team.find(
46: :all,
47: :conditions => ['teams.name like ? or aliases.name like ?', name_like, name_like],
48: :include => :aliases,
49: :limit => limit,
50: :order => 'teams.name'
51: )
52: end
# File app/models/team.rb, line 24
24: def Team.find_by_name_or_alias(name)
25: team = Team.find_by_name(name)
26: if team.nil?
27: team_alias = Alias.find_by_name(name)
28: if team_alias
29: team = team_alias.team
30: end
31: end
32: team
33: end
# File app/models/team.rb, line 35
35: def Team.find_by_name_or_alias_or_create(name)
36: team = find_by_name_or_alias(name)
37: if team.nil?
38: team = Team.create(:name => name)
39: end
40: team
41: end
# File app/models/team.rb, line 72
72: def add_alias_for_old_name
73: if !new_record? &&
74: @old_name.present? &&
75: name.present? &&
76: @old_name.casecmp(name) != 0 &&
77: !Alias.exists?(['name = ? and team_id = ?', @old_name, id]) &&
78: !Team.exists?(["name = ?", @old_name])
79:
80: new_alias = Alias.create!(:name => @old_name, :team => self)
81: unless new_alias.save
82: logger.error("Could not save alias #{new_alias}: #{new_alias.errors.full_messages.join(", ")}")
83: end
84: new_alias
85: end
86: end
Preserve team names in old results by creating a new Team for them, and moving the results.
Results are preserved by creating a new Team from the most recent Name. If a Team already exists with the Name‘s name, results will move to existing Team. This may be unxpected, can‘t think of a better way to handle it in this model.
# File app/models/team.rb, line 133
133: def create_team_for_historical_results!
134: name = names.sort_by(&:year).reverse!.first
135:
136: if name
137: team = Team.find_or_create_by_name(name.name)
138: results.each do |result|
139: team.results << result if result.date.year <= name.year
140: end
141:
142: name.destroy
143: names.each do |name|
144: team.names << name unless name == name
145: end
146: end
147: end
# File app/models/team.rb, line 158
158: def created_from_result?
159: !created_by.nil? && created_by.kind_of?(Event)
160: end
If name changes to match existing alias, destroy the alias
# File app/models/team.rb, line 68
68: def destroy_shadowed_aliases
69: Alias.destroy_all(['name = ?', name])
70: end
# File app/models/team.rb, line 60
60: def ensure_no_results
61: return true if results.empty?
62:
63: errors.add_to_base "Cannot delete team with results. #{name} has #{results.count} results."
64: false
65: end
# File app/models/team.rb, line 93
93: def has_alias?(alias_name)
94: aliases.detect { |a| a.name.casecmp(alias_name) == 0 }
95: end
# File app/models/team.rb, line 149
149: def member_in_year?(date = Date.today)
150: member
151: end
Moves another Team‘s aliases, results, and people to this Team, and delete the other Team. Also adds the other Team‘s name as a new alias
# File app/models/team.rb, line 100
100: def merge(team)
101: raise(ArgumentError, 'Cannot merge nil team') unless team
102: raise(ArgumentError, 'Cannot merge team onto itself') if team == self
103:
104: Team.transaction do
105: events_with_results = team.results.collect do |result|
106: event = result.event
107: event.disable_notification!
108: event
109: end || []
110:
111: team.create_team_for_historical_results!
112:
113: aliases << team.aliases
114: events << team.events
115: results << team.results(true)
116: people << team.people
117: Team.delete(team.id)
118: existing_alias = aliases.detect{ |a| a.name.casecmp(team.name) == 0 }
119: if existing_alias.nil? && Team.find_all_by_name(team.name).empty?
120: aliases.create(:name => team.name)
121: end
122: events_with_results.each do |event|
123: event.enable_notification!
124: end
125: end
126: end
# File app/models/team.rb, line 153
153: def name=(value)
154: @old_name = name unless @old_name
155: self[:name] = value
156: end
Otherwise, if we change name in memory more than once, @old_name will be out of date
# File app/models/team.rb, line 89
89: def reset_old_name
90: @old_name = name
91: end
# File app/models/team.rb, line 54
54: def teams_with_same_name
55: teams = Team.find_all_by_name(self.name) | Alias.find_all_teams_by_name(self.name)
56: teams.reject! { |team| team == self }
57: teams
58: end