Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow default_serializer_options to be configured #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions lib/rocket_pants/rspec_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def self.normalise_urls(object)

# Converts it to JSON and back again.
def self.normalise_as_json(object, options = {})
options = RocketPants.default_serializer_options.reverse_merge(options)
options = options.reverse_merge(:compact => true) if object.is_a?(Array)
object = RocketPants::Respondable.normalise_object(object, options)
j = ActiveSupport::JSON
Expand Down