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_def) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/acts-as-taggable-array-on/taggable.rb', line 8

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