diff options
Diffstat (limited to 'railties/doc/guides/source/creating_plugins')
10 files changed, 85 insertions, 54 deletions
diff --git a/railties/doc/guides/source/creating_plugins/appendix.txt b/railties/doc/guides/source/creating_plugins/appendix.txt index 19f677c5fd..5c3bd20a1b 100644 --- a/railties/doc/guides/source/creating_plugins/appendix.txt +++ b/railties/doc/guides/source/creating_plugins/appendix.txt @@ -10,6 +10,32 @@ If you prefer to use RSpec instead of tets, you may be interested in the http:// * http://daddy.platte.name/2007/05/rails-plugins-keep-initrb-thin.html * http://www.mbleigh.com/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins +=== Contents of 'lib/yaffle.rb' === + +*vendor/plugins/yaffle/lib/yaffle.rb:* + +[source, ruby] +---------------------------------------------- +require "yaffle/core_ext" +require "yaffle/acts_as_yaffle" +require "yaffle/commands" +require "yaffle/routing" + +%w{ models controllers helpers }.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 + +# optionally: +# Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file| +# require file +# end + +---------------------------------------------- + + === Final plugin directory structure === The final plugin should have a directory structure that looks something like this: @@ -47,7 +73,8 @@ The final plugin should have a directory structure that looks something like thi | |-- yaffle | | |-- acts_as_yaffle.rb | | |-- commands.rb -| | `-- core_ext.rb +| | |-- core_ext.rb +| | `-- routing.rb | `-- yaffle.rb |-- pkg | `-- yaffle-0.0.1.gem @@ -63,6 +90,7 @@ The final plugin should have a directory structure that looks something like thi | |-- definition_generator_test.rb | |-- migration_generator_test.rb | |-- route_generator_test.rb +| |-- routes_test.rb | |-- schema.rb | |-- test_helper.rb | |-- woodpecker_test.rb diff --git a/railties/doc/guides/source/creating_plugins/controllers.txt b/railties/doc/guides/source/creating_plugins/controllers.txt index e38cf8251e..7afdef032d 100644 --- a/railties/doc/guides/source/creating_plugins/controllers.txt +++ b/railties/doc/guides/source/creating_plugins/controllers.txt @@ -19,6 +19,10 @@ class WoodpeckersControllerTest < Test::Unit::TestCase @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 diff --git a/railties/doc/guides/source/creating_plugins/core_ext.txt b/railties/doc/guides/source/creating_plugins/core_ext.txt index efef0e1f70..cbedb9eaf2 100644 --- a/railties/doc/guides/source/creating_plugins/core_ext.txt +++ b/railties/doc/guides/source/creating_plugins/core_ext.txt @@ -67,13 +67,13 @@ $ ./script/console === Working with init.rb === -When rails loads plugins it looks for the file named init.rb. However, when the plugin is initialized, 'init.rb' is invoked via `eval` (not `require`) so it has slightly different behavior. +When rails loads plugins it looks for the file named 'init.rb' or 'rails/init.rb'. However, when the plugin is initialized, 'init.rb' is invoked via `eval` (not `require`) so it has slightly different behavior. Under certain circumstances if you reopen classes or modules in 'init.rb' you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from `init.rb`, as shown above. If you must reopen a class in `init.rb` you can use `module_eval` or `class_eval` to avoid any issues: -*vendor/plugins/yaffle/init.rb* +*vendor/plugins/yaffle/rails/init.rb* [source, ruby] --------------------------------------------------- @@ -86,7 +86,7 @@ end Another way is to explicitly define the top-level module space for all modules and classes, like `::Hash`: -*vendor/plugins/yaffle/init.rb* +*vendor/plugins/yaffle/rails/init.rb* [source, ruby] --------------------------------------------------- diff --git a/railties/doc/guides/source/creating_plugins/generator_commands.txt b/railties/doc/guides/source/creating_plugins/generator_commands.txt index 3ace3c7318..f60ea3d8f1 100644 --- a/railties/doc/guides/source/creating_plugins/generator_commands.txt +++ b/railties/doc/guides/source/creating_plugins/generator_commands.txt @@ -17,14 +17,6 @@ require 'rails_generator/scripts/destroy' class RouteGeneratorTest < Test::Unit::TestCase - def fake_rails_root - File.join(File.dirname(__FILE__), "rails_root") - end - - def routes_path - File.join(fake_rails_root, "config", "routes.rb") - end - def setup FileUtils.mkdir_p(File.join(fake_rails_root, "config")) end @@ -43,13 +35,13 @@ class RouteGeneratorTest < Test::Unit::TestCase File.open(routes_path, 'wb') {|f| f.write(content) } Rails::Generator::Scripts::Generate.new.run(["yaffle_route"], :destination => fake_rails_root) - assert_match /map\.yaffle/, File.read(routes_path) + assert_match /map\.yaffles/, File.read(routes_path) end def test_destroys_route content = <<-END ActionController::Routing::Routes.draw do |map| - map.yaffle + map.yaffles map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end @@ -57,8 +49,19 @@ class RouteGeneratorTest < Test::Unit::TestCase File.open(routes_path, 'wb') {|f| f.write(content) } Rails::Generator::Scripts::Destroy.new.run(["yaffle_route"], :destination => fake_rails_root) - assert_no_match /map\.yaffle/, File.read(routes_path) + assert_no_match /map\.yaffles/, File.read(routes_path) end + + private + + def fake_rails_root + File.join(File.dirname(__FILE__), "rails_root") + end + + def routes_path + File.join(fake_rails_root, "config", "routes.rb") + end + end ----------------------------------------------------------- @@ -86,7 +89,7 @@ module Yaffle #:nodoc: logger.route "map.yaffle" look_for = 'ActionController::Routing::Routes.draw do |map|' unless options[:pretend] - gsub_file('config/routes.rb', /(#{Regexp.escape(look_for)})/mi){|match| "#{match}\n map.yaffle\n"} + gsub_file('config/routes.rb', /(#{Regexp.escape(look_for)})/mi){|match| "#{match}\n map.yaffles\n"} end end end @@ -94,7 +97,7 @@ module Yaffle #:nodoc: module Destroy def yaffle_route logger.route "map.yaffle" - gsub_file 'config/routes.rb', /\n.+?map\.yaffle/mi, '' + gsub_file 'config/routes.rb', /\n.+?map\.yaffles/mi, '' end end diff --git a/railties/doc/guides/source/creating_plugins/generators.txt b/railties/doc/guides/source/creating_plugins/generators.txt index eb0fbb5ee9..8ef46561d1 100644 --- a/railties/doc/guides/source/creating_plugins/generators.txt +++ b/railties/doc/guides/source/creating_plugins/generators.txt @@ -32,14 +32,6 @@ require 'rails_generator/scripts/generate' class DefinitionGeneratorTest < Test::Unit::TestCase - def fake_rails_root - File.join(File.dirname(__FILE__), 'rails_root') - end - - def file_list - Dir.glob(File.join(fake_rails_root, "*")) - end - def setup FileUtils.mkdir_p(fake_rails_root) @original_files = file_list @@ -54,6 +46,16 @@ class DefinitionGeneratorTest < Test::Unit::TestCase new_file = (file_list - @original_files).first assert_equal "definition.txt", File.basename(new_file) end + + private + + def fake_rails_root + File.join(File.dirname(__FILE__), 'rails_root') + end + + def file_list + Dir.glob(File.join(fake_rails_root, "*")) + end end ------------------------------------------------------------------ diff --git a/railties/doc/guides/source/creating_plugins/helpers.txt b/railties/doc/guides/source/creating_plugins/helpers.txt index c2273813dd..fa4227be41 100644 --- a/railties/doc/guides/source/creating_plugins/helpers.txt +++ b/railties/doc/guides/source/creating_plugins/helpers.txt @@ -30,8 +30,6 @@ This is just a simple test to make sure the helper is being loaded correctly. A ActiveSupport::Dependencies.load_paths << path ActiveSupport::Dependencies.load_once_paths.delete(path) end - -ActionView::Base.send :include, WoodpeckersHelper ---------------------------------------------- diff --git a/railties/doc/guides/source/creating_plugins/migrations.txt b/railties/doc/guides/source/creating_plugins/migrations.txt index d158004ea3..e7d2e09069 100644 --- a/railties/doc/guides/source/creating_plugins/migrations.txt +++ b/railties/doc/guides/source/creating_plugins/migrations.txt @@ -108,29 +108,21 @@ This example will demonstrate how to use one of the built-in generator methods n require File.dirname(__FILE__) + '/test_helper.rb' require 'rails_generator' require 'rails_generator/scripts/generate' -require 'rails_generator/scripts/destroy' class MigrationGeneratorTest < Test::Unit::TestCase - def fake_rails_root - File.join(File.dirname(__FILE__), 'rails_root') - end - - def file_list - Dir.glob(File.join(fake_rails_root, "db", "migrate", "*")) - end - def setup FileUtils.mkdir_p(fake_rails_root) @original_files = file_list end def teardown + ActiveRecord::Base.pluralize_table_names = true FileUtils.rm_r(fake_rails_root) end def test_generates_correct_file_name - Rails::Generator::Scripts::Generate.new.run(["yaffle", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"], :destination => fake_rails_root) + Rails::Generator::Scripts::Generate.new.run(["yaffle_migration", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"], :destination => fake_rails_root) new_file = (file_list - @original_files).first assert_match /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migrations/, new_file assert_match /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migrations do |t|/, File.read(new_file) @@ -138,12 +130,21 @@ class MigrationGeneratorTest < Test::Unit::TestCase def test_pluralizes_properly ActiveRecord::Base.pluralize_table_names = false - Rails::Generator::Scripts::Generate.new.run(["yaffle", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"], :destination => fake_rails_root) + Rails::Generator::Scripts::Generate.new.run(["yaffle_migration", "some_name_nobody_is_likely_to_ever_use_in_a_real_migration"], :destination => fake_rails_root) new_file = (file_list - @original_files).first assert_match /add_yaffle_fields_to_some_name_nobody_is_likely_to_ever_use_in_a_real_migration/, new_file assert_match /add_column :some_name_nobody_is_likely_to_ever_use_in_a_real_migration do |t|/, File.read(new_file) end + + private + def fake_rails_root + File.join(File.dirname(__FILE__), 'rails_root') + end + def file_list + Dir.glob(File.join(fake_rails_root, "db", "migrate", "*")) + end + end ------------------------------------------------------------------ @@ -156,7 +157,7 @@ After running the test with 'rake' you can make it pass with: [source, ruby] ------------------------------------------------------------------ -class YaffleGenerator < Rails::Generator::NamedBase +class YaffleMigrationGenerator < Rails::Generator::NamedBase def manifest record do |m| m.migration_template 'migration:migration.rb', "db/migrate", {:assigns => yaffle_local_assigns, @@ -169,6 +170,7 @@ class YaffleGenerator < Rails::Generator::NamedBase def custom_file_name custom_name = class_name.underscore.downcase custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names + custom_name end def yaffle_local_assigns diff --git a/railties/doc/guides/source/creating_plugins/models.txt b/railties/doc/guides/source/creating_plugins/models.txt index dfe11f9c4e..8b66de0f99 100644 --- a/railties/doc/guides/source/creating_plugins/models.txt +++ b/railties/doc/guides/source/creating_plugins/models.txt @@ -66,10 +66,8 @@ Finally, add the following to your plugin's 'schema.rb': [source, ruby] ---------------------------------------------- -ActiveRecord::Schema.define(:version => 0) do - create_table :woodpeckers, :force => true do |t| - t.string :name - end +create_table :woodpeckers, :force => true do |t| + t.string :name end ---------------------------------------------- diff --git a/railties/doc/guides/source/creating_plugins/routes.txt b/railties/doc/guides/source/creating_plugins/routes.txt index cdc20e998e..249176729c 100644 --- a/railties/doc/guides/source/creating_plugins/routes.txt +++ b/railties/doc/guides/source/creating_plugins/routes.txt @@ -22,10 +22,6 @@ class RoutingTest < Test::Unit::TestCase private - # yes, I know about assert_recognizes, but it has proven problematic to - # use in these tests, since it uses RouteSet#recognize (which actually - # tries to instantiate the controller) and because it uses an awkward - # parameter order. def assert_recognition(method, path, options) result = ActionController::Routing::Routes.recognize_path(path, :method => method) assert_equal options, result @@ -33,15 +29,16 @@ class RoutingTest < Test::Unit::TestCase end -------------------------------------------------------- -*vendor/plugins/yaffle/init.rb* +Once you see the tests fail by running 'rake', you can make them pass with: + +*vendor/plugins/yaffle/lib/yaffle.rb* [source, ruby] -------------------------------------------------------- -require "routing" -ActionController::Routing::RouteSet::Mapper.send :include, Yaffle::Routing::MapperExtensions +require "yaffle/routing" -------------------------------------------------------- -*vendor/plugins/yaffle/lib/routing.rb* +*vendor/plugins/yaffle/lib/yaffle/routing.rb* [source, ruby] -------------------------------------------------------- @@ -54,6 +51,8 @@ module Yaffle #:nodoc: end end end + +ActionController::Routing::RouteSet::Mapper.send :include, Yaffle::Routing::MapperExtensions -------------------------------------------------------- *config/routes.rb* @@ -61,7 +60,6 @@ end [source, ruby] -------------------------------------------------------- ActionController::Routing::Routes.draw do |map| - ... map.yaffles end -------------------------------------------------------- diff --git a/railties/doc/guides/source/creating_plugins/tasks.txt b/railties/doc/guides/source/creating_plugins/tasks.txt index c71ba42bb0..d848c2cfa1 100644 --- a/railties/doc/guides/source/creating_plugins/tasks.txt +++ b/railties/doc/guides/source/creating_plugins/tasks.txt @@ -19,9 +19,7 @@ end When you run `rake -T` from your plugin you will see: --------------------------------------------------------- -... yaffle:squawk # Prints out the word 'Yaffle' -... --------------------------------------------------------- You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up. |