|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# https://github.com/nesquena/rabl#configuration |
| 4 | + |
| 5 | +Rabl.configure do |config| |
| 6 | + config.cache_sources = !Rails.env.development? # Defaults to false |
| 7 | + config.raise_on_missing_attribute = !Rails.env.production? # Defaults to false |
| 8 | + |
| 9 | + # config.cache_all_output = false |
| 10 | + # config.cache_engine = Rabl::CacheEngine.new # Defaults to Rails cache |
| 11 | + # config.perform_caching = false |
| 12 | + # config.escape_all_output = false |
| 13 | + # config.json_engine = nil # Class with #dump class method (defaults JSON) |
| 14 | + # config.msgpack_engine = nil # Defaults to ::MessagePack |
| 15 | + # config.bson_engine = nil # Defaults to ::BSON |
| 16 | + # config.plist_engine = nil # Defaults to ::Plist::Emit |
| 17 | + # config.include_json_root = true |
| 18 | + # config.include_msgpack_root = true |
| 19 | + # config.include_bson_root = true |
| 20 | + # config.include_plist_root = true |
| 21 | + # config.include_xml_root = false |
| 22 | + # config.include_child_root = true |
| 23 | + # config.enable_json_callbacks = false |
| 24 | + # config.xml_options = { :dasherize => true, :skip_types => false } |
| 25 | + # config.view_paths = [] |
| 26 | + # config.replace_nil_values_with_empty_strings = true # Defaults to false |
| 27 | + # config.replace_empty_string_values_with_nil_values = true # Defaults to false |
| 28 | + # config.exclude_nil_values = true # Defaults to false |
| 29 | + # config.exclude_empty_values_in_collections = true # Defaults to false |
| 30 | + # config.camelize_keys = :upper # Defaults to false |
| 31 | +end |
| 32 | + |
| 33 | +# Monkey Patch Rabl source lookup to make it compatible with Rails view lookup |
| 34 | +module Rabl |
| 35 | + module Sources |
| 36 | + private |
| 37 | + |
| 38 | + # Returns the rabl template path for Rails |
| 39 | + def fetch_rails_source(file, _options = {}) |
| 40 | + # use Rails template resolution mechanism if possible (find_template) |
| 41 | + source_format = request_format if defined?(request_format) |
| 42 | + |
| 43 | + lookup_proc = lambda do |partial| |
| 44 | + context_scope.lookup_context.find(file, context_scope.lookup_context.prefixes, partial, [], |
| 45 | + {formats: [source_format]}) |
| 46 | + end |
| 47 | + template = begin |
| 48 | + lookup_proc.call(false) |
| 49 | + rescue |
| 50 | + nil |
| 51 | + end |
| 52 | + template ||= begin |
| 53 | + lookup_proc.call(true) |
| 54 | + rescue |
| 55 | + nil |
| 56 | + end |
| 57 | + template&.identifier |
| 58 | + end |
| 59 | + end |
| 60 | +end |
0 commit comments