about summary refs log tree commit diff
path: root/app/models/account_metadata.rb
blob: 1085d7f145836dc532629f6525cb041835bdd1e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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