Module: Motor::Tags

Defined in:
lib/motor/tags.rb

Class Method Summary collapse

Class Method Details

.assign_tags(taggable, tags) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/motor/tags.rb', line 7

def assign_tags(taggable, tags)
  return taggable unless tags

  tags.each do |tag_name|
    next if taggable.taggable_tags.find { |tt| tt.tag.name.casecmp(tag_name).zero? }

    tag = Tag.find_or_initialize_by(name: tag_name)

    taggable.taggable_tags.new(tag: tag)
  end

  remove_missing_tags(taggable, tags) if taggable.persisted?

  taggable
end

.remove_missing_tags(taggable, tags) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/motor/tags.rb', line 23

def remove_missing_tags(taggable, tags)
  downcase_tags = tags.map(&:downcase)
  tags_to_remove = taggable.tags.reject { |tt| tt.name.downcase.in?(downcase_tags) }

  taggable.tags -= tags_to_remove

  taggable
end