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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment