redis_snippets 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -2
- data/app/presenters/snippet_presenter.rb +16 -3
- data/lib/redis_snippets/version.rb +1 -1
- data/spec/app/presenters/snippet_presenter_transform_spec.rb +45 -0
- data/spec/rails_helper.rb +2 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 717ab9484b932c8a8e7fa6aa05e58ef380f3ddd9468068903ea297b4590397eb
|
4
|
+
data.tar.gz: badd68f7355ebe589110d770e45117f03784f5d65ef2d678eac615db7ce3586d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01266e1500a427aa626d5d3bfc0d9d5ec30d21f0454add4dec24c1a3cc1d0ac660cf7864a20fc91591f795b1b946244689aa757cccde50d49f464dddee14c354
|
7
|
+
data.tar.gz: 72df68e6b25ad50e25b26679086d5fe0b2802168a167046b2eec92ed99b878e265b09ffb29f0bb5a05eb55fa6298a0d5eed862ab6e7b09f58a9246be77dec550
|
data/README.md
CHANGED
@@ -64,13 +64,55 @@ delivery or perhaps A/B testing 🤷♂.
|
|
64
64
|
|
65
65
|
## Multi Site
|
66
66
|
|
67
|
-
If you're using one app to serve multiple sites a little more configuration is
|
67
|
+
If you're using one app to serve multiple sites a little more configuration is
|
68
|
+
necessary.
|
68
69
|
|
69
70
|
``` ruby
|
70
71
|
Rails.application.config.redis_snippets = {
|
71
72
|
connection: ::Redis::Namespace.new("my_namespace", redis: ::Redis.new),
|
72
73
|
multi_site: true,
|
73
|
-
keys: [:key1, :key2]
|
74
|
+
keys: [:key1, :key2],
|
75
|
+
transform: "MySnippetTransform"
|
76
|
+
}
|
77
|
+
```
|
78
|
+
|
79
|
+
## Transforming Snippets
|
80
|
+
|
81
|
+
Sometimes you might want to transform snippets. This happens after
|
82
|
+
randomization so you get the actual snippet content to be rendered passed to
|
83
|
+
your transform class.
|
84
|
+
|
85
|
+
The transform class must respond to .transforms? and #transform. Example:
|
86
|
+
|
87
|
+
``` ruby
|
88
|
+
class MySnippetTransform
|
89
|
+
def initialize(content:, key:)
|
90
|
+
@content = content
|
91
|
+
@key = key
|
92
|
+
end
|
93
|
+
|
94
|
+
# Must return content.
|
95
|
+
def transform
|
96
|
+
@content.gsub("Amazon", "Etsy")
|
97
|
+
end
|
98
|
+
|
99
|
+
class << self
|
100
|
+
def transforms?(key:)
|
101
|
+
key == :advert_header
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
I personally use this to insert special CSS code for AdSense. This saves me
|
108
|
+
from having to do this manually whenever I update it or add it.
|
109
|
+
|
110
|
+
``` ruby
|
111
|
+
Rails.application.config.redis_snippets = {
|
112
|
+
connection: ::Redis::Namespace.new("my_namespace", redis: ::Redis.new),
|
113
|
+
multi_site: true,
|
114
|
+
keys: [:key1, :key2],
|
115
|
+
transform: "MySnippetTransform"
|
74
116
|
}
|
75
117
|
```
|
76
118
|
|
@@ -22,9 +22,13 @@ class SnippetPresenter
|
|
22
22
|
def prepare_snippet
|
23
23
|
@snippet = ""
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
return unless content = SnippetStoreService.send(snippet_key(@key))
|
26
|
+
|
27
|
+
snippets = content.split("#{SECTION_DELIMITER}")
|
28
|
+
@snippet = random_snippet(content)
|
29
|
+
|
30
|
+
if transform_class && transform_class.transforms?(key: @key)
|
31
|
+
@snippet = transform_class.new(content: @snippet, key: @key).transform
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
@@ -45,6 +49,15 @@ class SnippetPresenter
|
|
45
49
|
end
|
46
50
|
end
|
47
51
|
|
52
|
+
def transform_class
|
53
|
+
@klass ||=
|
54
|
+
if RedisSnippets::Engine.config.redis_snippets[:transform].is_a?(Proc)
|
55
|
+
klass = RedisSnippets::Engine.config.redis_snippets[:transform].call and klass.constantize
|
56
|
+
else
|
57
|
+
RedisSnippets::Engine.config.redis_snippets[:transform] and RedisSnippets::Engine.config.redis_snippets[:transform].constantize
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
48
61
|
class << self
|
49
62
|
def random_snippet(content)
|
50
63
|
snippets = content.split("#{SECTION_DELIMITER}").map { |section| section.gsub(/^\n/, "") }
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
class Transformer
|
4
|
+
def initialize(content:, key:)
|
5
|
+
@content = content
|
6
|
+
@key = key
|
7
|
+
end
|
8
|
+
|
9
|
+
# Must return content.
|
10
|
+
def transform
|
11
|
+
@content.gsub("Amazon", "Etsy")
|
12
|
+
end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def transforms?(key:)
|
16
|
+
key == :advert_header
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe SnippetPresenter do
|
22
|
+
describe "with a transform class" do
|
23
|
+
let(:code) { "<p>Buy this from Amazon.</p>" }
|
24
|
+
let(:view) { ActionController::Base.new.view_context }
|
25
|
+
|
26
|
+
subject(:header_presenter) { described_class.new(view: view, key: :advert_header) }
|
27
|
+
subject(:footer_presenter) { described_class.new(view: view, key: :advert_footer) }
|
28
|
+
|
29
|
+
describe "#call" do
|
30
|
+
before do
|
31
|
+
ENV["TRANSFORM"] = "1"
|
32
|
+
SnippetStoreService.update(snippet_key(:advert_header), code)
|
33
|
+
SnippetStoreService.update(snippet_key(:advert_footer), code)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "transforms the content" do
|
37
|
+
expect(header_presenter.call).to eq("<div class=\"snippet advert_header\"><p>Buy this from Etsy.</p></div>")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not transform the content if key is not allowed" do
|
41
|
+
expect(footer_presenter.call).to eq("<div class=\"snippet advert_footer\"><p>Buy this from Amazon.</p></div>")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/rails_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_snippets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Moen Wulffeld
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- redis_snippets.gemspec
|
109
109
|
- spec/app/helpers/redis_snippets/snippets_helper_spec.rb
|
110
110
|
- spec/app/presenters/snippet_presenter_spec.rb
|
111
|
+
- spec/app/presenters/snippet_presenter_transform_spec.rb
|
111
112
|
- spec/app/services/snippet_store_service_spec.rb
|
112
113
|
- spec/dummy/.ruby-version
|
113
114
|
- spec/dummy/Rakefile
|
@@ -162,13 +163,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
163
|
- !ruby/object:Gem::Version
|
163
164
|
version: '0'
|
164
165
|
requirements: []
|
165
|
-
rubygems_version: 3.0.
|
166
|
+
rubygems_version: 3.0.3
|
166
167
|
signing_key:
|
167
168
|
specification_version: 4
|
168
169
|
summary: Storing snippets of HTML, text, etc. in Redis for use in views.
|
169
170
|
test_files:
|
170
171
|
- spec/app/helpers/redis_snippets/snippets_helper_spec.rb
|
171
172
|
- spec/app/presenters/snippet_presenter_spec.rb
|
173
|
+
- spec/app/presenters/snippet_presenter_transform_spec.rb
|
172
174
|
- spec/app/services/snippet_store_service_spec.rb
|
173
175
|
- spec/dummy/.ruby-version
|
174
176
|
- spec/dummy/Rakefile
|