redis_snippets 0.0.10 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +4 -2
  3. data/.rspec +2 -0
  4. data/.travis.yml +19 -0
  5. data/Gemfile +12 -0
  6. data/README.md +84 -18
  7. data/Rakefile +5 -0
  8. data/app/controllers/redis_snippets/snippets_controller.rb +5 -6
  9. data/app/helpers/redis_snippets/snippets_helper.rb +5 -23
  10. data/app/presenters/snippet_presenter.rb +67 -0
  11. data/app/{models/redis_snippets/snippets.rb → services/snippet_store_service.rb} +9 -1
  12. data/app/views/redis_snippets/snippets/show.html.erb +11 -15
  13. data/config/routes.rb +1 -1
  14. data/lib/redis_snippets/engine.rb +3 -1
  15. data/lib/redis_snippets/redis.rb +10 -5
  16. data/lib/redis_snippets/{help.rb → util.rb} +2 -2
  17. data/lib/redis_snippets/version.rb +1 -1
  18. data/lib/redis_snippets.rb +4 -2
  19. data/redis_snippets.gemspec +5 -4
  20. data/spec/app/helpers/redis_snippets/snippets_helper_spec.rb +38 -0
  21. data/spec/app/presenters/snippet_presenter_spec.rb +53 -0
  22. data/spec/app/presenters/snippet_presenter_transform_spec.rb +45 -0
  23. data/spec/app/services/snippet_store_service_spec.rb +44 -0
  24. data/spec/dummy/.ruby-version +1 -0
  25. data/spec/dummy/Rakefile +6 -0
  26. data/spec/dummy/app/assets/config/manifest.js +3 -0
  27. data/spec/dummy/app/assets/images/.keep +0 -0
  28. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  29. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  30. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  31. data/spec/dummy/bin/rails +4 -0
  32. data/spec/dummy/bin/rake +4 -0
  33. data/spec/dummy/bin/setup +33 -0
  34. data/spec/dummy/config/application.rb +29 -0
  35. data/spec/dummy/config/boot.rb +5 -0
  36. data/spec/dummy/config/environment.rb +5 -0
  37. data/spec/dummy/config/environments/development.rb +62 -0
  38. data/spec/dummy/config/environments/test.rb +48 -0
  39. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  40. data/spec/dummy/config/initializers/assets.rb +12 -0
  41. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/spec/dummy/config/puma.rb +38 -0
  49. data/spec/dummy/config/routes.rb +3 -0
  50. data/spec/dummy/config.ru +5 -0
  51. data/spec/dummy/lib/assets/.keep +0 -0
  52. data/spec/dummy/log/.keep +0 -0
  53. data/spec/dummy/tmp/.keep +0 -0
  54. data/spec/dummy/tmp/development_secret.txt +1 -0
  55. data/spec/rails_helper.rb +54 -0
  56. data/spec/spec_helper.rb +13 -0
  57. data/spec/support/simplecov_setup.rb +5 -0
  58. metadata +105 -11
@@ -1,3 +1,3 @@
1
1
  module RedisSnippets
2
- VERSION = '0.0.10'
2
+ VERSION = '1.0.2'
3
3
  end
@@ -1,7 +1,9 @@
1
+ require 'rails'
1
2
  require 'redis'
2
3
  require 'redis/namespace'
3
4
 
4
5
  require 'redis_snippets/engine'
5
-
6
6
  require 'redis_snippets/redis'
7
- require 'redis_snippets/help'
7
+ require 'redis_snippets/util'
8
+
9
+ class UndefinedSnippetsKey < Exception; end
@@ -6,10 +6,10 @@ Gem::Specification.new do |spec|
6
6
  spec.name = "redis_snippets"
7
7
  spec.version = RedisSnippets::VERSION
8
8
  spec.authors = ["Martin Moen Wulffeld"]
9
- spec.email = ["martin@wulffeld.org"]
10
- spec.description = %q{Storing snippets of whatever in Redis.}
11
- spec.summary = %q{Storing snippets of whatever in Redis.}
12
- spec.homepage = "http://www.wulffeld.org/"
9
+ spec.email = ["wulffeld@gmail.com"]
10
+ spec.summary = %q{Storing snippets of HTML, text, etc. in Redis for use in views.}
11
+ spec.description = %q{A Ruby on Rails gem that facilitates fast retrieval of snippets of code or information for views.}
12
+ spec.homepage = "https://github.com/wulffeld/redis_snippets"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files`.split($/)
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "bundler", ">= 1.3.2"
21
21
  spec.add_development_dependency "rake"
22
22
 
23
+ spec.add_dependency "rails", ">= 5"
23
24
  spec.add_dependency "redis", ">= 2.0.0"
24
25
  spec.add_dependency "redis-namespace", ">= 1.2.1"
25
26
  end
@@ -0,0 +1,38 @@
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
@@ -0,0 +1,53 @@
1
+ require 'rails_helper'
2
+
3
+ describe SnippetPresenter 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(: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"]) }
16
+
17
+ describe "#call" do
18
+ before do
19
+ SnippetStoreService.update(snippet_key(:advert_header), code)
20
+ end
21
+
22
+ it "renders the content" do
23
+ expect(presenter.call).to eq("<div class=\"snippet advert_header\">#{code}</div>")
24
+ end
25
+
26
+ describe "classes argument" do
27
+ it "adds the snippet key as the class" do
28
+ expect(presenter.call).to eq("<div class=\"snippet advert_header\"><p>Buy this from Amazon.</p></div>")
29
+ end
30
+
31
+ it "adds more classes from a string" do
32
+ expect(presenter_with_string_class.call).to eq("<div class=\"snippet advert_header advert-responsive\"><p>Buy this from Amazon.</p></div>")
33
+ end
34
+
35
+ it "adds more classes from an array" do
36
+ expect(presenter_with_array_classes.call).to eq("<div class=\"snippet advert_header advert-responsive p-4\"><p>Buy this from Amazon.</p></div>")
37
+ end
38
+ end
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
+ end
@@ -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
@@ -0,0 +1,44 @@
1
+ require 'rails_helper'
2
+
3
+ describe SnippetStoreService do
4
+ let(:defined_key) { SnippetStoreService.advert_header }
5
+
6
+ let(:code) { "<p>Hello world.</p>" }
7
+
8
+ describe "#update" do
9
+ it "updates the content of a snippet" do
10
+ SnippetStoreService.update(snippet_key(:advert_header), code)
11
+
12
+ expect(SnippetStoreService.advert_header).to eq(code)
13
+ end
14
+
15
+ it "doesn't allowed undefined keys" do
16
+ expect { SnippetStoreService.update(:advert_sidebar, code) }.to raise_error(UndefinedSnippetsKey)
17
+ end
18
+ end
19
+
20
+ describe "#del" do
21
+ it "removes the content of a snippet" do
22
+ SnippetStoreService.update(snippet_key(:advert_header), code)
23
+ SnippetStoreService.del(snippet_key(:advert_header))
24
+
25
+ expect(SnippetStoreService.advert_header).to eq(nil)
26
+ end
27
+ end
28
+
29
+ describe "#method_missing" do
30
+ it "delegates missing methods to the store" do
31
+ SnippetStoreService.update(snippet_key(:advert_header), code)
32
+
33
+ expect(SnippetStoreService.advert_header).to eq(code)
34
+ end
35
+
36
+ it "returns nil for defined keys that has not been set" do
37
+ expect(defined_key).to eq(nil)
38
+ end
39
+
40
+ it "returns nil for undefined keys" do
41
+ expect(SnippetStoreService.advert_sidebar).to eq(nil)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../stylesheets .css
3
+ //= link manifest.js
File without changes
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag 'application', media: 'all' %>
9
+ </head>
10
+
11
+ <body>
12
+ <%= yield %>
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../config/application', __dir__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = File.expand_path('..', __dir__)
6
+
7
+ def system!(*args)
8
+ system(*args) || abort("\n== Command #{args} failed ==")
9
+ end
10
+
11
+ FileUtils.chdir APP_ROOT do
12
+ # This script is a way to setup or update your development environment automatically.
13
+ # This script is idempotent, so that you can run it at anytime and get an expectable outcome.
14
+ # Add necessary setup steps to this file.
15
+
16
+ puts '== Installing dependencies =='
17
+ system! 'gem install bundler --conservative'
18
+ system('bundle check') || system!('bundle install')
19
+
20
+ # puts "\n== Copying sample files =="
21
+ # unless File.exist?('config/database.yml')
22
+ # FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
23
+ # end
24
+
25
+ puts "\n== Preparing database =="
26
+ system! 'bin/rails db:prepare'
27
+
28
+ puts "\n== Removing old logs and tempfiles =="
29
+ system! 'bin/rails log:clear tmp:clear'
30
+
31
+ puts "\n== Restarting application server =="
32
+ system! 'bin/rails restart'
33
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'boot'
2
+
3
+ require "rails"
4
+ # Pick the frameworks you want:
5
+ require "active_model/railtie"
6
+ # require "active_job/railtie"
7
+ # require "active_record/railtie"
8
+ # require "active_storage/engine"
9
+ require "action_controller/railtie"
10
+ # require "action_mailer/railtie"
11
+ require "action_view/railtie"
12
+ require "action_cable/engine"
13
+ require "sprockets/railtie"
14
+ # require "rails/test_unit/railtie"
15
+
16
+ Bundler.require(*Rails.groups)
17
+ require "redis_snippets"
18
+
19
+ module Dummy
20
+ class Application < Rails::Application
21
+ # Initialize configuration defaults for originally generated Rails version.
22
+ config.load_defaults 6.0
23
+
24
+ # Settings in config/environments/* take precedence over those specified here.
25
+ # Application configuration can go into files in config/initializers
26
+ # -- all .rb files in that directory are automatically loaded after loading
27
+ # the framework and any gems in your application.
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../lib', __dir__)
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require_relative 'application'
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!
@@ -0,0 +1,62 @@
1
+ Rails.application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Do not eager load code on boot.
10
+ config.eager_load = false
11
+
12
+ # Show full error reports.
13
+ config.consider_all_requests_local = true
14
+
15
+ # Enable/disable caching. By default caching is disabled.
16
+ # Run rails dev:cache to toggle caching.
17
+ if Rails.root.join('tmp', 'caching-dev.txt').exist?
18
+ config.action_controller.perform_caching = true
19
+ config.action_controller.enable_fragment_cache_logging = true
20
+
21
+ config.cache_store = :memory_store
22
+ config.public_file_server.headers = {
23
+ 'Cache-Control' => "public, max-age=#{2.days.to_i}"
24
+ }
25
+ else
26
+ config.action_controller.perform_caching = false
27
+
28
+ config.cache_store = :null_store
29
+ end
30
+
31
+ # Store uploaded files on the local file system (see config/storage.yml for options).
32
+ config.active_storage.service = :local
33
+
34
+ # Don't care if the mailer can't send.
35
+ config.action_mailer.raise_delivery_errors = false
36
+
37
+ config.action_mailer.perform_caching = false
38
+
39
+ # Print deprecation notices to the Rails logger.
40
+ config.active_support.deprecation = :log
41
+
42
+ # Raise an error on page load if there are pending migrations.
43
+ config.active_record.migration_error = :page_load
44
+
45
+ # Highlight code that triggered database queries in logs.
46
+ config.active_record.verbose_query_logs = true
47
+
48
+ # Debug mode disables concatenation and preprocessing of assets.
49
+ # This option may cause significant delays in view rendering with a large
50
+ # number of complex assets.
51
+ config.assets.debug = true
52
+
53
+ # Suppress logger output for asset requests.
54
+ config.assets.quiet = true
55
+
56
+ # Raises error for missing translations.
57
+ # config.action_view.raise_on_missing_translations = true
58
+
59
+ # Use an evented file watcher to asynchronously detect changes in source code,
60
+ # routes, locales, etc. This feature depends on the listen gem.
61
+ # config.file_watcher = ActiveSupport::EventedFileUpdateChecker
62
+ end
@@ -0,0 +1,48 @@
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!
5
+
6
+ Rails.application.configure do
7
+ # Settings specified here will take precedence over those in config/application.rb.
8
+
9
+ config.cache_classes = false
10
+
11
+ # Do not eager load code on boot. This avoids loading your whole application
12
+ # just for the purpose of running a single test. If you are using a tool that
13
+ # preloads Rails for running tests, you may have to set it to true.
14
+ config.eager_load = false
15
+
16
+ # Configure public file server for tests with Cache-Control for performance.
17
+ config.public_file_server.enabled = true
18
+ config.public_file_server.headers = {
19
+ 'Cache-Control' => "public, max-age=#{1.hour.to_i}"
20
+ }
21
+
22
+ # Show full error reports and disable caching.
23
+ config.consider_all_requests_local = true
24
+ config.action_controller.perform_caching = false
25
+ config.cache_store = :null_store
26
+
27
+ # Raise exceptions instead of rendering exception templates.
28
+ config.action_dispatch.show_exceptions = false
29
+
30
+ # Disable request forgery protection in test environment.
31
+ config.action_controller.allow_forgery_protection = false
32
+
33
+ # Store uploaded files on the local file system in a temporary directory.
34
+ # config.active_storage.service = :test
35
+
36
+ # config.action_mailer.perform_caching = false
37
+
38
+ # Tell Action Mailer not to deliver emails to the real world.
39
+ # The :test delivery method accumulates sent emails in the
40
+ # ActionMailer::Base.deliveries array.
41
+ # config.action_mailer.delivery_method = :test
42
+
43
+ # Print deprecation notices to the stderr.
44
+ config.active_support.deprecation = :stderr
45
+
46
+ # Raises error for missing translations.
47
+ # config.action_view.raise_on_missing_translations = true
48
+ end