Class: Predicate

Inherits:
ControlledVocabularyTerm show all
Includes:
Shared::DwcOccurrenceHooks
Defined in:
app/models/predicate.rb

Constant Summary

Constants inherited from ControlledVocabularyTerm

ControlledVocabularyTerm::ALTERNATE_VALUES_FOR

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary

Attributes inherited from ControlledVocabularyTerm

#definition, #name, #project_id, #type, #uri, #uri_relation

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ControlledVocabularyTerm

clone_from_project, #form_of_uri, #uri_relation_is_a_skos_relation

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

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::HasPapertrail

#attribute_updated, #attribute_updater

Methods included from Shared::AlternateValues

#all_values_for, #alternate_valued?

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Class Method Details

.select_optimized(user_id, project_id, klass) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/predicate.rb', line 30

def self.select_optimized(user_id, project_id, klass)
  r = used_recently(user_id, project_id, klass).first(10)

  h = {
    quick: [],
    pinboard: Predicate.pinned_by(user_id).where(project_id: project_id).to_a,
    recent: []
  }

  if r.empty?
    h[:quick] = Predicate.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
  else
    h[:recent] = Predicate.where('"controlled_vocabulary_terms"."id" IN (?)', r[0..9] ).order(:name).to_a
    h[:quick] = (
      Predicate.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
      Predicate.where('"controlled_vocabulary_terms"."id" IN (?)', r[0..3] ).order(:name).to_a).uniq
  end

  h
end

.used_recently(user_id, project_id, klass) ⇒ Scope

Returns the max 10 most recently used predicates.

Returns:

  • (Scope)

    the max 10 most recently used predicates



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/predicate.rb', line 10

def self.used_recently(user_id, project_id, klass)
  t = InternalAttribute.arel_table
  p = Predicate.arel_table

  # i is a select manager
  i = t.project(t['controlled_vocabulary_term_id'], t['updated_at']).from(t)
    .where(t['updated_at'].gt( 10.weeks.ago ))
    .where(t['updated_by_id'].eq(user_id))
    .where(t['attribute_subject_type'].eq(klass))
    .where(t['project_id'].eq(project_id))
    .order(t['updated_at'].desc)

  # z is a table alias
  z = i.as('recent_t')

  Predicate.joins(
    Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['controlled_vocabulary_term_id'].eq(p['id'])))
  ).pluck(:controlled_vocabulary_term_id).uniq
end

Instance Method Details

#dwc_occurrencesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/predicate.rb', line 51

def dwc_occurrences
  if ::DWC_ATTRIBUTE_URIS.values.flatten.include?(uri)

    a = Queries::DwcOccurrence::Filter.new(
      collection_object_query: {
        data_attribute_predicate_id: id
      }
    ).all

    b = Queries::DwcOccurrence::Filter.new(
      collecting_event_query: {
        data_attribute_predicate_id: id
      }
    ).all

    ::Queries.union(::DwcOccurrence, [a,b])
  else
    ::DwcOccurrence.none
  end
end