Skip to content

Instantly share code, notes, and snippets.

@yosriady
Created November 30, 2015 13:04
Show Gist options
  • Save yosriady/49067c99af00848d5581 to your computer and use it in GitHub Desktop.
Save yosriady/49067c99af00848d5581 to your computer and use it in GitHub Desktop.
CS3218 Snippet
module PgTags
module Taggable
def self.included(base)
base.extend(ClassMethod)
end
module ClassMethod
def has_tags(*tag_types)
tag_types = tag_types.to_a.flatten.compact.map(&:to_sym)
class_eval do
class_attribute :tag_types
self.tag_types = tag_types
end
tag_types.each do |tag_type|
#== Scopes
scope :"with_any_#{tag_type}", ->(tags){ where("#{tag_type} && ARRAY[?]::varchar[]", tags) }
scope :"with_all_#{tag_type}", ->(tags){ where("#{tag_type} @> ARRAY[?]::varchar[]", tags) }
scope :"without_any_#{tag_type}", ->(tags){ where.not("#{tag_type} && ARRAY[?]::varchar[]", tags) }
scope :"without_all_#{tag_type}", ->(tags){ where.not("#{tag_type} @> ARRAY[?]::varchar[]", tags) }
self.class.class_eval do
define_method :"all_#{tag_type}" do |options = {}, &block|
subquery_scope = unscoped.select("unnest(#{table_name}.#{tag_type}) as tag").uniq
subquery_scope = subquery_scope.instance_eval(&block) if block
from(subquery_scope).pluck('tag')
end
define_method :"#{tag_type}_cloud" do |options = {}, &block|
subquery_scope = unscoped.select("unnest(#{table_name}.#{tag_type}) as tag")
subquery_scope = subquery_scope.instance_eval(&block) if block
from(subquery_scope).group('tag').order('tag').pluck('tag, count(*) as count')
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment