codenoble / cache-crispies

Speedy Rails JSON serialization with built-in caching
MIT License
156 stars 16 forks source link

Allow setting a Rails Engine to resolve file path #47

Closed floriandejonckheere closed 1 year ago

floriandejonckheere commented 2 years ago

When using CacheCrispies inside a Rails application that is split up in Rails Engines, the file path resolved is not correct: the assumption is made that serializers always reside under the Rails application root (Rails.root.join("app/serializers")), while they could actually reside under the engine root. This PR introduces a method on serializer classes that allow to set the Engine class in order to resolve the file path correctly. This value is inherited when subclassing serializers.

class RailsSerializer
end

# Assuming MyApp::Engine resides under `components` directory in Rails root
class EngineSerializer
  engine MyApp::Engine
end

RailsSerializer.send(:path)
# => #<Pathname:/app/serializers/rails_serializer.rb>

EngineSerializer.send(:path)
# => #<Pathname:/components/app/serializers/engine_serializer.rb>
adamcrown commented 1 year ago

Looks like a great addition. Thank you!

And sorry for the late response to this.