Please save the S2Rails initialization file in the config/initializers directory of the Rails project directory.
Please add the following process method to the ApplicationController class defined in app/controllers/application_controller.rb of the Rails project directory.
class ApplicationController < ActionController::Base ... def self.process(request, response) container = S2Rails.get_s2container(request) if container && container.has_component_def(self) result = container.get(self).process(request, response) begin container.destroy rescue => e s2logger.error(self) {"destroy of s2container failed. #{e.message} #{e.backtrace}"} end return result else return super # new.process(request, response); end end ... end
In S2Rails configuration file (s2rails.rb) installed by the setup, the following three processing is executed.
In S2Rails::Rack Middleware, S2ApplicationContext has been initialized.
The instantiate_controller method is called by the process method which had been added to ApplicationController at the setup.
S2Container that generates ActionController is generated with S2ApplicationContext initialized with S2Rails::Rack Middleware. The controller name is used for namespace specified when S2Container is generated. Moreover, namespace set to the S2Rails.IncludeNamespaces constant is used. %w[services daos models interceptors dbi] is set to the S2Rails.IncludeNamespaces constant as a default value. S2Container with the component included in these namespace is generated at each request.
Caution | |
---|---|
"Eager loading" of the class definition is executed at each request even when the environmental setting is "Development" because the component registration is executed in S2ApplicationContext at the time of loadding the class definition. |
As an example, Let's create sample conrtoller.
% ruby script/generate controller sample ... create app/controllers/sample_controller.rb ... %
Please edit the sample controller as follows. It registers as a component by the s2component method. When the namespace option is omitted, controller name "sample" is used for namespace. The accessor method that receives the hello component is defined with attr_accessor method.
class SampleController < ApplicationController s2component attr_accessor :hello def index @result = @hello.world end end
Next, please make the template file of the index action.
<%=h @result %>;
Let's make the Hello class to the app/models directory. By specifying the namespace as "models", the Hello component can be used also with ActionController other than SampleController. Namespace used by all requests is set to the S2Rails.IncludeNamespaces constant array. To use it only with SampleController, namespace should be set as "sample".
class Hello s2comp :namespace => 'models' def world return 'Hello World' end end
When you access "http://localhost:3000/sample/index" by a browser, it will be displayed as "Hello World".
© Copyright The Seasar Foundation and the others 2008-2009, all rights reserved. |