aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/creating_plugins/controllers.txt
blob: 7afdef032d9dc82f7f172cfe05d91fdd5bf84a68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
== Controllers ==

This section describes how to add a controller named 'woodpeckers' to your plugin that will behave the same as a controller in your main app.  This is very similar to adding a model.

You can test your plugin's controller as you would test any other controller:

*vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:*

[source, ruby]
----------------------------------------------
require File.dirname(__FILE__) + '/test_helper.rb'
require 'woodpeckers_controller'
require 'action_controller/test_process'

class WoodpeckersController; def rescue_action(e) raise e end; end

class WoodpeckersControllerTest < Test::Unit::TestCase
  def setup
    @controller = WoodpeckersController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
    
    ActionController::Routing::Routes.draw do |map|
      map.resources :woodpeckers
    end    
  end

  def test_index
    get :index
    assert_response :success
  end
end
----------------------------------------------

This is just a simple test to make sure the controller is being loaded correctly.  After watching it fail with `rake`, you can make it pass like so:

*vendor/plugins/yaffle/lib/yaffle.rb:*

[source, ruby]
----------------------------------------------
%w{ models controllers }.each do |dir|
  path = File.join(File.dirname(__FILE__), 'app', dir)
  $LOAD_PATH << path
  ActiveSupport::Dependencies.load_paths << path
  ActiveSupport::Dependencies.load_once_paths.delete(path)
end
----------------------------------------------


*vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:*

[source, ruby]
----------------------------------------------
class WoodpeckersController < ActionController::Base
  
  def index
    render :text => "Squawk!"
  end
  
end
----------------------------------------------

Now your test should be passing, and you should be able to use the Woodpeckers controller in your app.  If you add a route for the woodpeckers controller you can start up your server and go to http://localhost:3000/woodpeckers to see your controller in action.