Class: Depiction

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::DataAttributes, Shared::DwcOccurrenceHooks, Shared::IsData, Shared::PolymorphicAnnotator, Shared::Tags
Defined in:
app/models/depiction.rb

Overview

A depiction is the linkage between an image and a data object. For example an image may depiction a ColletingEvent, CollectionObject, or OTU.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#depiction_object_idInteger

Returns the id of the object being depicted.

Returns:

  • (Integer)

    the id of the object being depicted



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#depiction_object_typeString

Returns the type of object being depicted, a TW class that can be depicted (e.g. CollectionObject, CollectingEvent).

Returns:

  • (String)

    the type of object being depicted, a TW class that can be depicted (e.g. CollectionObject, CollectingEvent)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#image_idInteger

Returns the id of the image that stores the depiction.

Returns:

  • (Integer)

    the id of the image that stores the depiction



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#is_metadata_depictionBoolean?

Returns If true then this depiction depicts data that describes the entity, rather than the entity itself. For example, a CollectionObject depiction of a insect, vs. a picture of some text that says “the specimen is blue”.

Returns:

  • (Boolean, nil)

    If true then this depiction depicts data that describes the entity, rather than the entity itself. For example, a CollectionObject depiction of a insect, vs. a picture of some text that says “the specimen is blue”



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#project_idInteger

Returns the project ID.

Returns:

  • (Integer)

    the project ID



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#sled_image_idInteger

Returns If present this depiction was derived from sled.

Returns:

  • (Integer)

    If present this depiction was derived from sled



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#sled_image_x_positionInteger

Returns Not null if sled_image_is present. The column (top left 0,0) derived from.

Returns:

  • (Integer)

    Not null if sled_image_is present. The column (top left 0,0) derived from



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#sled_image_y_positionInteger

Returns Not null if sled_image_is present. The row (top left 0,0) derived from.

Returns:

  • (Integer)

    Not null if sled_image_is present. The row (top left 0,0) derived from



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#svg_clipxml?

Returns a clipping mask to isolate some portion of the picture.

Returns:

  • (xml, nil)

    a clipping mask to isolate some portion of the picture



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

#svg_view_boxString?

Returns sets the clipping box, identical dimensions to clip for rectangles.

Returns:

  • (String, nil)

    sets the clipping box, identical dimensions to clip for rectangles



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/depiction.rb', line 45

class Depiction < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::DwcOccurrenceHooks
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:depiction_object)

  acts_as_list scope: [:project_id, :depiction_object_type, :depiction_object_id]

  belongs_to :image, inverse_of: :depictions
  belongs_to :sled_image, inverse_of: :depictions
  has_one :sqed_depiction, dependent: :destroy

  accepts_nested_attributes_for :image
  accepts_nested_attributes_for :sqed_depiction, allow_destroy: true

  validates_presence_of :depiction_object
  validates_uniqueness_of :sled_image_id, scope: [:project_id, :sled_image_x_position, :sled_image_y_position], allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}
  validates_uniqueness_of :image_id, scope: [:depiction_object_type, :depiction_object_id] #, allow_nil: true, if: Proc.new {|n| !n.sled_image_id.nil?}

  before_validation :normalize_image

  # Deprecated for unify() functionality
  after_update :remove_media_observation2, if: Proc.new {|d| d.depiction_object_type_previously_was == 'Observation' && d.depiction_object.respond_to?(:type_was) && d.depiction_object.type_was == 'Observation::Media' }
  after_destroy :remove_media_observation, if: Proc.new {|d| d.depiction_object_type == 'Observation' && d.depiction_object.type == 'Observation::Media' }

  # TODO: almost certainly deprecate
  after_update :destroy_image_stub_collection_object, if: Proc.new {|d| d.depiction_object_type_previously_was == 'CollectionObject' && d.depiction_object_type == 'CollectionObject' }

  # !? This is purposefully redundant with Shared::DwcOccurrencHooks without
  # constraints because that version doesn't catch `saved_changes?` in specs.
  # Maybe because specs pass objects. Maybe because other hooks?
  after_save_commit :update_dwc_occurrence, unless: :no_dwc_occurrence

  def normalize_image
    if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
      self.image = o
    end
  end

  def from_sled?
    !sled_image_id.nil?
  end

  def sled_extraction_path(size = :thumb)
    if from_sled?
      x, y, w, h = svg_view_box.split(' ')

      box_width, box_height = nil, nil
      case size
      when :thumb, :medium
        box_width = Image::DEFAULT_SIZES[size][:width]
        box_height = Image::DEFAULT_SIZES[size][:height]
      when :original
        box_width = w.to_i
        box_height = h.to_i
      end

      "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
    else
      raise 'This is not a sled derived depiction'
    end
  end

  def dwc_occurrences
    # From CollectionObjects
    DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
      .distinct
  end

  private

  #  Deprecate for calls to unify() ?!
  def remove_media_observation2
    if v = depiction_object_id_previously_was
      o = Observation::Media.find(v)
      if o.depictions.size == 0
        o.destroy!
      end
    end
  end

  def remove_media_observation
    if depiction_object.depictions.size == 0
      depiction_object.destroy!
    end
  end

  def destroy_image_stub_collection_object
    if sqed_depiction.present?
      if v = depiction_object_id_previously_was
        o = CollectionObject.find(v)
        if o.is_image_stub?
          o.destroy!
        end
      end
    end
  end

end

Instance Method Details

#destroy_image_stub_collection_objectObject (private)



137
138
139
140
141
142
143
144
145
146
# File 'app/models/depiction.rb', line 137

def destroy_image_stub_collection_object
  if sqed_depiction.present?
    if v = depiction_object_id_previously_was
      o = CollectionObject.find(v)
      if o.is_image_stub?
        o.destroy!
      end
    end
  end
end

#dwc_occurrencesObject



111
112
113
114
115
116
117
# File 'app/models/depiction.rb', line 111

def dwc_occurrences
  # From CollectionObjects
  DwcOccurrence
    .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'CollectionObject'")
    .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'CollectionObject'})
    .distinct
end

#from_sled?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/depiction.rb', line 87

def from_sled?
  !sled_image_id.nil?
end

#normalize_imageObject



81
82
83
84
85
# File 'app/models/depiction.rb', line 81

def normalize_image
  if o = Image.where(project_id: Current.project_id, image_file_fingerprint: image.image_file_fingerprint).first
    self.image = o
  end
end

#remove_media_observationObject (private)



131
132
133
134
135
# File 'app/models/depiction.rb', line 131

def remove_media_observation
  if depiction_object.depictions.size == 0
    depiction_object.destroy!
  end
end

#remove_media_observation2Object (private)

Deprecate for calls to unify() ?!



122
123
124
125
126
127
128
129
# File 'app/models/depiction.rb', line 122

def remove_media_observation2
  if v = depiction_object_id_previously_was
    o = Observation::Media.find(v)
    if o.depictions.size == 0
      o.destroy!
    end
  end
end

#sled_extraction_path(size = :thumb) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/depiction.rb', line 91

def sled_extraction_path(size = :thumb)
  if from_sled?
    x, y, w, h = svg_view_box.split(' ')

    box_width, box_height = nil, nil
    case size
    when :thumb, :medium
      box_width = Image::DEFAULT_SIZES[size][:width]
      box_height = Image::DEFAULT_SIZES[size][:height]
    when :original
      box_width = w.to_i
      box_height = h.to_i
    end

    "#{image_id}/scale_to_box/#{x.to_i}/#{y.to_i}/#{w.to_i}/#{h.to_i}/#{box_width}/#{box_height}"
  else
    raise 'This is not a sled derived depiction'
  end
end