Created
May 12, 2009 19:55
-
-
Save tadman/110699 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
before_filter :set_time_zone | |
protected | |
# -- Page Title ----------------------------------------------------------- | |
def page_title | |
@page_title ||= [ ] | |
end | |
def page_operation | |
@page_operation ||= [ ] | |
end | |
def redirect_if_logged_in | |
redirect_to root_path if (logged_in?) | |
end | |
def respond_not_logged_in | |
@response = :not_logged_in | |
redirect_to(login_path(:r => request.request_uri)) | |
end | |
# -- Render Delegation Methods -------------------------------------------- | |
def render_not_found(options = { }) | |
self.page_title << (options.delete(:title) or 'Not Found') | |
render({ | |
:partial => 'not_found', | |
:status => 404, | |
:layout => 'application' | |
}.merge(options)) | |
false | |
end | |
def render_removed(options = { }) | |
self.page_title << (options.delete(:title) or 'Removed') | |
render({ | |
:partial => 'removed', | |
:status => 404, | |
:layout => 'application' | |
}.merge(options)) | |
false | |
end | |
def render_unpublished(options = { }) | |
self.page_title << (options.delete(:title) or 'Unpublished Item') | |
render({ | |
:partial => 'unpublished', | |
:status => 403, | |
:layout => 'application' | |
}.merge(options)) | |
false | |
end | |
def render_missing_params(options = { }) | |
self.page_title << (options.delete(:title) or 'Missing Parameters') | |
render({ | |
:partial => 'missing_params', | |
:status => 404, | |
:layout => 'application' | |
}.merge(options)) | |
false | |
end | |
def delegate_render(method) | |
send(method.to_sym) | |
render(:action => method.to_s) | |
end | |
# -- Return URL Support --------------------------------------------------- | |
helper_method :return_url | |
def return_url(fallback = nil) | |
@return_url or params[:r] or request.referer or fallback or url_for(:action => 'index') | |
end | |
def return_url=(value) | |
@return_url = value | |
end | |
def current_url | |
request.request_uri | |
end | |
def return_to_current_url | |
self.return_url = self.current_url | |
end | |
# -- Session Information -------------------------------------------------- | |
helper_method :session_user_id | |
def session_user_id | |
session[:user_id] | |
end | |
helper_method :session_user_ip | |
def session_user_ip | |
request.remote_ip | |
end | |
def session_user=(user) | |
case (user) | |
when nil, false: | |
session[:username] = nil | |
session[:user_id] = nil | |
session[:email] = nil | |
cookies[:name] = nil | |
else | |
session[:username] = user.username | |
session[:user_id] = user.id | |
session[:email] = user.email | |
cookies[:name] = user.username | |
end | |
end | |
helper_method :session_user_name | |
def session_user_name | |
session_user and session_user.username or cookies[:name] | |
end | |
def session_user_name=(value) | |
cookies[:name] = value | |
end | |
helper_method :admin? | |
def admin? | |
session_user and session_user.admin? | |
end | |
# -- Parameter Processing ------------------------------------------------- | |
def trim_params!(*list) | |
list = params.keys if (list.empty?) | |
list.flatten.each do |key| | |
case (params[key]) | |
when String: | |
params[key].trim! | |
end | |
end | |
end | |
def search_params(additional = { }) | |
# FIXME: Force some parameters to numeric only | |
options = { | |
:per_page => 25 | |
}.merge(additional.symbolize_keys) | |
params[:search] ? options.merge(params[:search].symbolize_keys) : options | |
end | |
def remote_ip | |
request.remote_ip | |
end | |
model_method :remote_ip | |
# -- Time Zone Management ------------------------------------------------- | |
def set_time_zone | |
# FUTURE: Set based on user local settings | |
Time.zone = 'Eastern Time (US & Canada)' | |
end | |
# -- Git Integration ------------------------------------------------------ | |
helper_method :current_branch | |
def current_branch | |
if (File.exists?("#{RAILS_ROOT}/config/.git_branch")) | |
File.open("#{RAILS_ROOT}/config/.git_branch") do |fh| | |
return fh.readline.split(/\//)[-1] | |
end | |
else | |
File.open("#{RAILS_ROOT}/.git/HEAD") do |fh| | |
return fh.readline.split(/\//)[-1] | |
end | |
end | |
end | |
helper_method :version_info | |
def version_info | |
[ Rails.env, current_branch ] * ' - ' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def page_headline | |
@page_title ||= [ ] | |
@page_title.collect do |i| | |
case (i) | |
when Array: | |
link_to(i[0], i[1]) | |
else | |
i | |
end | |
end.compact.join(' > ') + | |
(@page_operation.blank? ? '' : " (#{@page_operation.join(' ')})") | |
end | |
def page_title | |
@page_title ||= [ ] | |
([ 'APP NAME' ] + @page_title).collect do |i| | |
case (i) | |
when Array: | |
i[0] | |
else | |
i | |
end | |
end.join(' > ') + | |
(@page_operation.blank? ? '' : " (#{@page_operation.join(' ')})") | |
end | |
def return_field(default = nil) | |
hidden_field_tag(:r, url_for(return_url(default || request.referer))) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Examples::BaseController < ApplicationController | |
before_filter :load_example | |
protected | |
def load_example | |
@example = Example.find(params[:example_id] || params[:id]) | |
self.page_title << [ @example.name.html_safe, example_path(@example) ] | |
rescue ActiveRecord::RecordNotFound | |
render_not_found(:partial => 'examples/not_found') | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Accounts::ExamplesController < Examples::BaseController | |
skip_before_filter :load_example, :only => [ :index, :new, :create ] | |
before_filter :build_example, :only => [ :new, :create ] | |
def index | |
end | |
def new | |
self.page_title << 'Create Example' | |
end | |
def create | |
@example.save! | |
redirect_to(example_path(@example)) | |
rescue ActiveRecord::RecordInvalid | |
delegate_render(:new) | |
end | |
def show | |
end | |
def edit | |
self.page_operation << 'Edit' | |
end | |
def update | |
@example.update_attributes!(params[:example]) | |
flash[:notice] = { | |
:title => 'Success', | |
:text => 'Example updated.' | |
} | |
redirect_to(example_path(@example)) | |
rescue ActiveRecord::RecordInvalid | |
delegate_render(:edit) | |
end | |
def destroy | |
@example.destroy | |
redirect_to(examples_path) | |
end | |
protected | |
def build_example | |
@example = Example.new(params[:example]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment