From e90b0624409a292509b38ccfee0dd22d439d7020 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 25 Apr 2025 12:37:17 +0200 Subject: [PATCH] Configure the autoclose period at the collection level --- app/controllers/collections_controller.rb | 6 +- app/helpers/collections_helper.rb | 12 +++ app/models/card/closeable.rb | 18 ++--- app/models/collection.rb | 2 +- app/models/collection/auto_closing.rb | 19 +++++ app/views/cards/container/_closure.html.erb | 2 +- app/views/collections/edit.html.erb | 78 +------------------ .../collections/edit/_auto_close.html.erb | 6 ++ app/views/collections/edit/_name.html.erb | 7 ++ app/views/collections/edit/_users.html.erb | 42 ++++++++++ .../collections/edit/_workflows.html.erb | 21 +++++ ...27_add_auto_close_period_to_collections.rb | 11 +++ db/schema.rb | 5 +- db/schema_cache.yml | 46 ++++++----- .../collections_controller_test.rb | 4 +- test/fixtures/collections.yml | 1 + test/models/card/closeable_test.rb | 26 ++++++- 17 files changed, 189 insertions(+), 117 deletions(-) create mode 100644 app/helpers/collections_helper.rb create mode 100644 app/models/collection/auto_closing.rb create mode 100644 app/views/collections/edit/_auto_close.html.erb create mode 100644 app/views/collections/edit/_name.html.erb create mode 100644 app/views/collections/edit/_users.html.erb create mode 100644 app/views/collections/edit/_workflows.html.erb create mode 100644 db/migrate/20250425092727_add_auto_close_period_to_collections.rb diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index 36698afdf..f5eb8c9fc 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -6,7 +6,7 @@ class CollectionsController < ApplicationController end def create - @collection = Collection.create! collection_params + @collection = Collection.create! collection_params.with_defaults(all_access: true) redirect_to cards_path(collection_ids: [ @collection ]) end @@ -19,7 +19,7 @@ class CollectionsController < ApplicationController @collection.update! collection_params @collection.accesses.revise granted: grantees, revoked: revokees - redirect_to cards_path(collection_ids: [ @collection ]) + redirect_to edit_collection_path(@collection), notice: "Collection updated" end def destroy @@ -33,7 +33,7 @@ class CollectionsController < ApplicationController end def collection_params - params.expect(collection: [ :name, :all_access ]).with_defaults(all_access: true) + params.expect(collection: [ :name, :all_access, :auto_close_period ]) end def grantees diff --git a/app/helpers/collections_helper.rb b/app/helpers/collections_helper.rb new file mode 100644 index 000000000..5f872079c --- /dev/null +++ b/app/helpers/collections_helper.rb @@ -0,0 +1,12 @@ +module CollectionsHelper + def collection_auto_close_options + [ + [ "30 days", 30.days ], + [ "60 days", 60.days ], + [ "90 days", 90.days ], + [ "6 months", 180.days ], + [ "1 year", 365.days ], + [ "Never", nil ] + ] + end +end diff --git a/app/models/card/closeable.rb b/app/models/card/closeable.rb index 1040c27dd..b599b6a03 100644 --- a/app/models/card/closeable.rb +++ b/app/models/card/closeable.rb @@ -1,17 +1,6 @@ module Card::Closeable extend ActiveSupport::Concern - AUTO_CLOSE_OPTIONS = [ - [ "30 days", 30.days ], - [ "60 days", 60.days ], - [ "90 days", 90.days ], - [ "6 months", 180.days ], - [ "1 year", 365.days ], - [ "Never", nil ] - ].freeze - - AUTO_CLOSE_AFTER = 30.days - included do has_one :closure, dependent: :destroy @@ -19,7 +8,10 @@ module Card::Closeable scope :open, -> { where.missing(:closure) } scope :recently_closed_first, -> { closed.order("closures.created_at": :desc) } - scope :due_to_be_closed, -> { considering.where(last_active_at: ..AUTO_CLOSE_AFTER.ago) } + scope :in_auto_closing_collection, -> { joins(:collection).merge(Collection.auto_closing) } + scope :due_to_be_closed, -> { considering.in_auto_closing_collection.where("last_active_at <= DATETIME('now', '-' || auto_close_period || ' seconds')") } + + delegate :auto_closing?, :auto_close_period, to: :collection end class_methods do @@ -31,7 +23,7 @@ module Card::Closeable end def auto_close_at - last_active_at + AUTO_CLOSE_AFTER if last_active_at + last_active_at + auto_close_period if auto_closing? && last_active_at end def closed? diff --git a/app/models/collection.rb b/app/models/collection.rb index 9cee579e6..29e07a98b 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -1,5 +1,5 @@ class Collection < ApplicationRecord - include Accessible, Broadcastable, Filterable, Workflowing + include AutoClosing, Accessible, Broadcastable, Filterable, Workflowing belongs_to :creator, class_name: "User", default: -> { Current.user } diff --git a/app/models/collection/auto_closing.rb b/app/models/collection/auto_closing.rb new file mode 100644 index 000000000..e3a9c8990 --- /dev/null +++ b/app/models/collection/auto_closing.rb @@ -0,0 +1,19 @@ +module Collection::AutoClosing + extend ActiveSupport::Concern + + included do + scope :auto_closing, -> { where.not(auto_close_period: nil) } + before_create :set_default_auto_close_period + end + + def auto_closing? + auto_close_period.present? + end + + private + DEFAULT_AUTO_CLOSE_PERIOD = 30.days + + def set_default_auto_close_period + self.auto_close_period ||= DEFAULT_AUTO_CLOSE_PERIOD unless attribute_present?(:auto_close_period) + end +end diff --git a/app/views/cards/container/_closure.html.erb b/app/views/cards/container/_closure.html.erb index e9006f3aa..f69cab694 100644 --- a/app/views/cards/container/_closure.html.erb +++ b/app/views/cards/container/_closure.html.erb @@ -24,7 +24,7 @@ <% if card.doing? %> Returns to Considering if no activity <%= local_datetime_tag(card.auto_reconsider_at, style: :indays) -%>. - <% else %> + <% elsif card.auto_closing? %> Auto-closes if no activity <%= local_datetime_tag(card.auto_close_at, style: :indays) -%>. <% end %> diff --git a/app/views/collections/edit.html.erb b/app/views/collections/edit.html.erb index 43b022e57..62d50298e 100644 --- a/app/views/collections/edit.html.erb +++ b/app/views/collections/edit.html.erb @@ -17,56 +17,9 @@ Name and Access

Choose who can access this Collection.

<%= form_with model: @collection, class: "flex flex-column gap txt-large", data: { controller: "form" } do |form| %> -
- -
-
- <%= access_menu_tag @collection do %> -
  • -
    - <%= icon_tag "everyone" %> - Everyone -
    - -
    -
    Everyone
    -
    - - - - -
  • - - - - <% if User.active.count > 20 %> -
    - -
    - <% end %> - -
    - <% 20.times do %> - <%= access_toggles_for @selected_users, selected: true %> - <% end %> - - <% if @selected_users.any? && @unselected_users.any? %> -
    - <% end %> - - <%= access_toggles_for @unselected_users, selected: false %> -
    - <% end %> -
    + <%= render "collections/edit/name", form: form %> + <%= render "collections/edit/users", collection: @collection, selected_users: @selected_users, unselected_users: @unselected_users, form: form %>