fortitude-caching 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +2 -0
  4. data/README.md +41 -0
  5. data/fortitude-caching.gemspec +23 -0
  6. data/lib/fortitude/caching.rb +53 -0
  7. data/spec/caching_spec.rb +41 -0
  8. data/spec/dummy/Rakefile +6 -0
  9. data/spec/dummy/app/assets/config/manifest.js +4 -0
  10. data/spec/dummy/app/assets/images/.keep +0 -0
  11. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  12. data/spec/dummy/app/assets/javascripts/cable.js +13 -0
  13. data/spec/dummy/app/assets/javascripts/channels/.keep +0 -0
  14. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  15. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  16. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  17. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  18. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  19. data/spec/dummy/app/controllers/projects_controller.rb +31 -0
  20. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  21. data/spec/dummy/app/jobs/application_job.rb +2 -0
  22. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  23. data/spec/dummy/app/models/application_record.rb +3 -0
  24. data/spec/dummy/app/models/concerns/.keep +0 -0
  25. data/spec/dummy/app/views/base.rb +3 -0
  26. data/spec/dummy/app/views/projects/show_cached.rb +12 -0
  27. data/spec/dummy/app/views/projects/show_cached_with_needs.rb +10 -0
  28. data/spec/dummy/app/views/projects/show_uncached.rb +6 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +4 -0
  31. data/spec/dummy/bin/rake +4 -0
  32. data/spec/dummy/bin/setup +34 -0
  33. data/spec/dummy/bin/update +29 -0
  34. data/spec/dummy/config.ru +5 -0
  35. data/spec/dummy/config/application.rb +18 -0
  36. data/spec/dummy/config/boot.rb +5 -0
  37. data/spec/dummy/config/cable.yml +9 -0
  38. data/spec/dummy/config/environment.rb +5 -0
  39. data/spec/dummy/config/environments/development.rb +43 -0
  40. data/spec/dummy/config/environments/production.rb +86 -0
  41. data/spec/dummy/config/environments/test.rb +42 -0
  42. data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
  43. data/spec/dummy/config/initializers/assets.rb +11 -0
  44. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  45. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  46. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  47. data/spec/dummy/config/initializers/inflections.rb +16 -0
  48. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  49. data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
  50. data/spec/dummy/config/initializers/session_store.rb +3 -0
  51. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  52. data/spec/dummy/config/locales/en.yml +23 -0
  53. data/spec/dummy/config/puma.rb +47 -0
  54. data/spec/dummy/config/routes.rb +10 -0
  55. data/spec/dummy/config/secrets.yml +22 -0
  56. data/spec/dummy/config/spring.rb +6 -0
  57. data/spec/dummy/db/development.sqlite3 +0 -0
  58. data/spec/dummy/db/schema.rb +15 -0
  59. data/spec/dummy/lib/assets/.keep +0 -0
  60. data/spec/dummy/log/.keep +0 -0
  61. data/spec/dummy/public/404.html +67 -0
  62. data/spec/dummy/public/422.html +67 -0
  63. data/spec/dummy/public/500.html +66 -0
  64. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  65. data/spec/dummy/public/apple-touch-icon.png +0 -0
  66. data/spec/dummy/public/favicon.ico +0 -0
  67. data/spec/spec_helper.rb +5 -0
  68. metadata +244 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 948eeacecc444d50258a7f59e3bece61ab5853b2
4
+ data.tar.gz: 0021abb8ae5cd5fec827b2ffbdfeadaa8ab09629
5
+ SHA512:
6
+ metadata.gz: 7595200b374c985b10782398e70cb60223958a82c85066a9e0246e2f28f9bbb6af3de89aa340e0d926dad7b543dcdcd965ac35851996c6a693898b2255bb13e9
7
+ data.tar.gz: 8da0ae73c696b058a77ee67b1b1694bb4f8a923c93b595fcdb00b120d68876ff96e80289663a8510751f8b4b08b2bebb09540d2eafe70fcedbd08b6ca9d9b411
@@ -0,0 +1,17 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/config/database.yml
5
+ spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ spec/dummy/.sass-cache
8
+ spec/dummy/public/uploads/*
9
+ tmp/*
10
+ coverage/*
11
+ *.gem
12
+ Gemfile.lock
13
+ spec/dummy/db/test.sqlite3
14
+ gemfiles/*.lock
15
+ .ruby-gemset
16
+ spec/internal
17
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,41 @@
1
+ fortitude-caching
2
+ ====
3
+
4
+ Integrates [Rails fragment caching](http://guides.rubyonrails.org/caching_with_rails.html#fragment-caching) with [Fortitude](https://github.com/ageweke/fortitude).
5
+
6
+ ## Usage
7
+
8
+ 1. `include Fortitude::Caching` in your base widget class.
9
+ 2. Call `cacheable` for widgets that you want cached.
10
+
11
+ By using `fortitude-caching`, this:
12
+
13
+ ```rb
14
+ class Views::Pages::Show < Views::Base
15
+ needs :page
16
+
17
+ cacheable
18
+
19
+ def content
20
+ ...
21
+ end
22
+ end
23
+ ```
24
+
25
+ Is the equivalent of this:
26
+
27
+ ```rb
28
+ class Views::Pages::Show < Views::Base
29
+ needs :page
30
+
31
+ def content
32
+ cache(page) do
33
+ ...
34
+ end
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## License
40
+
41
+ MIT
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'fortitude-caching'
5
+ s.version = '0.0.1'
6
+
7
+ s.required_ruby_version = Gem::Requirement.new('>= 2.0.0')
8
+ s.authors = ['Adam Becker']
9
+ s.summary = 'Integrates Rails fragment caching with Fortitude.'
10
+ s.email = 'adam@dobt.co'
11
+ s.license = 'MIT'
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {features,spec}/*`.split("\n")
15
+
16
+ s.homepage = 'https://github.com/dobtco/fortitude-caching'
17
+
18
+ s.add_dependency 'rails', '>= 4.0.0', '< 6.0'
19
+ s.add_dependency 'fortitude', '~> 0.9.5'
20
+
21
+ s.add_development_dependency 'rspec-rails', '~> 3.5', '>= 3.5.2'
22
+ s.add_development_dependency 'sqlite3', '~> 1.3', '>= 1.3.12'
23
+ end
@@ -0,0 +1,53 @@
1
+ module Fortitude
2
+ module Caching
3
+ extend ActiveSupport::Concern
4
+
5
+ class NotCacheableError < RuntimeError
6
+ end
7
+
8
+ module ClassMethods
9
+ def cacheable(opts = {})
10
+ if extra_assigns == :use
11
+ extra_assigns :ignore
12
+ end
13
+
14
+ static_keys = Array(opts.fetch(:key, []))
15
+ options = opts.fetch(:options, {})
16
+
17
+ define_method(:cache_contents) do |&block|
18
+ if !cacheable_by_fortitude?
19
+ raise(
20
+ NotCacheableError,
21
+ "Can't digest the widget #{self}, since it's being rendered " \
22
+ "outside of an ActionView context."
23
+ )
24
+ end
25
+
26
+ cache calculate_cache_dependencies(assigns, static_keys), options do
27
+ block.call
28
+ end
29
+ end
30
+
31
+ define_method(:cacheable_by_fortitude?) do
32
+ respond_to?(:cache) &&
33
+ (
34
+ invoke_helper(:instance_variable_get, :@virtual_path) ||
35
+ options[:skip_digest]
36
+ )
37
+ end
38
+
39
+ around_content :cache_contents
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def calculate_cache_dependencies(assigns, static_keys)
46
+ (
47
+ assigns.to_a.sort_by(&:first).flatten +
48
+ static_keys +
49
+ [widget_locale]
50
+ )
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fortitude::Caching, type: :request do
4
+ def get_and_return_response(path)
5
+ get path
6
+ response.body
7
+ end
8
+
9
+ it 'caches without needs' do
10
+ expect(get_and_return_response('/projects/1/show_cached')).
11
+ to eq(get_and_return_response('/projects/1/show_cached'))
12
+ end
13
+
14
+ it 'has different caches for different locales' do
15
+ res1 = get_and_return_response('/projects/1/show_cached')
16
+ res2 = get_and_return_response('/projects/1/show_cached?locale=fr')
17
+ res3 = get_and_return_response('/projects/1/show_cached?locale=fr')
18
+
19
+ expect(res1).to_not eq res2
20
+ expect(res2).to eq res3
21
+ end
22
+
23
+ it 'caches with needs' do
24
+ res1 = get_and_return_response('/projects/1/show_cached_with_needs')
25
+ res2 = get_and_return_response('/projects/2/show_cached_with_needs')
26
+ res3 = get_and_return_response('/projects/2/show_cached_with_needs')
27
+
28
+ expect(res1).to_not eq res2
29
+ expect(res2).to eq res3
30
+ end
31
+
32
+ it 'does not cache if cacheable is not called' do
33
+ expect(get_and_return_response('/projects/1/show_uncached')).
34
+ to_not eq(get_and_return_response('/projects/1/show_uncached'))
35
+ end
36
+
37
+ it 'throws an error if a view cannot be cached' do
38
+ expect { get '/projects/1/cannot_cache' }.
39
+ to raise_error(Fortitude::Caching::NotCacheableError)
40
+ end
41
+ end
@@ -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,4 @@
1
+
2
+ //= link_tree ../images
3
+ //= link_directory ../javascripts .js
4
+ //= link_directory ../stylesheets .css
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ // Action Cable provides the framework to deal with WebSockets in Rails.
2
+ // You can generate new channels where WebSocket features live using the rails generate channel command.
3
+ //
4
+ //= require action_cable
5
+ //= require_self
6
+ //= require_tree ./channels
7
+
8
+ (function() {
9
+ this.App || (this.App = {});
10
+
11
+ App.cable = ActionCable.createConsumer();
12
+
13
+ }).call(this);
@@ -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,4 @@
1
+ module ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ layout false
3
+ end
@@ -0,0 +1,31 @@
1
+ class ProjectsController < ApplicationController
2
+ around_action :set_locale
3
+ before_action :set_project
4
+
5
+ def show_cached
6
+ end
7
+
8
+ def show_cached_with_needs
9
+ end
10
+
11
+ def show_uncached
12
+ end
13
+
14
+ def cannot_cache
15
+ render widget: Views::Projects::ShowCached.new
16
+ end
17
+
18
+ private
19
+
20
+ def set_project
21
+ @project = params[:id]
22
+ end
23
+
24
+ def set_locale
25
+ if params[:locale]
26
+ I18n.with_locale(params[:locale]) { yield }
27
+ else
28
+ yield
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: 'from@example.com'
3
+ layout 'mailer'
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ class Views::Base < Fortitude::Widgets::Html5
2
+ include Fortitude::Caching
3
+ end
@@ -0,0 +1,12 @@
1
+ class Views::Projects::ShowCached < Views::Base
2
+ cacheable
3
+
4
+ def content
5
+ @@count ||= 0
6
+ text "#{@@count += 1}"
7
+
8
+ if params[:locale]
9
+ text params[:locale]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class Views::Projects::ShowCachedWithNeeds < Views::Base
2
+ needs :project
3
+
4
+ cacheable
5
+
6
+ def content
7
+ @@count ||= 0
8
+ text "#{@@count += 1}"
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ class Views::Projects::ShowUncached < Views::Base
2
+ def content
3
+ @@count ||= 0
4
+ text "#{@@count += 1}"
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -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,34 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+ require 'fileutils'
4
+ include FileUtils
5
+
6
+ # path to your application root.
7
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
8
+
9
+ def system!(*args)
10
+ system(*args) || abort("\n== Command #{args} failed ==")
11
+ end
12
+
13
+ chdir APP_ROOT do
14
+ # This script is a starting point to setup your application.
15
+ # Add necessary setup steps to this file.
16
+
17
+ puts '== Installing dependencies =='
18
+ system! 'gem install bundler --conservative'
19
+ system('bundle check') || system!('bundle install')
20
+
21
+ # puts "\n== Copying sample files =="
22
+ # unless File.exist?('config/database.yml')
23
+ # cp 'config/database.yml.sample', 'config/database.yml'
24
+ # end
25
+
26
+ puts "\n== Preparing database =="
27
+ system! 'bin/rails db:setup'
28
+
29
+ puts "\n== Removing old logs and tempfiles =="
30
+ system! 'bin/rails log:clear tmp:clear'
31
+
32
+ puts "\n== Restarting application server =="
33
+ system! 'bin/rails restart'
34
+ end
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+ require 'fileutils'
4
+ include FileUtils
5
+
6
+ # path to your application root.
7
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
8
+
9
+ def system!(*args)
10
+ system(*args) || abort("\n== Command #{args} failed ==")
11
+ end
12
+
13
+ chdir APP_ROOT do
14
+ # This script is a way to update your development environment automatically.
15
+ # Add necessary update steps to this file.
16
+
17
+ puts '== Installing dependencies =='
18
+ system! 'gem install bundler --conservative'
19
+ system('bundle check') || system!('bundle install')
20
+
21
+ puts "\n== Updating database =="
22
+ system! 'bin/rails db:migrate'
23
+
24
+ puts "\n== Removing old logs and tempfiles =="
25
+ system! 'bin/rails log:clear tmp:clear'
26
+
27
+ puts "\n== Restarting application server =="
28
+ system! 'bin/rails restart'
29
+ end