Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
topical / lib / topical / taggable.rb
Size: Mime:
module Topical
  module Taggable
    module ClassMethods
      def topical_on(*contexts)
        include InstanceMethods

        class_attribute :topical_contexts
        self.topical_contexts = contexts
        Topical.register_contexts(contexts)

        has_many :taggings, as: :taggable, dependent: :destroy,
                            class_name: '::Topical::Tagging', autosave: true
        has_many :tags, through: :taggings, source: :tag,
                            class_name: '::Topical::Tag', autosave: true

        contexts.each do |context|
          has_many context, -> { by(context) },
                            through: :taggings, source: :tag,
                            class_name: '::Topical::Tag', autosave: true
          define_method "#{context}_list" do
            send(context).map(&:name).join(Topical.configuration.delimiter)
          end

          define_method "#{context}_list=" do |tag_list_string|
            Topical::TagFactory.new(self, context, tag_list_string).assign
          end
        end
      end

      def tagged_with(tag)
        if self.topical?
          joins(:tags).where('topical_tags.id' => tag.id)
        end
      end

      def tagged_on(context, name)
        if self.topical?
          joins(:tags).where('topical_tags.context' => context.to_s,
                             'topical_tags.name'    => name)
        end
      end

      def topical?
        topical_contexts.any?
      end
    end

    module InstanceMethods

    end
  end
end