aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/creating_plugins/models.txt
blob: 505ab44a71220649b4f45c7583f4e84da13b8179 (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
64
65
66
67
68
69
70
71
72
73
74
== Models ==

This section describes how to add a model named 'Woodpecker' to your plugin that will behave the same as a model in your main app.  When storing models, controllers, views and helpers in your plugin, it's customary to keep them in directories that match the rails directories.  For this example, create a file structure like this:

---------------------------------------------------------
vendor/plugins/yaffle/
|-- lib
|   |-- app
|   |   |-- controllers
|   |   |-- helpers
|   |   |-- models
|   |   |   `-- woodpecker.rb
|   |   `-- views
|   |-- yaffle
|   |   |-- acts_as_yaffle.rb
|   |   |-- commands.rb
|   |   `-- core_ext.rb
|   `-- yaffle.rb
---------------------------------------------------------

As always, start with a test:

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

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

class WoodpeckerTest < Test::Unit::TestCase
  load_schema

  def test_woodpecker
    assert_kind_of Woodpecker, Woodpecker.new
  end
end
----------------------------------------------

This is just a simple test to make sure the class 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 }.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
----------------------------------------------

Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser.  Removing directories from the 'load_once_paths' allow those changes to picked up as soon as you save the file - without having to restart the web server.  This is particularly useful as you develop the plugin.


*vendor/plugins/yaffle/lib/app/models/woodpecker.rb:*

[source, ruby]
----------------------------------------------
class Woodpecker < ActiveRecord::Base
end
----------------------------------------------

Finally, add the following to your plugin's 'schema.rb':

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

[source, ruby]
----------------------------------------------
create_table :woodpeckers, :force => true do |t|
  t.string :name
end
----------------------------------------------

Now your test should be passing, and you should be able to use the Woodpecker model from within your rails app, and any changes made to it are reflected immediately when running in development mode.