Class: Tag

Inherits:
Object
  • Object
show all
Defined in:
app/models/tag.rb

Constant Summary collapse

FORBIDDEN_CHARACTERS =
/<>,/
TAG_LIST_DELIMITER =
','

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'app/models/tag.rb', line 15

def context
  @context
end

#countObject

Returns the value of attribute count.



10
11
12
# File 'app/models/tag.rb', line 10

def count
  @count
end

#nameObject

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 create_tags! tags
  tags.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 delete_tags! tags
  tags.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 update_tags! before, after
  create_tags! after - before
  delete_tags! before - after
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (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