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

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, target) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/keyword.rb', line 49

def self.select_optimized(user_id, project_id, klass, target)
  r = used_recently(user_id, project_id, klass, target)
  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).pinned_in_project(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).pinned_in_project(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 = 'Tag', target) ⇒ Scope

Returns the max 10 most recently used keywords, klass=‘Tag’ or klass=‘BiocurationGroup’.

Returns:

  • (Scope)

    the max 10 most recently used keywords, klass=‘Tag’ or klass=‘BiocurationGroup’



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/keyword.rb', line 17

def self.used_recently(user_id, project_id, klass='Tag', target)
  if klass == 'BiocurationGroup'
    t = BiocurationGroup.arel_table
  else
    t = Tag.arel_table
  end
  k = ControlledVocabularyTerm.arel_table

  # i is a select manager
  if target.blank?
    i = t.project(t['keyword_id'], t['updated_at']).from(t)
      .where(t['updated_at'].gt( 1.months.ago ))
      .where(t['updated_by_id'].eq(user_id))
      .where(t['project_id'].eq(project_id))
      .order(t['updated_at'].desc)
  else
    i = t.project(t['keyword_id'], t['updated_at']).from(t)
         .where(t['tag_object_type'].eq(target))
         .where(t['updated_at'].gt( 1.months.ago ))
         .where(t['updated_by_id'].eq(user_id))
         .where(t['project_id'].eq(project_id))
         .order(t['updated_at'].desc)
  end

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

  ControlledVocabularyTerm.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



11
12
13
# File 'app/models/keyword.rb', line 11

def tagged_object_class_names
  tags.pluck(:tag_object_type)
end

#tagged_objectsObject



7
8
9
# File 'app/models/keyword.rb', line 7

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