Module: Binda::FieldUniqueness

Extended by:
ActiveSupport::Concern
Included in:
Date, Gallery, Relation, Selection, Text
Defined in:
app/models/concerns/binda/field_uniqueness.rb

Overview

Some fields are meant to be unique, meaning there must be only one instance associated with a specific component and field setting

Instance Method Summary collapse

Instance Method Details

#field_uniquenessboolean

Make sure there is only one instance associated simultaneously with a specific component and field setting



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/concerns/binda/field_uniqueness.rb', line 13

def field_uniqueness
  instances = Text.where(
    field_setting_id: self.field_setting.id,
    fieldable_id: self.fieldable_id,
    fieldable_type: self.fieldable_type
  )
  if instances.any? && instances.first.id != self.id
    errors.add(:base, I18n.t("binda.duplicate_validation", { 
      arg1: self.field_setting.field_type, 
      arg2: self.fieldable_id,
      arg3: self.field_setting.slug 
    }))
    return false
  else
    return true
  end
end