forked from codenoble/cache-crispies
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration for ETags and custom cache store
Closes codenoble#6
- Loading branch information
Showing
17 changed files
with
243 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'rails' | ||
|
||
module CacheCrispies | ||
class Configuration | ||
SETTINGS = [ | ||
:cache_store, | ||
:etags | ||
].freeze | ||
|
||
SETTINGS.each do |setting| | ||
attr_accessor setting | ||
end | ||
|
||
def initialize | ||
reset! | ||
end | ||
|
||
alias etags? etags | ||
|
||
# Resets all values to their defaults. Useful for testing. | ||
def reset! | ||
@cache_store = Rails.cache || ActiveSupport::Cache::NullStore.new | ||
@etags = false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'spec_helper' | ||
|
||
describe CacheCrispies::Configuration do | ||
describe '#cache_store' do | ||
context 'when Rails.cache is defined' do | ||
let(:cache_double) { double } | ||
before { expect(Rails).to receive(:cache).and_return cache_double } | ||
|
||
it 'is Rails.cache by default' do | ||
expect(subject.cache_store).to be cache_double | ||
end | ||
end | ||
|
||
context 'when Rails.cache is nil' do | ||
before { expect(Rails).to receive(:cache).and_return nil } | ||
|
||
it 'is NullStore by default' do | ||
expect(subject.cache_store).to be_kind_of ActiveSupport::Cache::NullStore | ||
end | ||
end | ||
|
||
it 'can be changed' do | ||
cache_store = ActiveSupport::Cache::NullStore.new | ||
|
||
expect { | ||
subject.cache_store = cache_store | ||
}.to change { subject.cache_store }.to cache_store | ||
end | ||
end | ||
|
||
describe '#etags' do | ||
it 'is false by default' do | ||
expect(subject.etags).to be false | ||
end | ||
|
||
it 'can be changed' do | ||
expect { | ||
subject.etags = true | ||
}.to change { subject.etags }.to true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
require 'spec_helper' | ||
require 'active_model' | ||
require 'action_controller' | ||
|
||
describe CacheCrispies::Controller do | ||
class Cereal | ||
include ActiveModel::Model | ||
attr_accessor :name | ||
end | ||
|
||
class CerealController < ActionController::Base | ||
include CacheCrispies::Controller | ||
end | ||
|
||
class CerealSerializerForController < CacheCrispies::Base | ||
serialize :name | ||
|
||
def self.key | ||
'cereal' | ||
end | ||
end | ||
|
||
let(:cereal_names) { ['Count Chocula', 'Eyeholes'] } | ||
let(:collection) { cereal_names.map { |name| Cereal.new(name: name) } } | ||
|
||
subject { CerealController.new } | ||
|
||
describe '#cache_render' do | ||
let(:etags) { false } | ||
let(:single_json) { { cereal: { name: cereal_names.first } }.to_json } | ||
let(:collection_json) { | ||
{ cereals: cereal_names.map { |name| { name: name } } }.to_json | ||
} | ||
|
||
before do | ||
expect( | ||
CacheCrispies | ||
).to receive_message_chain(:config, :etags?).and_return etags | ||
end | ||
|
||
context 'with etags disabled' do | ||
it 'does not set etags' do | ||
expect(subject).to receive(:render).with json: collection_json | ||
expect(subject.response).to_not receive(:weak_etag=) | ||
|
||
subject.cache_render CerealSerializerForController, collection | ||
end | ||
end | ||
|
||
context 'with etags enabled' do | ||
let(:etags) { true } | ||
|
||
it 'sets etags' do | ||
expect(subject).to receive(:render).with json: collection_json | ||
expect_any_instance_of( | ||
CacheCrispies::Plan | ||
).to receive(:etag).and_return 'test-etag' | ||
expect(subject).to receive_message_chain(:response, :weak_etag=).with 'test-etag' | ||
|
||
subject.cache_render CerealSerializerForController, collection | ||
end | ||
end | ||
|
||
it 'renders a json collection' do | ||
expect(subject).to receive(:render).with json: collection_json | ||
|
||
subject.cache_render CerealSerializerForController, collection | ||
end | ||
|
||
it 'renders a single json object' do | ||
expect(subject).to receive(:render).with json: single_json | ||
|
||
subject.cache_render CerealSerializerForController, collection.first | ||
end | ||
|
||
context 'with a status: option' do | ||
it 'passes the status option to the Rails render call' do | ||
expect(subject).to receive(:render).with( | ||
json: single_json, | ||
status: 418 | ||
) | ||
|
||
subject.cache_render( | ||
CerealSerializerForController, | ||
collection.first, | ||
status: 418 | ||
) | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
|
||
describe CacheCrispies do | ||
after { described_class.config.reset! } | ||
|
||
describe '.configure and .config' do | ||
it 'allows setting values' do | ||
expect { | ||
described_class.configure do |conf| | ||
conf.etags = true | ||
end | ||
}.to change { described_class.config.etags }.from(false).to true | ||
end | ||
end | ||
|
||
describe '.cache' do | ||
it 'delegates to config.cache_store' do | ||
cache_store = ActiveSupport::Cache::NullStore.new | ||
|
||
expect { | ||
described_class.configure do |conf| | ||
conf.cache_store = cache_store | ||
end | ||
}.to change { described_class.cache }.to cache_store | ||
end | ||
end | ||
end |
Oops, something went wrong.