diff options
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/schema_dumper.rb | 41 | ||||
-rw-r--r-- | activerecord/test/cases/schema_dumper_test.rb | 105 | ||||
-rw-r--r-- | guides/source/configuring.md | 4 | ||||
-rw-r--r-- | guides/source/routing.md | 36 | ||||
-rw-r--r-- | guides/source/testing.md | 6 |
6 files changed, 32 insertions, 164 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index fd2c5dd9bb..4aa1853bde 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,6 +1,4 @@ -* Option to remove standardized column types/arguments spaces in schema dump - with `ActiveRecord::SchemaDumper.standardized_argument_widths` and - `ActiveRecord::SchemaDumper.standardized_type_widths` methods. +* Remove standardized column types/arguments spaces in schema dump. *Tim Petricola* diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index be74922453..b65d5b56f1 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -16,22 +16,6 @@ module ActiveRecord cattr_accessor :ignore_tables @@ignore_tables = [] - ## - # :singleton-method: - # Define whether column arguments are lined up in dump. - # Acceptable values are true or false. - # This setting is only used if ActiveRecord::Base.schema_format == :ruby - cattr_accessor :standardized_argument_widths - @@standardized_argument_widths = true - - ## - # :singleton-method: - # Define whether columns types are lined up in dump. - # Acceptable values are true or false. - # This setting is only used if ActiveRecord::Base.schema_format == :ruby - cattr_accessor :standardized_type_widths - @@standardized_type_widths = true - class << self def dump(connection=ActiveRecord::Base.connection, stream=STDOUT, config = ActiveRecord::Base) new(connection, generate_options(config)).dump(stream) @@ -162,32 +146,13 @@ HEADER keys = @connection.migration_keys # figure out the lengths for each column based on above keys - lengths = if standardized_argument_widths - keys.map { |key| - column_specs.map { |spec| - spec[key] ? spec[key].length + 2 : 0 - }.max - } - else - [0] * keys.length - end + lengths = [0] * keys.length # the string we're going to sprintf our values against, with standardized column widths - format_string = if standardized_argument_widths - lengths.map { |len| "%-#{len}s" } - else - ["%s"] * keys.length - end + format_string = ["%s"] * keys.length # add column type definition to our format string - if standardized_type_widths - # find the max length for the 'type' column, which is special - type_length = column_specs.map { |column| column[:type].length }.max - - format_string.unshift " t.%-#{type_length}s " - else - format_string.unshift " t.%s " - end + format_string.unshift " t.%s " format_string *= "" diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 23cfe46d7f..57b1bc889a 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -70,38 +70,35 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{create_table "CamelCase"}, output end - def assert_line_up(lines, pattern, required = false) + def assert_no_line_up(lines, pattern) return assert(true) if lines.empty? matches = lines.map { |line| line.match(pattern) } - assert matches.all? if required matches.compact! return assert(true) if matches.empty? - assert_equal 1, matches.map { |match| match.offset(0).first }.uniq.length + line_matches = lines.map { |line| [line, line.match(pattern)] }.select { |line, match| match } + assert line_matches.all? { |line, match| + start = match.offset(0).first + line[start - 2..start - 1] == ", " + } end def column_definition_lines(output = standard_dump) output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map { |m| m.last.split(/\n/) } end - def test_types_line_up + def test_types_no_line_up column_definition_lines.each do |column_set| next if column_set.empty? - lengths = column_set.map do |column| - if match = column.match(/\bt\.\w+\s+(?="\w+?")/) - match[0].length - end - end.compact - - assert_equal 1, lengths.uniq.length + assert column_set.all? { |column| !column.match(/\bt\.\w+\s{2,}/) } end end - def test_arguments_line_up + def test_arguments_no_line_up column_definition_lines.each do |column_set| - assert_line_up(column_set, /default: /) - assert_line_up(column_set, /limit: /) - assert_line_up(column_set, /null: /) + assert_no_line_up(column_set, /default: /) + assert_no_line_up(column_set, /limit: /) + assert_no_line_up(column_set, /null: /) end end @@ -442,81 +439,3 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase assert_match %r{t\.time\s+"time_with_default",\s+default: '2000-01-01 07:17:04'}, output end end - -class SchemaDumperNoStandardizedArgumentWidthsTest < ActiveRecord::TestCase - include SchemaDumpingHelper - - setup do - ActiveRecord::SchemaDumper.standardized_argument_widths = false - ActiveRecord::SchemaMigration.create_table - end - - teardown do - ActiveRecord::SchemaDumper.standardized_argument_widths = true - end - - def standard_dump - @@standard_dump ||= perform_schema_dump - end - - def perform_schema_dump - dump_all_table_schema [] - end - - def assert_no_line_up(lines, pattern) - return assert(true) if lines.empty? - matches = lines.map { |line| line.match(pattern) } - matches.compact! - return assert(true) if matches.empty? - line_matches = lines.map { |line| [line, line.match(pattern)] }.select { |line, match| match } - assert line_matches.all? { |line, match| - start = match.offset(0).first - line[start - 2..start - 1] == ", " - } - end - - def column_definition_lines(output = standard_dump) - output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map { |m| m.last.split(/\n/) } - end - - def test_arguments_no_line_up - column_definition_lines.each do |column_set| - assert_no_line_up(column_set, /default: /) - assert_no_line_up(column_set, /limit: /) - assert_no_line_up(column_set, /null: /) - end - end -end - -class SchemaDumperNoStandardizedTypeWidthsTest < ActiveRecord::TestCase - include SchemaDumpingHelper - - setup do - ActiveRecord::SchemaDumper.standardized_type_widths = false - ActiveRecord::SchemaMigration.create_table - end - - teardown do - ActiveRecord::SchemaDumper.standardized_type_widths = true - end - - def standard_dump - @@standard_dump ||= perform_schema_dump - end - - def perform_schema_dump - dump_all_table_schema [] - end - - def column_definition_lines(output = standard_dump) - output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map { |m| m.last.split(/\n/) } - end - - def test_types_no_line_up - column_definition_lines.each do |column_set| - next if column_set.empty? - - assert column_set.all? { |column| !column.match(/\bt\.\w+\s{2,}/) } - end - end -end diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 3e27dfafa0..7239105b29 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -368,11 +368,9 @@ The MySQL adapter adds one additional configuration option: * `ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns as booleans. Defaults to `true`. -The schema dumper adds additional configuration options: +The schema dumper adds one additional configuration option: * `ActiveRecord::SchemaDumper.ignore_tables` accepts an array of tables that should _not_ be included in any generated schema file. This setting is ignored unless `config.active_record.schema_format == :ruby`. -* `ActiveRecord::SchemaDumper.standardized_argument_widths` configures whether colum arguments should be lined up or not in dump. By default this is `true`. This setting is ignored unless `config.active_record.schema_format == :ruby`. -* `ActiveRecord::SchemaDumper.standardized_type_widths` configures whether colum types should be lined up or not in dump. By default this is `true`. This setting is ignored unless `config.active_record.schema_format == :ruby`. ### Configuring Action Controller diff --git a/guides/source/routing.md b/guides/source/routing.md index 756e0fefd7..937e313663 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -553,29 +553,23 @@ In particular, simple routing makes it very easy to map legacy URLs to new Rails ### Bound Parameters -When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: `:controller` maps to the name of a controller in your application, and `:action` maps to the name of an action within that controller. For example, consider this route: +When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. For example, consider this route: ```ruby -get ':controller(/:action(/:id))' +get 'photos(/:id)', to: :display ``` -If an incoming request of `/photos/show/1` is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the `show` action of the `PhotosController`, and to make the final parameter `"1"` available as `params[:id]`. This route will also route the incoming request of `/photos` to `PhotosController#index`, since `:action` and `:id` are optional parameters, denoted by parentheses. +If an incoming request of `/photos/1` is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the `display` action of the `PhotosController`, and to make the final parameter `"1"` available as `params[:id]`. This route will also route the incoming request of `/photos` to `PhotosController#display`, since `:id` is an optional parameter, denoted by parentheses. ### Dynamic Segments -You can set up as many dynamic segments within a regular route as you like. Anything other than `:controller` or `:action` will be available to the action as part of `params`. If you set up this route: +You can set up as many dynamic segments within a regular route as you like. Any segment will be available to the action as part of `params`. If you set up this route: ```ruby -get ':controller/:action/:id/:user_id' +get 'photos/:id/:user_id', to: 'photos#show' ``` -An incoming path of `/photos/show/1/2` will be dispatched to the `show` action of the `PhotosController`. `params[:id]` will be `"1"`, and `params[:user_id]` will be `"2"`. - -NOTE: You can't use `:namespace` or `:module` with a `:controller` path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g: - -```ruby -get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/ -``` +An incoming path of `/photos/1/2` will be dispatched to the `show` action of the `PhotosController`. `params[:id]` will be `"1"`, and `params[:user_id]` will be `"2"`. TIP: By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, `id: /[^\/]+/` allows anything except a slash. @@ -584,32 +578,24 @@ TIP: By default, dynamic segments don't accept dots - this is because the dot is You can specify static segments when creating a route by not prepending a colon to a fragment: ```ruby -get ':controller/:action/:id/with_user/:user_id' +get 'photos/:id/with_user/:user_id', to: 'photos#show' ``` -This route would respond to paths such as `/photos/show/1/with_user/2`. In this case, `params` would be `{ controller: 'photos', action: 'show', id: '1', user_id: '2' }`. +This route would respond to paths such as `/photos/1/with_user/2`. In this case, `params` would be `{ controller: 'photos', action: 'show', id: '1', user_id: '2' }`. ### The Query String The `params` will also include any parameters from the query string. For example, with this route: ```ruby -get ':controller/:action/:id' +get 'photos/:id', to: 'photos#show' ``` -An incoming path of `/photos/show/1?user_id=2` will be dispatched to the `show` action of the `Photos` controller. `params` will be `{ controller: 'photos', action: 'show', id: '1', user_id: '2' }`. +An incoming path of `/photos/1?user_id=2` will be dispatched to the `show` action of the `Photos` controller. `params` will be `{ controller: 'photos', action: 'show', id: '1', user_id: '2' }`. ### Defining Defaults -You do not need to explicitly use the `:controller` and `:action` symbols within a route. You can supply them as defaults: - -```ruby -get 'photos/:id', to: 'photos#show' -``` - -With this route, Rails will match an incoming path of `/photos/12` to the `show` action of `PhotosController`. - -You can also define other defaults in a route by supplying a hash for the `:defaults` option. This even applies to parameters that you do not specify as dynamic segments. For example: +You can define defaults in a route by supplying a hash for the `:defaults` option. This even applies to parameters that you do not specify as dynamic segments. For example: ```ruby get 'photos/:id', to: 'photos#show', defaults: { format: 'jpg' } diff --git a/guides/source/testing.md b/guides/source/testing.md index 26d50bec0c..4ca3236ec1 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -37,10 +37,12 @@ controllers/ helpers/ mailers/ test_helper.rb fixtures/ integration/ models/ ``` -The `models` directory is meant to hold tests for your models, the `controllers` directory is meant to hold tests for your controllers and the `integration` directory is meant to hold tests that involve any number of controllers interacting. There is also a directory for testing your mailers and one for testing view helpers. +The `helpers`, `mailers`, and `models` directories are meant to hold tests for view helpers, mailers, and models, respectively. The `controllers` directory is meant to hold tests for controllers, routes, and views. The `integration` directory is meant to hold tests for interactions between controllers. Fixtures are a way of organizing test data; they reside in the `fixtures` directory. +A `jobs` directory will also be created when an associated test is first generated. + The `test_helper.rb` file holds the default configuration for your tests. @@ -1085,7 +1087,7 @@ end Testing Routes -------------- -Like everything else in your Rails application, you can test your routes. +Like everything else in your Rails application, you can test your routes. Route tests reside in `test/controllers/` or are part of controller tests. NOTE: If your application has complex routes, Rails provides a number of useful helpers to test them. |