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

Instance Attribute Summary

Attributes inherited from ControlledVocabularyTerm

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

Class Method Summary collapse

Methods inherited from ControlledVocabularyTerm

#uri_relation_is_a_skos_relation

Methods included from Housekeeping

#has_polymorphic_relationship?

Class Method Details

.find_for_autocomplete(params) ⇒ Object

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



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

def self.find_for_autocomplete(params)
  term = "#{params[:term]}%"
  where_string = "name LIKE '#{term}' OR name ILIKE '%#{term}' OR name = '#{term}' OR definition ILIKE '%#{term}'"
  ControlledVocabularyTerm.where(where_string).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



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
# File 'app/models/topic.rb', line 64

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

  case target
  when 'Citation'
    h[:recent] = (
      Topic.where(project_id: project_id, citations: {updated_by_id: user_id})
      .used_on_klass(klass)
      .used_recently('Citation')
      .limit(5).distinct.to_a +
    Topic.where(
      project_id: project_id,
      created_by_id: user_id,
      updated_at: (3.hours.ago..Time.now)
      ).limit(5).to_a
      ).uniq
  when 'Content'
    h[:recent] = Topic.joins(:contents)
      .where(project_id: project_id, contents: {updated_by_id: user_id}).used_recently('Content').limit(10).distinct.to_a
  end

  h[:quick] = (Topic.pinned_by(user_id)
    .pinboard_inserted.where(project_id: project_id).to_a + h[:recent][0..3]).uniq
  h
end

.used_recently(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
# File 'app/models/topic.rb', line 36

def self.used_recently(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['created_at']).from(t)
    .where(t['created_at'].gt(1.weeks.ago))
    .order(t['created_at'])

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

  Topic.joins(
    Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['topic_id'].eq(p['id'])))
  ).distinct.limit(10)
end