diff --git a/app/models/account.rb b/app/models/account.rb
index 0e7708847..29912092c 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -31,4 +31,8 @@ class Account < ApplicationRecord
def account
self
end
+
+ def system_user
+ users.where(role: :system).first!
+ end
end
diff --git a/app/models/board/accessible.rb b/app/models/board/accessible.rb
index e4eb26e32..c923949d0 100644
--- a/app/models/board/accessible.rb
+++ b/app/models/board/accessible.rb
@@ -49,7 +49,7 @@ module Board::Accessible
end
def watchers
- users.without(User.system).where(accesses: { involvement: :watching })
+ users.active.where(accesses: { involvement: :watching })
end
private
diff --git a/app/models/card/entropic.rb b/app/models/card/entropic.rb
index 23e33c18a..85132ebe7 100644
--- a/app/models/card/entropic.rb
+++ b/app/models/card/entropic.rb
@@ -26,7 +26,7 @@ module Card::Entropic
class_methods do
def auto_postpone_all_due
due_to_be_postponed.find_each do |card|
- card.auto_postpone(user: User.system)
+ card.auto_postpone(user: card.account.system_user)
end
end
end
diff --git a/app/models/card/eventable/system_commenter.rb b/app/models/card/eventable/system_commenter.rb
index 0c9fe3517..0e76700b3 100644
--- a/app/models/card/eventable/system_commenter.rb
+++ b/app/models/card/eventable/system_commenter.rb
@@ -8,7 +8,7 @@ class Card::Eventable::SystemCommenter
def comment
return unless comment_body.present?
- card.comments.create! creator: User.system, body: comment_body, created_at: event.created_at
+ card.comments.create! creator: card.account.system_user, body: comment_body, created_at: event.created_at
end
private
diff --git a/app/models/user/role.rb b/app/models/user/role.rb
index 0712e2cf3..3c2101f68 100644
--- a/app/models/user/role.rb
+++ b/app/models/user/role.rb
@@ -8,12 +8,6 @@ module User::Role
scope :active, -> { where(active: true, role: %i[ admin member ]) }
end
- class_methods do
- def system
- find_or_create_by!(account: Current.account, role: :system) { it.name = "System" }
- end
- end
-
def can_change?(other)
admin? || other == self
end
diff --git a/app/views/cards/comments/_watchers.html.erb b/app/views/cards/comments/_watchers.html.erb
index 800fbb014..a2574b8c2 100644
--- a/app/views/cards/comments/_watchers.html.erb
+++ b/app/views/cards/comments/_watchers.html.erb
@@ -2,11 +2,11 @@
Subscribers
- <%= pluralize(card.watchers.without(User.system).count, "person") %> will be notified when someone comments on this.
+ <%= pluralize(card.watchers.active.count, "person") %> will be notified when someone comments on this.
- <% card.watchers.without(User.system).alphabetically.each do |watcher| %>
+ <% card.watchers.active.alphabetically.each do |watcher| %>
<%= avatar_tag watcher %>
<% end %>
diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml
index c9e9dfd72..70a855634 100644
--- a/test/fixtures/users.yml
+++ b/test/fixtures/users.yml
@@ -31,3 +31,9 @@ mike:
role: admin
identity: mike
account: initech_uuid
+
+system_initech:
+ id: <%= ActiveRecord::FixtureSet.identify("system_initech", :uuid) %>
+ name: System
+ role: system
+ account: initech_uuid
diff --git a/test/models/account_test.rb b/test/models/account_test.rb
index d47ce6f22..7b212e8ab 100644
--- a/test/models/account_test.rb
+++ b/test/models/account_test.rb
@@ -41,6 +41,22 @@ class AccountTest < ActiveSupport::TestCase
assert_equal "David", admin.name
assert_equal "david@37signals.com", admin.identity.email_address
assert_equal "admin", admin.role
+
+ assert_predicate account.system_user, :present?
+ end
+ end
+
+ test "#system_user returns the system user of the account" do
+ system_user = User.find_by!(account: accounts("37s"), role: :system)
+
+ assert_equal system_user, accounts("37s").system_user
+ end
+
+ test "#system_user raises if there is no system user" do
+ account = Account.create!(name: "No System User Account")
+
+ assert_raises ActiveRecord::RecordNotFound do
+ account.system_user
end
end
end
diff --git a/test/models/card/entropic_test.rb b/test/models/card/entropic_test.rb
index 892678b2f..9707eabf4 100644
--- a/test/models/card/entropic_test.rb
+++ b/test/models/card/entropic_test.rb
@@ -43,7 +43,7 @@ class Card::EntropicTest < ActiveSupport::TestCase
end
assert cards(:logo).reload.postponed?
- assert_equal User.system, cards(:logo).postponed_by
+ assert_equal accounts("37s").system_user, cards(:logo).postponed_by
assert_not cards(:shipping).reload.postponed?
end
diff --git a/test/models/notifier/event_notifier_test.rb b/test/models/notifier/event_notifier_test.rb
index bc68b2a6c..82942b96c 100644
--- a/test/models/notifier/event_notifier_test.rb
+++ b/test/models/notifier/event_notifier_test.rb
@@ -7,7 +7,7 @@ class Notifier::EventNotifierTest < ActiveSupport::TestCase
test "generate does not create notifications if the event was system-generated" do
cards(:logo).drafted!
- events(:logo_published).update!(creator: User.system)
+ events(:logo_published).update!(creator: accounts("37s").system_user)
assert_no_difference -> { Notification.count } do
Notifier.for(events(:logo_published)).notify
diff --git a/test/models/user_test.rb b/test/models/user_test.rb
index 8098e2190..6f5028bdb 100644
--- a/test/models/user_test.rb
+++ b/test/models/user_test.rb
@@ -35,14 +35,4 @@ class UserTest < ActiveSupport::TestCase
assert_equal "DHH", User.new(name: "David Heinemeier Hansson").initials
assert_equal "ÉLH", User.new(name: "Éva-Louise Hernández").initials
end
-
- test "system user" do
- system_user = User.system
-
- assert system_user.system?
- assert_equal "System", system_user.name
- assert_equal "S", system_user.initials
-
- assert_not_includes User.active, system_user
- end
end