redis_snippets 1.0.2 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1157b175b0ce0c3c72be376383f9da2d522999f69304c093cd3ddbc22eb0d25d
4
- data.tar.gz: 7673c2ffe8fd7b0134847e1de912dd94aadf900a267c937625e3ace993f106ee
3
+ metadata.gz: a6e23e82090d87175ad955c952631f69c82c81f8a44bb31425e0d8fd23cd3efb
4
+ data.tar.gz: 7a10f98d066f22d13fc396738f5caacf784864f0ecd27bc64768c7b3e5c31592
5
5
  SHA512:
6
- metadata.gz: fc847d478f9c40aef9fe2ecb067eee24c700d23b1053bcb09280db46c5025b09a94e6363ecdb2f0d70e567a606b75cb53d0771e16844b3a934b6eb815c4bb44f
7
- data.tar.gz: fbfc1e84fc0c9794221880574abde61e7d3e2c11720c0c92062be5592c0f15d599f428b556abe777156fc2662790b4386c60cd553720d9567a3664337761602d
6
+ metadata.gz: e07f026b183cad598364729618af9fe667e3edadbf3797897d2f19adf05738f3e74373c342085230ec28fa549d892d0a54010779ff86e2c8a8861fadc84444d7
7
+ data.tar.gz: 90c0e1f7b2245e79afdb0acf63e7937455651177c547805eec6f578bec1c8791958f6f3d61a96ff806798074148c349ee622016285283925a1bb8bdf9eca38b4
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source "https://rubygems.org"
2
2
  gemspec
3
3
 
4
4
  group :test, :development do
5
- gem "rails", ">= 6.0"
5
+ gem "rails", "~> 6"
6
6
  gem "rspec-rails", ">= 3.9.0"
7
7
  end
8
8
 
@@ -8,7 +8,9 @@ module RedisSnippets
8
8
  end
9
9
 
10
10
  def snippet(key, classes = nil)
11
- SnippetPresenter.new(view: self, key: key, classes: classes).call
11
+ snippet = SnippetFinderService.new(key: key).call
12
+ return unless snippet
13
+ SnippetPresenter.new(view: self, key: key, snippet: snippet, classes: classes).call
12
14
  end
13
15
  end
14
16
  end
@@ -1,14 +1,10 @@
1
-
2
1
  class SnippetPresenter
3
2
  include RedisSnippets::Util
4
3
 
5
- SECTION_DELIMITER = "[section]"
6
-
7
- delegate :random_snippet, to: "self.class"
8
-
9
- def initialize(view:, key:, classes: nil)
4
+ def initialize(view:, key:, snippet:, classes: nil)
10
5
  @view = view
11
6
  @key = key
7
+ @snippet = snippet
12
8
  @classes = classes
13
9
  end
14
10
 
@@ -20,16 +16,9 @@ class SnippetPresenter
20
16
  protected
21
17
 
22
18
  def prepare_snippet
23
- @snippet = ""
24
-
25
- return unless content = SnippetStoreService.send(snippet_key(@key))
19
+ return unless transform_class&.transforms?(key: @key)
26
20
 
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
32
- end
21
+ @snippet = transform_class.new(content: @snippet, key: @key).transform
33
22
  end
34
23
 
35
24
  def snippet_class_list
@@ -42,11 +31,9 @@ class SnippetPresenter
42
31
 
43
32
  def render
44
33
  # If snippet is empty we avoid wrapping it in the div.
45
- if @snippet.blank?
46
- ""
47
- else
48
- @view.content_tag(:div, @snippet.html_safe, class: snippet_class_list)
49
- end
34
+ return "" if @snippet.blank?
35
+
36
+ @view.content_tag(:div, @snippet.html_safe, class: snippet_class_list)
50
37
  end
51
38
 
52
39
  def transform_class
@@ -57,11 +44,4 @@ class SnippetPresenter
57
44
  RedisSnippets::Engine.config.redis_snippets[:transform] and RedisSnippets::Engine.config.redis_snippets[:transform].constantize
58
45
  end
59
46
  end
60
-
61
- class << self
62
- def random_snippet(content)
63
- snippets = content.split("#{SECTION_DELIMITER}").map { |section| section.gsub(/^\n/, "") }
64
- snippets[rand(snippets.length)]
65
- end
66
- end
67
47
  end
@@ -0,0 +1,22 @@
1
+ class SnippetFinderService
2
+ include RedisSnippets::Util
3
+
4
+ SECTION_DELIMITER = "[section]"
5
+
6
+ def initialize(key:)
7
+ @key = key
8
+ end
9
+
10
+ def call
11
+ self.class.random_snippet(SnippetStoreService.send(snippet_key(@key)))
12
+ end
13
+
14
+ protected
15
+
16
+ class << self
17
+ def random_snippet(content)
18
+ snippets = content.split("#{SECTION_DELIMITER}").map { |section| section.gsub(/^\n/, "") }
19
+ snippets[rand(snippets.length)]
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module RedisSnippets
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.4'
3
3
  end
@@ -1 +1 @@
1
- 2.6.5
1
+ 3.1.2
@@ -1,7 +1,4 @@
1
- # The test environment is used exclusively to run your application's
2
- # test suite. You never need to work with it otherwise. Remember that
3
- # your test database is "scratch space" for the test suite and is wiped
4
- # and recreated between test runs. Don't rely on the data there!
1
+ require "active_support/core_ext/integer/time"
5
2
 
6
3
  Rails.application.configure do
7
4
  # Settings specified here will take precedence over those in config/application.rb.
@@ -0,0 +1,57 @@
1
+ require 'rails_helper'
2
+
3
+ describe RedisSnippets::SnippetsHelper, type: :helper do
4
+ let(:code) { "<p>Buy this from Amazon.</p>" }
5
+ let(:multiple_adverts) { <<-HEREDOC
6
+ <p>Buy this from Amazon.</p>
7
+ [section]
8
+ <p>Buy this from Apple.</p>
9
+ HEREDOC
10
+ }
11
+ let(:view) { ActionController::Base.new.view_context }
12
+
13
+ describe "#snippet_has_content?" do
14
+ before do
15
+ SnippetStoreService.update(snippet_key(:advert_header), code)
16
+ end
17
+
18
+ it "returns true if there's content in the snippet" do
19
+ expect(snippet_has_content?(:advert_header)).to be true
20
+ end
21
+
22
+ it "returns false if there's no content in the snippet" do
23
+ expect(snippet_has_content?(:advert_footer)).to be false
24
+ end
25
+ end
26
+
27
+ describe "#snippet" do
28
+ context "with a single snippet" do
29
+ before do
30
+ SnippetStoreService.update(snippet_key(:advert_header), code)
31
+ end
32
+
33
+ it "adds the snippet key as the class" do
34
+ expect(snippet(:advert_header)).to eq("<div class=\"snippet advert_header\"><p>Buy this from Amazon.</p></div>")
35
+ end
36
+
37
+ it "adds more classes from a string" do
38
+ expect(snippet(:advert_header, "advert-responsive")).to eq("<div class=\"snippet advert_header advert-responsive\"><p>Buy this from Amazon.</p></div>")
39
+ end
40
+
41
+ it "adds more classes from an array" do
42
+ expect(snippet(:advert_header, ["advert-responsive", "p-4"])).to eq("<div class=\"snippet advert_header advert-responsive p-4\"><p>Buy this from Amazon.</p></div>")
43
+ end
44
+ end
45
+
46
+ context "with multiple snippets" do
47
+ before do
48
+ SnippetStoreService.update(snippet_key(:advert_header), multiple_adverts)
49
+ end
50
+
51
+ it "adds the snippet key as the class" do
52
+ expect(SnippetFinderService).to receive(:rand).and_return(1)
53
+ expect(snippet(:advert_header)).to eq("<div class=\"snippet advert_header\"><p>Buy this from Apple.</p>\n</div>")
54
+ end
55
+ end
56
+ end
57
+ end
@@ -10,9 +10,9 @@ HEREDOC
10
10
  }
11
11
  let(:view) { ActionController::Base.new.view_context }
12
12
 
13
- subject(:presenter) { described_class.new(view: view, key: :advert_header) }
14
- subject(:presenter_with_string_class) { described_class.new(view: view, key: :advert_header, classes: "advert-responsive") }
15
- subject(:presenter_with_array_classes) { described_class.new(view: view, key: :advert_header, classes: ["advert-responsive", "p-4"]) }
13
+ subject(:presenter) { described_class.new(view: view, key: :advert_header, snippet: code) }
14
+ subject(:presenter_with_string_class) { described_class.new(view: view, key: :advert_header, snippet: code, classes: "advert-responsive") }
15
+ subject(:presenter_with_array_classes) { described_class.new(view: view, key: :advert_header, snippet: code, classes: ["advert-responsive", "p-4"]) }
16
16
 
17
17
  describe "#call" do
18
18
  before do
@@ -37,17 +37,4 @@ HEREDOC
37
37
  end
38
38
  end
39
39
  end
40
-
41
- describe ".random_snippet" do
42
- before do
43
- SnippetStoreService.update(snippet_key(:advert_header), multiple_adverts)
44
- end
45
-
46
- it "returns one of the sections" do
47
- allow(SnippetPresenter).to receive(:rand).and_return(0)
48
- expect(described_class.random_snippet(multiple_adverts)).to eq("<p>Buy this from Amazon.</p>\n")
49
- allow(SnippetPresenter).to receive(:rand).and_return(1)
50
- expect(described_class.random_snippet(multiple_adverts)).to eq("<p>Buy this from Apple.</p>\n")
51
- end
52
- end
53
40
  end
@@ -23,8 +23,8 @@ describe SnippetPresenter do
23
23
  let(:code) { "<p>Buy this from Amazon.</p>" }
24
24
  let(:view) { ActionController::Base.new.view_context }
25
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) }
26
+ subject(:header_finder) { described_class.new(view: view, key: :advert_header, snippet: code) }
27
+ subject(:footer_finder) { described_class.new(view: view, key: :advert_footer, snippet: code) }
28
28
 
29
29
  describe "#call" do
30
30
  before do
@@ -34,11 +34,11 @@ describe SnippetPresenter do
34
34
  end
35
35
 
36
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>")
37
+ expect(header_finder.call).to eq("<div class=\"snippet advert_header\"><p>Buy this from Etsy.</p></div>")
38
38
  end
39
39
 
40
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>")
41
+ expect(footer_finder.call).to eq("<div class=\"snippet advert_footer\"><p>Buy this from Amazon.</p></div>")
42
42
  end
43
43
  end
44
44
  end
@@ -0,0 +1,35 @@
1
+ require 'rails_helper'
2
+
3
+ describe SnippetFinderService do
4
+ let(:code) { "<p>Buy this from Amazon.</p>" }
5
+ let(:multiple_adverts) { <<-HEREDOC
6
+ <p>Buy this from Amazon.</p>
7
+ [section]
8
+ <p>Buy this from Apple.</p>
9
+ HEREDOC
10
+ }
11
+ let(:view) { ActionController::Base.new.view_context }
12
+
13
+ subject { described_class.new(key: :advert_header).call }
14
+
15
+ describe "#call" do
16
+ before do
17
+ SnippetStoreService.update(snippet_key(:advert_header), code)
18
+ end
19
+
20
+ it "finds the content" do
21
+ expect(subject).to eq(code)
22
+ end
23
+ end
24
+
25
+ describe ".random_snippet" do
26
+ before do
27
+ SnippetStoreService.update(snippet_key(:advert_header), multiple_adverts)
28
+ end
29
+
30
+ it "returns one of the sections" do
31
+ expect(described_class).to receive(:rand).and_return(0)
32
+ expect(described_class.random_snippet(multiple_adverts)).to eq("<p>Buy this from Amazon.</p>\n")
33
+ end
34
+ end
35
+ end
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.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Moen Wulffeld
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-17 00:00:00.000000000 Z
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,6 +97,7 @@ files:
97
97
  - app/controllers/redis_snippets/snippets_controller.rb
98
98
  - app/helpers/redis_snippets/snippets_helper.rb
99
99
  - app/presenters/snippet_presenter.rb
100
+ - app/services/snippet_finder_service.rb
100
101
  - app/services/snippet_store_service.rb
101
102
  - app/views/redis_snippets/snippets/show.html.erb
102
103
  - config/routes.rb
@@ -106,10 +107,6 @@ files:
106
107
  - lib/redis_snippets/util.rb
107
108
  - lib/redis_snippets/version.rb
108
109
  - redis_snippets.gemspec
109
- - spec/app/helpers/redis_snippets/snippets_helper_spec.rb
110
- - spec/app/presenters/snippet_presenter_spec.rb
111
- - spec/app/presenters/snippet_presenter_transform_spec.rb
112
- - spec/app/services/snippet_store_service_spec.rb
113
110
  - spec/dummy/.ruby-version
114
111
  - spec/dummy/Rakefile
115
112
  - spec/dummy/app/assets/config/manifest.js
@@ -141,14 +138,19 @@ files:
141
138
  - spec/dummy/log/.keep
142
139
  - spec/dummy/tmp/.keep
143
140
  - spec/dummy/tmp/development_secret.txt
141
+ - spec/helpers/redis_snippets/snippets_helper_spec.rb
142
+ - spec/presenters/snippet_presenter_spec.rb
143
+ - spec/presenters/snippet_presenter_transform_spec.rb
144
144
  - spec/rails_helper.rb
145
+ - spec/services/snippet_finder_service_spec.rb
146
+ - spec/services/snippet_store_service_spec.rb
145
147
  - spec/spec_helper.rb
146
148
  - spec/support/simplecov_setup.rb
147
149
  homepage: https://github.com/wulffeld/redis_snippets
148
150
  licenses:
149
151
  - MIT
150
152
  metadata: {}
151
- post_install_message:
153
+ post_install_message:
152
154
  rdoc_options: []
153
155
  require_paths:
154
156
  - lib
@@ -163,15 +165,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  - !ruby/object:Gem::Version
164
166
  version: '0'
165
167
  requirements: []
166
- rubygems_version: 3.1.2
167
- signing_key:
168
+ rubygems_version: 3.3.7
169
+ signing_key:
168
170
  specification_version: 4
169
171
  summary: Storing snippets of HTML, text, etc. in Redis for use in views.
170
172
  test_files:
171
- - spec/app/helpers/redis_snippets/snippets_helper_spec.rb
172
- - spec/app/presenters/snippet_presenter_spec.rb
173
- - spec/app/presenters/snippet_presenter_transform_spec.rb
174
- - spec/app/services/snippet_store_service_spec.rb
175
173
  - spec/dummy/.ruby-version
176
174
  - spec/dummy/Rakefile
177
175
  - spec/dummy/app/assets/config/manifest.js
@@ -203,6 +201,11 @@ test_files:
203
201
  - spec/dummy/log/.keep
204
202
  - spec/dummy/tmp/.keep
205
203
  - spec/dummy/tmp/development_secret.txt
204
+ - spec/helpers/redis_snippets/snippets_helper_spec.rb
205
+ - spec/presenters/snippet_presenter_spec.rb
206
+ - spec/presenters/snippet_presenter_transform_spec.rb
206
207
  - spec/rails_helper.rb
208
+ - spec/services/snippet_finder_service_spec.rb
209
+ - spec/services/snippet_store_service_spec.rb
207
210
  - spec/spec_helper.rb
208
211
  - spec/support/simplecov_setup.rb
@@ -1,38 +0,0 @@
1
- require 'rails_helper'
2
-
3
- describe RedisSnippets::SnippetsHelper, type: :helper do
4
- let(:code) { "<p>Buy this from Amazon.</p>" }
5
- let(:view) { ActionController::Base.new.view_context }
6
-
7
- describe "#snippet_has_content?" do
8
- before do
9
- SnippetStoreService.update(snippet_key(:advert_header), code)
10
- end
11
-
12
- it "returns true if there's content in the snippet" do
13
- expect(snippet_has_content?(:advert_header)).to be true
14
- end
15
-
16
- it "returns false if there's no content in the snippet" do
17
- expect(snippet_has_content?(:advert_footer)).to be false
18
- end
19
- end
20
-
21
- describe "#snippet" do
22
- before do
23
- SnippetStoreService.update(snippet_key(:advert_header), code)
24
- end
25
-
26
- it "adds the snippet key as the class" do
27
- expect(snippet(:advert_header)).to eq("<div class=\"snippet advert_header\"><p>Buy this from Amazon.</p></div>")
28
- end
29
-
30
- it "adds more classes from a string" do
31
- expect(snippet(:advert_header, "advert-responsive")).to eq("<div class=\"snippet advert_header advert-responsive\"><p>Buy this from Amazon.</p></div>")
32
- end
33
-
34
- it "adds more classes from an array" do
35
- expect(snippet(:advert_header, ["advert-responsive", "p-4"])).to eq("<div class=\"snippet advert_header advert-responsive p-4\"><p>Buy this from Amazon.</p></div>")
36
- end
37
- end
38
- end