Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / creating_plugins / models.txt
1 == Add a model ==
2
3 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:
4
5 ---------------------------------------------------------
6 vendor/plugins/yaffle/
7 |-- lib
8 | |-- app
9 | | |-- controllers
10 | | |-- helpers
11 | | |-- models
12 | | | `-- woodpecker.rb
13 | | `-- views
14 | |-- yaffle
15 | | |-- acts_as_yaffle.rb
16 | | |-- commands.rb
17 | | `-- core_ext.rb
18 | `-- yaffle.rb
19 ---------------------------------------------------------
20
21 As always, start with a test:
22
23 *vendor/plugins/yaffle/yaffle/woodpecker_test.rb:*
24
25 [source, ruby]
26 ----------------------------------------------
27 require File.dirname(__FILE__) + '/test_helper.rb'
28
29 class WoodpeckerTest < Test::Unit::TestCase
30 load_schema
31
32 def test_woodpecker
33 assert_kind_of Woodpecker, Woodpecker.new
34 end
35 end
36 ----------------------------------------------
37
38 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:
39
40 *vendor/plugins/yaffle/lib/yaffle.rb:*
41
42 [source, ruby]
43 ----------------------------------------------
44 %w{ models }.each do |dir|
45 path = File.join(File.dirname(__FILE__), 'app', dir)
46 $LOAD_PATH << path
47 ActiveSupport::Dependencies.load_paths << path
48 ActiveSupport::Dependencies.load_once_paths.delete(path)
49 end
50 ----------------------------------------------
51
52 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.
53
54
55 *vendor/plugins/yaffle/lib/app/models/woodpecker.rb:*
56
57 [source, ruby]
58 ----------------------------------------------
59 class Woodpecker < ActiveRecord::Base
60 end
61 ----------------------------------------------
62
63 Finally, add the following to your plugin's 'schema.rb':
64
65 *vendor/plugins/yaffle/test/schema.rb:*
66
67 [source, ruby]
68 ----------------------------------------------
69 ActiveRecord::Schema.define(:version => 0) do
70 create_table :woodpeckers, :force => true do |t|
71 t.string :name
72 end
73 end
74 ----------------------------------------------
75
76 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.