Class: Depiction

Overview

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

Constant Summary collapse

GRAPH_ENTRY_POINTS =
[:asserted_distributions].freeze

Instance Attribute Summary collapse

Class Method 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

#captionString?

Returns Figure description, as in ‘Figure 1. <caption>’.

Returns:

  • (String, nil)

    Figure description, as in ‘Figure 1. <caption>’



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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_idInteger

Returns the id of the object being depicted.

Returns:

  • (Integer)

    the id of the object being depicted



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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)



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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

#figure_labelString?

Returns Figure label, as in ‘<figure_label>. Dorsal habitus.’.

Returns:

  • (String, nil)

    Figure label, as in ‘<figure_label>. Dorsal habitus.’



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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”



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/depiction.rb', line 53

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

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  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' }

  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
    co = 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

    fo = DwcOccurrence
      .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
      .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
      .distinct

    ::Queries.union(DwcOccurrence, [co, fo])
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Depiction.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Depiction')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Depiction.arel_table

    case used_on
    when 'AssertedDistribution'
      Depiction.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  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

Class Method Details

.select_optimized(user_id, project_id, klass) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/depiction.rb', line 166

def self.select_optimized(user_id, project_id, klass)
  r = used_recently(user_id, project_id, klass)
  h = {
    quick: [],
    pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
    recent: []
  }

  if r.empty?
    h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
  else
    h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
    h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                 Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
  end

  h
end

.used_recently(user_id, project_id, used_on) ⇒ Scope

Returns the max 10 most recently used.

Returns:

  • (Scope)

    the max 10 most recently used



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/depiction.rb', line 133

def self.used_recently(user_id, project_id, used_on)
  t = case used_on
      when 'AssertedDistribution'
        AssertedDistribution.arel_table
      else
        return Depiction.none
      end

  # i is a select manager
  i = case used_on
      when 'AssertedDistribution'
        t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
          .where(
            t['updated_at'].gt(1.week.ago).and(
              t['asserted_distribution_object_type'].eq('Depiction')
            )
          )
          .where(t['updated_by_id'].eq(user_id))
          .where(t['project_id'].eq(project_id))
          .order(t['updated_at'].desc)
      end

  z = i.as('recent_t')
  p = Depiction.arel_table

  case used_on
  when 'AssertedDistribution'
    Depiction.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
    ).pluck(:id).uniq
  end
end

Instance Method Details

#destroy_image_stub_collection_objectObject (private)



203
204
205
206
207
208
209
210
211
212
# File 'app/models/depiction.rb', line 203

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



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/depiction.rb', line 117

def dwc_occurrences
  co = 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

  fo = DwcOccurrence
    .joins("JOIN depictions d on d.depiction_object_id = dwc_occurrence_object_id AND d.depiction_object_type = 'FieldOccurrence'")
    .where(d: {id:}, dwc_occurrences: {dwc_occurrence_object_type: 'FieldOccurrence'})
    .distinct

  ::Queries.union(DwcOccurrence, [co, fo])
end

#from_sled?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/depiction.rb', line 93

def from_sled?
  !sled_image_id.nil?
end

#normalize_imageObject



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

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)



197
198
199
200
201
# File 'app/models/depiction.rb', line 197

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

#remove_media_observation2Object (private)

Deprecate for calls to unify() ?!



188
189
190
191
192
193
194
195
# File 'app/models/depiction.rb', line 188

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/depiction.rb', line 97

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