From 835859e350ab41d4a93b077412c84cf2b0c37010 Mon Sep 17 00:00:00 2001 From: shlok007 Date: Wed, 2 Aug 2017 20:16:50 +0530 Subject: [PATCH] refactored DomainConstraint and conference_controller_spec use params id instead of OSEM_HOSTNAME to load conference --- app/controllers/conferences_controller.rb | 14 +++++++++----- config/initializers/domain_constraint.rb | 6 +++--- spec/controllers/conferences_controller_spec.rb | 7 ++++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index 6e5dd5dd..f141529d 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -1,7 +1,7 @@ class ConferencesController < ApplicationController protect_from_forgery with: :null_session before_action :respond_to_options - load_and_authorize_resource find_by: :short_title + load_and_authorize_resource find_by: :short_title, except: :show def index @current = Conference.where('end_date >= ?', Date.current).reorder(start_date: :asc) @@ -9,15 +9,19 @@ class ConferencesController < ApplicationController end def show - # have to change "localhost" to ENV['OSEM_HOSTNAME'] in production - check_custom_domain if request.host != 'localhost' + @conference = if params[:id] + Conference.find_by_short_title(params[:id]) + else + load_conference_by_domain + end + authorize! :show, @conference @program = @conference.program end private - def check_custom_domain - @conference = @conference.nil? ? Conference.find_by(custom_domain: request.domain) : @conference + def load_conference_by_domain + Conference.find_by(custom_domain: request.domain) end def respond_to_options diff --git a/config/initializers/domain_constraint.rb b/config/initializers/domain_constraint.rb index 67e19a75..5b35e5c7 100644 --- a/config/initializers/domain_constraint.rb +++ b/config/initializers/domain_constraint.rb @@ -1,6 +1,6 @@ class DomainConstraint def self.matches?(request) - @domains = Conference.pluck(:custom_domain).compact - @domains.include?(request.domain) + domains = Conference.where.not(custom_domain: nil).pluck(:custom_domain) + domains.include?(request.domain) end -end \ No newline at end of file +end diff --git a/spec/controllers/conferences_controller_spec.rb b/spec/controllers/conferences_controller_spec.rb index dffae77a..cdbebf9f 100644 --- a/spec/controllers/conferences_controller_spec.rb +++ b/spec/controllers/conferences_controller_spec.rb @@ -23,12 +23,17 @@ describe ConferencesController do get :show, id: conference.short_title expect(response).to render_template :show end + end - it 'assigns correct conference from a custom domain' do + context 'accessing conference via custom domain' do + before do conference.update_attribute(:custom_domain, 'lvh.me') @request.host = 'lvh.me' + end + it 'assigns correct conference' do get :show + expect(response).to render_template :show expect(assigns(:conference)).to eq conference end