Sha256: 05cb3e28f6c71ec76a3e38a10daae7caf9c66580125818bbd6a32708540dbd1b

Contents?: true

Size: 794 Bytes

Versions: 1

Compression:

Stored size: 794 Bytes

Contents

module MyTags::ActiveRecord
  def self.included(base)
    base.extend MyTags::ActiveRecord::ClassMethods
  end

  def tag_list
    @tag_list ||= MyTags::TagList.new self
  end

  def tag_list=(names)
    @tag_names = names.is_a?(MyTags::TagList) ? names : MyTags::TagList.new_with_names(self, names)
  end

  # Add/Update tags for object
  # @post.process_tags('cat, dog')
  # @post.process_tags(['cat','dog'])
  def process_tags(tag_list)
    tag_list = tag_list.split(',').map(&:strip) unless tag_list.is_a?(Array)
    self.tag_list = tag_list
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
my_tags-1.0.0 lib/my_tags/active_record.rb