Skip to content

Instantly share code, notes, and snippets.

@mikebannister
Created December 3, 2011 05:20
Show Gist options
  • Save mikebannister/1426151 to your computer and use it in GitHub Desktop.
Save mikebannister/1426151 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
protect_from_forgery
user_stamp Attachment, Post, Capsule
before_filter :authentication_check
before_filter :capsule_member
before_filter :set_user_time_zone
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, :with => :render_error
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
end
def default_url_options
{:host => CapsuleApp::Application.config.domain}
end
def find_invitee
@capsule ||= find_capsule
@invitee = Invitee.includes(:capsule).where(:user_id => current_user.id, :capsule_id => @capsule.id ).first
redirect_to(root_path, :notice => "Where you going? Nowhere, thats where.") unless @invitee
end
def find_capsule
@capsule ||= @invitee.capsule rescue Capsule.includes([:photos,{:posts => [:attachments, :photos]}, :pins]).where(:id => params[:capsule_id] || params[:id]).first
end
def capsule_member
if @capsule && @current_user
redirect_to root_path unless Invitee.exists?(:user_id => @current_user.id, :capsule_id => @capsule.id)
end
end
private
def set_user_time_zone
Time.zone = ActiveSupport::TimeZone[current_user.time_zone_offset] if current_user
end
def authentication_check
session[:"user_return_to"] = request.fullpath if request.get?
redirect_to login_path, :flash => flash unless signed_in? or invitation_code_valid?
end
def render_not_found(exception)
log_error(exception)
render :template => "content/error_404", :status => 404
end
def render_error(exception)
log_error(exception)
render :template => "content/error_500", :status => 500
end
def log_error(exception)
message = "\n#{exception.class} (#{exception.message}):\n"
message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
message << " " << Rails.backtrace_cleaner.clean(exception.backtrace).join("\n ")
Rails.logger.fatal("#{message}\n\n")
end
def invitation_code_valid?
if params[:controller] == "registrations" && ["new","create"].include?(params[:action])
if InviteCode.valid?(params[:code] || session["invite_code"] )
session["invite_code"] = params[:code]
return true
end
flash[:notice] = "Uh oh, that code no worky. Request an invite below and we will get you access lickity split"
end
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment