Skip to content

Commit

Permalink
Allow default_serializer_options to be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolai-b committed Dec 16, 2015
1 parent bddc27a commit a8380ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
8 changes: 8 additions & 0 deletions lib/rocket_pants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 deletions lib/rocket_pants/controller/respondable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -227,4 +225,4 @@ def metadata_for(object, options, type, singular)
end

end
end
end
27 changes: 14 additions & 13 deletions lib/rocket_pants/railtie.rb
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -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
Expand Down Expand Up @@ -54,4 +55,4 @@ class Railtie < Rails::Railtie
end

end
end
end

0 comments on commit a8380ef

Please sign in to comment.