Skip to content

Instantly share code, notes, and snippets.

@sipple
Created May 22, 2014 13:13
Show Gist options
  • Select an option

  • Save sipple/78ff9e167e8922459f01 to your computer and use it in GitHub Desktop.

Select an option

Save sipple/78ff9e167e8922459f01 to your computer and use it in GitHub Desktop.
require_dependency 'trusty_cms'
require 'login_system'
class ApplicationController < ActionController::Base
include LoginSystem
protect_from_forgery
before_filter :set_current_user
before_filter :set_timezone
before_filter :set_user_locale
before_filter :set_javascripts_and_stylesheets
before_filter :force_utf8_params if RUBY_VERSION =~ /1\.9/
before_filter :set_standard_body_style, :only => [:new, :edit, :update, :create]
skip_before_filter :set_current_user
attr_accessor :trusty_config, :cache
attr_reader :pagination_parameters
helper_method :pagination_parameters
def initialize
super
@trusty_config = TrustyCms::Config
end
# helpers to include additional assets from actions or views
helper_method :include_stylesheet, :include_javascript
def include_stylesheet(sheet)
@stylesheets << sheet
end
def include_javascript(script)
@javascripts << script
end
def template_name
case self.action_name
when 'index'
'index'
when 'new','create'
'new'
when 'show'
'show'
when 'edit', 'update'
'edit'
when 'remove', 'destroy'
'remove'
else
self.action_name
end
end
def rescue_action_in_public(exception)
case exception
when ActiveRecord::RecordNotFound, ActionController::UnknownController, ActionController::UnknownAction, ActionController::RoutingError
render :template => "site/not_found", :status => 404
else
super
end
end
private
def set_current_user
UserActionObserver.instance.current_user = current_user
end
def set_user_locale
I18n.locale = current_user && !current_user.locale.blank? ? current_user.locale : TrustyCms::Config['default_locale']
end
def set_timezone
Time.zone = TrustyCms::Config['local.timezone'].empty? ? Time.zone_default : TrustyCms::Config['local.timezone']
end
def set_javascripts_and_stylesheets
@stylesheets ||= []
@stylesheets.concat %w(admin/main)
@javascripts ||= []
end
def set_standard_body_style
@body_classes ||= []
@body_classes.concat(%w(reversed))
end
# When using TrustyCms with Ruby 1.9, the strings that come in from forms are ASCII-8BIT encoded.
# That causes problems, especially when using special chars and with certain DBs, like DB2
# That's why we force the encoding of the params to UTF-8
# That's what's happening in Rails 3, too: https://github.com/rails/rails/commit/25215d7285db10e2c04d903f251b791342e4dd6a
#
# See http://stackoverflow.com/questions/8268778/rails-2-3-9-encoding-of-query-parameters
# See https://rails.lighthouseapp.com/projects/8994/tickets/4807
# See http://jasoncodes.com/posts/ruby19-rails2-encodings (thanks for the following code, Jason!)
def force_utf8_params
traverse = lambda do |object, block|
if object.kind_of?(Hash)
object.each_value { |o| traverse.call(o, block) }
elsif object.kind_of?(Array)
object.each { |o| traverse.call(o, block) }
else
block.call(object)
end
object
end
force_encoding = lambda do |o|
o.force_encoding(Encoding::UTF_8) if o.respond_to?(:force_encoding)
end
traverse.call(params, force_encoding)
end
end
@sipple
Copy link
Copy Markdown
Author

sipple commented May 22, 2014

I found some code online earlier that lets me trace callback requests, and it shows the skipped calls beginning and ending. (I also threw a logging call in to be sure the method was actually running and this debugging code wasn't just showing it being a part of the chain.

(Note: Debugging code interacts poorly with markdown; I edited the debug line for the method being called so it's readable.)

Started GET "/" for 127.0.0.1 at 2014-05-22 09:21:14 -0400
Processing by SiteController#show_page as HTML
Parameters: {"url"=>"/"}
START [String](%28action_name ==))
ApplicationController 2014-05-22 09:21:14 -0400
NO METHOD: for
ENDED (action_name == 'new' || action_name == 'edit' || action_name == 'update' || action_name == 'create') 2014-05-22 09:21:14 -0400
START [String](%28action_name ==))
ApplicationController 2014-05-22 09:21:14 -0400
NO METHOD: for
ENDED (action_name == 'new' || action_name == 'edit' || action_name == 'update' || action_name == 'create') 2014-05-22 09:21:14 -0400
START Symbol
ApplicationController 2014-05-22 09:21:14 -0400
ENDED verify_authenticity_token 2014-05-22 09:21:14 -0400
START Symbol(set_current_user)
ApplicationController 2014-05-22 09:21:14 -0400
SETTING CURRENT USER
ENDED set_current_user 2014-05-22 09:21:14 -0400
START Symbol
ApplicationController 2014-05-22 09:21:14 -0400
ENDED set_timezone 2014-05-22 09:21:14 -0400
START Symbol
ApplicationController 2014-05-22 09:21:14 -0400
ENDED set_user_locale 2014-05-22 09:21:14 -0400
START Symbol
ApplicationController 2014-05-22 09:21:14 -0400
ENDED set_javascripts_and_stylesheets 2014-05-22 09:21:14 -0400
START Symbol
ApplicationController 2014-05-22 09:21:14 -0400
ENDED force_utf8_params 2014-05-22 09:21:14 -0400
START Symbol
SiteController 2014-05-22 09:21:14 -0400
ENDED configure_pagination 2014-05-22 09:21:14 -0400
START Symbol
SiteController 2014-05-22 09:21:14 -0400
Completed 500 Internal Server Error in 5.1ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment