lib/merb/merb_controller.rb in merb-0.2.0 vs lib/merb/merb_controller.rb in merb-0.3.0
- old
+ new
@@ -10,48 +10,56 @@
# params and cookies and headers. If the request is a file upload it will
# stream it into a tempfile and pass in the filename and tempfile object
# to your controller via params. It also parses the ?query=string and
# puts that into params as well.
class Controller
-
- trait :layout => :application
- trait :session_id_key => :_session_id
-
+
+ class_inheritable_accessor :_layout,
+ :_session_id_key,
+ :_template_extensions,
+ :_template_root,
+ :_layout_root
+ self._layout = :application
+ self._session_id_key = :_session_id
+ self._template_extensions = { }
+ self._template_root = File.expand_path(MERB_ROOT / "dist/app/views")
+ self._layout_root = File.expand_path(MERB_ROOT / "dist/app/views/layout")
+
include Merb::ControllerMixin
include Merb::RenderMixin
include Merb::ResponderMixin
attr_accessor :status, :body, :request
MULTIPART_REGEXP = /\Amultipart\/form-data.*boundary=\"?([^\";,]+)/n.freeze
-
+
# parses the http request into params, headers and cookies
# that you can use in your controller classes. Also handles
# file uploads by writing a tempfile and passing a reference
# in params.
def initialize(request, env, args, response)
@env = MerbHash[env.to_hash]
- @status, @method, @response, @headers = 200, (env['REQUEST_METHOD']||'GET').downcase.to_sym, response,
- {'Content-Type' =>'text/html'}
- cookies = query_parse(@env['HTTP_COOKIE'], ';,')
- querystring = query_parse(@env['QUERY_STRING'])
+ @status, @method, @response, @headers = 200, (env[Mongrel::Const::REQUEST_METHOD]||Mongrel::Const::GET).downcase.to_sym, response,
+ Mongrel::Const::CONTENT_TYPE_TEXT_HTML_HASH
+ cookies = query_parse(@env[Mongrel::Const::HTTP_COOKIE], ';,')
+ querystring = query_parse(@env[Mongrel::Const::QUERY_STRING])
- if MULTIPART_REGEXP =~ @env['CONTENT_TYPE'] && @method == :post
+ if MULTIPART_REGEXP =~ @env[Mongrel::Const::UPCASE_CONTENT_TYPE] && @method == :post
querystring.update(parse_multipart(request, $1))
elsif @method == :post
- if ['application/json', 'text/x-json'].include?(@env['CONTENT_TYPE'])
+ if [Mongrel::Const::APPLICATION_JSON, Mongrel::Const::TEXT_JSON].include?(@env[Mongrel::Const::UPCASE_CONTENT_TYPE])
MERB_LOGGER.info("JSON Request")
json = JSON.parse(request.read || "") || {}
json = MerbHash.new(json) if json.is_a? Hash
querystring.update(json)
else
querystring.update(query_parse(request.read))
end
end
@cookies, @params = cookies, querystring.update(args)
- @cookies[ancestral_trait[:session_id_key]] = @params[ancestral_trait[:session_id_key]] if @params.key?(ancestral_trait[:session_id_key])
+ @cookies[_session_id_key] = @params[_session_id_key] if @params.key?(_session_id_key)
allow = [:post, :put, :delete]
allow << :get if MERB_ENV == 'development'
if @params.key?(:_method) && allow.include?(@method)
@method = @params.delete(:_method).downcase.intern
@@ -79,11 +87,11 @@
else
raise MerbControllerError, "The before filter chain is broken dude. wtf?"
end
call_filters(after_filters)
finalize_session if respond_to?:finalize_session
- MERB_LOGGER.info("Time spent in #{action} action: #{Time.now - start} seconds")
+ MERB_LOGGER.info("Time spent in #{self.class}##{action} action: #{Time.now - start} seconds")
end
# override this method on your controller classes to specialize
# the output when the filter chain is halted.
def filters_halted
@@ -124,39 +132,37 @@
# never @response directly.
def response
@response
end
- trait :template_extensions => { }
- trait :template_root => File.expand_path(MERB_ROOT / "dist/app/views")
- trait :layout_root => File.expand_path(MERB_ROOT / "dist/app/views/layout")
+
# lookup the trait[:template_extensions] for the extname of the filename
# you pass. Answers with the engine that matches the extension, Template::Erubis
# is used if none matches.
def engine_for(file)
extension = File.extname(file)[1..-1]
- ancestral_trait[:template_extensions][extension]
+ _template_extensions[extension]
rescue
::Merb::Template::Erubis
end
# This method is called by templating-engines to register themselves with
# a list of extensions that will be looked up on #render of an action.
def self.register_engine(engine, *extensions)
[extensions].flatten.uniq.each do |ext|
- trait[:template_extensions][ext] = engine
+ _template_extensions[ext] = engine
end
end
# shared_accessor sets up a class instance variable that can
# be unique for each class but also inherits the shared attrs
# from its superclasses. Since @@class variables are almost
# global vars within an inheritance tree, we use
# @class_instance_variables instead
- shared_accessor :before_filters
- shared_accessor :after_filters
+ class_inheritable_accessor :before_filters
+ class_inheritable_accessor :after_filters
# calls a filter chain according to rules.
def call_filters(filter_set)
(filter_set || []).each do |(filter, rule)|
ok = false