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