about summary refs log tree commit diff
path: root/app/models/account.rb
diff options
context:
space:
mode:
authorDavid Yip <yipdw@member.fsf.org>2018-04-14 09:12:50 -0500
committerDavid Yip <yipdw@member.fsf.org>2018-04-14 09:12:50 -0500
commit813da6788e5129579db04e89aac3076902af2c7a (patch)
tree2c7c8435e4eae5baa4b6169973c8da4644d44ded /app/models/account.rb
parente3d0d72b299103d30fcda2b7175906c73c1571bc (diff)
parent78ed4ab75ff77d7cba60d478aa1f45d1c104785d (diff)
Merge remote-tracking branch 'origin/master' into gs-master
  Conflicts:
 	db/schema.rb
Diffstat (limited to 'app/models/account.rb')
-rw-r--r--app/models/account.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index dd8bad585..49ee50dfb 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -44,6 +44,7 @@
 #  memorial                :boolean          default(FALSE), not null
 #  moved_to_account_id     :integer
 #  featured_collection_url :string
+#  fields                  :jsonb
 #
 
 class Account < ApplicationRecord
@@ -192,6 +193,30 @@ class Account < ApplicationRecord
     @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
   end
 
+  def fields
+    (self[:fields] || []).map { |f| Field.new(self, f) }
+  end
+
+  def fields_attributes=(attributes)
+    fields = []
+
+    attributes.each_value do |attr|
+      next if attr[:name].blank?
+      fields << attr
+    end
+
+    self[:fields] = fields
+  end
+
+  def build_fields
+    return if fields.size >= 4
+
+    raw_fields = self[:fields] || []
+    add_fields = 4 - raw_fields.size
+    add_fields.times { raw_fields << { name: '', value: '' } }
+    self.fields = raw_fields
+  end
+
   def magic_key
     modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
       result = []
@@ -241,6 +266,17 @@ class Account < ApplicationRecord
     shared_inbox_url.presence || inbox_url
   end
 
+  class Field < ActiveModelSerializers::Model
+    attributes :name, :value, :account, :errors
+
+    def initialize(account, attr)
+      @account = account
+      @name    = attr['name']
+      @value   = attr['value']
+      @errors  = {}
+    end
+  end
+
   class << self
     def readonly_attributes
       super - %w(statuses_count following_count followers_count)