Create default pop reasons when creating accounts
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
Executable
+9
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user