Skip to content

Commit

Permalink
More tests and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jagthedrummer committed Nov 19, 2024
1 parent 8628ae6 commit 7c3e725
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bullet_train/app/models/concerns/memberships/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module Memberships::Base

after_commit :publish_changed_quantity

after_create :set_team_time_zone, if: :is_first_membership?

after_validation :remove_user_profile_photo, if: :user_profile_photo_removal?

scope :excluding_platform_agents, -> { where(platform_agent_of: nil) }
Expand Down Expand Up @@ -157,5 +159,13 @@ def publish_changed_quantity
ActiveSupport::Notifications.instrument("memberships.quantity-changed", {team:})
end

def set_team_time_zone
team.set_time_zone_from_user(user)
end

def is_first_membership?
team.memberships.count == 1 && team.memberships.first.id == id
end

ActiveSupport.run_load_hooks :bullet_train_memberships_base, self
end
5 changes: 5 additions & 0 deletions bullet_train/app/models/concerns/teams/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def initialize(attributes = nil)
self.time_zone = "UTC" if time_zone.blank?
end

def set_time_zone_from_user(user)
self.time_zone = user.time_zone if user.time_zone.present?
save
end

def platform_agent_access_tokens
Platform::AccessToken.joins(:application).where(resource_owner_id: users.where.not(platform_agent_of_id: nil), application: {team: nil})
end
Expand Down
30 changes: 30 additions & 0 deletions bullet_train/test/models/team_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,41 @@ class TeamTest < ActiveSupport::TestCase
assert_equal "UTC", team.time_zone
end

test "explicitly set time_zone is not clobbered by first user" do
team = Team.create!(name: "new test team", time_zone: "Eastern Time (US & Canada)")
user = User.create!(email: "[email protected]", password: "password", password_confirmation: "password", time_zone: "Central Time (US & Canada)")
Membership.create!(team: team, user: user)
team.reload
assert_equal "Eastern Time (US & Canada)", team.time_zone
end

test "a new team gets the time_zone of the first user when they join" do
team = Team.create!(name: "new test team")
user = User.create!(email: "[email protected]", password: "password", password_confirmation: "password", time_zone: "Central Time (US & Canada)")
Membership.create!(team: team, user: user)
team.reload
assert_equal "Central Time (US & Canada)", team.time_zone
end

test "default UTC time_zone is not clobbered if first user doesn't have a time zone set" do
team = Team.create!(name: "new test team")
user = User.create!(email: "[email protected]", password: "password", password_confirmation: "password", time_zone: nil)
Membership.create!(team: team, user: user)
team.reload
assert_equal "UTC", team.time_zone
end

test "default UTC time_zone is overwritten once the first user sets a time zone" do
team = Team.create!(name: "new test team")
user = User.create!(email: "[email protected]", password: "password", password_confirmation: "password", time_zone: nil)
Membership.create!(team: team, user: user)
team.reload
assert_equal "UTC", team.time_zone

user.time_zone = "Central Time (US & Canada)"
user.save

team.reload
assert_equal "Central Time (US & Canada)", team.time_zone
end
end

0 comments on commit 7c3e725

Please sign in to comment.