Class: Tag
- Inherits:
-
Object
- Object
- Tag
- Defined in:
- app/models/tag.rb
Constant Summary collapse
- FORBIDDEN_CHARACTERS =
/<>,/
- TAG_LIST_DELIMITER =
','
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#count ⇒ Object
Returns the value of attribute count.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .create_tags!(tags) ⇒ Object
- .delete_tags!(tags) ⇒ Object
- .update_tags!(before, after) ⇒ Object
- .valid_name?(name) ⇒ Boolean
Instance Method Summary collapse
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
15 16 17 |
# File 'app/models/tag.rb', line 15 def context @context end |
#count ⇒ Object
Returns the value of attribute count.
10 11 12 |
# File 'app/models/tag.rb', line 10 def count @count end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'app/models/tag.rb', line 10 def name @name end |
Class Method Details
.create_tags!(tags) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/models/tag.rb', line 39 def .each do |name| if tag = Tag.by_name(name) # TODO3 preformance lost tag.update_count! else tag = Tag.new name: name tag.count = 1 tag.save! end end end |
.delete_tags!(tags) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'app/models/tag.rb', line 57 def .each do |name| if tag = Tag.by_name(name) tag.update_count! tag.destroy if tag.count == 0 end end end |
.update_tags!(before, after) ⇒ Object
52 53 54 55 |
# File 'app/models/tag.rb', line 52 def before, after after - before before - after end |
.valid_name?(name) ⇒ Boolean
35 36 37 |
# File 'app/models/tag.rb', line 35 def valid_name? name name.present? and name !~ FORBIDDEN_CHARACTERS end |
Instance Method Details
#update_count! ⇒ Object
26 27 28 29 30 31 32 |
# File 'app/models/tag.rb', line 26 def update_count! new_count = Item.count tags: {_in: [name]} unless count == new_count self.count = new_count self.save! end end |