From afcc65d6fbc80701130231d6d508acdf37456547 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 4 Apr 2025 12:10:28 +0200 Subject: [PATCH] Create default pop reasons when creating accounts --- app/models/account.rb | 8 +------- app/models/account/pop_reasons.rb | 27 +++++++++++++++++++++++++ script/fill_account_pop_reasons.rb | 9 +++++++++ test/models/account/pop_reasons_test.rb | 8 ++++++++ 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 app/models/account/pop_reasons.rb create mode 100755 script/fill_account_pop_reasons.rb create mode 100644 test/models/account/pop_reasons_test.rb diff --git a/app/models/account.rb b/app/models/account.rb index 146674e23..1a6e45fde 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,5 +1,5 @@ class Account < ApplicationRecord - include Joinable + include PopReasons, Joinable has_many :buckets, dependent: :destroy has_many :bubbles, through: :buckets @@ -13,12 +13,6 @@ class Account < ApplicationRecord has_many :workflows, dependent: :destroy has_many :stages, through: :workflows, class_name: "Workflow::Stage" - has_many :pop_reasons, dependent: :destroy, class_name: "Pop::Reason" do - def labels - pluck(:label).presence || [ Pop::Reason::FALLBACK_LABEL ] - end - end - has_many :tags, dependent: :destroy has_many_attached :uploads diff --git a/app/models/account/pop_reasons.rb b/app/models/account/pop_reasons.rb new file mode 100644 index 000000000..6c190a4f2 --- /dev/null +++ b/app/models/account/pop_reasons.rb @@ -0,0 +1,27 @@ +module Account::PopReasons + extend ActiveSupport::Concern + + DEFAULT_LABELS = [ + "Completed", + "Duplicate", + "Maybe later", + "Working as intended" + ] + + included do + has_many :pop_reasons, dependent: :destroy, class_name: "Pop::Reason" do + def labels + pluck(:label).presence || [ Pop::Reason::FALLBACK_LABEL ] + end + end + + after_create :create_default_pop_reasons + end + + private + def create_default_pop_reasons + DEFAULT_LABELS.each do |label| + pop_reasons.create! label: label + end + end +end diff --git a/script/fill_account_pop_reasons.rb b/script/fill_account_pop_reasons.rb new file mode 100755 index 000000000..6cb543be7 --- /dev/null +++ b/script/fill_account_pop_reasons.rb @@ -0,0 +1,9 @@ +#!/usr/bin/env ruby + +require_relative "../config/environment" + +ApplicationRecord.with_each_tenant do |tenant| + Account.find_each do |account| + account.send(:create_default_pop_reasons) + end +end diff --git a/test/models/account/pop_reasons_test.rb b/test/models/account/pop_reasons_test.rb new file mode 100644 index 000000000..1004eda69 --- /dev/null +++ b/test/models/account/pop_reasons_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class PopReasonsTest < ActiveSupport::TestCase + test "create defaults pop reasons on creation" do + account = Account.create! name: "Rails" + assert_equal Account::PopReasons::DEFAULT_LABELS, account.pop_reasons.pluck(:label) + end +end