diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/account.rb | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index 440a731e3..979e0b12c 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -223,11 +223,19 @@ class Account < ApplicationRecord end def fields_attributes=(attributes) - fields = [] + fields = [] + old_fields = self[:fields] || [] if attributes.is_a?(Hash) attributes.each_value do |attr| next if attr[:name].blank? + + previous = old_fields.find { |item| item['value'] == attr[:value] } + + if previous && previous['verified_at'].present? + attr[:verified_at] = previous['verified_at'] + end + fields << attr end end @@ -235,13 +243,18 @@ class Account < ApplicationRecord self[:fields] = fields end + DEFAULT_FIELDS_SIZE = 4 + def build_fields - return if fields.size >= 4 + return if fields.size >= DEFAULT_FIELDS_SIZE + + tmp = self[:fields] || [] + + (DEFAULT_FIELDS_SIZE - tmp.size).times do + tmp << { name: '', value: '' } + end - raw_fields = self[:fields] || [] - add_fields = 4 - raw_fields.size - add_fields.times { raw_fields << { name: '', value: '' } } - self.fields = raw_fields + self.fields = tmp end def magic_key @@ -294,17 +307,32 @@ class Account < ApplicationRecord end class Field < ActiveModelSerializers::Model - attributes :name, :value, :account, :errors + attributes :name, :value, :verified_at, :account, :errors + + def initialize(account, attributes) + @account = account + @attributes = attributes + @name = attributes['name'].strip[0, 255] + @value = attributes['value'].strip[0, 255] + @verified_at = attributes['verified_at']&.to_datetime + @errors = {} + end + + def verified? + verified_at.present? + end + + def verifiable? + value.present? && /\A#{FetchLinkCardService::URL_PATTERN}\z/ =~ value + end - def initialize(account, attr) - @account = account - @name = attr['name'].strip[0, 255] - @value = attr['value'].strip[0, 255] - @errors = {} + def mark_verified! + @verified_at = Time.now.utc + @attributes['verified_at'] = @verified_at end def to_h - { name: @name, value: @value } + { name: @name, value: @value, verified_at: @verified_at } end end |