Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # support for using helper methods in controllers/models
- #
- class Helper
- ActionView::Helpers.constants.grep(/Helper/i).each do |const|
- include ActionView::Helpers.const_get(const)
- end
- attr_accessor 'controller'
- def initialize controller = nil
- @controller = controller
- extend @controller.master_helper_module if @controller
- end
- def session
- controller.session
- end
- end
- class ApplicationController < ActionController::Base
- cattr_accessor 'current'
- before_filter{|controller| ApplicationController.current = controller}
- protected
- def helper &block
- @helper = Helper.new(ApplicationController.current || self)
- block ? @helper.instance_eval(&block) : @helper
- end
- def ApplicationController.helper_import *methods
- methods.flatten.compact.each do |method|
- code = <<-code
- def #{ method } *a, &b
- helper{ #{ method }(*a, &b) }
- end
- protected '#{ method }'
- code
- eval code
- end
- end
- end
- class ActiveRecord::Base
- def helper &block
- helper = Helper.new(ApplicationController.current || ApplicationController.new)
- block ? helper.instance_eval(&block) : helper
- end
- end
- # support for immediate (throw/catch) base rendering methods
- #
- class ApplicationController < ActionController::Base
- protected
- def render! *a, &b
- render *a, &b unless a.empty? and b.nil?
- throw :render, self
- end
- def redirect_to! *a, &b
- redirect_to *a, &b
- ensure
- render!
- end
- def redirect_to_url! *a, &b
- redirect_to_url *a, &b
- ensure
- render!
- end
- def not_found! text = 'Not Found'
- render! :text => text, :status => 404
- end
- def perform_action_without_filters
- if self.class.action_methods.include?(action_name)
- catch(:render){ send(action_name) }
- render unless performed?
- elsif respond_to? :method_missing
- catch(:render){ send(:method_missing, action_name) }
- render unless performed?
- elsif template_exists? && template_public?
- render
- else
- raise ::ActionController::UnknownAction, "No action responded to #{action_name}", caller
- end
- end
- end
- # generic error classes used for model level application errors
- #
- class ActiveRecord::Base
- class Error < ::ActiveRecord::ActiveRecordError; end
- class RecordNotFound < ::ActiveRecord::RecordNotFound; end
- end
- class Object
- # support for logging to the rails log from anywhere in the codebase
- #
- def log(*a, &b)
- logger = RAILS_DEFAULT_LOGGER
- if a.empty? and b.nil?
- logger
- else
- logger.info(*a, &b)
- end
- end
- # support for executing sql on the raw connection, getting back an array of
- # results
- #
- def db(*a, &b)
- connection = ActiveRecord::Base.connection.raw_connection
- return connection if a.empty? and b.nil?
- adapter = ActiveRecord::Base.configurations[ RAILS_ENV ][ 'adapter' ].to_s
- case adapter
- when 'oci'
- cursor = connection.exec *a
- if b
- while row = cursor.fetch;
- b.call row
- end
- else
- returning [] do |rows|
- while row = cursor.fetch
- rows << row
- end
- end
- end
- when 'mysql'
- if b
- connection.query(*a).each do |row|
- b.call row
- end
- else
- returning [] do |rows|
- connection.query(*a).each do |row|
- rows << row
- end
- end
- end
- else
- raise ArgumentError, "#{ adapter } not implemented yet"
- end
- end
- end
Add Comment
Please, Sign In to add comment