Sha256: 8f5f680b1e73b0ee26efe6d9a14b9eceef232dc8faf35b773fb391908fb6b9d8

Contents?: true

Size: 995 Bytes

Versions: 7

Compression:

Stored size: 995 Bytes

Contents

# frozen_string_literal: true

# For Rails 4.2 only.
module Gutentag::ActiveRecord::InstanceMethods
  # The reason we overwrite the stored value is because new tags may be added to
  # the instance directly (e.g. article.tags << tag), which invokes the save
  # callbacks, but the old tag_names value is stored but not updated.
  def reset_tag_names
    # Update the underlying value rather than going through the setter, to
    # ensure this update doesn't get marked as a 'change'.
    self.tag_names = nil
  end

  def tag_names
    # If the underlying value is nil, we've not requested this from the
    # database yet.
    if read_attribute("tag_names") { nil }.nil?
      self.tag_names = tags.pluck(:name)
    end

    # Use ActiveRecord's underlying implementation with change tracking.
    super
  end

  def tag_names=(names)
    super Gutentag::TagNames.call(names)
  end

  private

  def persist_tags
    Gutentag::Persistence.new(Gutentag::ChangeState.new(self)).persist
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gutentag-2.5.1 lib/gutentag/active_record/instance_methods_4_2.rb
gutentag-2.5.0 lib/gutentag/active_record/instance_methods_4_2.rb
gutentag-2.4.1 lib/gutentag/active_record/instance_methods_4_2.rb
gutentag-2.4.0 lib/gutentag/active_record/instance_methods_4_2.rb
gutentag-2.3.2 lib/gutentag/active_record/instance_methods_4_2.rb
gutentag-2.3.1 lib/gutentag/active_record/instance_methods_4_2.rb
gutentag-2.3.0 lib/gutentag/active_record/instance_methods_4_2.rb