From b732aff523b3d274526b828b684f802cc144b544 Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Fri, 4 Nov 2016 15:34:44 +0200 Subject: [PATCH 1/2] 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 From b5192a89a9027fad5e8aebcb4fb3e7ba58c6d136 Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Thu, 17 Nov 2016 18:46:40 +0200 Subject: [PATCH 2/2] Move method to a more appropriate place --- app/controllers/api/application_controller.rb | 5 ----- app/controllers/concerns/public_api_exposing.rb | 6 ++++++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/application_controller.rb b/app/controllers/api/application_controller.rb index 461e1dc..b9568e7 100644 --- a/app/controllers/api/application_controller.rb +++ b/app/controllers/api/application_controller.rb @@ -1,7 +1,2 @@ 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/concerns/public_api_exposing.rb b/app/controllers/concerns/public_api_exposing.rb index 5fc08b8..17b541e 100644 --- a/app/controllers/concerns/public_api_exposing.rb +++ b/app/controllers/concerns/public_api_exposing.rb @@ -3,6 +3,12 @@ require 'active_support/concern' module PublicApiExposing extend ActiveSupport::Concern + def set_access_control_headers + if request.format.json? + response.headers['Access-Control-Allow-Origin'] = '*' + end + end + included do before_action :set_access_control_headers end