aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJorge Bejar <jorge@wyeworks.com>2015-05-27 21:10:29 -0300
committerSantiago Pastorino <santiago@wyeworks.com>2015-06-11 16:54:16 -0300
commitebcc15ca4ea22f8ace57a5251ceb8de4b917cd90 (patch)
tree1d4d935bdb1ccd123ef5b9de891b767d340688e8
parent72d0784611af76dd0bb3154a662c6ec14a022f4c (diff)
downloadrails-ebcc15ca4ea22f8ace57a5251ceb8de4b917cd90.tar.gz
rails-ebcc15ca4ea22f8ace57a5251ceb8de4b917cd90.tar.bz2
rails-ebcc15ca4ea22f8ace57a5251ceb8de4b917cd90.zip
Add rake-cors gem (commented) in Gemfile for rails api apps
-rw-r--r--railties/lib/rails/generators/rails/app/templates/Gemfile3
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/initializers/cors.rb14
-rw-r--r--railties/test/generators/api_app_generator_test.rb3
3 files changed, 20 insertions, 0 deletions
diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile
index cf811503be..606f1d4f96 100644
--- a/railties/lib/rails/generators/rails/app/templates/Gemfile
+++ b/railties/lib/rails/generators/rails/app/templates/Gemfile
@@ -25,6 +25,9 @@ source 'https://rubygems.org'
# Use ActiveModelSerializers to serialize JSON responses
gem 'active_model_serializers', '~> 0.10.0.rc1'
+# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
+# gem 'rack-cors'
+
<%- end -%>
<% if RUBY_ENGINE == 'ruby' -%>
group :development, :test do
diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/cors.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/cors.rb
new file mode 100644
index 0000000000..45c44d24f8
--- /dev/null
+++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/cors.rb
@@ -0,0 +1,14 @@
+# Avoid CORS issues when API is called from the frontend app
+# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests
+
+# Read more: https://github.com/cyu/rack-cors
+
+# Rails.application.config.middleware.insert_before 0, "Rack::Cors" do
+# allow do
+# origins 'example.com'
+#
+# resource '*',
+# headers: :any,
+# methods: [:get, :post, :put, :patch, :delete, :options, :head]
+# end
+# end
diff --git a/railties/test/generators/api_app_generator_test.rb b/railties/test/generators/api_app_generator_test.rb
index 16c0f1ae82..94d1008aae 100644
--- a/railties/test/generators/api_app_generator_test.rb
+++ b/railties/test/generators/api_app_generator_test.rb
@@ -45,9 +45,12 @@ class ApiAppGeneratorTest < Rails::Generators::TestCase
assert_match(/config.api_only = true/, content)
end
+ assert_file "config/initializers/cors.rb"
+
assert_file "config/initializers/wrap_parameters.rb" do |content|
assert_no_match(/wrap_parameters/, content)
end
+
assert_file "app/controllers/application_controller.rb", /ActionController::API/
end