summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Bajao <ebajao@gitlab.com>2019-06-05 13:50:37 +0800
committerPatrick Bajao <ebajao@gitlab.com>2019-06-05 14:36:54 +0800
commit56d52340da0f8f15179b83f1206544a2590c22ff (patch)
treef4a25dbe5d0c165e1368cc7f20ab955560ee048e
parent2eecfd8f9d111c6518930b818a16daea8263b37f (diff)
downloadgitlab-ce-56d52340da0f8f15179b83f1206544a2590c22ff.tar.gz
Use #cache_key of subject for generated redis key
This commit also includes some changes in specs to use `Class.new` approach.
-rw-r--r--lib/gitlab/markdown_cache/redis/store.rb2
-rw-r--r--spec/lib/gitlab/markdown_cache/active_record/extension_spec.rb26
-rw-r--r--spec/lib/gitlab/markdown_cache/redis/extension_spec.rb32
-rw-r--r--spec/lib/gitlab/markdown_cache/redis/store_spec.rb6
-rw-r--r--spec/models/concerns/cache_markdown_field_spec.rb4
5 files changed, 42 insertions, 28 deletions
diff --git a/lib/gitlab/markdown_cache/redis/store.rb b/lib/gitlab/markdown_cache/redis/store.rb
index a35eebc6a1b..2d4a89d9f1c 100644
--- a/lib/gitlab/markdown_cache/redis/store.rb
+++ b/lib/gitlab/markdown_cache/redis/store.rb
@@ -48,7 +48,7 @@ module Gitlab
"This class has no id to use for caching"
end
- "markdown_cache:#{@subject.class}:#{@subject.id}"
+ "markdown_cache:#{@subject.cache_key}"
end
end
end
diff --git a/spec/lib/gitlab/markdown_cache/active_record/extension_spec.rb b/spec/lib/gitlab/markdown_cache/active_record/extension_spec.rb
index 6700b53e790..18052b1991c 100644
--- a/spec/lib/gitlab/markdown_cache/active_record/extension_spec.rb
+++ b/spec/lib/gitlab/markdown_cache/active_record/extension_spec.rb
@@ -2,17 +2,19 @@
require 'spec_helper'
describe Gitlab::MarkdownCache::ActiveRecord::Extension do
- class ARThingWithMarkdownFields < ActiveRecord::Base
- self.table_name = 'issues'
- include CacheMarkdownField
- cache_markdown_field :title, whitelisted: true
- cache_markdown_field :description, pipeline: :single_line
+ let(:klass) do
+ Class.new(ActiveRecord::Base) do
+ self.table_name = 'issues'
+ include CacheMarkdownField
+ cache_markdown_field :title, whitelisted: true
+ cache_markdown_field :description, pipeline: :single_line
- attr_accessor :author, :project
+ attr_accessor :author, :project
+ end
end
let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }
- let(:thing) { ARThingWithMarkdownFields.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
+ let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
let(:markdown) { '`Foo`' }
let(:html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Foo</code></p>' }
@@ -21,7 +23,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
let(:updated_html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Bar</code></p>' }
context 'an unchanged markdown field' do
- let(:thing) { ARThingWithMarkdownFields.new(title: markdown) }
+ let(:thing) { klass.new(title: markdown) }
before do
thing.title = thing.title
@@ -35,7 +37,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end
context 'a changed markdown field' do
- let(:thing) { ARThingWithMarkdownFields.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
+ let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
before do
thing.title = updated_markdown
@@ -67,7 +69,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end
context 'a non-markdown field changed' do
- let(:thing) { ARThingWithMarkdownFields.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
+ let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }
before do
thing.state = 'closed'
@@ -81,7 +83,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end
context 'version is out of date' do
- let(:thing) { ARThingWithMarkdownFields.new(title: updated_markdown, title_html: html, cached_markdown_version: nil) }
+ let(:thing) { klass.new(title: updated_markdown, title_html: html, cached_markdown_version: nil) }
before do
thing.save
@@ -122,7 +124,7 @@ describe Gitlab::MarkdownCache::ActiveRecord::Extension do
end
describe '#cached_html_up_to_date?' do
- let(:thing) { ARThingWithMarkdownFields.create(title: updated_markdown, title_html: html, cached_markdown_version: nil) }
+ let(:thing) { klass.create(title: updated_markdown, title_html: html, cached_markdown_version: nil) }
subject { thing.cached_html_up_to_date?(:title) }
it 'returns false if markdown has been changed but html has not' do
diff --git a/spec/lib/gitlab/markdown_cache/redis/extension_spec.rb b/spec/lib/gitlab/markdown_cache/redis/extension_spec.rb
index d3d3cd6f03c..24cfb399cc3 100644
--- a/spec/lib/gitlab/markdown_cache/redis/extension_spec.rb
+++ b/spec/lib/gitlab/markdown_cache/redis/extension_spec.rb
@@ -2,30 +2,34 @@
require 'spec_helper'
describe Gitlab::MarkdownCache::Redis::Extension, :clean_gitlab_redis_cache do
- class ThingWithMarkdownFields
- include CacheMarkdownField
+ let(:klass) do
+ Class.new do
+ include CacheMarkdownField
- def initialize(title: nil, description: nil)
- @title, @description = title, description
- end
+ def initialize(title: nil, description: nil)
+ @title, @description = title, description
+ end
+
+ attr_reader :title, :description
- attr_reader :title, :description
+ cache_markdown_field :title, pipeline: :single_line
+ cache_markdown_field :description
- cache_markdown_field :title, pipeline: :single_line
- cache_markdown_field :description
+ def id
+ "test-markdown-cache"
+ end
- def id
- "test-markdown-cache"
+ def cache_key
+ "cache-key"
+ end
end
end
let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }
- let(:thing) { ThingWithMarkdownFields.new(title: "`Hello`", description: "`World`") }
- let(:expected_cache_key) { "markdown_cache:ThingWithMarkdownFields:test-markdown-cache" }
+ let(:thing) { klass.new(title: "`Hello`", description: "`World`") }
+ let(:expected_cache_key) { "markdown_cache:cache-key" }
it 'defines the html attributes' do
- thing = ThingWithMarkdownFields.new
-
expect(thing).to respond_to(:title_html, :description_html, :cached_markdown_version)
end
diff --git a/spec/lib/gitlab/markdown_cache/redis/store_spec.rb b/spec/lib/gitlab/markdown_cache/redis/store_spec.rb
index 59c038cfb2f..e7701cf1306 100644
--- a/spec/lib/gitlab/markdown_cache/redis/store_spec.rb
+++ b/spec/lib/gitlab/markdown_cache/redis/store_spec.rb
@@ -16,10 +16,14 @@ describe Gitlab::MarkdownCache::Redis::Store, :clean_gitlab_redis_cache do
def id
'test-redisbacked-store'
end
+
+ def cache_key
+ "cache-key"
+ end
end
end
let(:storable) { storable_class.new }
- let(:cache_key) { "markdown_cache:#{storable_class}:#{storable.id}" }
+ let(:cache_key) { "markdown_cache:#{storable.cache_key}" }
subject(:store) { described_class.new(storable) }
diff --git a/spec/models/concerns/cache_markdown_field_spec.rb b/spec/models/concerns/cache_markdown_field_spec.rb
index 52f8b052ad4..9e6b5d56805 100644
--- a/spec/models/concerns/cache_markdown_field_spec.rb
+++ b/spec/models/concerns/cache_markdown_field_spec.rb
@@ -30,6 +30,10 @@ describe CacheMarkdownField, :clean_gitlab_redis_cache do
def id
"test-markdown-cache"
end
+
+ def cache_key
+ "cache-key"
+ end
end
end