Sha256: 177e7d5fdc24361365c48c0e848ee0ecbc0fbfb376878870d9dcec33776a9c85

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

module Storytime::PostTags
  extend ActiveSupport::Concern

  included do
    has_many :taggings, dependent: :destroy, foreign_key: "post_id", class_name: "Storytime::Tagging"
    has_many :tags, through: :taggings, class_name: "Storytime::Tag"

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

    def tag_list=(names_or_ids)
      self.tags = names_or_ids.map do |n|
        if n.empty? || n == "nv__"
          ""
        elsif n.include?("nv__") || n.to_i == 0
          Storytime::Tag.where(name: n.sub("nv__", "").strip).first_or_create do |tag|
            tag.site_id = self.site_id
          end
        else
          Storytime::Tag.find(n)
        end
      end.delete_if { |x| x == "" }
    end
  end

  module ClassMethods
    def tagged_with(name)
      if t = Storytime::Tag.find_by(name: name)
        joins(:taggings).where(storytime_taggings: { tag_id: t.id }) 
      else
        none
      end
    end

    def tag_counts
      Storytime::Tag.select("storytime_tags.*, count(storytime_taggings.tag_id) as count").joins(:taggings).group("storytime_tags.id")
      #Tagging.group("storytime_taggings.tag_id").includes(:tag)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
storytime-2.1.6 app/models/concerns/storytime/post_tags.rb
storytime-2.1.5 app/models/concerns/storytime/post_tags.rb
storytime-2.1.4 app/models/concerns/storytime/post_tags.rb
storytime-2.1.3 app/models/concerns/storytime/post_tags.rb
storytime-2.1.2 app/models/concerns/storytime/post_tags.rb
storytime-2.1.1 app/models/concerns/storytime/post_tags.rb
storytime-2.1.0 app/models/concerns/storytime/post_tags.rb
storytime-2.0.0 app/models/concerns/storytime/post_tags.rb