diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models/account.rb | 6 | ||||
-rw-r--r-- | app/models/account_metadata.rb | 30 | ||||
-rw-r--r-- | app/models/concerns/account_associations.rb | 3 |
3 files changed, 39 insertions, 0 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index 7d19aac08..dab091bae 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -562,6 +562,8 @@ class Account < ApplicationRecord before_validation :prepare_username, on: :create before_destroy :clean_feed_manager + after_create_commit :set_metadata, if: :local? + private def prepare_contents @@ -605,4 +607,8 @@ class Account < ApplicationRecord end end end + + def set_metadata + self.metadata = AccountMetadata.new(account_id: id, fields: {}) if metadata.nil? + end end diff --git a/app/models/account_metadata.rb b/app/models/account_metadata.rb new file mode 100644 index 000000000..1085d7f14 --- /dev/null +++ b/app/models/account_metadata.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: account_metadata +# +# id :bigint(8) not null, primary key +# account_id :bigint(8) not null +# fields :jsonb not null +# + +class AccountMetadata < ApplicationRecord + include Cacheable + + belongs_to :account, inverse_of: :metadata + cache_associated :account + + def fields + self[:fields].presence || {} + end + + class << self + def create_or_update(fields) + create(fields).presence || update(fields) + end + + def create_or_update!(fields) + create(fields).presence || update!(fields) + end + end +end diff --git a/app/models/concerns/account_associations.rb b/app/models/concerns/account_associations.rb index 10eaa4874..5a54f2208 100644 --- a/app/models/concerns/account_associations.rb +++ b/app/models/concerns/account_associations.rb @@ -66,5 +66,8 @@ module AccountAssociations # Domain permissions has_many :domain_permissions, class_name: 'AccountDomainPermission', inverse_of: :account, dependent: :destroy + + # Custom metadata + has_one :metadata, class_name: 'AccountMetadata', inverse_of: :account, dependent: :destroy end end |