Skip to content

Commit 061ec98

Browse files
committed
Added jobs to Team
Added counter_cache for team_size Destroy dependent for Team
1 parent 1a0c19a commit 061ec98

File tree

9 files changed

+35
-13
lines changed

9 files changed

+35
-13
lines changed

app/models/opportunity.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Opportunity < ActiveRecord::Base
1818
validates :description, presence: true, length: { minimum: 10, maximum: 600 }
1919
validates :team_document_id, presence: true
2020
validates :opportunity_type, inclusion: { in: OPPORTUNITY_TYPES }
21-
validates :salary, presence: true, allow_blank: false, numericality: true, inclusion: 0..800_000, allow_blank: true
21+
validates :salary, presence: true, numericality: true, inclusion: 0..800_000, allow_blank: true
2222
validates :location_city, presence: true, allow_blank: false, unless: lambda { location && anywhere?(location) }
2323

2424
before_validation :set_location_city
@@ -28,9 +28,11 @@ class Opportunity < ActiveRecord::Base
2828
after_save :remove_from_index, unless: :alive?
2929
after_create :pay_for_it!
3030

31+
#this scope should be renamed.
3132
scope :valid, where(deleted: false).where('expires_at > ?', Time.now).order('created_at DESC')
3233
scope :by_city, ->(city) { where('LOWER(location_city) LIKE ?', "%#{city.try(:downcase)}%") }
3334
scope :by_tag, ->(tag) { where('LOWER(cached_tags) LIKE ?', "%#{tag}%") unless tag.nil? }
35+
#remove default scope
3436
default_scope valid
3537

3638
attr_accessor :title

app/models/pg_team.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#Rename to Team when Mongodb is dropped
22
class PgTeam < ActiveRecord::Base
3-
self.table_name = 'teams'
4-
#TODO add inverse_of
5-
has_one :account, class_name: 'Teams::Account' , foreign_key: 'team_id'
3+
self.table_name = 'teams'
4+
#TODO add inverse_of
5+
has_one :account, class_name: 'Teams::Account', foreign_key: 'team_id', dependent: :destroy
6+
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 :jobs, class_name: 'Opportunity', foreign_key: 'team_id', dependent: :destroy
11+
612

7-
has_many :members, class_name: 'Teams::Member', foreign_key: 'team_id'
8-
has_many :links, class_name: 'Teams::Link', foreign_key: 'team_id'
9-
has_many :locations, class_name: 'Teams::Location' , foreign_key: 'team_id'
1013
end

app/models/team.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ def fix_website_url!
756756
end
757757
end
758758

759+
#Will delete , it not even working
759760
def upcoming_events
760761
team_members.collect do |member|
761762

@@ -774,6 +775,7 @@ def jobs
774775
all_jobs.valid
775776
end
776777

778+
#Replaced with jobs
777779
def all_jobs
778780
Opportunity.where(team_document_id: self.id.to_s).order('created_at DESC')
779781
end
@@ -986,6 +988,7 @@ def id_of(user)
986988
user.is_a?(User) ? user.id : user
987989
end
988990

991+
#Replaced with team_size attribute
989992
def update_team_size!
990993
self.size = User.where(team_document_id: self.id.to_s).count
991994
end

app/models/teams/member.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Teams::Member < ActiveRecord::Base
2-
belongs_to :team, class_name: 'PgTeam', foreign_key: 'team_id'
2+
belongs_to :team, class_name: 'PgTeam', foreign_key: 'team_id' , counter_cache: :team_size
33
belongs_to :user
44
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddTeamIdToOpportunities < ActiveRecord::Migration
2+
def change
3+
add_column :opportunities, :team_id, :integer
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddTeamsizeToTeamsMember < ActiveRecord::Migration
2+
def change
3+
add_column :teams_members, :team_size, :integer, default: 0
4+
end
5+
end

db/schema.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
t.string "location_city"
206206
t.boolean "apply", :default => false
207207
t.string "public_id"
208+
t.integer "team_id"
208209
end
209210

210211
create_table "pictures", :force => true do |t|
@@ -388,10 +389,11 @@
388389
end
389390

390391
create_table "teams_members", :force => true do |t|
391-
t.integer "team_id", :null => false
392-
t.integer "user_id", :null => false
393-
t.datetime "created_at", :null => false
394-
t.datetime "updated_at", :null => false
392+
t.integer "team_id", :null => false
393+
t.integer "user_id", :null => false
394+
t.datetime "created_at", :null => false
395+
t.datetime "updated_at", :null => false
396+
t.integer "team_size", :default => 0
395397
end
396398

397399
create_table "tokens", :force => true do |t|

spec/models/pg_team_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
it {is_expected.to have_many :locations}
77
it {is_expected.to have_many :links}
88
it {is_expected.to have_many :members}
9+
it {is_expected.to have_many :jobs}
910
end

spec/models/teams/member_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'rails_helper'
22

33
RSpec.describe Teams::Member, :type => :model do
4-
it {is_expected.to belong_to(:team)}
4+
it {is_expected.to belong_to(:team).counter_cache(:team_size)}
5+
it {is_expected.to belong_to(:user)}
56
end

0 commit comments

Comments
 (0)