Class: Topic

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

Constant Summary

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?

Methods included from ActiverecordUtilities

#trim_attributes

Class Method Details

+ (Object) find_for_autocomplete(params)

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



18
19
20
21
22
# File 'app/models/topic.rb', line 18

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

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

if `Content` then not used

Returns:

  • (Hash)

    topics optimized for user selection



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/topic.rb', line 56

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)
                     .used_on_klass(klass).used_recently('Citation').limit(10).distinct.to_a
    when 'Content'
      h[:recent] = Topic.joins(:contents)
                     .where(project_id: project_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

+ (Scope) used_recently(used_on = 'Citation')

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



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

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