Sha256: 9860a2ff6a26cf70f563657cfc237ff7405e3a608fd22fb7f46a7de5ba9e749b

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

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 |options = {}, &block|
            query_scope = all
            query_scope = query_scope.merge(instance_eval(&block)) if block

            query_scope.uniq.pluck("unnest(#{table_name}.#{tag_name})")
          end

          define_method :"#{tag_name}_cloud" do |options = {}, &block|
            query_scope = select("unnest(#{table_name}.#{tag_name}) as tag")
            query_scope = query_scope.merge(instance_eval(&block)) if block

            from(query_scope).group('tag').order('tag').pluck('tag, count(*)')
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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