diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/contributing_to_rails.textile | 2 | ||||
-rw-r--r-- | railties/guides/source/generators.textile | 64 | ||||
-rw-r--r-- | railties/guides/source/initialization.textile | 4 | ||||
-rw-r--r-- | railties/lib/rails/commands/dbconsole.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/commands/runner.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/generators/actions.rb | 4 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/app_generator.rb | 3 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml | 17 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/generator/USAGE | 5 | ||||
-rw-r--r-- | railties/lib/rails/tasks/routes.rake | 2 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/testing.rake | 3 | ||||
-rw-r--r-- | railties/lib/rails/version.rb | 4 | ||||
-rw-r--r-- | railties/railties.gemspec | 2 | ||||
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 2 |
15 files changed, 58 insertions, 60 deletions
diff --git a/railties/guides/source/contributing_to_rails.textile b/railties/guides/source/contributing_to_rails.textile index fb81bab98d..91ffa8ccc3 100644 --- a/railties/guides/source/contributing_to_rails.textile +++ b/railties/guides/source/contributing_to_rails.textile @@ -69,7 +69,7 @@ All of the Rails tests must pass with any code you submit, otherwise you have no NOTE: Ensure you install bundler v1.0 <shell> -gem install -v=1.0.0.rc.2 bundler +gem install -v=1.0.0.rc.6 bundler bundle install --without db </shell> diff --git a/railties/guides/source/generators.textile b/railties/guides/source/generators.textile index c5b41673e1..b1f8ea29da 100644 --- a/railties/guides/source/generators.textile +++ b/railties/guides/source/generators.textile @@ -1,6 +1,6 @@ h2. Creating and Customizing Rails Generators -Rails generators are an essential tool if you plan to improve your workflow and in this guide you will learn how to create and customize already existing generators. +Rails generators are an essential tool if you plan to improve your workflow. With this guide you will learn how to create generators and customize existing ones. In this guide you will: @@ -13,7 +13,7 @@ In this guide you will: endprologue. -NOTE: This guide is about Rails generators for versions >= 3.0. Rails generators from previous versions are not supported. +NOTE: This guide is about generators in Rails 3, previous versions are not covered. h3. First Contact @@ -35,7 +35,7 @@ h3. Creating Your First Generator Since Rails 3.0, generators are built on top of "Thor":http://github.com/wycats/thor. Thor provides powerful options parsing and a great API for manipulating files. For instance, let's build a generator that creates an initializer file named +initializer.rb+ inside +config/initializers+. -The first step is to create a file at +RAILS_APP/lib/generators/initializer_generator.rb+ with the following content: +The first step is to create a file at +lib/generators/initializer_generator.rb+ with the following content: <ruby> class InitializerGenerator < Rails::Generators::Base @@ -74,7 +74,7 @@ Now we can see the new description by invoking +--help+ on the new generator. Th h3. Creating Generators with Generators -A faster way to create a generator is using the generator's generator: +Generators themselves have a generator: <shell> $ rails generate generator initializer @@ -84,7 +84,7 @@ $ rails generate generator initializer create lib/generators/initializer/templates </shell> -And it will create a new generator as follows: +This is the generator just created: <ruby> class InitializerGenerator < Rails::Generators::NamedBase @@ -92,7 +92,7 @@ class InitializerGenerator < Rails::Generators::NamedBase end </ruby> -First, notice that we are inheriting from +Rails::Generators::NamedBase+ instead of +Rails::Generators::Base+. This means that our generator expects as least one argument, which will be the name of the initializer. +First, notice that we are inheriting from +Rails::Generators::NamedBase+ instead of +Rails::Generators::Base+. This means that our generator expects at least one argument, which will be the name of the initializer. We can see that by invoking the description of this new generator (don't forget to delete the old generator file): @@ -102,7 +102,9 @@ Usage: rails generate initializer NAME [options] </shell> -We can also see in our new generator that it has a class method called +source_root+. This method points to where our generator templates will be placed and by default it points to the created directory under +RAILS_APP/lib/generators/initializer/templates+. In order to understand what a generator template means, let's create a file at +RAILS_APP/lib/generators/initializer/templates/initializer.rb+ with the following content: +We can also see that our new generator has a class method called +source_root+. This method points to where our generator templates will be placed, if any, and by default it points to the created directory +lib/generators/initializer/templates+. + +In order to understand what a generator template means, let's create the file +lib/generators/initializer/templates/initializer.rb+ with the following content: <ruby> # Add initialization content here @@ -124,16 +126,14 @@ end And let's execute our generator: <shell> -$ rails generate initializer foo +$ rails generate initializer core_extensions </shell> -We can see that now a initializer named foo was created at +config/initializers/foo.rb+ with the contents of our template. That means that +copy_file+ copied a file in our source root to the destination path we gave. The method +file_name+ is automatically created when we inherit from +Rails::Generators::NamedBase+. +We can see that now a initializer named core_extensions was created at +config/initializers/core_extensions.rb+ with the contents of our template. That means that +copy_file+ copied a file in our source root to the destination path we gave. The method +file_name+ is automatically created when we inherit from +Rails::Generators::NamedBase+. h3. Generators Lookup -Now that we've created our first generator, we need to briefly discuss generator lookup. The way Rails finds generators is exactly the same way Ruby find files, i.e. using +$LOAD_PATHS+. - -For instance, when you say +rails generate initializer foo+, Rails knows you want to invoke the initializer generator and then search for the following generators in the $LOAD_PATHS: +When you run +rails generate initializer core_extensions+ Rails requires these files in turn until one is found: <shell> rails/generators/initializer/initializer_generator.rb @@ -142,11 +142,13 @@ rails/generators/initializer_generator.rb generators/initializer_generator.rb </shell> -If none of them is found, it raises an error message. +If none is found you get an error message. + +INFO: The examples above put files under the application's +lib+ because said directoty belongs to +$LOAD_PATH+. h3. Customizing Your Workflow -Rails generators are flexible enough to let you customize your scaffold the way you want. In your +config/application.rb+ there is a section just for generators: +Rails own generators are flexible enough to let you customize scaffolding. They can be configured in +config/application.rb+, these are some defaults: <ruby> config.generators do |g| @@ -166,7 +168,7 @@ $ rails generate scaffold User name:string invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml - route map.resources :users + route resources :users invoke scaffold_controller create app/controllers/users_controller.rb invoke erb @@ -186,9 +188,9 @@ $ rails generate scaffold User name:string create public/stylesheets/scaffold.css </shell> -Looking at this output, it's easy to understand how generators work on Rails 3.0 and above. The scaffold generator doesn't actually generate anything, it just invokes others to do the work. This allows us to add/replace/remove any of those invocations. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication. +Looking at this output, it's easy to understand how generators work in Rails 3.0 and above. The scaffold generator doesn't actually generate anything, it just invokes others to do the work. This allows us to add/replace/remove any of those invocations. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication. -Our first customization on the workflow will be to stop generating stylesheets and test fixtures on scaffold. We can achieve that by changing our application to the following: +Our first customization on the workflow will be to stop generating stylesheets and test fixtures for scaffolds. We can achieve that by changing our configuration to the following: <ruby> config.generators do |g| @@ -199,7 +201,7 @@ config.generators do |g| end </ruby> -If we generate another resource on scaffold, we can notice that neither stylesheets nor fixtures are created anymore. If you want to customize it further, for example to use +Datamapper+ and +RSpec+ instead of +ActiveRecord+ and +TestUnit+, it's just a matter of adding their gems to your application and configuring your generators. +If we generate another resource with the scaffold generator, we can notice that neither stylesheets nor fixtures are created anymore. If you want to customize it further, for example to use DataMapper and RSpec instead of Active Record and TestUnit, it's just a matter of adding their gems to your application and configuring your generators. To demonstrate this, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator: @@ -224,18 +226,18 @@ end We can try out our new generator by creating a helper for users: <shell> -$ rails generate my_helper users +$ rails generate my_helper products </shell> And it will generate the following helper file in +app/helpers+: <ruby> -module UsersHelper - attr_reader :users, :user +module ProductsHelper + attr_reader :products, :product end </ruby> -Which is what we expected. We can now tell scaffold to use our new helper generator by configuring +config/application.rb+ once again: +Which is what we expected. We can now tell scaffold to use our new helper generator by editing +config/application.rb+ once again: <ruby> config.generators do |g| @@ -247,7 +249,7 @@ config.generators do |g| end </ruby> -And see it in action when invoking generator once again: +and see it in action when invoking the generator: <shell> $ rails generate scaffold Post body:text @@ -260,7 +262,7 @@ We can notice on the output that our new helper was invoked instead of the Rails Since Rails 3.0, this is easy to do due to the hooks concept. Our new helper does not need to be focused in one specific test framework, it can simply provide a hook and a test framework just needs to implement this hook in order to be compatible. -To do that, we can change your generator to the following: +To do that, we can change the generator this way: <ruby> class MyHelperGenerator < Rails::Generators::NamedBase @@ -287,9 +289,9 @@ And now you can re-run scaffold for another resource and see it generating tests h3. Customizing Your Workflow by Changing Generators Templates -In the step above, we simply wanted to add a line to the generated helper, without adding any extra functionality. There is a simpler way to do that, and it's by replacing the templates of already existing generators. +In the step above we simply wanted to add a line to the generated helper, without adding any extra functionality. There is a simpler way to do that, and it's by replacing the templates of already existing generators, in that case +Rails::Generators::HelperGenerator+. -In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths. And one of them is inside +RAILS_APP/lib/templates+. Since we want to customize +Rails::Generators::HelperGenerator+, we can do that by simply making a template copy inside +RAILS_APP/lib/templates/rails/helper+ with the name +helper.rb+. So let's create that file with the following content: +In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths. And one of them is +lib/templates+. Since we want to customize +Rails::Generators::HelperGenerator+, we can do that by simply making a template copy inside +lib/templates/rails/helper+ with the name +helper.rb+. So let's create that file with the following content: <erb> module <%= class_name %>Helper @@ -297,7 +299,7 @@ module <%= class_name %>Helper end </erb> -So now we can revert the changes in +config/application.rb+: +and revert the last change in +config/application.rb+: <ruby> config.generators do |g| @@ -308,11 +310,11 @@ config.generators do |g| end </ruby> -If you generate another resource, you can see that we got exactly the same result! This is useful if you want to customize your scaffold templates and/or layout by just creating +edit.html.erb+, +index.html.erb+ and so on inside +RAILS_APP/lib/templates/erb/scaffold+. +If you generate another resource, you can see that we get exactly the same result! This is useful if you want to customize your scaffold templates and/or layout by just creating +edit.html.erb+, +index.html.erb+ and so on inside +lib/templates/erb/scaffold+. h3. Adding Generators Fallbacks -One last feature about generators which is quite useful for plugin generators is fallbacks. For example, imagine that you want to add a feature on top of TestUnit test framework, like "shoulda":http://github.com/thoughtbot/shoulda does. Since TestUnit already implements all generators required by Rails and shoulda just wants to overwrite part of it, there is no need for shoulda to reimplement some generators again, it can simply tell Rails to use a +TestUnit+ generator if none was found under the +Shoulda+ namespace. +One last feature about generators which is quite useful for plugin generators is fallbacks. For example, imagine that you want to add a feature on top of TestUnit like "shoulda":http://github.com/thoughtbot/shoulda does. Since TestUnit already implements all generators required by Rails and shoulda just wants to overwrite part of it, there is no need for shoulda to reimplement some generators again, it can simply tell Rails to use a +TestUnit+ generator if none was found under the +Shoulda+ namespace. We can easily simulate this behavior by changing our +config/application.rb+ once again: @@ -328,7 +330,7 @@ config.generators do |g| end </ruby> -Now, if you create a Comment scaffold, you will see that the shoulda generators are being invoked, and at the end, they are just falling back to test unit generators: +Now, if you create a Comment scaffold, you will see that the shoulda generators are being invoked, and at the end, they are just falling back to TestUnit generators: <shell> $ rails generate scaffold Comment body:text @@ -363,6 +365,8 @@ h3. Changelog "Lighthouse Ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/102 +* August 23, 2010: Edit pass by "Xavier Noria":credits.html#fxn + * April 30, 2010: Reviewed by José Valim * November 20, 2009: First version by José Valim diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index 0a2f60c30a..531ddd998b 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -150,7 +150,7 @@ Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This * nokogiri-1.4.3.1.gem * polyglot-0.3.1.gem * rack-1.2.1.gem -* rack-mount-0.6.10.gem +* rack-mount-0.6.12.gem * rack-test-0.5.4.gem * rails-3.0.0.beta4.gem * railties-3.0.0.beta4.gem @@ -1374,7 +1374,7 @@ the _version_ file contains this code (comments stripped): module ActionPack #:nodoc: module VERSION #:nodoc: MAJOR = 3 - MINOR = 0 + MINOR = 1 TINY = "0.beta1" STRING = [MAJOR, MINOR, TINY].join('.') diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb index 5bbaf725df..14d245ab2e 100644 --- a/railties/lib/rails/commands/dbconsole.rb +++ b/railties/lib/rails/commands/dbconsole.rb @@ -42,7 +42,7 @@ module Rails def find_cmd(*commands) dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR) - commands += commands.map{|cmd| "#{cmd}.exe"} if Config::CONFIG['host_os'] =~ /mswin|mingw/ + commands += commands.map{|cmd| "#{cmd}.exe"} if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ full_path_command = nil found = commands.detect do |cmd| diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb index c43a233bc3..54a9e6ec59 100644 --- a/railties/lib/rails/commands/runner.rb +++ b/railties/lib/rails/commands/runner.rb @@ -19,7 +19,7 @@ ARGV.clone.options do |opts| opts.on("-h", "--help", "Show this help message.") { $stderr.puts opts; exit } - if Config::CONFIG['host_os'] !~ /mswin|mingw/ + if RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ opts.separator "" opts.separator "You can also use runner as a shebang line for your scripts like this:" opts.separator "-------------------------------------------------------------" diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index ee3e3ba040..555bc9dbc8 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -119,7 +119,7 @@ module Rails root = File.exist?("#{root_path}/#{flag}") ? root_path : default raise "Could not find root path for #{self}" unless root - Config::CONFIG['host_os'] =~ /mswin|mingw/ ? + RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? Pathname.new(root).expand_path : Pathname.new(root).realpath end end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 668ef48892..da64560d83 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -242,7 +242,7 @@ module Rails def rake(command, options={}) log :rake, command env = options[:env] || 'development' - sudo = options[:sudo] && Config::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : '' + sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : '' in_root { run("#{sudo}#{extify(:rake)} #{command} RAILS_ENV=#{env}", :verbose => false) } end @@ -309,7 +309,7 @@ module Rails # Add an extension to the given name based on the platform. # def extify(name) - if Config::CONFIG['host_os'] =~ /mswin|mingw/ + if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ "#{name}.bat" else name diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index a90f109844..6eba0f77e7 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -394,6 +394,7 @@ module Rails when "postgresql" then "pg" when "sqlite3" then "sqlite3-ruby" when "frontbase" then "ruby-frontbase" + when "mysql" then "mysql2" else options[:database] end end @@ -415,7 +416,7 @@ module Rails "/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4 "/opt/local/var/run/mysql5/mysqld.sock", # mac + darwinports + mysql5 "/opt/lampp/var/mysql/mysql.sock" # xampp for linux - ].find { |f| File.exist?(f) } unless Config::CONFIG['host_os'] =~ /mswin|mingw/ + ].find { |f| File.exist?(f) } unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ end def empty_directory_with_gitkeep(destination, config = {}) diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml index ffc8a0a8cb..5d28c7c312 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml @@ -1,21 +1,12 @@ # MySQL. Versions 4.1 and 5.0 are recommended. # # Install the MySQL driver: -# gem install mysql -# On Mac OS X: -# sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql -# On Mac OS X Leopard: -# sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config -# This sets the ARCHFLAGS environment variable to your native architecture -# On Windows: -# gem install mysql -# Choose the win32 build. -# Install MySQL and put its /bin directory on your path. +# gem install mysql2 # # And be sure to use new-style password hashing: # http://dev.mysql.com/doc/refman/5.0/en/old-client.html development: - adapter: mysql + adapter: mysql2 encoding: utf8 reconnect: false database: <%= app_name %>_development @@ -32,7 +23,7 @@ development: # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: - adapter: mysql + adapter: mysql2 encoding: utf8 reconnect: false database: <%= app_name %>_test @@ -46,7 +37,7 @@ test: <% end -%> production: - adapter: mysql + adapter: mysql2 encoding: utf8 reconnect: false database: <%= app_name %>_production diff --git a/railties/lib/rails/generators/rails/generator/USAGE b/railties/lib/rails/generators/rails/generator/USAGE index d68539e4a6..d28eb3d7d8 100644 --- a/railties/lib/rails/generators/rails/generator/USAGE +++ b/railties/lib/rails/generators/rails/generator/USAGE @@ -1,6 +1,6 @@ Description: - Stubs out a new generator at lib/generators. Pass the generator name, either - CamelCased or under_scored, as an argument. + Stubs out a new generator at lib/generators. Pass the generator name as an argument, + either CamelCased or snake_cased. Example: `rails generate generator Awesome` @@ -8,4 +8,5 @@ Example: creates a standard awesome generator: lib/generators/awesome/ lib/generators/awesome/awesome_generator.rb + lib/generators/awesome/USAGE lib/generators/awesome/templates/ diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index c0a2fe38bd..65cf79a0a2 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -17,7 +17,7 @@ task :routes => :environment do name = named_routes.send(key, route).to_s reqs = route.requirements.dup - reqs[:to] = route.app unless route.app.is_a?(ActionDispatch::Routing::RouteSet::Dispatcher) + reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ reqs = reqs.empty? ? "" : reqs.inspect {:name => name, :verb => route.verb.to_s, :path => route.path, :reqs => reqs} diff --git a/railties/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake index 38c14fcd6b..713833f884 100644 --- a/railties/lib/rails/test_unit/testing.rake +++ b/railties/lib/rails/test_unit/testing.rake @@ -1,3 +1,4 @@ +require 'rbconfig' require 'rake/testtask' # Monkey-patch to silence the description from Rake::TestTask to cut down on rake -T noise @@ -62,7 +63,7 @@ end module Kernel def silence_stderr old_stderr = STDERR.dup - STDERR.reopen(Config::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null') + STDERR.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null') STDERR.sync = true yield ensure diff --git a/railties/lib/rails/version.rb b/railties/lib/rails/version.rb index c5d1d02bc1..0213d46254 100644 --- a/railties/lib/rails/version.rb +++ b/railties/lib/rails/version.rb @@ -1,9 +1,9 @@ module Rails module VERSION #:nodoc: MAJOR = 3 - MINOR = 0 + MINOR = 1 TINY = 0 - BUILD = "rc" + BUILD = "beta" STRING = [MAJOR, MINOR, TINY, BUILD].join('.') end diff --git a/railties/railties.gemspec b/railties/railties.gemspec index d76b74190b..adb2a7610b 100644 --- a/railties/railties.gemspec +++ b/railties/railties.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.rdoc_options << '--exclude' << '.' s.has_rdoc = false - s.add_dependency('rake', '>= 0.8.3') + s.add_dependency('rake', '>= 0.8.4') s.add_dependency('thor', '~> 0.14.0') s.add_dependency('activesupport', version) s.add_dependency('actionpack', version) diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 849c8001f4..dcd7629505 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -144,7 +144,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_config_another_database run_generator([destination_root, "-d", "mysql"]) assert_file "config/database.yml", /mysql/ - assert_file "Gemfile", /^gem\s+["']mysql["']$/ + assert_file "Gemfile", /^gem\s+["']mysql2["']$/ end def test_config_database_is_not_added_if_skip_active_record_is_given |