Class: Tag
Direct Known Subclasses
Defined Under Namespace
Classes: TagAlreadyLabeled, TagAlreadyUnlabeled, UnacceptableTagType
Constant Summary collapse
- TYPE_NORMAL =
0
- TYPE_AUTH =
1
Class Method Summary collapse
-
.description(desc = nil) ⇒ Object
Set or read description of the Tag class.
-
.find_tag_class(name) ⇒ Object
one_to_many :tag_attributes, :one_to_one=>true.
-
.type_id(type_id = nil) ⇒ Object
Declare the integer number for the tag.
Instance Method Summary collapse
-
#accept_mapping?(taggable_obj) ⇒ Boolean
Check the object class type before associating to the Tag.
- #after_initialize ⇒ Object
- #label(canonical_uuid) ⇒ Object
- #labeled?(canonical_uuid) ⇒ Boolean
- #unlabel(canonical_uuid) ⇒ Object
Methods inherited from BaseNew
Proxy, install_data, install_data_hooks, #with_timestamps?
Class Method Details
.description(desc = nil) ⇒ Object
Set or read description of the Tag class.
100 101 102 103 104 105 |
# File 'app/models/tag.rb', line 100 def description(desc=nil) if desc @description = desc end @description end |
.find_tag_class(name) ⇒ Object
one_to_many :tag_attributes, :one_to_one=>true
72 73 74 75 76 |
# File 'app/models/tag.rb', line 72 def self.find_tag_class(name) self.subclasses.find { |m| m.to_s.sub(/^#{self.class}::/, '') == name } end |
.type_id(type_id = nil) ⇒ Object
Declare the integer number for the tag.
Also set the value to sti map in class Tag. class Tag1 < Tag
type_id 123456
end
puts Tag1.type_id # == 123456
89 90 91 92 93 94 95 96 |
# File 'app/models/tag.rb', line 89 def type_id(type_id=nil) if type_id.is_a?(Fixnum) @type_id = type_id Tag.sti_model_map[type_id] = self Tag.sti_key_map[self.to_s] = type_id end @type_id || raise("#{self}.type_id is unset. Please set the unique number for the tag instance.") end |
Instance Method Details
#accept_mapping?(taggable_obj) ⇒ Boolean
Check the object class type before associating to the Tag. Child class must implement this method.
112 113 114 |
# File 'app/models/tag.rb', line 112 def accept_mapping?(taggable_obj) raise NotImplementedError end |
#after_initialize ⇒ Object
116 117 118 119 |
# File 'app/models/tag.rb', line 116 def after_initialize super self[:type_id] = self.class.type_id end |
#label(canonical_uuid) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/models/tag.rb', line 51 def label(canonical_uuid) tgt = Taggable.find(canonical_uuid) raise(UnacceptableTagType, self, tgt) if accept_mapping?(tgt) raise(TagAlreadyLabeled) if labeled?(canonical_uuid) TagMapping.create(:uuid=>canonical_uuid, :tag_id=>self.pk) self end |
#labeled?(canonical_uuid) ⇒ Boolean
45 46 47 48 49 |
# File 'app/models/tag.rb', line 45 def labeled?(canonical_uuid) # TODO: check if the uuid exists !TagMapping.filter(:uuid=>canonical_uuid, :tag_id=>self.pk).empty? end |
#unlabel(canonical_uuid) ⇒ Object
60 61 62 63 64 |
# File 'app/models/tag.rb', line 60 def unlabel(canonical_uuid) t = TagMapping.find(:uuid=>canonical_uuid, :tag_id=>self.pk) || raise(TagAlreadyUnlabeled) t.delete self end |