From db86d17880867cd4c5a9194d3f6dab617fd5c862 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Wed, 4 Jun 2025 14:38:22 +0200 Subject: [PATCH] Refactor to introduce an entropy model Instead of keeping two tracks of similar logic (auto-reconsider / auto-close). --- app/models/account.rb | 2 +- .../account/{entropy.rb => entropic.rb} | 2 +- app/models/card.rb | 2 +- app/models/card/entropic.rb | 35 ++++++++++ app/models/card/entropy.rb | 66 ++++--------------- app/models/collection.rb | 2 +- .../collection/{entropy.rb => entropic.rb} | 2 +- app/views/cards/container/_closure.html.erb | 10 +-- app/views/cards/display/_preview.html.erb | 2 +- .../cards/display/preview/_bubble.html.erb | 4 +- .../{entropy_test.rb => entropic_test.rb} | 20 +++--- 11 files changed, 73 insertions(+), 74 deletions(-) rename app/models/account/{entropy.rb => entropic.rb} (95%) create mode 100644 app/models/card/entropic.rb rename app/models/collection/{entropy.rb => entropic.rb} (95%) rename test/models/card/{entropy_test.rb => entropic_test.rb} (83%) diff --git a/app/models/account.rb b/app/models/account.rb index b2a4d71b5..afd022441 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,5 +1,5 @@ class Account < ApplicationRecord - include Entropy, Joinable + include Entropic, Joinable has_many_attached :uploads end diff --git a/app/models/account/entropy.rb b/app/models/account/entropic.rb similarity index 95% rename from app/models/account/entropy.rb rename to app/models/account/entropic.rb index 39c47f2b5..a60af2b4b 100644 --- a/app/models/account/entropy.rb +++ b/app/models/account/entropic.rb @@ -1,4 +1,4 @@ -module Account::Entropy +module Account::Entropic extend ActiveSupport::Concern included do diff --git a/app/models/card.rb b/app/models/card.rb index 656a51b12..0a109e5ba 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -1,5 +1,5 @@ class Card < ApplicationRecord - include Assignable, Colored, Engageable, Entropy, Eventable, + include Assignable, Colored, Engageable, Entropic, Eventable, Golden, Mentions, Pinnable, Closeable, Readable, Searchable, Staged, Statuses, Taggable, Watchable diff --git a/app/models/card/entropic.rb b/app/models/card/entropic.rb new file mode 100644 index 000000000..92725454b --- /dev/null +++ b/app/models/card/entropic.rb @@ -0,0 +1,35 @@ +module Card::Entropic + extend ActiveSupport::Concern + + included do + scope :entropic_by, ->(period_name) do + left_outer_joins(collection: :entropy_configuration) + .where("last_active_at <= DATETIME('now', '-' || COALESCE(entropy_configurations.#{period_name}, ?) || ' seconds')", + Entropy::Configuration.default.public_send(period_name)) + end + + scope :stagnated, -> { doing.entropic_by(:auto_reconsider_period) } + scope :due_to_be_closed, -> { considering.entropic_by(:auto_close_period) } + delegate :auto_close_period, :auto_reconsider_period, to: :collection + end + + class_methods do + def auto_close_all_due + due_to_be_closed.find_each do |card| + card.close(user: User.system, reason: "Closed") + end + end + + def auto_reconsider_all_stagnated + stagnated.find_each(&:reconsider) + end + end + + def entropy + Card::Entropy.for(self) + end + + def has_entropy? + entropy.present? + end +end diff --git a/app/models/card/entropy.rb b/app/models/card/entropy.rb index df7f7aa4c..c2cc45e10 100644 --- a/app/models/card/entropy.rb +++ b/app/models/card/entropy.rb @@ -1,62 +1,24 @@ -module Card::Entropy - extend ActiveSupport::Concern +class Card::Entropy + attr_reader :card, :auto_clean_period - AUTO_RECONSIDER_PERIOD = 30.days - ENTROPY_REMINDER_BEFORE = 7.days + class << self + def for(card) + return unless card.last_active_at - included do - scope :entropic_by, ->(period_name) do - left_outer_joins(collection: :entropy_configuration) - .where("last_active_at <= DATETIME('now', '-' || COALESCE(entropy_configurations.#{period_name}, ?) || ' seconds')", - Entropy::Configuration.default.public_send(period_name)) - end - - scope :stagnated, -> { doing.entropic_by(:auto_reconsider_period) } - scope :due_to_be_closed, -> { considering.entropic_by(:auto_close_period) } - delegate :auto_close_period, :auto_reconsider_period, to: :collection - end - - class_methods do - def auto_close_all_due - due_to_be_closed.find_each do |card| - card.close(user: User.system, reason: "Closed") + if card.considering? + new(card, card.auto_close_period) + elsif card.doing? + new(card, card.auto_reconsider_period) end end - - def auto_reconsider_all_stagnated - stagnated.find_each(&:reconsider) - end end - def subject_to_entropy? - auto_reconsidering? || auto_closing? + def initialize(card, auto_clean_period) + @card = card + @auto_clean_period = auto_clean_period end - def auto_reconsidering? - doing? && last_active_at - end - - def auto_closing? - considering? && last_active_at - end - - def auto_close_at - last_active_at + auto_close_period if auto_closing? - end - - def days_until_close - (auto_close_at.to_date - Date.current).to_i if auto_close_at - end - - def auto_reconsider_at - last_active_at + auto_reconsider_period if auto_reconsidering? - end - - def days_until_reconsider - (auto_reconsider_at.to_date - Date.current).to_i if auto_reconsider_at - end - - def entropy_cleaned_at - auto_close_at || auto_reconsider_at + def auto_clean_at + card.last_active_at + auto_clean_period end end diff --git a/app/models/collection.rb b/app/models/collection.rb index 0fc1e912e..105cd6eed 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -1,5 +1,5 @@ class Collection < ApplicationRecord - include AutoClosing, Accessible, Broadcastable, Entropy, Filterable, Workflowing + include AutoClosing, Accessible, Broadcastable, Entropic, Filterable, Workflowing belongs_to :creator, class_name: "User", default: -> { Current.user } diff --git a/app/models/collection/entropy.rb b/app/models/collection/entropic.rb similarity index 95% rename from app/models/collection/entropy.rb rename to app/models/collection/entropic.rb index b7b55133b..9a4a6b362 100644 --- a/app/models/collection/entropy.rb +++ b/app/models/collection/entropic.rb @@ -1,4 +1,4 @@ -module Collection::Entropy +module Collection::Entropic extend ActiveSupport::Concern included do diff --git a/app/views/cards/container/_closure.html.erb b/app/views/cards/container/_closure.html.erb index 24931b77c..c260fde03 100644 --- a/app/views/cards/container/_closure.html.erb +++ b/app/views/cards/container/_closure.html.erb @@ -22,10 +22,12 @@ - <% if card.doing? %> - Falls back to Considering if no activity <%= local_datetime_tag(card.auto_reconsider_at, style: :indays) -%>. - <% elsif card.auto_closing? %> - Auto-closes if no activity <%= local_datetime_tag(card.auto_close_at, style: :indays) -%>. + <% if card.has_entropy? %> + <% if card.doing? %> + Falls back to Considering if no activity <%= local_datetime_tag(card.entropy.auto_clean_at, style: :indays) -%>. + <% else %> + Auto-closes if no activity <%= local_datetime_tag(card.entropy.auto_clean_at, style: :indays) -%>. + <% end %> <% end %> <% end %> diff --git a/app/views/cards/display/_preview.html.erb b/app/views/cards/display/_preview.html.erb index 761df138d..be5de2077 100644 --- a/app/views/cards/display/_preview.html.erb +++ b/app/views/cards/display/_preview.html.erb @@ -29,7 +29,7 @@ <%= render "cards/display/common/background", card: card %> - <% if card.subject_to_entropy? %> + <% if card.has_entropy? %> <%= render "cards/display/preview/bubble", card: card %> <% end %> <% end %> diff --git a/app/views/cards/display/preview/_bubble.html.erb b/app/views/cards/display/preview/_bubble.html.erb index f0e3df352..676bef94c 100644 --- a/app/views/cards/display/preview/_bubble.html.erb +++ b/app/views/cards/display/preview/_bubble.html.erb @@ -1,7 +1,7 @@