Sha256: e240cebe68fb8d00210ca862a93f5410a7cb9c27f33110b2214319a19e7dfc6f

Contents?: true

Size: 948 Bytes

Versions: 1

Compression:

Stored size: 948 Bytes

Contents

require 'active_support/concern'

module Gutentag::ActiveRecord
  extend ActiveSupport::Concern

  UNIQUENESS_METHOD = ActiveRecord::VERSION::MAJOR == 3 ? :uniq : :distinct

  module ClassMethods
    def has_many_tags
      has_many :taggings, :class_name => 'Gutentag::Tagging', :as => :taggable,
        :dependent => :destroy
      has_many :tags,     :class_name => 'Gutentag::Tag',
        :through => :taggings

      after_save :persist_tags
    end

    def tagged_with(*tags)
      joins(:tags).where(
        Gutentag::Tag.table_name => {:name => Gutentag::TagNames.call(tags)}
      ).public_send UNIQUENESS_METHOD
    end
  end

  def reset_tag_names
    @tag_names = nil
  end

  def tag_names
    @tag_names ||= tags.pluck(:name)
  end

  def tag_names=(names)
    Gutentag.dirtier.call self, names if Gutentag.dirtier

    @tag_names = names
  end

  private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gutentag-0.8.0 lib/gutentag/active_record.rb