Skip to content

Commit

Permalink
use less queries for storing taggings
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Oct 10, 2010
1 parent 9e6a9e7 commit 30cb0d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/rack/cache/tags/store/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Tagging < ::ActiveRecord::Base
end

def store(url, tags)
tags.each { |tag| Tagging.find_or_create_by_url_and_tag(url, tag) }
tags -= taggings_by_url_and_tags(url, tags).map(&:tag)
tags.each { |tag| Tagging.create(:url => url, :tag => tag) }
end

def purge(tags)
Expand All @@ -25,6 +26,10 @@ def taggings_by_tags(tags)
sql = "tag IN (?) #{[' OR tag LIKE ?'] * tags.size}"
Tagging.where(sql, tags, *tags.map { |tag| "#{tag.split(':').first}%" })
end

def taggings_by_url_and_tags(url, tags)
Tagging.where("url = ? AND tag IN (?)", url, tags)
end
end
end
end
Expand Down
22 changes: 19 additions & 3 deletions test/tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ def setup
@store = Rack::Cache::Tags::Store::ActiveRecord.new
end

def create_taggings(taggings)
taggings.each { |url, tags| tags.each { |tag| create_tagging(url, tag) } }
end

def create_tagging(url, tag)
ActiveRecord::Tagging.create!(:url => url, :tag => tag)
end

test 'stores tags for the current url' do
create_taggings('http://example.com/' => %w(bar-2), 'http://example.com/bar' => %w(bar-2))
respond_with 200, { Rack::Cache::Tags::TAGS_HEADER => %w(foo-1 bar-2) }, ''

get('http://example.com/')
actual = ActiveRecord::Tagging.all.map { |tagging| [tagging.url, tagging.tag] }
assert_equal [%W(http://example.com/ foo-1), %W(http://example.com/ bar-2)], actual

actual = ActiveRecord::Tagging.all.map { |tagging| [tagging.url, tagging.tag] }
expected = [%W(http://example.com/ foo-1), %W(http://example.com/ bar-2), %W(http://example.com/bar bar-2)]
assert_equal expected.sort, actual.sort
end

test 'expands tags to urls for purge headers and deletes the tagging' do
Expand All @@ -41,5 +51,11 @@ def setup
create_tags('http://example.com/foo', tags)
assert_equal tags, store.taggings_by_tags(%w(foo-1)).map(&:tag)
end

test 'taggings_by_url_and_tags' do
create_taggings('/foo' => %w(tag-1 tag-2), '/bar' => %w(tag-1))
taggings = store.taggings_by_url_and_tags('/foo', %w(tag-1 tag-3))
assert_equal [['/foo', 'tag-1']], taggings.map { |tagging| [tagging.url, tagging.tag] }
end
end

3 changes: 1 addition & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
FileUtils.touch(log) unless File.exists?(log)

ActiveRecord::Base.logger = Logger.new(log)
ActiveRecord::LogSubscriber.attach_to(:active_record)
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Migration.verbose = false

Expand Down Expand Up @@ -56,4 +55,4 @@ def get(url)
def env_for(*args)
Rack::MockRequest.env_for(*args)
end
end
end

0 comments on commit 30cb0d2

Please sign in to comment.