Class: Tag

Overview

A Tag links a ControlledVocabularyTerm::Keyword to a Data object. They are in essence a way to informally group data for future operation. See also DataAttribute Predicates.

Defined Under Namespace

Modules: MatrixHooks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MatrixHooks

#in_scope_observation_matrix_column_items, #in_scope_observation_matrix_row_items, #increment_matrix_counts, #out_of_scope_observation_matrix_column_items, #out_of_scope_observation_matrix_row_items, #purge_from_matrices

Methods included from Shared::MatrixHooks::Dynamic

#dynamic_add_to_matrix_column_items, #dynamic_add_to_matrix_row_items, #dynamic_cleanup_in_scope_column_items, #dynamic_cleanup_in_scope_row_items, #dynamic_cleanup_out_of_scope_column_items, #dynamic_cleanup_out_of_scope_row_items, #dynamic_column_items_in, #dynamic_column_items_out, #dynamic_inspect_matrices, #dynamic_remove_from_matrix_column_items, #dynamic_remove_from_matrix_row_items, #dynamic_row_items_in, #dynamic_row_items_out, #dynamic_syncronize_matrices, #dynamic_update_matrix_column_items?, #dynamic_update_matrix_row_items?, #in_scope_observation_matrix_column_items, #in_scope_observation_matrix_row_items, #out_of_scope_observation_matrix_column_items, #out_of_scope_observation_matrix_row_items, #prepare_matrix_items

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::AttributeAnnotations

#annotated_column, #annotated_value_is_not_identical_to_attribute, #annotation_value, #attribute_to_annotate_is_valid_for_object, #original_value

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#keyword_idInteger

Returns the keyword used in this tag.

Returns:

  • (Integer)

    the keyword used in this 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/tag.rb', line 28

class Tag < ApplicationRecord
  
  # Tags can not be cited.
  include Housekeeping
  include Shared::AttributeAnnotations
  include Shared::IsData

  include Shared::MatrixHooks::Dynamic # Must preceed Tag::MatrixHooks
  include Tag::MatrixHooks # Must come after Shared::MatrixHooks::Dynamic 

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:tag_object)
  acts_as_list scope: [:tag_object_id, :tag_object_type, :keyword_id, :project_id]

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

  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

  after_create :add_source_to_project, if: Proc.new { |tag| tag.tag_object.is_a?(Source) }

  def self.tag_objects(objects, keyword_id = nil)
    return nil if keyword_id.nil? or objects.empty?
    objects.each do |o|
      o.tags << Tag.new(keyword_id: keyword_id)
    end
  end

  def self.exists?(global_id, keyword_id, project_id)
    o = GlobalID::Locator.locate(global_id)
    return false unless o
    Tag.where(project_id: project_id, tag_object: o, keyword_id: keyword_id).first
  end

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

  def self.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: [])
    begin
      Tag.transaction do 
        object_ids.each do |id|
          Tag.find_or_create_by(keyword_id: keyword_id, tag_object_type: object_type, tag_object_id: id)
        end
      end
    rescue ActiveRecord::RecordInvalid
      return false 
    end
  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

  # @return [Boolean]
  #   destroy all tags with the keyword_id provided, true if success, false if failure
  def self.batch_remove(keyword_id, klass = nil)
    return false if keyword_id.blank?
    if klass.blank?
      return true if Tag.where(keyword_id: keyword_id).destroy_all
    else
      return true if Tag.where(keyword_id: keyword_id, tag_object_type: klass).destroy_all
    end
    false
  end

  def add_source_to_project
    !!ProjectSource.find_or_create_by(project: project, source: tag_object)
  end

end

#positionInteger

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/tag.rb', line 28

class Tag < ApplicationRecord
  
  # Tags can not be cited.
  include Housekeeping
  include Shared::AttributeAnnotations
  include Shared::IsData

  include Shared::MatrixHooks::Dynamic # Must preceed Tag::MatrixHooks
  include Tag::MatrixHooks # Must come after Shared::MatrixHooks::Dynamic 

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:tag_object)
  acts_as_list scope: [:tag_object_id, :tag_object_type, :keyword_id, :project_id]

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

  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

  after_create :add_source_to_project, if: Proc.new { |tag| tag.tag_object.is_a?(Source) }

  def self.tag_objects(objects, keyword_id = nil)
    return nil if keyword_id.nil? or objects.empty?
    objects.each do |o|
      o.tags << Tag.new(keyword_id: keyword_id)
    end
  end

  def self.exists?(global_id, keyword_id, project_id)
    o = GlobalID::Locator.locate(global_id)
    return false unless o
    Tag.where(project_id: project_id, tag_object: o, keyword_id: keyword_id).first
  end

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

  def self.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: [])
    begin
      Tag.transaction do 
        object_ids.each do |id|
          Tag.find_or_create_by(keyword_id: keyword_id, tag_object_type: object_type, tag_object_id: id)
        end
      end
    rescue ActiveRecord::RecordInvalid
      return false 
    end
  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

  # @return [Boolean]
  #   destroy all tags with the keyword_id provided, true if success, false if failure
  def self.batch_remove(keyword_id, klass = nil)
    return false if keyword_id.blank?
    if klass.blank?
      return true if Tag.where(keyword_id: keyword_id).destroy_all
    else
      return true if Tag.where(keyword_id: keyword_id, tag_object_type: klass).destroy_all
    end
    false
  end

  def add_source_to_project
    !!ProjectSource.find_or_create_by(project: project, source: tag_object)
  end

end

#project_idInteger

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/tag.rb', line 28

class Tag < ApplicationRecord
  
  # Tags can not be cited.
  include Housekeeping
  include Shared::AttributeAnnotations
  include Shared::IsData

  include Shared::MatrixHooks::Dynamic # Must preceed Tag::MatrixHooks
  include Tag::MatrixHooks # Must come after Shared::MatrixHooks::Dynamic 

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:tag_object)
  acts_as_list scope: [:tag_object_id, :tag_object_type, :keyword_id, :project_id]

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

  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

  after_create :add_source_to_project, if: Proc.new { |tag| tag.tag_object.is_a?(Source) }

  def self.tag_objects(objects, keyword_id = nil)
    return nil if keyword_id.nil? or objects.empty?
    objects.each do |o|
      o.tags << Tag.new(keyword_id: keyword_id)
    end
  end

  def self.exists?(global_id, keyword_id, project_id)
    o = GlobalID::Locator.locate(global_id)
    return false unless o
    Tag.where(project_id: project_id, tag_object: o, keyword_id: keyword_id).first
  end

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

  def self.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: [])
    begin
      Tag.transaction do 
        object_ids.each do |id|
          Tag.find_or_create_by(keyword_id: keyword_id, tag_object_type: object_type, tag_object_id: id)
        end
      end
    rescue ActiveRecord::RecordInvalid
      return false 
    end
  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

  # @return [Boolean]
  #   destroy all tags with the keyword_id provided, true if success, false if failure
  def self.batch_remove(keyword_id, klass = nil)
    return false if keyword_id.blank?
    if klass.blank?
      return true if Tag.where(keyword_id: keyword_id).destroy_all
    else
      return true if Tag.where(keyword_id: keyword_id, tag_object_type: klass).destroy_all
    end
    false
  end

  def add_source_to_project
    !!ProjectSource.find_or_create_by(project: project, source: tag_object)
  end

end

#tag_object_attributeString

Returns the specific attribute being referenced with the tag (not required).

Returns:

  • (String)

    the specific attribute being referenced with the tag (not required)



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/tag.rb', line 28

class Tag < ApplicationRecord
  
  # Tags can not be cited.
  include Housekeeping
  include Shared::AttributeAnnotations
  include Shared::IsData

  include Shared::MatrixHooks::Dynamic # Must preceed Tag::MatrixHooks
  include Tag::MatrixHooks # Must come after Shared::MatrixHooks::Dynamic 

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:tag_object)
  acts_as_list scope: [:tag_object_id, :tag_object_type, :keyword_id, :project_id]

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

  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

  after_create :add_source_to_project, if: Proc.new { |tag| tag.tag_object.is_a?(Source) }

  def self.tag_objects(objects, keyword_id = nil)
    return nil if keyword_id.nil? or objects.empty?
    objects.each do |o|
      o.tags << Tag.new(keyword_id: keyword_id)
    end
  end

  def self.exists?(global_id, keyword_id, project_id)
    o = GlobalID::Locator.locate(global_id)
    return false unless o
    Tag.where(project_id: project_id, tag_object: o, keyword_id: keyword_id).first
  end

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

  def self.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: [])
    begin
      Tag.transaction do 
        object_ids.each do |id|
          Tag.find_or_create_by(keyword_id: keyword_id, tag_object_type: object_type, tag_object_id: id)
        end
      end
    rescue ActiveRecord::RecordInvalid
      return false 
    end
  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

  # @return [Boolean]
  #   destroy all tags with the keyword_id provided, true if success, false if failure
  def self.batch_remove(keyword_id, klass = nil)
    return false if keyword_id.blank?
    if klass.blank?
      return true if Tag.where(keyword_id: keyword_id).destroy_all
    else
      return true if Tag.where(keyword_id: keyword_id, tag_object_type: klass).destroy_all
    end
    false
  end

  def add_source_to_project
    !!ProjectSource.find_or_create_by(project: project, source: tag_object)
  end

end

#tag_object_idInteger

Returns Rails polymorphic, id of the object being tagged.

Returns:

  • (Integer)

    Rails polymorphic, id 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/tag.rb', line 28

class Tag < ApplicationRecord
  
  # Tags can not be cited.
  include Housekeeping
  include Shared::AttributeAnnotations
  include Shared::IsData

  include Shared::MatrixHooks::Dynamic # Must preceed Tag::MatrixHooks
  include Tag::MatrixHooks # Must come after Shared::MatrixHooks::Dynamic 

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:tag_object)
  acts_as_list scope: [:tag_object_id, :tag_object_type, :keyword_id, :project_id]

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

  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

  after_create :add_source_to_project, if: Proc.new { |tag| tag.tag_object.is_a?(Source) }

  def self.tag_objects(objects, keyword_id = nil)
    return nil if keyword_id.nil? or objects.empty?
    objects.each do |o|
      o.tags << Tag.new(keyword_id: keyword_id)
    end
  end

  def self.exists?(global_id, keyword_id, project_id)
    o = GlobalID::Locator.locate(global_id)
    return false unless o
    Tag.where(project_id: project_id, tag_object: o, keyword_id: keyword_id).first
  end

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

  def self.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: [])
    begin
      Tag.transaction do 
        object_ids.each do |id|
          Tag.find_or_create_by(keyword_id: keyword_id, tag_object_type: object_type, tag_object_id: id)
        end
      end
    rescue ActiveRecord::RecordInvalid
      return false 
    end
  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

  # @return [Boolean]
  #   destroy all tags with the keyword_id provided, true if success, false if failure
  def self.batch_remove(keyword_id, klass = nil)
    return false if keyword_id.blank?
    if klass.blank?
      return true if Tag.where(keyword_id: keyword_id).destroy_all
    else
      return true if Tag.where(keyword_id: keyword_id, tag_object_type: klass).destroy_all
    end
    false
  end

  def add_source_to_project
    !!ProjectSource.find_or_create_by(project: project, source: tag_object)
  end

end

#tag_object_typeString

Returns Rails polymorphic, type of the object being tagged.

Returns:

  • (String)

    Rails polymorphic, 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/tag.rb', line 28

class Tag < ApplicationRecord
  
  # Tags can not be cited.
  include Housekeeping
  include Shared::AttributeAnnotations
  include Shared::IsData

  include Shared::MatrixHooks::Dynamic # Must preceed Tag::MatrixHooks
  include Tag::MatrixHooks # Must come after Shared::MatrixHooks::Dynamic 

  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:tag_object)
  acts_as_list scope: [:tag_object_id, :tag_object_type, :keyword_id, :project_id]

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

  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

  after_create :add_source_to_project, if: Proc.new { |tag| tag.tag_object.is_a?(Source) }

  def self.tag_objects(objects, keyword_id = nil)
    return nil if keyword_id.nil? or objects.empty?
    objects.each do |o|
      o.tags << Tag.new(keyword_id: keyword_id)
    end
  end

  def self.exists?(global_id, keyword_id, project_id)
    o = GlobalID::Locator.locate(global_id)
    return false unless o
    Tag.where(project_id: project_id, tag_object: o, keyword_id: keyword_id).first
  end

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

  def self.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: [])
    begin
      Tag.transaction do 
        object_ids.each do |id|
          Tag.find_or_create_by(keyword_id: keyword_id, tag_object_type: object_type, tag_object_id: id)
        end
      end
    rescue ActiveRecord::RecordInvalid
      return false 
    end
  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

  # @return [Boolean]
  #   destroy all tags with the keyword_id provided, true if success, false if failure
  def self.batch_remove(keyword_id, klass = nil)
    return false if keyword_id.blank?
    if klass.blank?
      return true if Tag.where(keyword_id: keyword_id).destroy_all
    else
      return true if Tag.where(keyword_id: keyword_id, tag_object_type: klass).destroy_all
    end
    false
  end

  def add_source_to_project
    !!ProjectSource.find_or_create_by(project: project, source: tag_object)
  end

end

Class Method Details

.annotated_attribute_columnObject

The column name containing the attribute name being annotated



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

def self.annotated_attribute_column
  :tag_object_attribute
end

.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: []) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/tag.rb', line 73

def self.batch_create(keyword_id: nil, object_type: nil, user_id: nil, project_id: nil, object_ids: [])
  begin
    Tag.transaction do 
      object_ids.each do |id|
        Tag.find_or_create_by(keyword_id: keyword_id, tag_object_type: object_type, tag_object_id: id)
      end
    end
  rescue ActiveRecord::RecordInvalid
    return false 
  end
end

.batch_remove(keyword_id, klass = nil) ⇒ Boolean (protected)

Returns destroy all tags with the keyword_id provided, true if success, false if failure.

Returns:

  • (Boolean)

    destroy all tags with the keyword_id provided, true if success, false if failure



107
108
109
110
111
112
113
114
115
# File 'app/models/tag.rb', line 107

def self.batch_remove(keyword_id, klass = nil)
  return false if keyword_id.blank?
  if klass.blank?
    return true if Tag.where(keyword_id: keyword_id).destroy_all
  else
    return true if Tag.where(keyword_id: keyword_id, tag_object_type: klass).destroy_all
  end
  false
end

.exists?(global_id, keyword_id, project_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.exists?(global_id, keyword_id, project_id)
  o = GlobalID::Locator.locate(global_id)
  return false unless o
  Tag.where(project_id: project_id, tag_object: o, keyword_id: keyword_id).first
end

.tag_objects(objects, keyword_id = nil) ⇒ Object



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

def self.tag_objects(objects, keyword_id = nil)
  return nil if keyword_id.nil? or objects.empty?
  objects.each do |o|
    o.tags << Tag.new(keyword_id: keyword_id)
  end
end

Instance Method Details

#add_source_to_projectObject (protected)



117
118
119
# File 'app/models/tag.rb', line 117

def add_source_to_project
  !!ProjectSource.find_or_create_by(project: project, source: tag_object)
end

#keyword_is_allowed_on_objectObject (protected)



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

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_can_be_tagged_with_keywordObject (protected)



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

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

#reject_keyword(attributed) ⇒ Object (protected)



101
102
103
# File 'app/models/tag.rb', line 101

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