Class: Keyword

Inherits:
ControlledVocabularyTerm show all
Defined in:
app/models/keyword.rb

Direct Known Subclasses

BiocurationGroup

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

#uri_relation_is_a_skos_relation

Methods included from SoftValidation

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

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/keyword.rb', line 36

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

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

  h
end

.used_recently(user_id, project_id, klass) ⇒ Scope

Returns the max 10 most recently used keywords

Returns:

  • (Scope)

    the max 10 most recently used keywords



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

def self.used_recently(user_id, project_id, klass)
  t = Tag.arel_table
  k = Keyword.arel_table 

  # i is a select manager
  i = t.project(t['keyword_id'], t['created_at']).from(t)
    .where(t['created_at'].gt( 1.weeks.ago ))
    .where(t['created_by_id'].eq(user_id))
    .where(t['project_id'].eq(project_id))
    .order(t['created_at'].desc)

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

  Keyword.used_on_klass(klass).joins(
    Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['keyword_id'].eq(k['id'])))
  ).pluck(:id).uniq
end

Instance Method Details

#tagged_object_class_namesObject



32
33
34
# File 'app/models/keyword.rb', line 32

def tagged_object_class_names
  tags.pluck(:tag_object_type)
end

#tagged_objectsObject



28
29
30
# File 'app/models/keyword.rb', line 28

def tagged_objects
  tags.collect{|t| t.tag_object}
end