Sha256: 36a16aa7c345ec7ce8f317a7bd6e13cdd6f6a95aa9f4e82f7e35c347c84d0d33

Contents?: true

Size: 751 Bytes

Versions: 1

Compression:

Stored size: 751 Bytes

Contents

class Gutentag::Persistence
  def self.after_save(taggable)
    new(taggable).persist
  end

  def initialize(taggable)
    @taggable = taggable
    @existing = normalised taggable.tags.collect(&:name)
    @changes  = normalised taggable.tag_names
  end

  def persist
    remove_old
    add_new

    taggable.reset_tag_names
  end

  private

  attr_reader :taggable, :existing, :changes

  def add_new
    (changes - existing).each do |name|
      taggable.tags << Gutentag::Tag.find_or_create(name)
    end
  end

  def normalised(names)
    names.collect { |name| Gutentag::TagName.normalise(name) }.uniq
  end

  def remove_old
    (existing - changes).each do |name|
      taggable.tags.delete Gutentag::Tag.find_by_name(name)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gutentag-0.3.0 lib/gutentag/persistence.rb