Skip to content

Commit 2e778cd

Browse files
committed
Mongo 💀
1 parent 429d2d6 commit 2e778cd

15 files changed

+324
-74
lines changed

app/jobs/refresh_user_job.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ def perform(user_id, full=false)
77

88
user = User.find(user_id)
99

10-
if user.github_id
11-
user.destroy_github_cache
12-
end
13-
1410
return if !full && user.last_refresh_at > 3.days.ago
1511

1612
begin
@@ -21,7 +17,6 @@ def perform(user_id, full=false)
2117
user.calculate_score!
2218
ensure
2319
user.touch(:last_refresh_at)
24-
user.destroy_github_cache
2520
end
2621
end
2722
end

app/jobs/team_migrator_batch_job.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#TODO DELETE ME
2+
class TeamMigratorBatchJob
3+
include Sidekiq::Worker
4+
5+
def perform
6+
Team.each do |team|
7+
TeamMigratorJob.perform_async(team.id.to_s)
8+
end
9+
end
10+
end

app/jobs/team_migrator_job.rb

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#TODO DELETE ME
2+
class TeamMigratorJob
3+
include Sidekiq::Worker
4+
5+
sidekiq_options backtrace: true
6+
7+
def perform(id)
8+
team = Team.find(id)
9+
if pgteam = find_or_initialize_team(id, team)
10+
extract_account(pgteam, team)
11+
12+
extract_locations(pgteam, team)
13+
extract_links(pgteam, team)
14+
add_members(pgteam)
15+
add_jobs(pgteam)
16+
convert_followers(pgteam)
17+
add_pending_requests(pgteam, team)
18+
end
19+
end
20+
21+
22+
private
23+
24+
def find_or_initialize_team(id, team)
25+
begin
26+
PgTeam.find_or_initialize_by_mongo_id(id) do |pgteam|
27+
pgteam.name = team.name
28+
pgteam.slug = team.slug
29+
pgteam.created_at = team.created_at
30+
pgteam.updated_at = team.updated_at
31+
pgteam.website = team.website
32+
pgteam.about = team.about
33+
pgteam.total = team.total
34+
pgteam.size = team.size
35+
pgteam.mean = team.mean
36+
pgteam.median = team.median
37+
pgteam.score = team.score
38+
pgteam.twitter = team.twitter
39+
pgteam.facebook = team.facebook
40+
pgteam.premium = team.premium
41+
pgteam.analytics = team.analytics
42+
pgteam.valid_jobs = team.valid_jobs
43+
pgteam.hide_from_featured = team.hide_from_featured
44+
pgteam.preview_code = team.preview_code
45+
pgteam.youtube_url = team.youtube_url
46+
pgteam.github = team.github
47+
pgteam.highlight_tags = team.highlight_tags
48+
pgteam.branding = team.branding
49+
pgteam.headline = team.headline
50+
pgteam.big_quote = team.big_quote
51+
pgteam.big_image = team.big_image
52+
pgteam.featured_banner_image = team.featured_banner_image
53+
pgteam.benefit_name_1 = team.benefit_name_1
54+
pgteam.benefit_description_1 = team.benefit_description_1
55+
pgteam.benefit_name_2 = team.benefit_name_2
56+
pgteam.benefit_description_2 = team.benefit_description_2
57+
pgteam.benefit_name_3 = team.benefit_name_3
58+
pgteam.benefit_description_3 = team.benefit_description_3
59+
pgteam.reason_name_1 = team.reason_name_1
60+
pgteam.reason_description_1 = team.reason_description_1
61+
pgteam.reason_name_2 = team.reason_name_2
62+
pgteam.reason_description_2 = team.reason_description_2
63+
pgteam.reason_name_3 = team.reason_name_3
64+
pgteam.reason_description_3 = team.reason_description_3
65+
pgteam.why_work_image = team.why_work_image
66+
pgteam.organization_way = team.organization_way
67+
pgteam.organization_way_name = team.organization_way_name
68+
pgteam.organization_way_photo = team.organization_way_photo
69+
pgteam.office_photos = team.office_photos
70+
pgteam.upcoming_events = team.upcoming_events
71+
pgteam.featured_links_title = team.featured_links_title
72+
pgteam.blog_feed = team.blog_feed
73+
pgteam.our_challenge = team.our_challenge
74+
pgteam.your_impact = team.your_impact
75+
pgteam.interview_steps = team.interview_steps
76+
pgteam.hiring_tagline = team.hiring_tagline
77+
pgteam.link_to_careers_page = team.link_to_careers_page
78+
# pgteam.avatar = team.avatar
79+
pgteam.achievement_count = team.achievement_count
80+
pgteam.endorsement_count = team.endorsement_count
81+
pgteam.invited_emails = team.invited_emails
82+
pgteam.pending_join_requests = team.pending_join_requests
83+
pgteam.upgraded_at = team.upgraded_at
84+
pgteam.paid_job_posts = team.paid_job_posts
85+
pgteam.monthly_subscription = team.monthly_subscription
86+
pgteam.stack_list = team.stack_list
87+
pgteam.number_of_jobs_to_show = team.number_of_jobs_to_show
88+
pgteam.location = team.location
89+
pgteam.country_id = team.country_id
90+
pgteam.github_organization_name = team.github_organization_name
91+
pgteam.save!
92+
93+
end
94+
rescue ActiveRecord::RecordInvalid
95+
false
96+
end
97+
end
98+
99+
def extract_account(pgteam, team)
100+
return unless account = team.account
101+
return if pgteam.account
102+
begin
103+
pgaccount = pgteam.build_account(
104+
stripe_card_token: account.stripe_card_token,
105+
stripe_customer_token: account.stripe_customer_token,
106+
admin_id: account.admin_id
107+
)
108+
pgaccount.plans << Plan.where(id: account.plan_ids)
109+
pgaccount.save!
110+
rescue ActiveRecord::RecordInvalid => e
111+
# @just3ws, uncomment the following line and get all ID of the corrupted accounts
112+
# raise e
113+
false
114+
end
115+
116+
end
117+
118+
def extract_locations(pgteam, team)
119+
locations = team.team_locations
120+
return unless locations.any?
121+
return if pgteam.locations.any?
122+
locations.each do |location|
123+
pgteam.locations.create! name: location.name,
124+
description: location.description,
125+
address: location.address,
126+
city: location.city,
127+
state_code: location.state_code,
128+
country: location.country
129+
130+
end
131+
end
132+
133+
def extract_links(pgteam, team)
134+
links = team.featured_links
135+
return if links.empty?
136+
return if pgteam.links.any?
137+
links.each do |link|
138+
pgteam.links.create! name: link.name,
139+
url: link.url
140+
end
141+
end
142+
143+
def add_members(pgteam)
144+
users = User.where(team_document_id: pgteam.mongo_id)
145+
users.each do |user|
146+
pgteam.members.create! user: user, state: 'active'
147+
end
148+
users.update_all(team_id: pgteam.id)
149+
end
150+
151+
def add_jobs(pgteam)
152+
Opportunity.where(team_document_id: pgteam.mongo_id).update_all(team_id: pgteam.id)
153+
end
154+
155+
def convert_followers(pgteam)
156+
FollowedTeam.where(team_document_id: pgteam.mongo_id).update_all(team_id: pgteam.id)
157+
end
158+
159+
def add_pending_requests(pgteam, team)
160+
pending_team_members = team.pending_team_members
161+
return if pending_team_members.empty?
162+
pending_team_members.each do |pending_team_member|
163+
user = User.find pending_team_member.user_id
164+
pgteam.members.create user: user,
165+
created_at: pending_team_member.created_at,
166+
updated_at: pending_team_member.updated_at
167+
end
168+
end
169+
end

app/models/concerns/team_migration.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module TeamMigration
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
scope :zombies, -> { where(state: 'zombie') }
6+
end
7+
8+
module ClassMethods
9+
10+
def the_walking_deads
11+
active_teams_ids = Teams::Member.pluck(:team_id).uniq
12+
where('id not in (?)', active_teams_ids)
13+
end
14+
15+
def mark_the_walking_deads!
16+
the_walking_deads.update_all(state: 'zombie')
17+
end
18+
19+
def kill_zombies!
20+
zombies.destroy_all
21+
end
22+
23+
end
24+
end

app/models/concerns/team_search.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module TeamSearch
2+
extend ActiveSupport::Concern
3+
included do
4+
include Elasticsearch::Model
5+
6+
#TODO
7+
end
8+
end

app/models/followed_team.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
# TODO, rename to Teams::Follower
12
class FollowedTeam < ActiveRecord::Base
3+
belongs_to :team, class_name: 'PgTeam'
4+
belongs_to :user
25
end
36

47
# == Schema Information
5-
# Schema version: 20140728214411
68
#
79
# Table name: followed_teams
810
#
911
# id :integer not null, primary key
1012
# user_id :integer
1113
# team_document_id :string(255)
12-
# created_at :datetime default(2014-02-20 22:39:11 UTC)
14+
# created_at :datetime default(2012-03-12 21:01:09 UTC)
15+
# team_id :integer
1316
#

app/models/pg_team.rb

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
#Rename to Team when Mongodb is dropped
22
class PgTeam < ActiveRecord::Base
3+
include TeamSearch
4+
include TeamMigration
5+
36
self.table_name = 'teams'
47
#TODO add inverse_of
5-
has_one :account, class_name: 'Teams::Account', foreign_key: 'team_id', dependent: :destroy
8+
has_one :account, class_name: 'Teams::Account', foreign_key: 'team_id', dependent: :delete
69

7-
has_many :members, class_name: 'Teams::Member', foreign_key: 'team_id', dependent: :destroy
8-
has_many :links, class_name: 'Teams::Link', foreign_key: 'team_id', dependent: :destroy
9-
has_many :locations, class_name: 'Teams::Location', foreign_key: 'team_id', dependent: :destroy
10+
has_many :members, class_name: 'Teams::Member', foreign_key: 'team_id', dependent: :delete_all
11+
has_many :links, class_name: 'Teams::Link', foreign_key: 'team_id', dependent: :delete_all
12+
has_many :locations, class_name: 'Teams::Location', foreign_key: 'team_id', dependent: :delete_all
1013
has_many :jobs, class_name: 'Opportunity', foreign_key: 'team_id', dependent: :destroy
1114

12-
before_validation :create_slug!
15+
has_many :follows , class_name: 'FollowedTeam', foreign_key: 'team_id', dependent: :destroy
16+
has_many :followers, through: :follows
17+
18+
accepts_nested_attributes_for :locations, :links, allow_destroy: true, reject_if: :all_blank
1319

14-
validates_uniqueness_of :slug
20+
scope :featured, ->{ where(premium: true, valid_jobs: true, hide_from_featured: false) }
1521

22+
mount_uploader :avatar, TeamUploader
23+
24+
before_validation :create_slug!
1625

17-
private
26+
validates :slug, uniqueness: true, presence: true
1827

19-
def create_slug!
20-
self.slug = name.parameterize
21-
end
28+
private def create_slug!
29+
self.slug = name.parameterize
30+
end
2231

2332
end
2433
#
@@ -69,20 +78,15 @@ def create_slug!
6978
# organization_way :text
7079
# organization_way_name :text
7180
# organization_way_photo :text
72-
# office_photos :string(255) default("{}")
73-
# upcoming_events :string(255) default("{}")
7481
# featured_links_title :string(255)
7582
# blog_feed :text
7683
# our_challenge :text
7784
# your_impact :text
78-
# interview_steps :string(255) default("{}")
7985
# hiring_tagline :text
8086
# link_to_careers_page :text
8187
# avatar :string(255)
8288
# achievement_count :integer default(0)
8389
# endorsement_count :integer default(0)
84-
# invited_emails :string(255) default("{}")
85-
# pending_join_requests :string(255) default("{}")
8690
# upgraded_at :datetime
8791
# paid_job_posts :integer default(0)
8892
# monthly_subscription :boolean default(FALSE)
@@ -93,4 +97,11 @@ def create_slug!
9397
# name :string(255)
9498
# github_organization_name :string(255)
9599
# team_size :integer
100+
# mongo_id :string(255)
101+
# office_photos :string(255) default([]), is an Array
102+
# upcoming_events :text default([]), is an Array
103+
# interview_steps :text default([]), is an Array
104+
# invited_emails :string(255) default([]), is an Array
105+
# pending_join_requests :string(255) default([]), is an Array
106+
# state :string(255) default("active")
96107
#

0 commit comments

Comments
 (0)