It's more easy to use json file with other languages like javascript for 
example.
I join the patch I've done for make it work.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
>From b36181e7521e87b2a5088ff6c37f8bd281028dc8 Mon Sep 17 00:00:00 2001
From: Laurent Arnoud <laur...@spkdev.net>
Date: Fri, 29 Aug 2014 19:30:35 +0200
Subject: [PATCH] Added json support for config_for

---
 railties/lib/rails/application.rb               | 10 ++-
 railties/test/application/configuration_test.rb | 88 +++++++++++++++++++++++--
 2 files changed, 88 insertions(+), 10 deletions(-)

diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 6a4660b..0efa4d8 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -222,16 +222,20 @@ module Rails
     #     end
     def config_for(name)
       yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml")
+      json = Pathname.new("#{paths["config"].existent.first}/#{name}.json")
 
       if yaml.exist?
         require "yaml"
         require "erb"
         (YAML.load(ERB.new(yaml.read).result) || {})[Rails.env] || {}
+      elsif json.exist?
+        require "erb"
+        (JSON.parse(ERB.new(json.read).result) || {})[Rails.env] || {}
       else
-        raise "Could not load configuration. No such file - #{yaml}"
+        raise "Could not load configuration. No such file - #{yaml} | #{json}"
       end
-    rescue Psych::SyntaxError => e
-      raise "YAML syntax error occurred while parsing #{yaml}. " \
+    rescue Psych::SyntaxError, JSON::ParserError => e
+      raise "Syntax error occurred while parsing. " \
         "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
         "Error: #{e.message}"
     end
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index db2106a..46570b5 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -1019,7 +1019,7 @@ module ApplicationTests
       assert_equal 'custom key', Rails.application.config.my_custom_config['key']
     end
 
-    test "config_for raises an exception if the file does not exist" do
+    test "config_for raises an exception if the yaml or json file does not exist" do
       add_to_config <<-RUBY
         config.my_custom_config = config_for('custom')
       RUBY
@@ -1028,10 +1028,10 @@ module ApplicationTests
         require "#{app_path}/config/environment"
       end
 
-      assert_equal "Could not load configuration. No such file - #{app_path}/config/custom.yml", exception.message
+      assert_equal "Could not load configuration. No such file - #{app_path}/config/custom.yml | #{app_path}/config/custom.json", exception.message
     end
 
-    test "config_for without the environment configured returns an empty hash" do
+    test "config_for yaml without the environment configured returns an empty hash" do
       app_file 'config/custom.yml', <<-RUBY
       test:
         key: 'custom key'
@@ -1045,7 +1045,7 @@ module ApplicationTests
       assert_equal({}, Rails.application.config.my_custom_config)
     end
 
-    test "config_for with empty file returns an empty hash" do
+    test "config_for with empty yaml file returns an empty hash" do
       app_file 'config/custom.yml', <<-RUBY
       RUBY
 
@@ -1057,7 +1057,7 @@ module ApplicationTests
       assert_equal({}, Rails.application.config.my_custom_config)
     end
 
-    test "config_for containing ERB tags should evaluate" do
+    test "config_for yaml containing ERB tags should evaluate" do
       app_file 'config/custom.yml', <<-RUBY
       development:
         key: <%= 'custom key' %>
@@ -1071,7 +1071,7 @@ module ApplicationTests
       assert_equal 'custom key', Rails.application.config.my_custom_config['key']
     end
 
-    test "config_for with syntax error show a more descritive exception" do
+    test "config_for yaml with syntax error show a more descritive exception" do
       app_file 'config/custom.yml', <<-RUBY
       development:
         key: foo:
@@ -1085,7 +1085,81 @@ module ApplicationTests
         require "#{app_path}/config/environment"
       end
 
-      assert_match 'YAML syntax error occurred while parsing', exception.message
+      assert_match 'Syntax error occurred while parsing', exception.message
+    end
+
+    test "config_for loads custom configuration from json files" do
+      app_file 'config/custom.json', <<-RUBY
+      {"development": {
+        "key": "custom key"}}
+      RUBY
+
+      add_to_config <<-RUBY
+        config.my_custom_config = config_for('custom')
+      RUBY
+
+      require "#{app_path}/config/environment"
+
+      assert_equal 'custom key', Rails.application.config.my_custom_config['key']
+    end
+
+    test "config_for json without the environment configured returns an empty hash" do
+      app_file 'config/custom.json', <<-RUBY
+      {"test": {
+        "key": "custom key"}}
+      RUBY
+
+      add_to_config <<-RUBY
+        config.my_custom_config = config_for('custom')
+      RUBY
+      require "#{app_path}/config/environment"
+
+      assert_equal({}, Rails.application.config.my_custom_config)
+    end
+
+    test "config_for with empty json file returns syntax error" do
+      app_file 'config/custom.json', <<-RUBY
+      RUBY
+
+      add_to_config <<-RUBY
+        config.my_custom_config = config_for('custom')
+      RUBY
+      exception = assert_raises(RuntimeError) do
+        require "#{app_path}/config/environment"
+      end
+
+      assert_match 'Syntax error occurred while parsing', exception.message
+    end
+
+    test "config_for json containing ERB tags should evaluate" do
+      app_file 'config/custom.json', <<-RUBY
+      {"development": {
+        "key": "<%= 'custom key' %>"}}
+      RUBY
+
+      add_to_config <<-RUBY
+        config.my_custom_config = config_for('custom')
+      RUBY
+      require "#{app_path}/config/environment"
+
+      assert_equal 'custom key', Rails.application.config.my_custom_config['key']
+    end
+
+    test "config_for json with syntax error show a more descritive exception" do
+      app_file 'config/custom.json', <<-RUBY
+      {"development": {
+        "key": "foo}}
+      RUBY
+
+      add_to_config <<-RUBY
+        config.my_custom_config = config_for('custom')
+      RUBY
+
+      exception = assert_raises(RuntimeError) do
+        require "#{app_path}/config/environment"
+      end
+
+      assert_match 'Syntax error occurred while parsing', exception.message
     end
   end
 end
-- 
2.1.0

Reply via email to