Sha256: 09ebbfbc81908055c0294c53e852c04035d39968b0c569ac95566558282b8322

Contents?: true

Size: 952 Bytes

Versions: 2

Compression:

Stored size: 952 Bytes

Contents

module ActsAsTaggableArrayOn
  module Taggable
    def self.included(base)
      base.extend(ClassMethod)
    end

    module ClassMethod
      def acts_as_taggable_array_on(*tag_def)
        tag_name = tag_def.first

        scope :"with_any_#{tag_name}", ->(* tags){where("#{tag_name} && ARRAY[?]", tags)}
        scope :"with_all_#{tag_name}", ->(* tags){where("#{tag_name} @> ARRAY[?]", tags)}
        scope :"without_any_#{tag_name}", ->(* tags){where.not("#{tag_name} && ARRAY[?]", tags)}
        scope :"without_all_#{tag_name}", ->(* tags){where.not("#{tag_name} @> ARRAY[?]", tags)}

        self.class.class_eval do
          define_method :"all_#{tag_name}" do
            all.uniq.pluck("unnest(#{tag_name})")
          end

          define_method :"#{tag_name}_cloud" do
            from(select("unnest(#{tag_name}) as tag")).group('tag').order('tag').pluck('tag, count(*) as count')
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts-as-taggable-array-on-0.1.1 lib/acts-as-taggable-array-on/taggable.rb
acts-as-taggable-array-on-0.1.0 lib/acts-as-taggable-array-on/taggable.rb