Files
mozo-backend/app/controllers/application_controller.rb
T

75 lines
2.2 KiB
Ruby

class ApplicationController < ActionController::Base
before_filter :set_locale
layout :layout_by_resource
protect_from_forgery
rescue_from SimplyStored::RecordNotFound, with: :show_404
private
def authorize_cmtool
redirect_to '/', alert: t('general.unauthorized') unless current_user.present? && current_user.admin?
end
def broadcast_user(uid, event, data = {})
message = {channel: "/user/#{uid}", data: {event: event, data: data}}
uri = URI.parse(event_host)
Net::HTTP.post_form(uri, :message => message.to_json)
end
def broadcast_supplier(sid, event, data = {})
message = {channel: "/supplier/#{sid}", data: {event: event, data: data}}
uri = URI.parse(event_host)
Net::HTTP.post_form(uri, :message => message.to_json)
end
def set_locale
I18n.locale = (params[:locale].presence || :nl).to_sym
end
def layout_by_resource
if devise_controller?
return 'obtain_token' if session[:user_return_to].present?
return 'theme1' if session[:supplier_return_to].present?
end
"theme1"
end
def check_active_list_state
if current_user.try(:active_list_id)
unless active_list.active?
current_user.list_is_closed!
redirect_to user_root_path, alert: t('messages.the_list_has_been_closed', list: List.model_name.human)
end
end
end
def active_list
return nil unless current_user.try(:active_list_id).present?
@active_list ||= List.find(current_user.active_list_id)
end
alias :active_list_object :active_list
helper_method :active_list_object
def js_alert(*args)
options = args.extract_options!
message = args.first || ''
{ok: false, message: message}.merge(options).to_json
end
alias json_alert js_alert
def js_notice(*args)
options = args.extract_options!
message = args.first || ''
{ok: true, message: message}.merge(options).to_json
end
alias json_notice js_notice
# Return the hostname of the event server
def event_host
#TODO: do not do and environment check, its ugly!
"http://#{Rails.env.production? ? 'events.qwaiter.com' : 'localhost'}:9296/faye"
end
helper_method :event_host
def show_404
render 'dashboard/404', layout: true, status: 404
end
end