Module: Representative

Included in:
AgentContact, FileVersion, Instance, NameCorporateEntity, NameFamily, NamePerson, NameSoftware
Defined in:
backend/app/model/mixins/representative.rb

Instance Method Summary collapse

Instance Method Details

#before_validationObject

Handle the representative property for subrecords i.e. is_representative, is_display_name, authorized



5
6
7
8
9
10
11
# File 'backend/app/model/mixins/representative.rb', line 5

def before_validation
  super

  representative_for_types.keys.each do |property|
    self.send("#{property}=", nil) if self.send(property) != 1
  end
end

#representative_for_typesObject

Example: { is_representative: [:digital_object] }



19
20
21
# File 'backend/app/model/mixins/representative.rb', line 19

def representative_for_types
  raise 'Not implemented'
end

#representative_id_for_type(type) ⇒ Object

Append _id to ‘type’ i.e. :resource -> :resource_id



14
15
16
# File 'backend/app/model/mixins/representative.rb', line 14

def representative_id_for_type(type)
  type.to_s.concat('_id').to_sym
end

#validateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'backend/app/model/mixins/representative.rb', line 23

def validate
  # property is the representative field i.e. `:is_representative`, `:is_display_name`, `:authorized`
  # records are symbols referring to (parent) record types the property applies to (i.e. :resource)
  representative_for_types.each do |property, records|
    next unless self.send(property) # bail if this property is not truthy

    records.each do |type|
      id_field = representative_id_for_type(type)
      validates_unique(
        [property, id_field],
        :message => "A #{type} can have only one #{self.class.name} defined as: #{property}"
      )
      map_validation_to_json_property([property, id_field], property)
    end
  end

  super
end