Sha256: a1f4d468c19ab41e1db7ce82f1998b5642a25cc971c83f52df5d6d9d238f63cb

Contents?: true

Size: 931 Bytes

Versions: 3

Compression:

Stored size: 931 Bytes

Contents

# encoding: utf-8

module PagesCore
  module Taggable
    extend ActiveSupport::Concern

    included do
      has_many :taggings, as: :taggable, dependent: :destroy
      has_many :tags, through: :taggings
    end

    module ClassMethods
      def tagged_with(*tags)
        all
          .includes(:tags)
          .where("tags.name IN (?)", Tag.parse(tags))
          .references(:tags)
      end
    end

    def serialized_tags
      tags.order("name ASC").map(&:name).to_json
    end

    def serialized_tags=(json)
      tag_with(ActiveSupport::JSON.decode(json))
    end

    def tag_with(*list)
      Tag.transaction do
        taggings.destroy_all
        Tag.parse(list).each do |name|
          Tag.find_or_create_by(name: name).on(self)
        end
      end
    end

    def tag_list=(tag_list)
      tag_with(tag_list)
    end

    def tag_list
      tags.order("name ASC").map(&:name).join(", ")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pages_core-3.4.3 app/models/concerns/pages_core/taggable.rb
pages_core-3.5.1 app/models/concerns/pages_core/taggable.rb
pages_core-3.4.2 app/models/concerns/pages_core/taggable.rb