diff --git a/app/models/card/entropic.rb b/app/models/card/entropic.rb index 9d1a6c71a..4b11a2099 100644 --- a/app/models/card/entropic.rb +++ b/app/models/card/entropic.rb @@ -4,16 +4,19 @@ module Card::Entropic included do scope :due_to_be_postponed, -> do active + .joins(board: :account) .left_outer_joins(board: :entropy) - .where("last_active_at <= DATE_SUB(NOW(), INTERVAL COALESCE(entropies.auto_postpone_period, ?) SECOND)", - Current.account.entropy.auto_postpone_period) + .joins("LEFT OUTER JOIN entropies AS account_entropies ON account_entropies.account_id = accounts.id AND account_entropies.container_type = 'Account' AND account_entropies.container_id = accounts.id") + .where("last_active_at <= DATE_SUB(NOW(), INTERVAL COALESCE(entropies.auto_postpone_period, account_entropies.auto_postpone_period) SECOND)") end scope :postponing_soon, -> do active + .joins(board: :account) .left_outer_joins(board: :entropy) - .where("last_active_at > DATE_SUB(NOW(), INTERVAL COALESCE(entropies.auto_postpone_period, ?) SECOND)", Current.account.entropy.auto_postpone_period) - .where("last_active_at <= DATE_SUB(NOW(), INTERVAL CAST(COALESCE(entropies.auto_postpone_period, ?) * 0.75 AS SIGNED) SECOND)", Current.account.entropy.auto_postpone_period) + .joins("LEFT OUTER JOIN entropies AS account_entropies ON account_entropies.account_id = accounts.id AND account_entropies.container_type = 'Account' AND account_entropies.container_id = accounts.id") + .where("last_active_at > DATE_SUB(NOW(), INTERVAL COALESCE(entropies.auto_postpone_period, account_entropies.auto_postpone_period) SECOND)") + .where("last_active_at <= DATE_SUB(NOW(), INTERVAL CAST(COALESCE(entropies.auto_postpone_period, account_entropies.auto_postpone_period) * 0.75 AS SIGNED) SECOND)") end delegate :auto_postpone_period, to: :board diff --git a/test/fixtures/boards.yml b/test/fixtures/boards.yml index e9d6b75bf..ef3db1eda 100644 --- a/test/fixtures/boards.yml +++ b/test/fixtures/boards.yml @@ -12,9 +12,9 @@ private: all_access: false account: 37s_uuid -staplers: - id: <%= ActiveRecord::FixtureSet.identify("staplers", :uuid) %> - name: Staplers +miltons_wish_list: + id: <%= ActiveRecord::FixtureSet.identify("miltons_wish_list", :uuid) %> + name: Milton's Wish List creator: mike_uuid all_access: true account: initech_uuid diff --git a/test/fixtures/cards.yml b/test/fixtures/cards.yml index a94f3e3a7..5bfa77e6d 100644 --- a/test/fixtures/cards.yml +++ b/test/fixtures/cards.yml @@ -58,3 +58,24 @@ buy_domain: last_active_at: <%= 1.week.ago %> account: 37s_uuid +radio: + id: <%= ActiveRecord::FixtureSet.identify("radio", :uuid) %> + number: 1 + board: miltons_wish_list_uuid + creator: mike_uuid + title: I want to play my radio at a reasonable volume + created_at: <%= 1.week.ago %> + status: published + last_active_at: <%= 1.week.ago %> + account: initech_uuid + +paycheck: + id: <%= ActiveRecord::FixtureSet.identify("paycheck", :uuid) %> + number: 2 + board: miltons_wish_list_uuid + creator: mike_uuid + title: I haven't received my paycheck + created_at: <%= 1.week.ago %> + status: published + last_active_at: <%= 1.week.ago %> + account: initech_uuid diff --git a/test/fixtures/entropies.yml b/test/fixtures/entropies.yml index 2f66e05ef..53edb7cc4 100644 --- a/test/fixtures/entropies.yml +++ b/test/fixtures/entropies.yml @@ -15,3 +15,15 @@ private_board: account: 37s_uuid container: private_uuid (Board) auto_postpone_period: <%= 30.days.to_i %> + +initech_account: + id: <%= ActiveRecord::FixtureSet.identify("initech_account", :uuid) %> + account: initech_uuid + container: initech_uuid (Account) + auto_postpone_period: <%= 30.days.to_i %> + +miltons_wish_list_board: + id: <%= ActiveRecord::FixtureSet.identify("miltons_wish_list_board", :uuid) %> + account: initech_uuid + container: miltons_wish_list_uuid (Board) + auto_postpone_period: <%= 90.days.to_i %> diff --git a/test/models/card/entropic_test.rb b/test/models/card/entropic_test.rb index c39d1df7f..55c6552b1 100644 --- a/test/models/card/entropic_test.rb +++ b/test/models/card/entropic_test.rb @@ -68,4 +68,26 @@ class Card::EntropicTest < ActiveSupport::TestCase assert_includes Card.postponing_soon, cards(:logo) assert_not_includes Card.postponing_soon, cards(:shipping) end + + test "due_to_be_postponed scope works properly cross-account" do + cards(:logo).update!(last_active_at: entropies(:writebook_board).auto_postpone_period.seconds.ago - 2.days) + cards(:radio).update!(last_active_at: entropies(:miltons_wish_list_board).auto_postpone_period.seconds.ago - 2.days) + + actual = Card.due_to_be_postponed.to_a + + cards(:logo, :radio).each do |card| + assert_includes actual, card + end + end + + test "postponing_soon scope works properly cross-account" do + cards(:logo).update!(last_active_at: entropies(:writebook_board).auto_postpone_period.seconds.ago + 2.days) + cards(:radio).update!(last_active_at: entropies(:miltons_wish_list_board).auto_postpone_period.seconds.ago + 2.days) + + actual = Card.postponing_soon.to_a + + cards(:logo, :radio).each do |card| + assert_includes actual, card + end + end end diff --git a/test/models/card_test.rb b/test/models/card_test.rb index 876863dd8..4858e7203 100644 --- a/test/models/card_test.rb +++ b/test/models/card_test.rb @@ -80,11 +80,12 @@ class CardTest < ActiveSupport::TestCase end test "open" do - assert_equal cards(:logo, :layout, :text, :buy_domain).to_set, Card.open.to_set + assert_equal cards(:logo, :layout, :text, :buy_domain).to_set, accounts("37s").cards.open.to_set + assert_equal cards(:radio, :paycheck).to_set, accounts("initech").cards.open.to_set end test "card_unassigned" do - assert_equal cards(:shipping, :text, :buy_domain).to_set, Card.unassigned.to_set + assert_equal cards(:shipping, :text, :buy_domain).to_set, accounts("37s").cards.unassigned.to_set end test "assigned to" do