Class: Labimotion::Element

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Collectable, ElementUIStateScopes, GenericRevisions, LinkedProperties, Segmentable, Workflow, PgSearch::Model, Taggable
Defined in:
lib/labimotion/models/element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LinkedProperties

#detach_properties

Methods included from GenericRevisions

#create_vault, #delete_attachments, #save_to_vault

Methods included from Segmentable

#copy_segments, #save_segments

Methods included from Workflow

#split_workflow

Instance Attribute Details

#can_copyObject

Returns the value of attribute can_copy.



24
25
26
# File 'lib/labimotion/models/element.rb', line 24

def can_copy
  @can_copy
end

Class Method Details

.get_associated_elements(element_ids) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/labimotion/models/element.rb', line 93

def self.get_associated_elements(element_ids)
  pids = Labimotion::Element.where(id: element_ids).pluck :id
  get_ids = proc do |eids|
    eids.each do |p|
      cs = Labimotion::Element.find_by(id: p)&.elements.where.not(id: pids).pluck :id
      next if cs.empty?

      pids = (pids << cs).flatten.uniq
      get_ids.call(cs)
    end
  end
  get_ids.call(pids)
  pids
end

.get_associated_samples(element_ids) ⇒ Object



66
67
68
# File 'lib/labimotion/models/element.rb', line 66

def self.get_associated_samples(element_ids)
  Labimotion::ElementsSample.where(element_id: element_ids).pluck(:sample_id)
end

Instance Method Details

#analysesObject



70
71
72
# File 'lib/labimotion/models/element.rb', line 70

def analyses
  container ? container.analyses : []
end

#attachmentsObject



62
63
64
# File 'lib/labimotion/models/element.rb', line 62

def attachments
  Attachment.where(attachable_id: self.id, attachable_type: self.class.name)
end

#auto_set_short_labelObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/labimotion/models/element.rb', line 74

def auto_set_short_label
  return if short_label && !short_label_changed?

  return if parent && (self.short_label = "#{parent.short_label}-#{parent.children.with_deleted.count.to_i.succ}")

  prefix = element_klass.klass_prefix
  if creator.counters[element_klass.name].nil?
    creator.counters[element_klass.name] = '0'
    creator.update_columns(counters: creator.counters)
    creator.reload
  end
  counter = creator.counters[element_klass.name].to_i.succ
  self.short_label = "#{creator.initials}-#{prefix}#{counter}"
end

#migrate_workflowObject



128
129
130
131
132
133
134
135
# File 'lib/labimotion/models/element.rb', line 128

def migrate_workflow
  return if properties.nil? || properties_release.nil?

  return if properties['flow'].nil? && properties_release['flow'].nil?

  update_column(:properties, split_workflow(properties)) if properties['flow']
  update_column(:properties_release, split_workflow(properties_release)) if properties_release['flow']
end

#split(user, col_ids) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/labimotion/models/element.rb', line 115

def split(user, col_ids)
  subelement = self.dup
  subelement.name = self.name if self.name.present?
  subelement.parent = self
  subelement.properties = detach_properties(properties)
  subelement.created_by = user.id
  collections = (Collection.where(id: col_ids) | Collection.where(user_id: user, label: 'All', is_locked: true))
  subelement.collections << collections
  subelement.container = Container.create_root_container
  subelement.save!
  subelement
end

#thumb_svgObject



108
109
110
111
112
113
# File 'lib/labimotion/models/element.rb', line 108

def thumb_svg
  image_atts = attachments.select(&:type_image?)
  attachment = image_atts[0] || attachments[0]
  preview = attachment&.read_thumbnail
  (preview && Base64.encode64(preview)) || 'not available'
end

#update_counterObject



89
90
91
# File 'lib/labimotion/models/element.rb', line 89

def update_counter
  creator.increment_counter element_klass.name
end