Prev | Current Page 132 | Next

Brad Ediger

"Advanced Rails"

Here is
the abridged code for the HTTP Authentication plugin??™s class methods:
module HttpAuthentication
module Basic
extend self
module ControllerMethods
def authenticate_or_request_with_http_basic(realm = "Application",
&login_procedure)
authenticate_with_http_basic(&login_procedure) ||
request_http_basic_authentication(realm)
end
def authenticate_with_http_basic(&login_procedure)
HttpAuthentication::Basic.authenticate(self, &login_procedure)
end
def request_http_basic_authentication(realm = "Application")
HttpAuthentication::Basic.authentication_request(self, realm)
end
end
end
end
Now, the methods are self-contained within HttpAuthentication::Basic::
ControllerMethods. A simple statement in the plugin??™s init.rb file adds the methods to
ActionController::Base:
ActionController::Base.send :include,
HttpAuthentication::Basic::ControllerMethods
Testing Plugins
Like the rest of Rails, plugins have very mature testing facilities. However, plugin
tests usually require a bit more work than standard Rails tests, as the tests are
designed to be run on their own, outside of the Rails framework. Some things to
keep in mind when writing tests for plugins:
Testing Plugins | 91
??? Unlike in the Rails plugin initializer, when running tests, load paths are not set up
automatically, and Dependencies does not load missing constants for you. You
need to manually set up the load paths and require any parts of the plugin that you
will be testing, as in this example from the HTTP Authentication plugin:
$LOAD_PATH << File.


Pages:
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144