Module: ActsAsTaggableArrayOn::Taggable::ClassMethod

Defined in:
lib/acts-as-taggable-array-on/taggable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_taggable_array_on(tag_name) ⇒ Object Also known as: taggable_array



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/acts-as-taggable-array-on/taggable.rb', line 12

def acts_as_taggable_array_on(tag_name, *)
  tag_array_type_fetcher = -> { TYPE_MATCHER[columns_hash[tag_name.to_s].type] }
  parser = ActsAsTaggableArrayOn.parser

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

  self.class.class_eval do
    define_method :"all_#{tag_name}" do |options = {}, &block|
      subquery_scope = unscoped.select("unnest(#{table_name}.#{tag_name}) as tag").distinct
      subquery_scope = subquery_scope.instance_eval(&block) if block

      from(subquery_scope).pluck('tag')
    end

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

      from(subquery_scope).group('tag').order('tag').pluck(Arel.sql('tag, count(*) as count'))
    end
  end
end