Module: AgentManager::Mixin

Included in:
AgentCorporateEntity, AgentFamily, AgentPerson, AgentSoftware
Defined in:
backend/app/model/mixins/agent_manager.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'backend/app/model/mixins/agent_manager.rb', line 60

def self.included(base)
  base.extend(ClassMethods)
  base.set_model_scope :global

  base.include(Relationships)
  base.include(RelatedAgents)
  base.include(Events)
  base.include(MetadataRights)

  ArchivesSpaceService.loaded_hook do
    base.define_relationship(:name => :linked_agents,
                             :contains_references_to_types => proc {
                               base.relationship_dependencies[:linked_agents]
                             })
  end
end

Instance Method Details

#before_saveObject

Generally we won’t hit this because records are created using create_from_json. But just to keep the tests happy.



106
107
108
109
110
111
112
# File 'backend/app/model/mixins/agent_manager.rb', line 106

def before_save
  if self.agent_sha1.nil?
    self.agent_sha1 = SecureRandom.hex
  end

  super
end

#linked_agent_rolesObject



125
126
127
128
129
130
131
132
133
# File 'backend/app/model/mixins/agent_manager.rb', line 125

def linked_agent_roles
  role_ids = self.class.find_relationship(:linked_agents).values_for_property(self, :role_id).uniq.compact

  # Hackish: we only want to return roles corresponding to linked archival
  # records (not events), so we filter it at this level.
  valid_enum = BackendEnumSource.values_for("linked_agent_role")

  BackendEnumSource.values_for_ids(role_ids).values.reject {|v| !valid_enum.include?(v) }
end

#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'backend/app/model/mixins/agent_manager.rb', line 78

def update_from_json(json, opts = {}, apply_nested_records = true)
  klass = self.class
  klass.ensure_authorized_name(json)
  klass.ensure_display_name(json)
  klass.combine_unauthorized_names(json)
  klass.set_publish_for_subrecords_with_subjects(json)

  # Force validation to make sure we're left with a valid record after our
  # changes
  json.to_hash

  # Called for the sake of updating the JSON blob sent to the realtime indexer
  self.class.populate_display_name(json)

  # ANW-951 Any agent person created from a user record can be safely
  # deemed unique, so a sha calculation can be skipped.
  opts[:skip_sha] = klass == AgentPerson && !klass.to_jsonmodel(self[:id])['is_user'].nil?

  if opts[:skip_sha]
    super(json, opts)
  else
    super(json, opts.merge(:agent_sha1 => self.class.calculate_hash(json)))
  end
end

#validateObject



115
116
117
118
119
120
121
122
# File 'backend/app/model/mixins/agent_manager.rb', line 115

def validate
  super
  validates_unique([:agent_sha1], :message => AGENT_MUST_BE_UNIQUE)
  map_validation_to_json_property([:agent_sha1], :names)
  map_validation_to_json_property([:agent_sha1], :dates_of_existence)
  map_validation_to_json_property([:agent_sha1], :external_documents)
  map_validation_to_json_property([:agent_sha1], :notes)
end