From a8380ef982f4fe0a359057fd209c5e74d4527577 Mon Sep 17 00:00:00 2001 From: Nikolai B Date: Wed, 16 Dec 2015 14:13:49 +0000 Subject: [PATCH] Allow default_serializer_options to be configured --- lib/rocket_pants.rb | 8 +++++++ lib/rocket_pants/controller/respondable.rb | 8 +++---- lib/rocket_pants/railtie.rb | 27 +++++++++++----------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/rocket_pants.rb b/lib/rocket_pants.rb index b572721..1567a13 100644 --- a/lib/rocket_pants.rb +++ b/lib/rocket_pants.rb @@ -110,6 +110,14 @@ def pass_through_errors=(value) @@pass_through_errors = value end + def default_serializer_options=(value) + @@default_serializer_options = value + end + + def default_serializer_options + defined?(@@default_serializer_options) ? @@default_serializer_options : {} + end + end end diff --git a/lib/rocket_pants/controller/respondable.rb b/lib/rocket_pants/controller/respondable.rb index 6b6476e..da8e626 100644 --- a/lib/rocket_pants/controller/respondable.rb +++ b/lib/rocket_pants/controller/respondable.rb @@ -102,10 +102,8 @@ def normalise_object(object, options = {}) end def default_serializer_options - { - :url_options => url_options, - :root => false - } + RocketPants.default_serializer_options.reverse_merge({:url_options => url_options}) || + {:url_options => url_options, :root => false} end def encode_to_json(object) @@ -227,4 +225,4 @@ def metadata_for(object, options, type, singular) end end -end \ No newline at end of file +end diff --git a/lib/rocket_pants/railtie.rb b/lib/rocket_pants/railtie.rb index 9cb91fa..3c38021 100644 --- a/lib/rocket_pants/railtie.rb +++ b/lib/rocket_pants/railtie.rb @@ -1,11 +1,11 @@ module RocketPants class Railtie < Rails::Railtie - config.rocket_pants = ActiveSupport::OrderedOptions.new - config.rocket_pants.use_caching = nil - config.rocket_pants.header_metadata = nil - config.rocket_pants.pass_through_errors = nil - config.rocket_pants.pass_through_errors = nil + config.rocket_pants = ActiveSupport::OrderedOptions.new + config.rocket_pants.use_caching = nil + config.rocket_pants.header_metadata = nil + config.rocket_pants.pass_through_errors = nil + config.rocket_pants.default_serializer_options = nil config.i18n.railties_load_path << File.expand_path('../locale/en.yml', __FILE__) @@ -14,13 +14,14 @@ class Railtie < Rails::Railtie end initializer "rocket_pants.configuration" do |app| - rp_config = app.config.rocket_pants - rp_config.use_caching = Rails.env.production? if rp_config.use_caching.nil? - RocketPants.caching_enabled = rp_config.use_caching - RocketPants.header_metadata = rp_config.header_metadata unless rp_config.header_metadata.nil? - RocketPants.serializers_enabled = rp_config.serializers_enabled unless rp_config.serializers_enabled.nil? - RocketPants.show_exception_message = rp_config.show_exception_message unless rp_config.show_exception_message.nil? - RocketPants.pass_through_errors = rp_config.pass_through_errors unless rp_config.pass_through_errors.nil? + rp_config = app.config.rocket_pants + rp_config.use_caching = Rails.env.production? if rp_config.use_caching.nil? + RocketPants.caching_enabled = rp_config.use_caching + RocketPants.header_metadata = rp_config.header_metadata unless rp_config.header_metadata.nil? + RocketPants.serializers_enabled = rp_config.serializers_enabled unless rp_config.serializers_enabled.nil? + RocketPants.show_exception_message = rp_config.show_exception_message unless rp_config.show_exception_message.nil? + RocketPants.pass_through_errors = rp_config.pass_through_errors unless rp_config.pass_through_errors.nil? + RocketPants.default_serializer_options = rp_config.default_serializer_options unless rp_config.default_serializer_options.nil? # Set the rocket pants cache if present. RocketPants.cache = rp_config.cache if rp_config.cache end @@ -54,4 +55,4 @@ class Railtie < Rails::Railtie end end -end \ No newline at end of file +end