Skip to content

Commit

Permalink
Allow setting a Rails Engine to resolve file path
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Aug 4, 2022
1 parent dea85f8 commit fd04d44
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/cache_crispies/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ class << self
alias do_caching? do_caching
end

# Get or set root path of Rails application or engine.
# It uses Rails by default, but can be overriden with your Rails Engine class.
# Calling the method with an argument will set the value, calling it without
# any arguments will get the value.
def self.engine(value = nil)
@engine ||= superclass.try(:engine)
@engine ||= Rails

# method called with no args so act as a getter
return @engine if value.nil?

# method called with args so act as a setter
@engine = value
end

# Get or set a JSON key to use as a root key on a non-collection
# serializable. By default it's the name of the class without the
# "Serializer" part. But it can be overridden in a subclass to be anything.
Expand Down Expand Up @@ -190,7 +205,7 @@ def self.path
parts = %w[app serializers]
parts += to_s.deconstantize.split('::').map(&:underscore)
parts << "#{to_s.demodulize.underscore}.rb"
Rails.root.join(*parts)
engine.root.join(*parts)
end
end
private_class_method :path
Expand Down
43 changes: 43 additions & 0 deletions spec/cache_crispies/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def visible?
end
end

class MyEngine
def self.root
end
end

class CacheCrispiesEngineTestSerializer < CacheCrispiesTestSerializer
engine MyEngine
end

let(:model) do
OpenStruct.new(
id: 42,
Expand All @@ -52,6 +61,7 @@ def visible?
end

let(:serializer) { CacheCrispiesTestSerializer }
let(:engine_serializer) { CacheCrispiesEngineTestSerializer }
let(:instance) { serializer.new(model) }
subject { instance }

Expand Down Expand Up @@ -161,6 +171,24 @@ def visible?
end
end

describe '.engine' do
it 'defaults to Rails' do
expect(serializer.engine).to eq Rails
end

context 'when called with an argument' do
after { serializer.remove_instance_variable :@engine }

it 'sets and returns a value' do
expect {
serializer.engine MyEngine
}.to change {
serializer.engine
}.from(Rails).to MyEngine
end
end
end

describe '.cache_key_addons' do
it 'returns an empty array by default' do
expect(serializer.cache_key_addons).to eq []
Expand Down Expand Up @@ -203,6 +231,12 @@ def visible?
let(:serializer_file_digest) {
Digest::MD5.file(serializer_file_path).to_s
}
let(:engine_serializer_file_path) {
File.expand_path('../fixtures/engine_test_serializer.rb', __dir__)
}
let(:engine_serializer_file_digest) {
Digest::MD5.file(engine_serializer_file_path).to_s
}

before do
allow(NutritionSerializer).to receive(:file_hash).and_return(
Expand All @@ -211,6 +245,9 @@ def visible?
allow(Rails).to receive_message_chain(:root, :join).and_return(
serializer_file_path
)
allow(MyEngine).to receive_message_chain(:root, :join).and_return(
engine_serializer_file_path
)
end

it 'includes the file name' do
Expand All @@ -230,6 +267,12 @@ def visible?
"#{serializer}-#{serializer_file_digest}+#{nested_serializer_digest}"
)
end

context 'when an engine is set' do
it "includes a digest of the serializer class file's contents" do
expect(engine_serializer.cache_key_base).to include engine_serializer_file_digest
end
end
end

describe '.attributes' do
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/engine_test_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Nothing has to actually be in this file.
# It's just here so test can MD5 sum it's content.
# This is an alternative serializer file.

0 comments on commit fd04d44

Please sign in to comment.