lib/wcc/contentful/services.rb in wcc-contentful-1.4.0 vs lib/wcc/contentful/services.rb in wcc-contentful-1.5.0.rc1

- old
+ new

@@ -9,14 +9,15 @@ end end attr_reader :configuration - def initialize(configuration) + def initialize(configuration, model_namespace: nil) raise ArgumentError, 'Not yet configured!' unless configuration @configuration = configuration + @model_namespace = model_namespace end # Gets the data-store which executes the queries run against the dynamic # models in the WCC::Contentful::Model namespace. # This is one of the following based on the configured store method: @@ -118,9 +119,44 @@ SyncEngine.new( store: store, client: client, key: 'sync:token' ) + end + end + + # Returns a callable object which can be used to render a rich text document. + # This object will have all the connected services injected into it. + # The implementation class is configured by {WCC::Contentful::Configuration#rich_text_renderer}. + # In a rails context the default implementation is {WCC::Contentful::ActionViewRichTextRenderer}. + def rich_text_renderer + @rich_text_renderer ||= + if implementation_class = configuration&.rich_text_renderer + store = self.store + config = configuration + model_namespace = @model_namespace || WCC::Contentful::Model + + # Wrap the implementation in a subclass that injects the services + Class.new(implementation_class) do + define_method :initialize do |document, *args, **kwargs| + # Implementation might choose to override these, so call super last + @store = store + @config = config + @model_namespace = model_namespace + super(document, *args, **kwargs) + end + end + else + # Create a renderer that renders a more helpful error message, but delay the error message until #to_html + # is actually invoked in case the user never actually uses the renderer. + Class.new(WCC::Contentful::RichTextRenderer) do + def call + raise WCC::Contentful::RichTextRenderer::AbstractRendererError, + 'No rich text renderer implementation has been configured. ' \ + 'Please install a supported implementation such as ActionView, ' \ + 'or set WCC::Contentful.configuration.rich_text_renderer to a custom implementation.' + end + end end end # Gets the configured instrumentation adapter, defaulting to ActiveSupport::Notifications def instrumentation