Class: Topic

Inherits:
ControlledVocabularyTerm show all
Includes:
Shared::Tags
Defined in:
app/models/topic.rb

Overview

A Topic is a user defined subject. It is used in conjuction with a citation on an OTU. Topics assert that “this source says something about this taxon on this topic.”

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

Methods included from Shared::Tags

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

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

.find_for_autocomplete(params) ⇒ Object

TODO: Deprecate for CVT + params (if not already done)



26
27
28
29
30
31
# File 'app/models/topic.rb', line 26

def self.find_for_autocomplete(params)
  term = params[:term]
  ControlledVocabularyTerm
    .where('name ILIKE ? OR name = ? OR definition ILIKE ?', "%#{term}%", term, "%#{term}%")
    .where(project_id: params[:project_id], type: 'Topic')
end

.select_optimized(user_id, project_id, klass, target = 'Citation') ⇒ Hash

if ‘Content` then not used

Returns:

  • (Hash)

    topics optimized for user selection



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/topic.rb', line 72

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

  if r.empty?
    h[:quick] = Topic.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a
  else
    h[:recent] = Topic.where('"controlled_vocabulary_terms"."id" IN (?)', r.first(10) ).order(:name).to_a
    h[:quick] = (Topic.pinned_by(user_id).pinboard_inserted.where(project_id:).to_a +
        Topic.where('"controlled_vocabulary_terms"."id" IN (?)', r.first(5) ).order(:name).to_a +
        Topic.where(project_id:, created_by_id: user_id, updated_at: (3.hours.ago..Time.now)).limit(5).to_a).uniq
  end

  h
end

.used_recently(user_id, project_id, klass, used_on = 'Citation') ⇒ Scope

Returns the max 10 most recently used topics, as used on Content or Citation.

Parameters:

  • used_on (String) (defaults to: 'Citation')

    one of ‘Citation` (default) or `Content`

Returns:

  • (Scope)

    the max 10 most recently used topics, as used on Content or Citation



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/topic.rb', line 36

def self.used_recently(user_id, project_id, klass, used_on = 'Citation')
  t = case used_on
      when 'Citation'
        CitationTopic.arel_table
      when 'Content'
        ::Content.arel_table
      end

  p = Topic.arel_table

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

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

  if klass.blank?
    Topic.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['topic_id'].eq(p['id'])))
    ).pluck(:id).uniq
  else
    Topic.used_on_klass(klass).joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['topic_id'].eq(p['id'])))
    ).pluck(:id).uniq
  end
end