From 28f3dbedd0ee03fb7e1495fbfbe41b4b8cf7413f Mon Sep 17 00:00:00 2001 From: Alex Smelik Date: Wed, 12 Sep 2012 10:45:45 -0700 Subject: [PATCH 1/2] Let MultiJson select default JSON adapter (engine), support for latest MultiJson syntax --- lib/rabl-rails.rb | 5 ++--- lib/rabl-rails/renderers/json.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/rabl-rails.rb b/lib/rabl-rails.rb index d6b645b..830c797 100644 --- a/lib/rabl-rails.rb +++ b/lib/rabl-rails.rb @@ -27,7 +27,6 @@ module RablRails @@include_json_root = true mattr_reader :json_engine - @@json_engine = :yajl mattr_accessor :use_custom_responder @@use_custom_responder = false @@ -42,7 +41,7 @@ module RablRails end def self.json_engine=(name) - MultiJson.engine = name + MultiJson.respond_to?(:use) ? MultiJson.use(name) : MultiJson.engine = name @@json_engine = name rescue LoadError Rails.logger.warn %Q(WARNING: rabl-rails could not load "#{self.json_engine}" as JSON engine, fallback to default) @@ -53,6 +52,6 @@ module RablRails end def self.load_default_engines! - self.json_engine = :yajl + self.json_engine = MultiJson.respond_to?(default_adapter) ? MultiJson.default_adapter : MultiJson.default_engine end end diff --git a/lib/rabl-rails/renderers/json.rb b/lib/rabl-rails/renderers/json.rb index 632aaec..5ab487a 100644 --- a/lib/rabl-rails/renderers/json.rb +++ b/lib/rabl-rails/renderers/json.rb @@ -3,7 +3,7 @@ module RablRails class JSON < Base def format_output(hash) hash = { _options[:root_name] => hash } if _options[:root_name] && RablRails.include_json_root - MultiJson.encode(hash) + MultiJson.respond_to?(:dump) ? MultiJson.dump(hash) : MultiJson.encode(hash) end end end From 360d70b8381efc0f4565769f388588f8f5219fa8 Mon Sep 17 00:00:00 2001 From: Alex Smelik Date: Thu, 13 Sep 2012 08:05:52 -0700 Subject: [PATCH 2/2] Revert to old MultiJson syntax. --- CHANGELOG.md | 7 +++++-- lib/rabl-rails.rb | 13 +++++++------ lib/rabl-rails/renderers/json.rb | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e839416..2b3873e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # CHANGELOG +## 0.1.4 (Unreleased) + * Use MultiJson's preferred JSON engine as default + ## 0.1.3 - * Render correcly when variables are not passed via the assigns ivar but as helper methods + * Render correctly when variables are not passed via the assigns ivar but as helper methods (decent_exposure, focused_controller) * Add custom Responder @@ -13,7 +16,7 @@ ## 0.1.1 * Add CHANGELOG - * Remove unnused test in loop + * Remove unused test in loop * Speed up rendering by not double copying variable from context * Rename private variable to avoid name conflict * Remove sqlite3 development dependency \ No newline at end of file diff --git a/lib/rabl-rails.rb b/lib/rabl-rails.rb index 830c797..d57e57d 100644 --- a/lib/rabl-rails.rb +++ b/lib/rabl-rails.rb @@ -26,8 +26,6 @@ module RablRails mattr_accessor :include_json_root @@include_json_root = true - mattr_reader :json_engine - mattr_accessor :use_custom_responder @@use_custom_responder = false @@ -41,10 +39,13 @@ module RablRails end def self.json_engine=(name) - MultiJson.respond_to?(:use) ? MultiJson.use(name) : MultiJson.engine = name - @@json_engine = name + MultiJson.engine = name rescue LoadError - Rails.logger.warn %Q(WARNING: rabl-rails could not load "#{self.json_engine}" as JSON engine, fallback to default) + Rails.logger.warn %Q(WARNING: rabl-rails could not load "#{name}" as JSON engine, fallback to default) + end + + def self.json_engine + MultiJson.engine end def self.cache_templates? @@ -52,6 +53,6 @@ module RablRails end def self.load_default_engines! - self.json_engine = MultiJson.respond_to?(default_adapter) ? MultiJson.default_adapter : MultiJson.default_engine + self.json_engine = MultiJson.default_engine end end diff --git a/lib/rabl-rails/renderers/json.rb b/lib/rabl-rails/renderers/json.rb index 5ab487a..632aaec 100644 --- a/lib/rabl-rails/renderers/json.rb +++ b/lib/rabl-rails/renderers/json.rb @@ -3,7 +3,7 @@ module RablRails class JSON < Base def format_output(hash) hash = { _options[:root_name] => hash } if _options[:root_name] && RablRails.include_json_root - MultiJson.respond_to?(:dump) ? MultiJson.dump(hash) : MultiJson.encode(hash) + MultiJson.encode(hash) end end end