Skip to content

Commit a982f41

Browse files
committed
clean up Team model duplication and fix grouping
- Remove duplicate association declarations - Remove duplicate method declarations - Remove duplicate mount_uploader - Remove duplicate callbacks - Remove duplicate validations - Group associations - Group callbacks - Group validations
1 parent 894861d commit a982f41

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

app/models/team.rb

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,34 @@ class Team < ActiveRecord::Base
1818

1919
mount_uploader :avatar, TeamUploader
2020

21-
scope :featured, ->{ where(premium: true, valid_jobs: true, hide_from_featured: false) }
21+
has_many :followers, through: :follows, source: :team
22+
has_many :follows, class_name: 'FollowedTeam', foreign_key: 'team_id', dependent: :destroy
23+
has_many :jobs, class_name: 'Opportunity', foreign_key: 'team_id', dependent: :destroy
24+
has_many :links, class_name: 'Teams::Link', foreign_key: 'team_id', dependent: :delete_all
25+
has_many :locations, class_name: 'Teams::Location', foreign_key: 'team_id', dependent: :delete_all
26+
has_many :members, class_name: 'Teams::Member', foreign_key: 'team_id', dependent: :delete_all
27+
has_one :account, class_name: 'Teams::Account', foreign_key: 'team_id', dependent: :delete
28+
29+
accepts_nested_attributes_for :locations, :links, allow_destroy: true, reject_if: :all_blank
2230

2331
before_validation :create_slug!
32+
before_validation :fix_website_url!
33+
before_save :update_team_size!
34+
before_save :clear_cache_if_premium_team
35+
after_create :generate_event
36+
after_save :reindex_search
37+
after_destroy :reindex_search
38+
after_destroy :remove_dependencies
2439

2540
validates :slug, uniqueness: true, presence: true
41+
validates :name, presence: true
42+
43+
scope :featured, ->{ where(premium: true, valid_jobs: true, hide_from_featured: false) }
2644

2745
def top_three_team_members
2846
members.first(3)
2947
end
3048

31-
has_many :followers, through: :follows, source: :team
32-
33-
has_many :follows, class_name: 'FollowedTeam', foreign_key: 'team_id', dependent: :destroy
34-
has_many :jobs, class_name: 'Opportunity', foreign_key: 'team_id', dependent: :destroy
35-
has_many :links, class_name: 'Teams::Link', foreign_key: 'team_id', dependent: :delete_all
36-
has_many :locations, class_name: 'Teams::Location', foreign_key: 'team_id', dependent: :delete_all
37-
has_many :members, class_name: 'Teams::Member', foreign_key: 'team_id', dependent: :delete_all
38-
has_one :account, class_name: 'Teams::Account', foreign_key: 'team_id', dependent: :delete
39-
4049
def featured_links
4150
links
4251
end
@@ -49,44 +58,10 @@ def admins
4958
[]
5059
end
5160

52-
has_many :jobs, class_name: 'Opportunity', foreign_key: 'team_id', dependent: :destroy
53-
54-
private def create_slug!
55-
self.slug = name.parameterize
56-
end
57-
5861
def all_jobs
5962
jobs.order('created_at DESC')
6063
end
6164

62-
has_many :follows, class_name: 'FollowedTeam', foreign_key: 'team_id', dependent: :destroy
63-
has_many :followers, through: :follows, source: :team
64-
65-
accepts_nested_attributes_for :locations, :links, allow_destroy: true, reject_if: :all_blank
66-
67-
scope :featured, ->{ where(premium: true, valid_jobs: true, hide_from_featured: false) }
68-
69-
mount_uploader :avatar, TeamUploader
70-
71-
before_validation :create_slug!
72-
73-
validates :slug, uniqueness: true, presence: true
74-
validates :name, presence: true
75-
76-
private def create_slug!
77-
self.slug = name.parameterize
78-
end
79-
80-
before_save :update_team_size!
81-
before_save :clear_cache_if_premium_team
82-
before_validation :fix_website_url!
83-
after_create :generate_event
84-
after_save :reindex_search
85-
after_destroy :reindex_search
86-
after_destroy :remove_dependencies
87-
88-
scope :featured, ->{ where(premium: true, valid_jobs: true, hide_from_featured: false) }
89-
9065
def self.search(query_string, country, page, per_page, search_type = :query_and_fetch)
9166
country = query_string.gsub!(/country:(.+)/, '') && $1 if country.nil?
9267
query = ''
@@ -217,7 +192,6 @@ def has_member?(user)
217192
members.include?(user)
218193
end
219194

220-
221195
def branding_hex_color
222196
branding || DEFAULT_HEX_BRAND
223197
end

0 commit comments

Comments
 (0)