Module: Shoppy::Concerns::Models::Taggable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/shoppy/concerns/models/taggable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_tags!(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/shoppy/concerns/models/taggable.rb', line 4

def add_tags!(*args)
  tags = args
  tags.each do |tag|
    if Tag.exists?(name: tag)
      self.tags += [tag]
    else
      new_tag = Tag.create(name: tag)
      self.tags += [new_tag]
    end
  end
  self.save
end

#remove_tags!(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'app/models/shoppy/concerns/models/taggable.rb', line 17

def remove_tags!(*args)
  tags = args
  tags.each do |tag|
    if self.tags.include?(Tag.find_by(name: tag))
      self.tags -= [Tag.find_by(name: tag)]
    end
  end
  self.save
end