Class: DataAssociation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/data_association.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.associations_for(parent_class:, parent_id:, key: nil) ⇒ ActiveRecord<DataAssociation>

Returns associations for the object identified by parent class and id. If key is non-nil, returns the current value for the key, otherwise returns all values ordered by key and last update.

Parameters:

  • parent_class (string)

    the name of the class of the owner object

  • parent_id (integer)

    the record id for the owner object

  • key (string) (defaults to: nil)

    the key value, may be nil

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/data_association.rb', line 64

def self.associations_for(parent_class:, parent_id:, key: nil)
  if key
    DataAssociation
      .includes(:upload)
      .where(parent_id: parent_id, parent_class: parent_class, key: key.to_s)
      .descending_by_recent_update
  else
    DataAssociation
      .includes(:upload)
      .where(parent_id: parent_id, parent_class: parent_class)
      .descending_by_recent_update
  end
end

.create_from(parent_id:, parent_class:, key:, value:, upload: nil) ⇒ Object

Creates new association for the object identified by parent class and id



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/data_association.rb', line 43

def self.create_from(parent_id:, parent_class:, key:, value:, upload: nil)
  upload_id = nil
  upload_id = upload.id if upload

  DataAssociation.new(
    parent_id: parent_id,
    parent_class: parent_class,
    key: key.to_s,
    object: { key => value }.to_json,
    upload_id: upload_id
  )
end

.descending_by_recent_updateObject

Scope method to order data associations by key and then duplicates by descending order on update and ID. So, the first key-value pair, is the most recent update.



81
82
83
# File 'app/models/data_association.rb', line 81

def self.descending_by_recent_update
  order(:key, updated_at: :desc, id: :desc)
end

.find_parent(parent_class, parent_id) ⇒ Object



27
28
29
# File 'app/models/data_association.rb', line 27

def self.find_parent(parent_class, parent_id)
  Object.const_get(parent_class).find(parent_id)
end

.select_most_recent(associations) ⇒ Object

Filters data associations to keep most recent update for a key. In case of ties, selects largest ID.

Parameters:

Returns:

  • associations filtered to include the most recently updated values



90
91
92
93
94
95
# File 'app/models/data_association.rb', line 90

def self.select_most_recent(associations)
  associations
    .descending_by_recent_update
    .group_by(&:key)
    .map { |_, group| group.first }
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



21
22
23
24
25
# File 'app/models/data_association.rb', line 21

def as_json(_options = {})
  result = super(include: :upload)
  result = result.merge url: upload.url if upload
  result
end

#full_objectObject



10
11
12
13
14
# File 'app/models/data_association.rb', line 10

def full_object
  HashWithIndifferentAccess.new(JSON.parse(object, symbolize_names: true))
rescue StandardError
  HashWithIndifferentAccess.new
end

#may_delete(user) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/models/data_association.rb', line 31

def may_delete(user)
  parent = DataAssociation.find_parent(parent_class, parent_id)
  puts "Class = #{parent_class}"
  return parent.sample && parent.sample.user_id = user.id if parent_class == 'Item'
  return user.admin? if parent_class == 'Collection' # since collections are managed by admins?
  return true if parent_class == 'Operation' # since operations don't yet have owners (actually they do now, so this should be fixed)
  return true if parent_class == 'Plan' # plans don't have owners yet either
  return true if parent_class == 'OperationType' # operation types don't have owners yet either
end

#valueObject



16
17
18
19
# File 'app/models/data_association.rb', line 16

def value
  h = full_object
  h[h.keys.first]
end