Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Add configuration for allowing rerendering
Browse files Browse the repository at this point in the history
+ Adds configure block
+ Changes default to not cleanup re-rendering cache keys
  • Loading branch information
danielwestendorf committed Oct 4, 2017
1 parent 6a10113 commit 9a56a37
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
get_schwifty (0.1.0)
get_schwifty (0.1.1)
rails (> 5)

GEM
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ GetSchwifty is all about quick responses by utilizing background jobs to do the
## Caveats

- There is no gurantee that the content will load. If your background job queue get's backed up, you'll be not showing content anytime soon.
- Don't cache the view's which use the `get_schwifty` helper. You're fine to cache the content within the rendered partial, however. I may remove this limitation with a configuration option.

## Extracted from HireLoop

Expand Down
4 changes: 4 additions & 0 deletions lib/generators/get_schwifty/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class InstallGenerator < Rails::Generators::Base

desc "Installs required files for getting schwifty in here"

def copy_initializer
template "get_schwifty.rb", "config/initializers/get_schwifty.rb"
end

def copy_controller
template "controllers/get_schwifty_controller.rb", "app/controllers/get_schwifty_controller.rb"
end
Expand Down
18 changes: 18 additions & 0 deletions lib/generators/templates/get_schwifty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# GetSchwifty configuration initializer
# Use this file to configure GetSchwifty for your needs
GetSchwifty.configure do |config|
# Configure re-rendering
#
# By default, job parameters are stored in the Rails cache and not removed after render
# If your're not expring keys with a Least Recently Used policy, you cache could fill up
# with values which will never be accessed again.
#
# Allow rerendering
# This allows caching of `get_schwifty` helper calls in views
# config.allow_rerender = true # Default

# Disable rerendering
# This disables rerendering, and the cachability of `get_schwifty` helper calls. Subscriptions
# will be rejected after the first render of a cached `get_schwifty` call
# config.allow_rerender = false
end
7 changes: 7 additions & 0 deletions lib/get_schwifty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
require "get_schwifty/cable/base"

module GetSchwifty
mattr_accessor :allow_rerender
@@allow_rerender = true

def self.configure
yield self
end

class Engine < ::Rails::Engine
config.assets.paths += [File.expand_path("../../app/assets/javascripts", __FILE__)] if config.respond_to?(:assets)

Expand Down
2 changes: 1 addition & 1 deletion lib/get_schwifty/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def subscribed
end

def rendered
Rails.cache.write(channel_name, nil)
Rails.cache.write(channel_name, nil) unless GetSchwifty.allow_rerender
end

def route
Expand Down
2 changes: 1 addition & 1 deletion lib/get_schwifty/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GetSchwifty
VERSION = '0.1.0'
VERSION = "0.1.1"
end
18 changes: 18 additions & 0 deletions test/dummy/config/initializers/get_schwifty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# GetSchwifty configuration initializer
# Use this file to configure GetSchwifty for your needs
GetSchwifty.configure do |config|
# Configure re-rendering
#
# By default, job parameters are stored in the Rails cache and not removed after render
# If your're not expring keys with a Least Recently Used policy, you cache could fill up
# with values which will never be accessed again.
#
# Allow rerendering
# This allows caching of `get_schwifty` helper calls in views
# config.allow_rerender = true # Default

# Disable rerendering
# This disables rerendering, and the cachability of `get_schwifty` helper calls. Subscriptions
# will be rejected after the first render of a cached `get_schwifty` call
# config.allow_rerender = false
end
10 changes: 8 additions & 2 deletions test/get_schwifty_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
require 'test_helper'

class GetSchwifty::Test < ActiveSupport::TestCase
test "truth" do
assert_kind_of Module, GetSchwifty
test "mattr configuration" do
GetSchwifty.allow_rerender = false
refute GetSchwifty.allow_rerender
end

test "configuraiton block" do
GetSchwifty.configure { |config| config.allow_rerender = false }
refute GetSchwifty.allow_rerender
end
end

0 comments on commit 9a56a37

Please sign in to comment.