Class: Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping, Shared::AttributeAnnotations, Shared::IsData
Defined in:
app/models/tag.rb

Overview

A Tag links a ControlledVocabularyTerm::Keyword to a Data object.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Instance Attribute Details

- (Integer) keyword_id

Returns the controlled vocabulary term used in the tag

Returns:

  • (Integer)

    the controlled vocabulary term used in the tag



28
29
30
31
32
33
34
35
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
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
92
93
94
95
96
97
98
99
# File 'app/models/tag.rb', line 28

class Tag < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::AttributeAnnotations

  acts_as_list scope: [:tag_object_id, :tag_object_type]

  belongs_to :keyword, inverse_of: :tags, validate: true
  belongs_to :tag_object, polymorphic: true

  # Not all tagged subclasses are keyword based, use this object for display
  belongs_to :controlled_vocabulary_term, foreign_key: :keyword_id, inverse_of: :tags

  validates :keyword, presence: true
  validate :keyword_is_allowed_on_object
  validate :object_can_be_tagged_with_keyword

  validates_uniqueness_of :keyword_id, scope: [:tag_object_id, :tag_object_type]

  accepts_nested_attributes_for :keyword, reject_if: :reject_keyword, allow_destroy: true

  # The column name containing the attribute name being annotated
  def self.annotated_attribute_column
    :tag_object_attribute
  end

  def tag_object_class
    tag_object.class
  end

  def tag_object_global_entity
    self.tag_object.to_global_id if self.tag_object.present?
  end

  def tag_object_global_entity=(entity)
    self.tag_object = GlobalID::Locator.locate entity
  end

  def self.find_for_autocomplete(params)
    # TODO: @mjy below code is running but not giving results we want
    terms = params[:term].split.collect { |t| "'#{t}%'" }.join(' or ')
    joins(:keyword).where('controlled_vocabulary_terms.name like ?', terms).with_project_id(params[:project_id]) 
    terms
  end

  # @return [TagObject]
  #   alias to simplify reference across classes 
  def annotated_object
    tag_object
  end

  protected

  def keyword_is_allowed_on_object
    return true if keyword.nil? || tag_object.nil? || !keyword.respond_to?(:can_tag)
    if !keyword.can_tag.include?(tag_object.class.name)
      errors.add(:keyword, "this keyword class (#{tag_object.class}) can not be attached to a #{tag_object_type}")
    end
  end

  def object_can_be_tagged_with_keyword
    return true if keyword.nil? || tag_object.nil? || !tag_object.respond_to?(:taggable_with)
    if !tag_object.taggable_with.include?(keyword.class.name)
      errors.add(:tag_object, "this tag_object_type (#{tag_object.class}) can not be tagged with this keyword class (#{keyword.class})")
    end
  end

  def reject_keyword(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

end

- (Integer) position

Returns a user definable sort code on the tags on an object, handled by acts_as_list

Returns:

  • (Integer)

    a user definable sort code on the tags on an object, handled by acts_as_list



28
29
30
31
32
33
34
35
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
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
92
93
94
95
96
97
98
99
# File 'app/models/tag.rb', line 28

class Tag < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::AttributeAnnotations

  acts_as_list scope: [:tag_object_id, :tag_object_type]

  belongs_to :keyword, inverse_of: :tags, validate: true
  belongs_to :tag_object, polymorphic: true

  # Not all tagged subclasses are keyword based, use this object for display
  belongs_to :controlled_vocabulary_term, foreign_key: :keyword_id, inverse_of: :tags

  validates :keyword, presence: true
  validate :keyword_is_allowed_on_object
  validate :object_can_be_tagged_with_keyword

  validates_uniqueness_of :keyword_id, scope: [:tag_object_id, :tag_object_type]

  accepts_nested_attributes_for :keyword, reject_if: :reject_keyword, allow_destroy: true

  # The column name containing the attribute name being annotated
  def self.annotated_attribute_column
    :tag_object_attribute
  end

  def tag_object_class
    tag_object.class
  end

  def tag_object_global_entity
    self.tag_object.to_global_id if self.tag_object.present?
  end

  def tag_object_global_entity=(entity)
    self.tag_object = GlobalID::Locator.locate entity
  end

  def self.find_for_autocomplete(params)
    # TODO: @mjy below code is running but not giving results we want
    terms = params[:term].split.collect { |t| "'#{t}%'" }.join(' or ')
    joins(:keyword).where('controlled_vocabulary_terms.name like ?', terms).with_project_id(params[:project_id]) 
    terms
  end

  # @return [TagObject]
  #   alias to simplify reference across classes 
  def annotated_object
    tag_object
  end

  protected

  def keyword_is_allowed_on_object
    return true if keyword.nil? || tag_object.nil? || !keyword.respond_to?(:can_tag)
    if !keyword.can_tag.include?(tag_object.class.name)
      errors.add(:keyword, "this keyword class (#{tag_object.class}) can not be attached to a #{tag_object_type}")
    end
  end

  def object_can_be_tagged_with_keyword
    return true if keyword.nil? || tag_object.nil? || !tag_object.respond_to?(:taggable_with)
    if !tag_object.taggable_with.include?(keyword.class.name)
      errors.add(:tag_object, "this tag_object_type (#{tag_object.class}) can not be tagged with this keyword class (#{keyword.class})")
    end
  end

  def reject_keyword(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

end

- (Integer) project_id

the project ID

Returns:

  • (Integer)


28
29
30
31
32
33
34
35
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
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
92
93
94
95
96
97
98
99
# File 'app/models/tag.rb', line 28

class Tag < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::AttributeAnnotations

  acts_as_list scope: [:tag_object_id, :tag_object_type]

  belongs_to :keyword, inverse_of: :tags, validate: true
  belongs_to :tag_object, polymorphic: true

  # Not all tagged subclasses are keyword based, use this object for display
  belongs_to :controlled_vocabulary_term, foreign_key: :keyword_id, inverse_of: :tags

  validates :keyword, presence: true
  validate :keyword_is_allowed_on_object
  validate :object_can_be_tagged_with_keyword

  validates_uniqueness_of :keyword_id, scope: [:tag_object_id, :tag_object_type]

  accepts_nested_attributes_for :keyword, reject_if: :reject_keyword, allow_destroy: true

  # The column name containing the attribute name being annotated
  def self.annotated_attribute_column
    :tag_object_attribute
  end

  def tag_object_class
    tag_object.class
  end

  def tag_object_global_entity
    self.tag_object.to_global_id if self.tag_object.present?
  end

  def tag_object_global_entity=(entity)
    self.tag_object = GlobalID::Locator.locate entity
  end

  def self.find_for_autocomplete(params)
    # TODO: @mjy below code is running but not giving results we want
    terms = params[:term].split.collect { |t| "'#{t}%'" }.join(' or ')
    joins(:keyword).where('controlled_vocabulary_terms.name like ?', terms).with_project_id(params[:project_id]) 
    terms
  end

  # @return [TagObject]
  #   alias to simplify reference across classes 
  def annotated_object
    tag_object
  end

  protected

  def keyword_is_allowed_on_object
    return true if keyword.nil? || tag_object.nil? || !keyword.respond_to?(:can_tag)
    if !keyword.can_tag.include?(tag_object.class.name)
      errors.add(:keyword, "this keyword class (#{tag_object.class}) can not be attached to a #{tag_object_type}")
    end
  end

  def object_can_be_tagged_with_keyword
    return true if keyword.nil? || tag_object.nil? || !tag_object.respond_to?(:taggable_with)
    if !tag_object.taggable_with.include?(keyword.class.name)
      errors.add(:tag_object, "this tag_object_type (#{tag_object.class}) can not be tagged with this keyword class (#{keyword.class})")
    end
  end

  def reject_keyword(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

end

- (String) tag_object_attribute

Returns the specific attribute (column) that this tag is in reference to. Optional. When not provided the tag pertains to the whole object.

Returns:

  • (String)

    the specific attribute (column) that this tag is in reference to. Optional. When not provided the tag pertains to the whole object.



28
29
30
31
32
33
34
35
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
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
92
93
94
95
96
97
98
99
# File 'app/models/tag.rb', line 28

class Tag < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::AttributeAnnotations

  acts_as_list scope: [:tag_object_id, :tag_object_type]

  belongs_to :keyword, inverse_of: :tags, validate: true
  belongs_to :tag_object, polymorphic: true

  # Not all tagged subclasses are keyword based, use this object for display
  belongs_to :controlled_vocabulary_term, foreign_key: :keyword_id, inverse_of: :tags

  validates :keyword, presence: true
  validate :keyword_is_allowed_on_object
  validate :object_can_be_tagged_with_keyword

  validates_uniqueness_of :keyword_id, scope: [:tag_object_id, :tag_object_type]

  accepts_nested_attributes_for :keyword, reject_if: :reject_keyword, allow_destroy: true

  # The column name containing the attribute name being annotated
  def self.annotated_attribute_column
    :tag_object_attribute
  end

  def tag_object_class
    tag_object.class
  end

  def tag_object_global_entity
    self.tag_object.to_global_id if self.tag_object.present?
  end

  def tag_object_global_entity=(entity)
    self.tag_object = GlobalID::Locator.locate entity
  end

  def self.find_for_autocomplete(params)
    # TODO: @mjy below code is running but not giving results we want
    terms = params[:term].split.collect { |t| "'#{t}%'" }.join(' or ')
    joins(:keyword).where('controlled_vocabulary_terms.name like ?', terms).with_project_id(params[:project_id]) 
    terms
  end

  # @return [TagObject]
  #   alias to simplify reference across classes 
  def annotated_object
    tag_object
  end

  protected

  def keyword_is_allowed_on_object
    return true if keyword.nil? || tag_object.nil? || !keyword.respond_to?(:can_tag)
    if !keyword.can_tag.include?(tag_object.class.name)
      errors.add(:keyword, "this keyword class (#{tag_object.class}) can not be attached to a #{tag_object_type}")
    end
  end

  def object_can_be_tagged_with_keyword
    return true if keyword.nil? || tag_object.nil? || !tag_object.respond_to?(:taggable_with)
    if !tag_object.taggable_with.include?(keyword.class.name)
      errors.add(:tag_object, "this tag_object_type (#{tag_object.class}) can not be tagged with this keyword class (#{keyword.class})")
    end
  end

  def reject_keyword(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

end

- (Integer) tag_object_id

Returns Rails polymorphic. The id of of the object being tagged.

Returns:

  • (Integer)

    Rails polymorphic. The id of of the object being tagged.



28
29
30
31
32
33
34
35
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
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
92
93
94
95
96
97
98
99
# File 'app/models/tag.rb', line 28

class Tag < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::AttributeAnnotations

  acts_as_list scope: [:tag_object_id, :tag_object_type]

  belongs_to :keyword, inverse_of: :tags, validate: true
  belongs_to :tag_object, polymorphic: true

  # Not all tagged subclasses are keyword based, use this object for display
  belongs_to :controlled_vocabulary_term, foreign_key: :keyword_id, inverse_of: :tags

  validates :keyword, presence: true
  validate :keyword_is_allowed_on_object
  validate :object_can_be_tagged_with_keyword

  validates_uniqueness_of :keyword_id, scope: [:tag_object_id, :tag_object_type]

  accepts_nested_attributes_for :keyword, reject_if: :reject_keyword, allow_destroy: true

  # The column name containing the attribute name being annotated
  def self.annotated_attribute_column
    :tag_object_attribute
  end

  def tag_object_class
    tag_object.class
  end

  def tag_object_global_entity
    self.tag_object.to_global_id if self.tag_object.present?
  end

  def tag_object_global_entity=(entity)
    self.tag_object = GlobalID::Locator.locate entity
  end

  def self.find_for_autocomplete(params)
    # TODO: @mjy below code is running but not giving results we want
    terms = params[:term].split.collect { |t| "'#{t}%'" }.join(' or ')
    joins(:keyword).where('controlled_vocabulary_terms.name like ?', terms).with_project_id(params[:project_id]) 
    terms
  end

  # @return [TagObject]
  #   alias to simplify reference across classes 
  def annotated_object
    tag_object
  end

  protected

  def keyword_is_allowed_on_object
    return true if keyword.nil? || tag_object.nil? || !keyword.respond_to?(:can_tag)
    if !keyword.can_tag.include?(tag_object.class.name)
      errors.add(:keyword, "this keyword class (#{tag_object.class}) can not be attached to a #{tag_object_type}")
    end
  end

  def object_can_be_tagged_with_keyword
    return true if keyword.nil? || tag_object.nil? || !tag_object.respond_to?(:taggable_with)
    if !tag_object.taggable_with.include?(keyword.class.name)
      errors.add(:tag_object, "this tag_object_type (#{tag_object.class}) can not be tagged with this keyword class (#{keyword.class})")
    end
  end

  def reject_keyword(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

end

- (String) tag_object_type

Returns Rails polymorphic. The type of the object being tagged.

Returns:

  • (String)

    Rails polymorphic. The type of the object being tagged.



28
29
30
31
32
33
34
35
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
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
92
93
94
95
96
97
98
99
# File 'app/models/tag.rb', line 28

class Tag < ActiveRecord::Base
  include Housekeeping
  include Shared::IsData
  include Shared::AttributeAnnotations

  acts_as_list scope: [:tag_object_id, :tag_object_type]

  belongs_to :keyword, inverse_of: :tags, validate: true
  belongs_to :tag_object, polymorphic: true

  # Not all tagged subclasses are keyword based, use this object for display
  belongs_to :controlled_vocabulary_term, foreign_key: :keyword_id, inverse_of: :tags

  validates :keyword, presence: true
  validate :keyword_is_allowed_on_object
  validate :object_can_be_tagged_with_keyword

  validates_uniqueness_of :keyword_id, scope: [:tag_object_id, :tag_object_type]

  accepts_nested_attributes_for :keyword, reject_if: :reject_keyword, allow_destroy: true

  # The column name containing the attribute name being annotated
  def self.annotated_attribute_column
    :tag_object_attribute
  end

  def tag_object_class
    tag_object.class
  end

  def tag_object_global_entity
    self.tag_object.to_global_id if self.tag_object.present?
  end

  def tag_object_global_entity=(entity)
    self.tag_object = GlobalID::Locator.locate entity
  end

  def self.find_for_autocomplete(params)
    # TODO: @mjy below code is running but not giving results we want
    terms = params[:term].split.collect { |t| "'#{t}%'" }.join(' or ')
    joins(:keyword).where('controlled_vocabulary_terms.name like ?', terms).with_project_id(params[:project_id]) 
    terms
  end

  # @return [TagObject]
  #   alias to simplify reference across classes 
  def annotated_object
    tag_object
  end

  protected

  def keyword_is_allowed_on_object
    return true if keyword.nil? || tag_object.nil? || !keyword.respond_to?(:can_tag)
    if !keyword.can_tag.include?(tag_object.class.name)
      errors.add(:keyword, "this keyword class (#{tag_object.class}) can not be attached to a #{tag_object_type}")
    end
  end

  def object_can_be_tagged_with_keyword
    return true if keyword.nil? || tag_object.nil? || !tag_object.respond_to?(:taggable_with)
    if !tag_object.taggable_with.include?(keyword.class.name)
      errors.add(:tag_object, "this tag_object_type (#{tag_object.class}) can not be tagged with this keyword class (#{keyword.class})")
    end
  end

  def reject_keyword(attributed)
    attributed['name'].blank? || attributed['definition'].blank?
  end

end

Class Method Details

+ (Object) annotated_attribute_column

The column name containing the attribute name being annotated



50
51
52
# File 'app/models/tag.rb', line 50

def self.annotated_attribute_column
  :tag_object_attribute
end

+ (Object) find_for_autocomplete(params)



66
67
68
69
70
71
# File 'app/models/tag.rb', line 66

def self.find_for_autocomplete(params)
  # TODO: @mjy below code is running but not giving results we want
  terms = params[:term].split.collect { |t| "'#{t}%'" }.join(' or ')
  joins(:keyword).where('controlled_vocabulary_terms.name like ?', terms).with_project_id(params[:project_id]) 
  terms
end

Instance Method Details

- (TagObject) annotated_object

Returns alias to simplify reference across classes

Returns:

  • (TagObject)

    alias to simplify reference across classes



75
76
77
# File 'app/models/tag.rb', line 75

def annotated_object
  tag_object
end

- (Object) keyword_is_allowed_on_object (protected)



81
82
83
84
85
86
# File 'app/models/tag.rb', line 81

def keyword_is_allowed_on_object
  return true if keyword.nil? || tag_object.nil? || !keyword.respond_to?(:can_tag)
  if !keyword.can_tag.include?(tag_object.class.name)
    errors.add(:keyword, "this keyword class (#{tag_object.class}) can not be attached to a #{tag_object_type}")
  end
end

- (Object) object_can_be_tagged_with_keyword (protected)



88
89
90
91
92
93
# File 'app/models/tag.rb', line 88

def object_can_be_tagged_with_keyword
  return true if keyword.nil? || tag_object.nil? || !tag_object.respond_to?(:taggable_with)
  if !tag_object.taggable_with.include?(keyword.class.name)
    errors.add(:tag_object, "this tag_object_type (#{tag_object.class}) can not be tagged with this keyword class (#{keyword.class})")
  end
end

- (Object) reject_keyword(attributed) (protected)



95
96
97
# File 'app/models/tag.rb', line 95

def reject_keyword(attributed)
  attributed['name'].blank? || attributed['definition'].blank?
end

- (Object) tag_object_class



54
55
56
# File 'app/models/tag.rb', line 54

def tag_object_class
  tag_object.class
end

- (Object) tag_object_global_entity



58
59
60
# File 'app/models/tag.rb', line 58

def tag_object_global_entity
  self.tag_object.to_global_id if self.tag_object.present?
end

- (Object) tag_object_global_entity=(entity)



62
63
64
# File 'app/models/tag.rb', line 62

def tag_object_global_entity=(entity)
  self.tag_object = GlobalID::Locator.locate entity
end