refactored DomainConstraint and conference_controller_spec

use params id instead of OSEM_HOSTNAME to load conference
This commit is contained in:
shlok007 2017-08-02 20:16:50 +05:30 committed by Shlok Srivastava
parent 66b48be968
commit 835859e350
3 changed files with 18 additions and 9 deletions

View file

@ -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

View file

@ -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
end

View file

@ -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