Module: Glue::Taggable::ClassMethods

Defined in:
lib/glue/taggable.rb

Instance Method Summary collapse

Instance Method Details

#find_with_any_tag(*names) ⇒ Object

Find objects with any of the provided tags. UNION (OR)



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/glue/taggable.rb', line 218

def find_with_any_tag(*names)
  relation = relations.reject{|r| r.name != :tags}.first
  info = ogmanager.store.join_table_info(relation)
  count = names.size
  names = names.map { |n| ogmanager.store.quote(n) }.join(',')
  sql = %{        
    SELECT *
    FROM #{info[:owner_table]} AS o
    WHERE o.oid IN (
      SELECT j.#{info[:owner_key]}
      FROM #{info[:target_table]} AS t
      JOIN #{info[:table]} AS j
      ON t.oid = j.#{info[:target_key]}
      WHERE (t.name IN (#{names}))
      GROUP BY j.#{info[:owner_key]}
      )
  }
  return self.select(sql)
end

#find_with_tags(*names) ⇒ Object Also known as: find_with_tag

Find objects with all of the provided tags. INTERSECTION (AND)



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/glue/taggable.rb', line 193

def find_with_tags(*names)
  relation = relations.reject{|r| r.name != :tags}.first
  info = ogmanager.store.join_table_info(relation)
  count = names.size
  names = names.map { |n| ogmanager.store.quote(n) }.join(',')
  sql = %{
    SELECT *
    FROM #{info[:owner_table]} AS o
    WHERE o.oid IN (
      SELECT j.#{info[:owner_key]}
      FROM #{info[:target_table]} AS t
      JOIN #{info[:table]} AS j
      ON t.oid = j.#{info[:target_key]}
      WHERE (t.name IN (#{names}))
      GROUP BY j.#{info[:owner_key]}
      HAVING COUNT(j.#{info[:owner_key]}) = #{count}
    )
  }
  return self.select(sql)
end