diff options
-rw-r--r-- | actionpack/CHANGELOG.md | 4 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/debug_exceptions.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/inspector.rb | 2 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing/inspector_test.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/templates/migration.rb | 20 | ||||
-rw-r--r-- | activesupport/lib/active_support/test_case.rb | 12 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/declarative.rb | 40 | ||||
-rw-r--r-- | guides/source/4_0_release_notes.textile | 12 | ||||
-rw-r--r-- | guides/source/contributing_to_ruby_on_rails.textile | 25 | ||||
-rw-r--r-- | guides/source/migrations.textile | 47 | ||||
-rw-r--r-- | railties/CHANGELOG.md | 11 | ||||
-rw-r--r-- | railties/lib/rails/application/routes_inspector.rb | 121 | ||||
-rw-r--r-- | railties/lib/rails/info_controller.rb | 3 | ||||
-rw-r--r-- | railties/lib/rails/tasks/routes.rake | 2 | ||||
-rw-r--r-- | railties/test/application/routes_inspect_test.rb | 168 | ||||
-rw-r--r-- | railties/test/generators/migration_generator_test.rb | 29 |
16 files changed, 150 insertions, 353 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index a616eaa09c..be793d905f 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,6 +1,6 @@ ## Rails 4.0.0 (unreleased) ## -* Add routes to page while debugging a RoutingError in development. *Richard Schneeman and Mattt Thompson* +* Show routes in exception page while debugging a `RoutingError` in development. *Richard Schneeman and Mattt Thompson* * Add `ActionController::Flash.add_flash_types` method to allow people to register their own flash types. e.g.: @@ -8,7 +8,7 @@ add_flash_types :error, :warning end - If you add the above code, you can use `<%= error %>` in an erb, and `rediect_to /foo, :error => 'message'` in a controller. + If you add the above code, you can use `<%= error %>` in an erb, and `redirect_to /foo, :error => 'message'` in a controller. *kennyj* diff --git a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb index af3e6b3557..0f0589a844 100644 --- a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb @@ -83,11 +83,10 @@ module ActionDispatch @stderr_logger ||= ActiveSupport::Logger.new($stderr) end - private def formatted_routes(exception) return false unless @routes_app.respond_to?(:routes) if exception.is_a?(ActionController::RoutingError) || exception.is_a?(ActionView::Template::Error) - inspector = ActionDispatch::Routing::RouteInspector.new + inspector = ActionDispatch::Routing::RoutesInspector.new inspector.format(@routes_app.routes.routes).join("\n") end end diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index 17e19453ea..bc7229b6a1 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -62,7 +62,7 @@ module ActionDispatch ## # This class is just used for displaying route information when someone # executes `rake routes`. People should not use this class. - class RouteInspector # :nodoc: + class RoutesInspector # :nodoc: def initialize @engines = Hash.new end diff --git a/actionpack/test/dispatch/routing/inspector_test.rb b/actionpack/test/dispatch/routing/inspector_test.rb index ae830300a1..97fc4f3e4b 100644 --- a/actionpack/test/dispatch/routing/inspector_test.rb +++ b/actionpack/test/dispatch/routing/inspector_test.rb @@ -5,10 +5,10 @@ require 'action_dispatch/routing/inspector' module ActionDispatch module Routing - class RouteInspectTest < ActiveSupport::TestCase + class RoutesInspectorTest < ActiveSupport::TestCase def setup @set = ActionDispatch::Routing::RouteSet.new - @inspector = ActionDispatch::Routing::RouteInspector.new + @inspector = ActionDispatch::Routing::RoutesInspector.new app = ActiveSupport::OrderedOptions.new app.config = ActiveSupport::OrderedOptions.new app.config.assets = ActiveSupport::OrderedOptions.new diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb index b1a0f83b5f..34eaffcb0f 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -2,30 +2,42 @@ class <%= migration_class_name %> < ActiveRecord::Migration <%- if migration_action == 'add' -%> def change <% attributes.each do |attribute| -%> + <%- if attribute.reference? -%> + add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %> + <%- else -%> add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> <%- if attribute.has_index? -%> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> <%- end -%> + <%- end -%> <%- end -%> end <%- else -%> def up <% attributes.each do |attribute| -%> - <%- if migration_action -%> - <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %> +<%- if migration_action -%> + <%- if attribute.reference? -%> + remove_reference :<%= table_name %>, :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %> + <%- else -%> + remove_column :<%= table_name %>, :<%= attribute.name %> <%- end -%> <%- end -%> +<%- end -%> end def down <% attributes.reverse.each do |attribute| -%> - <%- if migration_action -%> +<%- if migration_action -%> + <%- if attribute.reference? -%> + add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %> + <%- else -%> add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> <%- if attribute.has_index? -%> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> <%- end -%> <%- end -%> <%- end -%> +<%- end -%> end <%- end -%> -end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index a6f3b43792..b6afeda966 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -3,7 +3,6 @@ require 'minitest/spec' require 'active_support/testing/setup_and_teardown' require 'active_support/testing/assertions' require 'active_support/testing/deprecation' -require 'active_support/testing/declarative' require 'active_support/testing/isolation' require 'active_support/testing/mocha_module' require 'active_support/core_ext/kernel/reporting' @@ -13,12 +12,6 @@ module ActiveSupport include ActiveSupport::Testing::MochaModule - if MiniTest::Unit::VERSION < '2.6.1' - class << self - alias :name :to_s - end - end - # Use AS::TestCase for the base class when describing a model register_spec_type(self) do |desc| desc < ActiveRecord::Model @@ -41,7 +34,10 @@ module ActiveSupport include ActiveSupport::Testing::SetupAndTeardown include ActiveSupport::Testing::Assertions include ActiveSupport::Testing::Deprecation - extend ActiveSupport::Testing::Declarative + + class << self + alias :test :it + end # test/unit backwards compatibility methods alias :assert_raise :assert_raises diff --git a/activesupport/lib/active_support/testing/declarative.rb b/activesupport/lib/active_support/testing/declarative.rb deleted file mode 100644 index 508e37254a..0000000000 --- a/activesupport/lib/active_support/testing/declarative.rb +++ /dev/null @@ -1,40 +0,0 @@ -module ActiveSupport - module Testing - module Declarative - - def self.extended(klass) #:nodoc: - klass.class_eval do - - unless method_defined?(:describe) - def self.describe(text) - class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def self.name - "#{text}" - end - RUBY_EVAL - end - end - - end - end - - unless defined?(Spec) - # test "verify something" do - # ... - # end - def test(name, &block) - test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym - defined = instance_method(test_name) rescue false - raise "#{test_name} is already defined in #{self}" if defined - if block_given? - define_method(test_name, &block) - else - define_method(test_name) do - flunk "No implementation provided for #{name}" - end - end - end - end - end - end -end diff --git a/guides/source/4_0_release_notes.textile b/guides/source/4_0_release_notes.textile index b7ac11999a..895372ba63 100644 --- a/guides/source/4_0_release_notes.textile +++ b/guides/source/4_0_release_notes.textile @@ -122,6 +122,16 @@ h3. Action Pack h4. Action Controller +* Add <tt>ActionController::Flash.add_flash_types</tt> method to allow people to register their own flash types. e.g.: + +<ruby> +class ApplicationController + add_flash_types :error, :warning +end +</ruby> + +If you add the above code, you can use <tt><%= error %></tt> in an erb, and <tt>redirect_to /foo, :error => 'message'</tt> in a controller. + * Remove Active Model dependency from Action Pack. * Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping: @@ -186,6 +196,8 @@ h5(#actioncontroller_deprecations). Deprecations h4. Action Dispatch +* Show routes in exception page while debugging a <tt>RoutingError</tt> in development. + * Include <tt>mounted_helpers</tt> (helpers for accessing mounted engines) in <tt>ActionDispatch::IntegrationTest</tt> by default. * Added <tt>ActionDispatch::SSL</tt> middleware that when included force all the requests to be under HTTPS protocol. diff --git a/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile index 1dadce2083..fd5e2b28c0 100644 --- a/guides/source/contributing_to_ruby_on_rails.textile +++ b/guides/source/contributing_to_ruby_on_rails.textile @@ -66,12 +66,26 @@ Install first libxml2 and libxslt together with their development files for Noko $ sudo apt-get install libxml2 libxml2-dev libxslt1-dev </shell> +If you are on Fedora or CentOS, you can run + +<shell> +$ sudo yum install libxml2 libxml2-devel libxslt libxslt-devel +</shell> + +If you have any problems with these libraries, you should install them manually compiling the source code. Just follow the instructions "here":http://nokogiri.org/tutorials/installing_nokogiri.html#red_hat__centos . + Also, SQLite3 and its development files for the +sqlite3-ruby+ gem -- in Ubuntu you're done with just <shell> $ sudo apt-get install sqlite3 libsqlite3-dev </shell> +And if you are on Fedora or CentOS, you're done with + +<shell> +$ sudo yum install sqlite3 sqlite3-devel +</shell> + Get a recent version of "Bundler":http://gembundler.com/: <shell> @@ -150,6 +164,13 @@ $ sudo apt-get install mysql-server libmysqlclient15-dev $ sudo apt-get install postgresql postgresql-client postgresql-contrib libpq-dev </shell> +On Fedora or CentOS, just run: + +<shell> +$ sudo yum install mysql-server mysql-devel +$ sudo yum install postgresql-server postgresql-devel +</shell> + After that run: <shell> @@ -172,7 +193,7 @@ and create the test databases: <shell> $ cd activerecord -$ rake mysql:build_databases +$ bundle exec rake mysql:build_databases </shell> PostgreSQL's authentication works differently. A simple way to set up the development environment for example is to run with your development account @@ -185,7 +206,7 @@ and then create the test databases with <shell> $ cd activerecord -$ rake postgresql:build_databases +$ bundle exec rake postgresql:build_databases </shell> NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation. diff --git a/guides/source/migrations.textile b/guides/source/migrations.textile index 342b5a4d57..06e85e5914 100644 --- a/guides/source/migrations.textile +++ b/guides/source/migrations.textile @@ -111,6 +111,7 @@ Active Record provides methods that perform common data definition tasks in a database independent way (you'll read about them in detail later): * +add_column+ +* +add_reference+ * +add_index+ * +change_column+ * +change_table+ @@ -120,6 +121,7 @@ database independent way (you'll read about them in detail later): * +remove_column+ * +remove_index+ * +rename_column+ +* +remove_reference+ If you need to perform tasks specific to your database (for example create a "foreign key":#active-record-and-referential-integrity constraint) then the @@ -332,6 +334,51 @@ NOTE: The generated migration file for destructive migrations will still be old-style using the +up+ and +down+ methods. This is because Rails needs to know the original data types defined when you made the original changes. +Also the generator accepts column type as +references+(also available as +belongs_to+), for instance + +<shell> +$ rails generate migration AddUserRefToProducts user:references +</shell> + +generates + +<ruby> +class AddUserRefToProducts < ActiveRecord::Migration + def change + add_reference :products, :user, :index => true + end +end +</ruby> + +This migration will create a user_id column and appropriate index. + +h4. Supported type modifiers + +You can also specify some options just after the field type between curly braces. You can use the +following modifiers: + +* +limit+ Sets the maximum size of the +string/text/binary/integer+ fields +* +precision+ Defines the precision for the +decimal+ fields +* +scale+ Defines the scale for the +decimal+ fields +* +polymorphic+ Adds a +type+ column for +belongs_to+ associations + +For instance running + +<shell> +$ rails generate migration AddDetailsToProducts price:decimal{5,2} supplier:references{polymorphic} +</shell> + +will produce a migration that looks like this + +<ruby> +class AddDetailsToProducts < ActiveRecord::Migration + def change + add_column :products, :price, :precision => 5, :scale => 2 + add_reference :products, :user, :polymorphic => true, :index => true + end +end +</ruby> + h3. Writing a Migration Once you have created your migration using one of the generators it's time to diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 7223270210..5165d9401f 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,16 @@ ## Rails 4.0.0 (unreleased) ## +* The migration generator will now produce AddXXXToYYY/RemoveXXXFromYYY migrations with references statements, for instance + + rails g migration AddReferencesToProducts user:references supplier:references{polymorphic} + + will generate the migration with: + + add_reference :products, :user, index: true + add_reference :products, :supplier, polymorphic: true, index: true + + *Aleksey Magusev* + * Allow scaffold/model/migration generators to accept a `polymorphic` modifier for `references`/`belongs_to`, for instance diff --git a/railties/lib/rails/application/routes_inspector.rb b/railties/lib/rails/application/routes_inspector.rb deleted file mode 100644 index 6b2caf8277..0000000000 --- a/railties/lib/rails/application/routes_inspector.rb +++ /dev/null @@ -1,121 +0,0 @@ -require 'delegate' - -module Rails - class Application - class RouteWrapper < SimpleDelegator - def endpoint - rack_app ? rack_app.inspect : "#{controller}##{action}" - end - - def constraints - requirements.except(:controller, :action) - end - - def rack_app(app = self.app) - @rack_app ||= begin - class_name = app.class.name.to_s - if class_name == "ActionDispatch::Routing::Mapper::Constraints" - rack_app(app.app) - elsif ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/ - app - end - end - end - - def verb - super.source.gsub(/[$^]/, '') - end - - def path - super.spec.to_s - end - - def name - super.to_s - end - - def reqs - @reqs ||= begin - reqs = endpoint - reqs += " #{constraints.inspect}" unless constraints.empty? - reqs - end - end - - def controller - requirements[:controller] || ':controller' - end - - def action - requirements[:action] || ':action' - end - - def internal? - path =~ %r{/rails/info.*|^#{Rails.application.config.assets.prefix}} - end - - def engine? - rack_app && rack_app.respond_to?(:routes) - end - end - - ## - # This class is just used for displaying route information when someone - # executes `rake routes`. People should not use this class. - class RoutesInspector # :nodoc: - def initialize - @engines = Hash.new - end - - def format(all_routes, filter = nil) - if filter - all_routes = all_routes.select{ |route| route.defaults[:controller] == filter } - end - - routes = collect_routes(all_routes) - - formatted_routes(routes) + - formatted_routes_for_engines - end - - def collect_routes(routes) - routes = routes.collect do |route| - RouteWrapper.new(route) - end.reject do |route| - route.internal? - end.collect do |route| - collect_engine_routes(route) - - {:name => route.name, :verb => route.verb, :path => route.path, :reqs => route.reqs } - end - end - - def collect_engine_routes(route) - name = route.endpoint - return unless route.engine? - return if @engines[name] - - routes = route.rack_app.routes - if routes.is_a?(ActionDispatch::Routing::RouteSet) - @engines[name] = collect_routes(routes.routes) - end - end - - def formatted_routes_for_engines - @engines.map do |name, routes| - ["\nRoutes for #{name}:"] + formatted_routes(routes) - end.flatten - end - - def formatted_routes(routes) - name_width = routes.map{ |r| r[:name].length }.max - verb_width = routes.map{ |r| r[:verb].length }.max - path_width = routes.map{ |r| r[:path].length }.max - - routes.map do |r| - "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}" - end - end - end - end -end diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb index 83ab8c7e9d..512803aeac 100644 --- a/railties/lib/rails/info_controller.rb +++ b/railties/lib/rails/info_controller.rb @@ -15,8 +15,7 @@ class Rails::InfoController < ActionController::Base end def routes - inspector = Rails::Application::RoutesInspector.new - inspector = ActionDispatch::Routing::RouteInspector.new + inspector = ActionDispatch::Routing::RoutesInspector.new @info = inspector.format(_routes.routes).join("\n") end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index afc4e147e1..95f47566ef 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -2,6 +2,6 @@ desc 'Print out all defined routes in match order, with names. Target specific c task :routes => :environment do all_routes = Rails.application.routes.routes require 'action_dispatch/routing/inspector' - inspector = ActionDispatch::Routing::RouteInspector.new + inspector = ActionDispatch::Routing::RoutesInspector.new puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n" end diff --git a/railties/test/application/routes_inspect_test.rb b/railties/test/application/routes_inspect_test.rb deleted file mode 100644 index 68a8afc93e..0000000000 --- a/railties/test/application/routes_inspect_test.rb +++ /dev/null @@ -1,168 +0,0 @@ -require 'minitest/autorun' -require 'rails/application/routes_inspector' -require 'action_controller' -require 'rails/engine' - -module ApplicationTests - class RoutesInspectTest < ActiveSupport::TestCase - def setup - @set = ActionDispatch::Routing::RouteSet.new - @inspector = Rails::Application::RoutesInspector.new - app = ActiveSupport::OrderedOptions.new - app.config = ActiveSupport::OrderedOptions.new - app.config.assets = ActiveSupport::OrderedOptions.new - app.config.assets.prefix = '/sprockets' - Rails.stubs(:application).returns(app) - Rails.stubs(:env).returns("development") - end - - def draw(&block) - @set.draw(&block) - @inspector.format(@set.routes) - end - - def test_displaying_routes_for_engines - engine = Class.new(Rails::Engine) do - def self.to_s - "Blog::Engine" - end - end - engine.routes.draw do - get '/cart', :to => 'cart#show' - end - - output = draw do - get '/custom/assets', :to => 'custom_assets#show' - mount engine => "/blog", :as => "blog" - end - - expected = [ - "custom_assets GET /custom/assets(.:format) custom_assets#show", - " blog /blog Blog::Engine", - "\nRoutes for Blog::Engine:", - "cart GET /cart(.:format) cart#show" - ] - assert_equal expected, output - end - - def test_cart_inspect - output = draw do - get '/cart', :to => 'cart#show' - end - assert_equal ["cart GET /cart(.:format) cart#show"], output - end - - def test_inspect_shows_custom_assets - output = draw do - get '/custom/assets', :to => 'custom_assets#show' - end - assert_equal ["custom_assets GET /custom/assets(.:format) custom_assets#show"], output - end - - def test_inspect_routes_shows_resources_route - output = draw do - resources :articles - end - expected = [ - " articles GET /articles(.:format) articles#index", - " POST /articles(.:format) articles#create", - " new_article GET /articles/new(.:format) articles#new", - "edit_article GET /articles/:id/edit(.:format) articles#edit", - " article GET /articles/:id(.:format) articles#show", - " PATCH /articles/:id(.:format) articles#update", - " PUT /articles/:id(.:format) articles#update", - " DELETE /articles/:id(.:format) articles#destroy" ] - assert_equal expected, output - end - - def test_inspect_routes_shows_root_route - output = draw do - root :to => 'pages#main' - end - assert_equal ["root GET / pages#main"], output - end - - def test_inspect_routes_shows_dynamic_action_route - output = draw do - get 'api/:action' => 'api' - end - assert_equal [" GET /api/:action(.:format) api#:action"], output - end - - def test_inspect_routes_shows_controller_and_action_only_route - output = draw do - get ':controller/:action' - end - assert_equal [" GET /:controller/:action(.:format) :controller#:action"], output - end - - def test_inspect_routes_shows_controller_and_action_route_with_constraints - output = draw do - get ':controller(/:action(/:id))', :id => /\d+/ - end - assert_equal [" GET /:controller(/:action(/:id))(.:format) :controller#:action {:id=>/\\d+/}"], output - end - - def test_rake_routes_shows_route_with_defaults - output = draw do - get 'photos/:id' => 'photos#show', :defaults => {:format => 'jpg'} - end - assert_equal [%Q[ GET /photos/:id(.:format) photos#show {:format=>"jpg"}]], output - end - - def test_rake_routes_shows_route_with_constraints - output = draw do - get 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/ - end - assert_equal [" GET /photos/:id(.:format) photos#show {:id=>/[A-Z]\\d{5}/}"], output - end - - class RackApp - def self.call(env) - end - end - - def test_rake_routes_shows_route_with_rack_app - output = draw do - get 'foo/:id' => RackApp, :id => /[A-Z]\d{5}/ - end - assert_equal [" GET /foo/:id(.:format) #{RackApp.name} {:id=>/[A-Z]\\d{5}/}"], output - end - - def test_rake_routes_shows_route_with_rack_app_nested_with_dynamic_constraints - constraint = Class.new do - def to_s - "( my custom constraint )" - end - end - - output = draw do - scope :constraint => constraint.new do - mount RackApp => '/foo' - end - end - - assert_equal [" /foo #{RackApp.name} {:constraint=>( my custom constraint )}"], output - end - - def test_rake_routes_dont_show_app_mounted_in_assets_prefix - output = draw do - get '/sprockets' => RackApp - end - assert_no_match(/RackApp/, output.first) - assert_no_match(/\/sprockets/, output.first) - end - - def test_redirect - output = draw do - get "/foo" => redirect("/foo/bar"), :constraints => { :subdomain => "admin" } - get "/bar" => redirect(path: "/foo/bar", status: 307) - get "/foobar" => redirect{ "/foo/bar" } - end - - assert_equal " foo GET /foo(.:format) redirect(301, /foo/bar) {:subdomain=>\"admin\"}", output[0] - assert_equal " bar GET /bar(.:format) redirect(307, path: /foo/bar)", output[1] - assert_equal "foobar GET /foobar(.:format) redirect(301)", output[2] - end - end -end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index b320e40654..86e3793289 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -76,6 +76,23 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_remove_migration_with_references_options + migration = "remove_references_from_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :up, content do |up| + assert_match(/remove_reference :books, :author/, up) + assert_match(/remove_reference :books, :distributor, polymorphic: true/, up) + end + + assert_method :down, content do |down| + assert_match(/add_reference :books, :author, index: true/, down) + assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, down) + end + end + end + def test_add_migration_with_attributes_and_indices migration = "add_title_with_index_and_body_to_posts" run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"] @@ -138,6 +155,18 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_add_migration_with_references_options + migration = "add_references_to_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |up| + assert_match(/add_reference :books, :author, index: true/, up) + assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, up) + end + end + end + def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove migration = "create_books" run_generator [migration, "title:string", "content:text"] |