Skip to content

Instantly share code, notes, and snippets.

@aroop
Forked from dhh/gist:1014971
Created June 16, 2011 18:37
Show Gist options
  • Save aroop/1029911 to your computer and use it in GitHub Desktop.
Save aroop/1029911 to your computer and use it in GitHub Desktop.
Use concerns to keep your models manageable
class WmsWebCma::ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale
helper_method :current_agent, :company_branding
def current_agent
@current_agent ||= (session[:current_agent] ? Agent.find(session[:current_agent]["uuid"]) : nil)
end
def company_branding
@company_branding ||= WmsWebCma::Branding.company(agent_uuid: agent_uuid_for_branding) if current_agent
end
def logged_in?
current_agent
end
def has_subscription?
warn current_agent.subscription('webcma').inspect if current_agent
@has_subscription ||= current_agent && current_agent.subscription('webcma').try(:valid)
end
def has_mld_id?
@has_mld_id ||= current_agent && current_agent.mls_agentid.present?
end
def render_unauthorized(message = "")
@message = message
render :template => "wms_web_cma/errors/unauthorized", :status => :unauthorized
return false
end
def render_no_mls
render :template => "wms_web_cma/errors/no_mls", :status => :unauthorized
return false
end
protected
def agent_uuid_for_branding
if params[:brand_as].present?
agent_uuid = params[:brand_as]
else
agent_uuid = current_agent.uuid
end
agent_uuid
end
private
def set_locale
I18n.locale = params[:locale] if params[:locale].present?
end
def default_url_options(options = {})
{locale: I18n.locale}
end
end
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
# app/models/concerns/trashable.rb
module Trashable
extend ActiveSupport::Concern
included do
default_scope where(trashed: false)
scope :trashed, where(trashed: true)
end
def trash
update_attribute :trashed, true
end
end
# app/models/message.rb
class Message < ActiveRecord::Base
include Trashable, Subscribable, Commentable, Eventable
end
.unauthorized_page
#left_col
#message_container
%h1{:class => "error_title"}
MLS is Required
%br
%span{:class => "error_message"}
We are unable to detect which MLS you are associated with. Please contact the IT help desk for assistance via email (helpdesk@howardhanna.com) or call (412) 967-7100 x236. Standard operating hours are 8:30AM - 5:15PM Monday through Friday.
%br
%br
%span{:class => "error_message"}
Or
%a{ href: logout_sessions_path }
try logging in again.
class WmsWebCma::SessionsController < WmsWebCma::ApplicationController
before_filter RubyCAS::Filter, :only => :new
def index
reset_session
redirect_to new_session_path
end
def new
if has_subscription?
if has_mld_id?
redirect_to documents_path
else
render_no_mls
end
elsif session[:current_agent]
render_unauthorized
else
redirect_to chrome_url(request.original_url, Rails.application.config.app_urls[:chrome])
end
end
def logout
end
def check_status
if has_subscription?
render :json => {success: true}
else
render_unauthorized
end
end
# This will set the domain as the same the one the request came from so that
# If we're on gohanna, then chrome url is chrome.gohanna.com
#
# Also sets the service param so that chrome redirects here after login
def chrome_url(request_url, chrome_url)
request_uri = URI(request_url)
chrome_uri = URI(chrome_url)
re = /^(.+\.)?(.+\..+|localhost)$/
domain = request_uri.host.gsub(re,'\2')
subdomain = chrome_uri.host.gsub(re,'\1')
unless domain =~ /^.+\.dev$/ || domain =~ /localhost/
chrome_uri.host = subdomain + domain
end
"#{chrome_uri.to_s}/sessions?service=#{request.original_url}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment