Class: Labimotion::Element

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Collectable, ElementUIStateScopes, GenericRevisions, Segmentable, Workflow, PgSearch, 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 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.



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

def can_copy
  @can_copy
end

Class Method Details

.get_associated_elements(element_ids) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/labimotion/models/element.rb', line 86

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



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

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

Instance Method Details

#analysesObject



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

def analyses
  container ? container.analyses : []
end

#attachmentsObject



59
60
61
# File 'lib/labimotion/models/element.rb', line 59

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

#auto_set_short_labelObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/labimotion/models/element.rb', line 71

def auto_set_short_label
  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



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

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

#thumb_svgObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/labimotion/models/element.rb', line 101

def thumb_svg
  if Labimotion::IS_RAILS5 == true
    image_atts = attachments.select do |a_img|
      a_img&.content_type&.match(Regexp.union(%w[jpg jpeg png tiff tif]))
    end

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

#update_counterObject



82
83
84
# File 'lib/labimotion/models/element.rb', line 82

def update_counter
  creator.increment_counter element_klass.name
end