Create default pop reasons when creating accounts

This commit is contained in:
Jorge Manrubia
2025-04-04 12:10:28 +02:00
parent 77d0cd5e7f
commit afcc65d6fb
4 changed files with 45 additions and 7 deletions
+1 -7
View File
@@ -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
+27
View File
@@ -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
+9
View File
@@ -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
+8
View File
@@ -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