Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Last active October 24, 2017 20:16
Show Gist options
  • Save staycreativedesign/9c9a13eda6b08c9b3a09dd80f21d11c1 to your computer and use it in GitHub Desktop.
Save staycreativedesign/9c9a13eda6b08c9b3a09dd80f21d11c1 to your computer and use it in GitHub Desktop.
RSpec.describe Team, :type => :model do
let(:admin) { create(:user, role: Role::ADMIN, first_name: 'Administrator') }
let(:man) { create(:user, role: Role::COACH, first_name: 'Coach') }
it "doesn't allow for more than 4 teams for a sponsor" do
create_list(:team, 4, sponsor: admin)
admin.reload
admin.sponsored_teams.build(sponsor: admin)
admin.save
expect(admin.sponsored_teams.count).to eq(4)
end
it "if more than 4 team sponsored new team goes to first downline" do
create(:team, sponsor: admin, coach: man)
3.times do
create(:team, sponsor: admin)
end
admin.sponsored_teams.build(sponsor: admin)
admin.save
expect(man.sponsored_teams.count).to eq(1)
end
end
def foo(team)
# check current sponsor sponsored teams
# if equal or greather than 4
# find the first coach of a sponsored team by current sponsor and add the
# team as a sponsored team of coach
# else
# :
if team.sponsor.sponsored_teams.count >= 4
u = User.find(team.sponsor.id)
foo = User.find(u.sponsored_teams.first.coach.id)
team = foo.sponsored_teams << team
return team
else
end
end
@nilbus
Copy link

nilbus commented Oct 24, 2017

u = team.sponsor # avoid the extra lookup from `User.find`. u is the sponsor.
foo = u.sponsored_teams.first.coach # again avoid an extra lookup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment