From b732aff523b3d274526b828b684f802cc144b544 Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Fri, 4 Nov 2016 15:34:44 +0200 Subject: [PATCH] Add CORS header in order to access the public API from JavaScript (I have no idea what I'm doing) --- app/controllers/api/application_controller.rb | 5 +++++ app/controllers/api/event_types_controller.rb | 1 + app/controllers/api/events_controller.rb | 1 + app/controllers/api/halls_controller.rb | 1 + app/controllers/api/slots_controller.rb | 1 + app/controllers/api/speakers_controller.rb | 1 + app/controllers/api/tracks_controller.rb | 1 + app/controllers/concerns/public_api_exposing.rb | 9 +++++++++ 8 files changed, 20 insertions(+) create mode 100644 app/controllers/concerns/public_api_exposing.rb diff --git a/app/controllers/api/application_controller.rb b/app/controllers/api/application_controller.rb index b9568e7..461e1dc 100644 --- a/app/controllers/api/application_controller.rb +++ b/app/controllers/api/application_controller.rb @@ -1,2 +1,7 @@ class Api::ApplicationController < ::ApplicationController + def set_access_control_headers + if request.format.json? + response.headers['Access-Control-Allow-Origin'] = '*' + end + end end diff --git a/app/controllers/api/event_types_controller.rb b/app/controllers/api/event_types_controller.rb index 875b988..a49fa3b 100644 --- a/app/controllers/api/event_types_controller.rb +++ b/app/controllers/api/event_types_controller.rb @@ -1,5 +1,6 @@ class Api::EventTypesController < Api::ApplicationController include ::CurrentConferenceAssigning + include ::PublicApiExposing before_filter :require_current_conference! def index diff --git a/app/controllers/api/events_controller.rb b/app/controllers/api/events_controller.rb index 8c9bd8a..a390c72 100644 --- a/app/controllers/api/events_controller.rb +++ b/app/controllers/api/events_controller.rb @@ -1,5 +1,6 @@ class Api::EventsController < Api::ApplicationController include ::CurrentConferenceAssigning + include ::PublicApiExposing before_filter :require_current_conference! def index diff --git a/app/controllers/api/halls_controller.rb b/app/controllers/api/halls_controller.rb index 98c397a..c449b2d 100644 --- a/app/controllers/api/halls_controller.rb +++ b/app/controllers/api/halls_controller.rb @@ -1,5 +1,6 @@ class Api::HallsController < Api::ApplicationController include ::CurrentConferenceAssigning + include ::PublicApiExposing before_filter :require_current_conference! def index diff --git a/app/controllers/api/slots_controller.rb b/app/controllers/api/slots_controller.rb index 4a02389..60cf154 100644 --- a/app/controllers/api/slots_controller.rb +++ b/app/controllers/api/slots_controller.rb @@ -1,5 +1,6 @@ class Api::SlotsController < Api::ApplicationController include ::CurrentConferenceAssigning + include ::PublicApiExposing before_filter :require_current_conference! def index diff --git a/app/controllers/api/speakers_controller.rb b/app/controllers/api/speakers_controller.rb index c98a7fc..cd8cfc5 100644 --- a/app/controllers/api/speakers_controller.rb +++ b/app/controllers/api/speakers_controller.rb @@ -1,5 +1,6 @@ class Api::SpeakersController < Api::ApplicationController include ::CurrentConferenceAssigning + include ::PublicApiExposing before_filter :require_current_conference! def index diff --git a/app/controllers/api/tracks_controller.rb b/app/controllers/api/tracks_controller.rb index e3e5263..87f8ccf 100644 --- a/app/controllers/api/tracks_controller.rb +++ b/app/controllers/api/tracks_controller.rb @@ -1,5 +1,6 @@ class Api::TracksController < Api::ApplicationController include ::CurrentConferenceAssigning + include ::PublicApiExposing before_filter :require_current_conference! def index diff --git a/app/controllers/concerns/public_api_exposing.rb b/app/controllers/concerns/public_api_exposing.rb new file mode 100644 index 0000000..5fc08b8 --- /dev/null +++ b/app/controllers/concerns/public_api_exposing.rb @@ -0,0 +1,9 @@ +require 'active_support/concern' + +module PublicApiExposing + extend ActiveSupport::Concern + + included do + before_action :set_access_control_headers + end +end