Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

always use wildcard lookups

  • Loading branch information...
commit e9692c0ebbc1152faa703351497a3537fddd6e8f 1 parent cbe6c6e
@svenfuchs authored
Showing with 30 additions and 6 deletions.
  1. +10 −5 lib/rack/cache/tags/store/active_record.rb
  2. +20 −1 test/test_tags.rb
View
15 lib/rack/cache/tags/store/active_record.rb
@@ -12,12 +12,17 @@ class Tagging < ::ActiveRecord::Base
def store(url, tags)
tags.each { |tag| Tagging.find_or_create_by_url_and_tag(url, tag) }
end
-
+
def purge(tags)
- tags = Tagging.where(:tag => tags)
- urls = tags.map(&:url)
- Tagging.where(:url => urls).delete_all
- urls
+ urls_by_tags(tags).tap { |urls| Tagging.where(:url => urls).delete_all }
+ end
+
+ def urls_by_tags(tags)
+ taggings_by_tags(tags).map(&:url)
+ end
+
+ def taggings_by_tags(tags)
+ Tagging.where((['tag LIKE ?'] * tags.size).join(' OR '), *tags.map { |tag| "#{tag}%" })
end
end
end
View
21 test/test_tags.rb
@@ -3,6 +3,12 @@
class TagsTest < Test::Unit::TestCase
include Rack::Cache::Tags::Store
+ attr_reader :store
+
+ def setup
+ @store = Rack::Cache::Tags::Store::ActiveRecord.new
+ end
+
test 'stores tags for the current url' do
respond_with 200, { Rack::Cache::Tags::TAGS_HEADER => %w(foo-1 bar-2) }, ''
@@ -23,4 +29,17 @@ class TagsTest < Test::Unit::TestCase
assert_equal [%w(http://example.com/baz baz-1)], actual
assert_equal %w(http://example.com/foo http://example.com/bar), headers[Rack::Cache::Tags::PURGE_HEADER]
end
-end
+
+ test 'active_record store finds tags w/o methods' do
+ tags = %w(foo-1 foo-2)
+ create_tags('http://example.com/foo', tags)
+ assert_equal tags, store.taggings_by_tags(tags).map(&:tag)
+ end
+
+ test 'active_record store finds tags w/ methods' do
+ tags = %w(foo-1:title foo-1:body)
+ create_tags('http://example.com/foo', tags)
+ assert_equal tags, store.taggings_by_tags(%w(foo-1)).map(&:tag)
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.