diff options
234 files changed, 7910 insertions, 1535 deletions
@@ -12,10 +12,13 @@ gem 'jquery-rails', '~> 2.1.4', github: 'rails/jquery-rails' gem 'turbolinks' gem 'coffee-rails', github: 'rails/coffee-rails' -gem 'journey', github: 'rails/journey', branch: 'master' +gem 'thread_safe', '~> 0.1' gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders', branch: 'master' +# Needed for compiling the ActionDispatch::Journey parser +gem 'racc', '>=1.4.6', require: false + # This needs to be with require false to avoid # it being automatically loaded by sprockets gem 'uglifier', require: false diff --git a/README.rdoc b/README.rdoc index 87ec64e3b5..91a5f27add 100644 --- a/README.rdoc +++ b/README.rdoc @@ -56,10 +56,10 @@ can read more about Action Pack in its {README}[link:/rails/rails/blob/master/ac 5. Follow the guidelines to start developing your application. You may find the following resources handy: * The README file created within your application. -* The {Getting Started with Rails}[http://guides.rubyonrails.org/getting_started.html]. -* The {Ruby on Rails Tutorial}[http://railstutorial.org/book]. -* The {Ruby on Rails Guides}[http://guides.rubyonrails.org]. -* The {API Documentation}[http://api.rubyonrails.org]. +* {Getting Started with Rails}[http://guides.rubyonrails.org/getting_started.html]. +* {Ruby on Rails Tutorial}[http://ruby.railstutorial.org/ruby-on-rails-tutorial-book]. +* {Ruby on Rails Guides}[http://guides.rubyonrails.org]. +* {The API Documentation}[http://api.rubyonrails.org]. == Contributing diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index af91907f46..2f99b61b50 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,37 +1,37 @@ ## Rails 4.0.0 (unreleased) ## -* Explicit multipart messages no longer set the order of the MIME parts. - *Nate Berkopec* - -* Do not render views when mail() isn't called. - Fix #7761 +* Explicit multipart messages no longer set the order of the MIME parts. + *Nate Berkopec* - *Yves Senn* +* Do not render views when mail() isn't called. + Fix #7761 -* Allow delivery method options to be set per mail instance *Aditya Sanghi* + *Yves Senn* - If your smtp delivery settings are dynamic, - you can now override settings per mail instance for e.g. +* Allow delivery method options to be set per mail instance *Aditya Sanghi* - def my_mailer(user,company) - mail to: user.email, subject: "Welcome!", - delivery_method_options: {user_name: company.smtp_user, - password: company.smtp_password} - end + If your smtp delivery settings are dynamic, + you can now override settings per mail instance for e.g. - This will ensure that your default SMTP settings will be overridden - by the company specific ones. You only have to override the settings - that are dynamic and leave the static setting in your environment - configuration file (e.g. config/environments/production.rb) + def my_mailer(user,company) + mail to: user.email, subject: "Welcome!", + delivery_method_options: { user_name: company.smtp_user, + password: company.smtp_password } + end -* Allow to set default Action Mailer options via `config.action_mailer.default_options=` *Robert Pankowecki* + This will ensure that your default SMTP settings will be overridden + by the company specific ones. You only have to override the settings + that are dynamic and leave the static setting in your environment + configuration file (e.g. config/environments/production.rb) -* Raise an `ActionView::MissingTemplate` exception when no implicit template could be found. *Damien Mathieu* +* Allow to set default Action Mailer options via `config.action_mailer.default_options=` *Robert Pankowecki* -* Asynchronously send messages via the Rails Queue *Brian Cardarella* +* Raise an `ActionView::MissingTemplate` exception when no implicit template could be found. *Damien Mathieu* -* Allow callbacks to be defined in mailers similar to `ActionController::Base`. You can configure default - settings, headers, attachments, delivery settings or change delivery using - `before_filter`, `after_filter` etc. *Justin S. Leitgeb* +* Asynchronously send messages via the Rails Queue *Brian Cardarella* + +* Allow callbacks to be defined in mailers similar to `ActionController::Base`. You can configure default + settings, headers, attachments, delivery settings or change delivery using + `before_filter`, `after_filter` etc. *Justin S. Leitgeb* Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/actionmailer/CHANGELOG.md) for previous changes. diff --git a/actionmailer/actionmailer.gemspec b/actionmailer/actionmailer.gemspec index ea6df10a6f..67ec0d1097 100644 --- a/actionmailer/actionmailer.gemspec +++ b/actionmailer/actionmailer.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'README.rdoc', 'MIT-LICENSE', 'lib/**/*'].select { |path| File.file? path } + s.files = Dir['CHANGELOG.md', 'README.rdoc', 'MIT-LICENSE', 'lib/**/*'] s.require_path = 'lib' s.requirements << 'none' diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index dcf13e927c..4a61bac0a5 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -314,7 +314,7 @@ module ActionMailer # These options are specified on the class level, like # <tt>ActionMailer::Base.raise_delivery_errors = true</tt> # - # * <tt>default</tt> - You can pass this in at a class level as well as within the class itself as + # * <tt>default_options</tt> - You can pass this in at a class level as well as within the class itself as # per the above section. # # * <tt>logger</tt> - the logger is used for generating information on the mailing run if available. @@ -499,6 +499,7 @@ module ActionMailer # method, for instance). def initialize(method_name=nil, *args) super() + @_mail_was_called = false @_message = Mail.new process(method_name, *args) if method_name end @@ -506,7 +507,8 @@ module ActionMailer def process(*args) #:nodoc: lookup_context.skip_default_locale! - @_message = NullMail.new unless super + super + @_message = NullMail.new unless @_mail_was_called end class NullMail #:nodoc: @@ -666,11 +668,11 @@ module ActionMailer # end # def mail(headers={}, &block) + @_mail_was_called = true m = @_message - # At the beginning, do not consider class default for parts order neither content_type + # At the beginning, do not consider class default for content_type content_type = headers[:content_type] - parts_order = headers[:parts_order] # Call all the procs (if any) class_default = self.class.default diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 4a608a9ad4..170923673b 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -505,6 +505,12 @@ class BaseTest < ActiveSupport::TestCase mail.deliver end + test 'the return value of mailer methods is not relevant' do + mail = BaseMailer.with_nil_as_return_value + assert_equal('Welcome', mail.body.to_s.strip) + mail.deliver + end + # Before and After hooks class MyObserver diff --git a/actionmailer/test/mailers/base_mailer.rb b/actionmailer/test/mailers/base_mailer.rb index 52342bc59e..8fca6177bd 100644 --- a/actionmailer/test/mailers/base_mailer.rb +++ b/actionmailer/test/mailers/base_mailer.rb @@ -118,4 +118,9 @@ class BaseMailer < ActionMailer::Base def without_mail_call end + + def with_nil_as_return_value(hash = {}) + mail(:template_name => "welcome") + nil + end end diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 9db30c8cf7..88c4bea3d2 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,36 +1,71 @@ ## Rails 4.0.0 (unreleased) ## +* Integrate the Journey gem into Action Dispatch so that the global namespace + is not polluted with names that may be used as models. + + *Andrew White* + +* Extract support for email address obfuscation via `:encode`, `:replace_at`, and `replace_dot` + options from the `mail_to` helper into the `actionview-encoded_mail_to` gem. + + *Nick Reed + DHH* + +* Handle `:protocol` option in `stylesheet_link_tag` and `javascript_include_tag` + + *Vasiliy Ermolovich* + +* Clear url helper methods when routes are reloaded. *Andrew White* + +* Fix a bug in `ActionDispatch::Request#raw_post` that caused `env['rack.input']` + to be read but not rewound. + + *Matt Venables* + +* Prevent raising EOFError on multipart GET request (IE issue). *Adam Stankiewicz* + * Rename all action callbacks from *_filter to *_action to avoid the misconception that these callbacks are only suited for transforming or halting the response. With the new style, it's more inviting to use them as they were intended, like setting shared ivars for views. - + Example: - + class PeopleController < ActionController::Base - before_action :set_person, except: [ :index, :new, :create ] - before_action :ensure_permission, only: [ :edit, :update ] - + before_action :set_person, except: [:index, :new, :create] + before_action :ensure_permission, only: [:edit, :update] + ... - + private def set_person @person = current_account.people.find(params[:id]) end - + def ensure_permission current_person.can_change?(@person) end end - + The old *_filter methods still work with no deprecation notice. - + *DHH* -* Add :if / :unless conditions to fragment cache: +* Add `cache_if` and `cache_unless` for conditional fragment caching: + + Example: + + <%= cache_if condition, project do %> + <b>All the topics on this project</b> + <%= render project.topics %> + <% end %> + + # and - <%= cache @model, if: some_condition(@model) do %> + <%= cache_unless condition, project do %> + <b>All the topics on this project</b> + <%= render project.topics %> + <% end %> - *Stephen Ausman + Fabrizio Regini* + *Stephen Ausman + Fabrizio Regini + Angelo Capilleri* * Add filter capability to ActionController logs for redirect locations: @@ -47,7 +82,7 @@ an invalid `:layout` argument. #8376 - render :partial => 'partial', :layout => true + render partial: 'partial', layout: true # results in ActionView::MissingTemplate: Missing partial /true @@ -65,11 +100,11 @@ *Drew Ulmer* -* No sort Hash options in #grouped_options_for_select. *Sergey Kojin* +* No sort Hash options in `grouped_options_for_select`. *Sergey Kojin* -* Accept symbols as #send_data :disposition value *Elia Schito* +* Accept symbols as `send_data :disposition` value *Elia Schito* -* Add i18n scope to distance_of_time_in_words. *Steve Klabnik* +* Add i18n scope to `distance_of_time_in_words`. *Steve Klabnik* * `assert_template`: - is no more passing with empty string. @@ -97,7 +132,7 @@ resources :users end - *Guillermo Iguaran* + *Guillermo Iguaran + Amparo Luna* * Fix error when using a non-hash query argument named "params" in `url_for`. @@ -124,26 +159,22 @@ *Joost Baaij* -* Fix input name when `:multiple => true` and `:index` are set. +* Fix input name when `multiple: true` and `:index` are set. Before: - check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1) + check_box("post", "comment_ids", { multiple: true, index: "foo" }, 1) #=> <input name=\"post[foo][comment_ids]\" type=\"hidden\" value=\"0\" /><input id=\"post_foo_comment_ids_1\" name=\"post[foo][comment_ids]\" type=\"checkbox\" value=\"1\" /> After: - check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1) + check_box("post", "comment_ids", { multiple: true, index: "foo" }, 1) #=> <input name=\"post[foo][comment_ids][]\" type=\"hidden\" value=\"0\" /><input id=\"post_foo_comment_ids_1\" name=\"post[foo][comment_ids][]\" type=\"checkbox\" value=\"1\" /> Fix #8108 *Daniel Fox, Grant Hutchins & Trace Wax* -* Clear url helpers when reloading routes. - - *Santiago Pastorino* - * `BestStandardsSupport` middleware now appends it's `X-UA-Compatible` value to app's returned value if any. Fix #8086 @@ -309,7 +340,7 @@ New applications are generated with: - protect_from_forgery :with => :exception + protect_from_forgery with: :exception *Sergey Nartimov* @@ -317,7 +348,7 @@ * Add `separator` option for `ActionView::Helpers::TextHelper#excerpt`: - excerpt('This is a very beautiful morning', 'very', :separator => ' ', :radius => 1) + excerpt('This is a very beautiful morning', 'very', separator: ' ', radius: 1) # => ...a very beautiful... *Guirec Corbel* @@ -434,7 +465,7 @@ We recommend the use of Unobtrusive JavaScript instead. For example: - link_to "Greeting", "#", :class => "nav_link" + link_to "Greeting", "#", class: "nav_link" $(function() { $('.nav_link').click(function() { @@ -480,7 +511,7 @@ * Remove `ActionDispatch::Head` middleware in favor of `Rack::Head`. *Santiago Pastorino* -* Deprecate `:confirm` in favor of `:data => { :confirm => "Text" }` option for `button_to`, `button_tag`, `image_submit_tag`, `link_to` and `submit_tag` helpers. +* Deprecate `:confirm` in favor of `data: { confirm: "Text" }` option for `button_to`, `button_tag`, `image_submit_tag`, `link_to` and `submit_tag` helpers. *Carlos Galdino + Rafael Mendonça França* @@ -492,7 +523,7 @@ add_flash_types :error, :warning end - If you add the above code, you can use `<%= error %>` in an erb, and `redirect_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* @@ -575,7 +606,7 @@ * Templates without a handler extension now raises a deprecation warning but still defaults to ERb. In future releases, it will simply return the template contents. *Steve Klabnik* -* Deprecate `:disable_with` in favor of `:data => { :disable_with => "Text" }` option from `submit_tag`, `button_tag` and `button_to` helpers. +* Deprecate `:disable_with` in favor of `data: { disable_with: "Text" }` option from `submit_tag`, `button_tag` and `button_to` helpers. *Carlos Galdino + Rafael Mendonça França* @@ -597,7 +628,7 @@ * Add backtrace to development routing error page. *Richard Schneeman* -* Replace `include_seconds` boolean argument with `:include_seconds => true` option +* Replace `include_seconds` boolean argument with `include_seconds: true` option in `distance_of_time_in_words` and `time_ago_in_words` signature. *Dmitriy Kiriyenko* * Make current object and counter (when it applies) variables accessible when @@ -621,11 +652,11 @@ * Changed default value for `config.action_view.embed_authenticity_token_in_remote_forms` to `false`. This change breaks remote forms that need to work also without javascript, so if you need such behavior, you can either set it to `true` or explicitly pass - `:authenticity_token => true` in form options + `authenticity_token: true` in form options * Added ActionDispatch::SSL middleware that when included force all the requests to be under HTTPS protocol. *Rafael Mendonça França* -* Add `include_hidden` option to select tag. With `:include_hidden => false` select with `multiple` attribute doesn't generate hidden input with blank value. *Vasiliy Ermolovich* +* Add `include_hidden` option to select tag. With `include_hidden: false` select with `multiple` attribute doesn't generate hidden input with blank value. *Vasiliy Ermolovich* * Removed default `size` option from the `text_field`, `search_field`, `telephone_field`, `url_field`, `email_field` helpers. *Philip Arndt* @@ -642,7 +673,7 @@ * Don't ignore `force_ssl` in development. This is a change of behavior - use a `:if` condition to recreate the old behavior. class AccountsController < ApplicationController - force_ssl :if => :ssl_configured? + force_ssl if: :ssl_configured? def ssl_configured? !Rails.env.development? @@ -711,15 +742,15 @@ *Carlos Antonio da Silva + Rafael Mendonça França* -* check_box with `:form` html5 attribute will now replicate the `:form` +* `check_box` with `:form` html5 attribute will now replicate the `:form` attribute to the hidden field as well. *Carlos Antonio da Silva* * Turn off verbose mode of rack-cache, we still have X-Rack-Cache to check that info. Closes #5245. *Santiago Pastorino* -* `label` form helper accepts :for => nil to not generate the attribute. *Carlos Antonio da Silva* +* `label` form helper accepts `for: nil` to not generate the attribute. *Carlos Antonio da Silva* -* Add `:format` option to number_to_percentage *Rodrigo Flores* +* Add `:format` option to `number_to_percentage`. *Rodrigo Flores* * Add `config.action_view.logger` to configure logger for Action View. *Rafael Mendonça França* @@ -739,7 +770,7 @@ * Deprecated `ActionController::Routing` in favour of `ActionDispatch::Routing`. -* `check_box helper` with `:disabled => true` will generate a disabled +* `check_box helper` with `disabled: true` will generate a disabled hidden field to conform with the HTML convention where disabled fields are not submitted with the form. This is a behavior change, previously the hidden tag had a value of the disabled checkbox. *Tadas Tamosauskas* diff --git a/actionpack/Rakefile b/actionpack/Rakefile index 50e3bb0d48..ba7956c3ab 100644 --- a/actionpack/Rakefile +++ b/actionpack/Rakefile @@ -15,7 +15,7 @@ Rake::TestTask.new(:test_action_pack) do |t| # make sure we include the tests in alphabetical order as on some systems # this will not happen automatically and the tests (as a whole) will error - t.test_files = Dir.glob('test/{abstract,controller,dispatch,template,assertions}/**/*_test.rb').sort + t.test_files = Dir.glob('test/{abstract,controller,dispatch,template,assertions,journey}/**/*_test.rb').sort t.warning = true t.verbose = true @@ -75,3 +75,9 @@ task :lines do puts "Total: Lines #{total_lines}, LOC #{total_codelines}" end + +rule '.rb' => '.y' do |t| + sh "racc -l -o #{t.name} #{t.source}" +end + +task compile: 'lib/action_dispatch/journey/parser.rb' diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 883aaab6fb..c65870cac6 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'README.rdoc', 'MIT-LICENSE', 'lib/**/*'].select { |path| File.file? path } + s.files = Dir['CHANGELOG.md', 'README.rdoc', 'MIT-LICENSE', 'lib/**/*'] s.require_path = 'lib' s.requirements << 'none' @@ -23,7 +23,6 @@ Gem::Specification.new do |s| s.add_dependency 'builder', '~> 3.1.0' s.add_dependency 'rack', '~> 1.4.1' s.add_dependency 'rack-test', '~> 0.6.1' - s.add_dependency 'journey', '~> 2.0.0' s.add_dependency 'erubis', '~> 2.7.0' s.add_development_dependency 'activemodel', version diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index d4e73bf257..812a35735f 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -19,7 +19,7 @@ module AbstractController def inherited(klass) helpers = _helpers klass._helpers = Module.new { include helpers } - klass.class_eval { default_helper_module! unless anonymous? } + klass.class_eval { default_helper_module! } unless klass.anonymous? super end @@ -113,7 +113,7 @@ module AbstractController # helpers with the following behavior: # # String or Symbol:: :FooBar or "FooBar" becomes "foo_bar_helper", - # and "foo_bar_helper.rb" is loaded using require_dependency. + # and "foo_bar_helper.rb" is loaded using require_dependency. # # Module:: No further processing # diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 12da273af9..73799e8085 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -212,21 +212,23 @@ module AbstractController delegate :_layout_conditions, :to => "self.class" module ClassMethods - def inherited(klass) + def inherited(klass) # :nodoc: super klass._write_layout_method end # This module is mixed in if layout conditions are provided. This means # that if no layout conditions are used, this method is not used - module LayoutConditions - # Determines whether the current action has a layout by checking the - # action name against the :only and :except conditions set on the - # layout. + module LayoutConditions # :nodoc: + private + + # Determines whether the current action has a layout definition by + # checking the action name against the :only and :except conditions + # set by the <tt>layout</tt> method. # # ==== Returns - # * <tt> Boolean</tt> - True if the action has a layout, false otherwise. - def conditional_layout? + # * <tt> Boolean</tt> - True if the action has a layout definition, false otherwise. + def _conditional_layout? return unless super conditions = _layout_conditions @@ -271,7 +273,7 @@ module AbstractController # # ==== Returns # * <tt>String</tt> - A template name - def _implied_layout_name + def _implied_layout_name # :nodoc: controller_path end @@ -279,7 +281,7 @@ module AbstractController # # If a layout is not explicitly mentioned then look for a layout with the controller's name. # if nothing is found then try same procedure to find super class's layout. - def _write_layout_method + def _write_layout_method # :nodoc: remove_possible_method(:_layout) prefixes = _implied_layout_name =~ /\blayouts/ ? [] : ["layouts"] @@ -318,7 +320,7 @@ module AbstractController self.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def _layout - if conditional_layout? + if _conditional_layout? #{layout_definition} else #{name_clause} @@ -329,7 +331,7 @@ module AbstractController end end - def _normalize_options(options) + def _normalize_options(options) # :nodoc: super if _include_layout?(options) @@ -340,21 +342,27 @@ module AbstractController attr_internal_writer :action_has_layout - def initialize(*) + def initialize(*) # :nodoc: @_action_has_layout = true super end + # Controls whether an action should be rendered using a layout. + # If you want to disable any <tt>layout</tt> settings for the + # current action so that it is rendered without a layout then + # either override this method in your controller to return false + # for that action or set the <tt>action_has_layout</tt> attribute + # to false before rendering. def action_has_layout? @_action_has_layout end - def conditional_layout? + private + + def _conditional_layout? true end - private - # This will be overwritten by _write_layout_method def _layout; end diff --git a/actionpack/lib/action_controller/metal/helpers.rb b/actionpack/lib/action_controller/metal/helpers.rb index d2cbbd3330..35facd13c8 100644 --- a/actionpack/lib/action_controller/metal/helpers.rb +++ b/actionpack/lib/action_controller/metal/helpers.rb @@ -1,4 +1,3 @@ - module ActionController # The \Rails framework provides a large number of helpers for working with assets, dates, forms, # numbers and model objects, to name a few. These helpers are available to all templates @@ -91,11 +90,11 @@ module ActionController end def all_helpers_from_path(path) - helpers = [] - Array(path).each do |_path| - extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_helper.rb$/ + helpers = Array(path).flat_map do |_path| + extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_helper.rb$/ names = Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') } - helpers += names.sort + names.sort! + names end helpers.uniq! helpers diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index 283f6413ec..f50394837b 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -384,6 +384,8 @@ module ActionController # # RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L] module Token + TOKEN_REGEX = /^Token / + AUTHN_PAIR_DELIMITERS = /(?:,|;|\t+)/ extend self module ControllerMethods @@ -431,20 +433,34 @@ module ActionController # Returns an Array of [String, Hash] if a token is present. # Returns nil if no token is found. def token_and_options(request) - if request.authorization.to_s[/^Token (.*)/] - values = Hash[$1.split(',').map do |value| - value.strip! # remove any spaces between commas and values - key, value = value.split(/\=\"?/) # split key=value pairs - if value - value.chomp!('"') # chomp trailing " in value - value.gsub!(/\\\"/, '"') # unescape remaining quotes - [key, value] - end - end.compact] - [values.delete("token"), values.with_indifferent_access] + authorization_request = request.authorization.to_s + if authorization_request[TOKEN_REGEX] + params = token_params_from authorization_request + [params.shift.last, Hash[params].with_indifferent_access] end end + def token_params_from(auth) + rewrite_param_values params_array_from raw_params auth + end + + # Takes raw_params and turns it into an array of parameters + def params_array_from(raw_params) + raw_params.map { |param| param.split %r/=(.+)?/ } + end + + # This removes the `"` characters wrapping the value. + def rewrite_param_values(array_params) + array_params.each { |param| param.last.gsub! %r/^"|"$/, '' } + end + + # This method takes an authorization body and splits up the key-value + # pairs by the standardized `:`, `;`, or `\t` delimiters defined in + # `AUTHN_PAIR_DELIMITERS`. + def raw_params(auth) + auth.sub(TOKEN_REGEX, '').split /"\s*#{AUTHN_PAIR_DELIMITERS}\s*/ + end + # Encodes the given token and options into an Authorization header value. # # token - String token. diff --git a/actionpack/lib/action_controller/metal/redirecting.rb b/actionpack/lib/action_controller/metal/redirecting.rb index b23938e7d9..091facfd8d 100644 --- a/actionpack/lib/action_controller/metal/redirecting.rb +++ b/actionpack/lib/action_controller/metal/redirecting.rb @@ -74,7 +74,7 @@ module ActionController private def _extract_redirect_to_status(options, response_status) - status = if options.is_a?(Hash) && options.key?(:status) + if options.is_a?(Hash) && options.key?(:status) Rack::Utils.status_code(options.delete(:status)) elsif response_status.key?(:status) Rack::Utils.status_code(response_status[:status]) @@ -94,8 +94,7 @@ module ActionController when String request.protocol + request.host_with_port + options when :back - raise RedirectBackError unless refer = request.headers["Referer"] - refer + request.headers["Referer"] or raise RedirectBackError when Proc _compute_redirect_to_location options.call else diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 25e72adbe0..8faa5f8a13 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -1,5 +1,6 @@ require 'active_support/concern' require 'active_support/core_ext/hash/indifferent_access' +require 'active_support/core_ext/array/wrap' require 'active_support/rescuable' module ActionController diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index d002babee3..938161dc69 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -63,6 +63,7 @@ module ActionDispatch autoload :Static end + autoload :Journey autoload :MiddlewareStack, 'action_dispatch/middleware/stack' autoload :Routing diff --git a/actionpack/lib/action_dispatch/http/filter_parameters.rb b/actionpack/lib/action_dispatch/http/filter_parameters.rb index 4a7df6b657..02ab49b44e 100644 --- a/actionpack/lib/action_dispatch/http/filter_parameters.rb +++ b/actionpack/lib/action_dispatch/http/filter_parameters.rb @@ -1,4 +1,3 @@ -require 'mutex_m' require 'active_support/core_ext/hash/keys' require 'active_support/core_ext/object/duplicable' @@ -21,8 +20,6 @@ module ActionDispatch # end # => reverses the value to all keys matching /secret/i module FilterParameters - @@parameter_filter_for = {}.extend(Mutex_m) - ENV_MATCH = [/RAW_POST_DATA/, "rack.request.form_vars"] # :nodoc: NULL_PARAM_FILTER = ParameterFilter.new # :nodoc: NULL_ENV_FILTER = ParameterFilter.new ENV_MATCH # :nodoc: @@ -65,11 +62,7 @@ module ActionDispatch end def parameter_filter_for(filters) - @@parameter_filter_for.synchronize do - # Do we *actually* need this cache? Constructing ParameterFilters - # doesn't seem too expensive. - @@parameter_filter_for[filters] ||= ParameterFilter.new(filters) - end + ParameterFilter.new(filters) end KV_RE = '[^&;=]+' @@ -79,7 +72,6 @@ module ActionDispatch parameter_filter.filter([[$1, $2]]).first.join("=") end end - end end end diff --git a/actionpack/lib/action_dispatch/http/parameters.rb b/actionpack/lib/action_dispatch/http/parameters.rb index 9a7b5bc8c7..6610315da7 100644 --- a/actionpack/lib/action_dispatch/http/parameters.rb +++ b/actionpack/lib/action_dispatch/http/parameters.rb @@ -12,7 +12,11 @@ module ActionDispatch # Returns both GET and POST \parameters in a single hash. def parameters @env["action_dispatch.request.parameters"] ||= begin - params = request_parameters.merge(query_parameters) + params = begin + request_parameters.merge(query_parameters) + rescue EOFError + query_parameters.dup + end params.merge!(path_parameters) encode_params(params).with_indifferent_access end diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 3de927abc8..d60c8775af 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -205,8 +205,9 @@ module ActionDispatch # work with raw requests directly. def raw_post unless @env.include? 'RAW_POST_DATA' - @env['RAW_POST_DATA'] = body.read(@env['CONTENT_LENGTH'].to_i) - body.rewind if body.respond_to?(:rewind) + raw_post_body = body + @env['RAW_POST_DATA'] = raw_post_body.read(@env['CONTENT_LENGTH'].to_i) + raw_post_body.rewind if raw_post_body.respond_to?(:rewind) end @env['RAW_POST_DATA'] end diff --git a/actionpack/lib/action_dispatch/journey.rb b/actionpack/lib/action_dispatch/journey.rb new file mode 100644 index 0000000000..ad42713482 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey.rb @@ -0,0 +1,5 @@ +require 'action_dispatch/journey/router' +require 'action_dispatch/journey/gtg/builder' +require 'action_dispatch/journey/gtg/simulator' +require 'action_dispatch/journey/nfa/builder' +require 'action_dispatch/journey/nfa/simulator' diff --git a/actionpack/lib/action_dispatch/journey/backwards.rb b/actionpack/lib/action_dispatch/journey/backwards.rb new file mode 100644 index 0000000000..3bd20fdf81 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/backwards.rb @@ -0,0 +1,5 @@ +module Rack # :nodoc: + Mount = ActionDispatch::Journey::Router + Mount::RouteSet = ActionDispatch::Journey::Router + Mount::RegexpWithNamedGroups = ActionDispatch::Journey::Path::Pattern +end diff --git a/actionpack/lib/action_dispatch/journey/formatter.rb b/actionpack/lib/action_dispatch/journey/formatter.rb new file mode 100644 index 0000000000..4a344f71af --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/formatter.rb @@ -0,0 +1,144 @@ +module ActionDispatch + module Journey + # The Formatter class is used for formatting URLs. For example, parameters + # passed to +url_for+ in rails will eventually call Formatter#generate. + class Formatter # :nodoc: + attr_reader :routes + + def initialize(routes) + @routes = routes + @cache = nil + end + + def generate(type, name, options, recall = {}, parameterize = nil) + constraints = recall.merge(options) + missing_keys = [] + + match_route(name, constraints) do |route| + parameterized_parts = extract_parameterized_parts(route, options, recall, parameterize) + next if !name && route.requirements.empty? && route.parts.empty? + + missing_keys = missing_keys(route, parameterized_parts) + next unless missing_keys.empty? + params = options.dup.delete_if do |key, _| + parameterized_parts.key?(key) || route.defaults.key?(key) + end + + return [route.format(parameterized_parts), params] + end + + raise Router::RoutingError.new "missing required keys: #{missing_keys}" + end + + def clear + @cache = nil + end + + private + + def extract_parameterized_parts(route, options, recall, parameterize = nil) + constraints = recall.merge(options) + data = constraints.dup + + keys_to_keep = route.parts.reverse.drop_while { |part| + !options.key?(part) || (options[part] || recall[part]).nil? + } | route.required_parts + + (data.keys - keys_to_keep).each do |bad_key| + data.delete(bad_key) + end + + parameterized_parts = data.dup + + if parameterize + parameterized_parts.each do |k, v| + parameterized_parts[k] = parameterize.call(k, v) + end + end + + parameterized_parts.keep_if { |_, v| v } + parameterized_parts + end + + def named_routes + routes.named_routes + end + + def match_route(name, options) + if named_routes.key?(name) + yield named_routes[name] + else + routes = non_recursive(cache, options.to_a) + + hash = routes.group_by { |_, r| r.score(options) } + + hash.keys.sort.reverse_each do |score| + next if score < 0 + + hash[score].sort_by { |i, _| i }.each do |_, route| + yield route + end + end + end + end + + def non_recursive(cache, options) + routes = [] + stack = [cache] + + while stack.any? + c = stack.shift + routes.concat(c[:___routes]) if c.key?(:___routes) + + options.each do |pair| + stack << c[pair] if c.key?(pair) + end + end + + routes + end + + # Returns an array populated with missing keys if any are present. + def missing_keys(route, parts) + missing_keys = [] + tests = route.path.requirements + route.required_parts.each { |key| + if tests.key?(key) + missing_keys << key unless /\A#{tests[key]}\Z/ === parts[key] + else + missing_keys << key unless parts[key] + end + } + missing_keys + end + + def possibles(cache, options, depth = 0) + cache.fetch(:___routes) { [] } + options.find_all { |pair| + cache.key?(pair) + }.map { |pair| + possibles(cache[pair], options, depth + 1) + }.flatten(1) + end + + # Returns +true+ if no missing keys are present, otherwise +false+. + def verify_required_parts!(route, parts) + missing_keys(route, parts).empty? + end + + def build_cache + root = { ___routes: [] } + routes.each_with_index do |route, i| + leaf = route.required_defaults.inject(root) do |h, tuple| + h[tuple] ||= {} + end + (leaf[:___routes] ||= []) << [i, route] + end + root + end + + def cache + @cache ||= build_cache + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/gtg/builder.rb b/actionpack/lib/action_dispatch/journey/gtg/builder.rb new file mode 100644 index 0000000000..7d2791714b --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/gtg/builder.rb @@ -0,0 +1,162 @@ +require 'action_dispatch/journey/gtg/transition_table' + +module ActionDispatch + module Journey # :nodoc: + module GTG # :nodoc: + class Builder # :nodoc: + DUMMY = Nodes::Dummy.new + + attr_reader :root, :ast, :endpoints + + def initialize(root) + @root = root + @ast = Nodes::Cat.new root, DUMMY + @followpos = nil + end + + def transition_table + dtrans = TransitionTable.new + marked = {} + state_id = Hash.new { |h,k| h[k] = h.length } + + start = firstpos(root) + dstates = [start] + until dstates.empty? + s = dstates.shift + next if marked[s] + marked[s] = true # mark s + + s.group_by { |state| symbol(state) }.each do |sym, ps| + u = ps.map { |l| followpos(l) }.flatten + next if u.empty? + + if u.uniq == [DUMMY] + from = state_id[s] + to = state_id[Object.new] + dtrans[from, to] = sym + + dtrans.add_accepting(to) + ps.each { |state| dtrans.add_memo(to, state.memo) } + else + dtrans[state_id[s], state_id[u]] = sym + + if u.include?(DUMMY) + to = state_id[u] + + accepting = ps.find_all { |l| followpos(l).include?(DUMMY) } + + accepting.each { |accepting_state| + dtrans.add_memo(to, accepting_state.memo) + } + + dtrans.add_accepting(state_id[u]) + end + end + + dstates << u + end + end + + dtrans + end + + def nullable?(node) + case node + when Nodes::Group + true + when Nodes::Star + true + when Nodes::Or + node.children.any? { |c| nullable?(c) } + when Nodes::Cat + nullable?(node.left) && nullable?(node.right) + when Nodes::Terminal + !node.left + when Nodes::Unary + nullable?(node.left) + else + raise ArgumentError, 'unknown nullable: %s' % node.class.name + end + end + + def firstpos(node) + case node + when Nodes::Star + firstpos(node.left) + when Nodes::Cat + if nullable?(node.left) + firstpos(node.left) | firstpos(node.right) + else + firstpos(node.left) + end + when Nodes::Or + node.children.map { |c| firstpos(c) }.flatten.uniq + when Nodes::Unary + firstpos(node.left) + when Nodes::Terminal + nullable?(node) ? [] : [node] + else + raise ArgumentError, 'unknown firstpos: %s' % node.class.name + end + end + + def lastpos(node) + case node + when Nodes::Star + firstpos(node.left) + when Nodes::Or + node.children.map { |c| lastpos(c) }.flatten.uniq + when Nodes::Cat + if nullable?(node.right) + lastpos(node.left) | lastpos(node.right) + else + lastpos(node.right) + end + when Nodes::Terminal + nullable?(node) ? [] : [node] + when Nodes::Unary + lastpos(node.left) + else + raise ArgumentError, 'unknown lastpos: %s' % node.class.name + end + end + + def followpos(node) + followpos_table[node] + end + + private + + def followpos_table + @followpos ||= build_followpos + end + + def build_followpos + table = Hash.new { |h, k| h[k] = [] } + @ast.each do |n| + case n + when Nodes::Cat + lastpos(n.left).each do |i| + table[i] += firstpos(n.right) + end + when Nodes::Star + lastpos(n).each do |i| + table[i] += firstpos(n) + end + end + end + table + end + + def symbol(edge) + case edge + when Journey::Nodes::Symbol + edge.regexp + else + edge.left + end + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/gtg/simulator.rb b/actionpack/lib/action_dispatch/journey/gtg/simulator.rb new file mode 100644 index 0000000000..58ad803841 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/gtg/simulator.rb @@ -0,0 +1,44 @@ +require 'strscan' + +module ActionDispatch + module Journey # :nodoc: + module GTG # :nodoc: + class MatchData # :nodoc: + attr_reader :memos + + def initialize(memos) + @memos = memos + end + end + + class Simulator # :nodoc: + attr_reader :tt + + def initialize(transition_table) + @tt = transition_table + end + + def simulate(string) + input = StringScanner.new(string) + state = [0] + while sym = input.scan(%r([/.?]|[^/.?]+)) + state = tt.move(state, sym) + end + + acceptance_states = state.find_all { |s| + tt.accepting? s + } + + return if acceptance_states.empty? + + memos = acceptance_states.map { |x| tt.memo(x) }.flatten.compact + + MatchData.new(memos) + end + + alias :=~ :simulate + alias :match :simulate + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/gtg/transition_table.rb b/actionpack/lib/action_dispatch/journey/gtg/transition_table.rb new file mode 100644 index 0000000000..da0cddd93c --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/gtg/transition_table.rb @@ -0,0 +1,156 @@ +require 'action_dispatch/journey/nfa/dot' + +module ActionDispatch + module Journey # :nodoc: + module GTG # :nodoc: + class TransitionTable # :nodoc: + include Journey::NFA::Dot + + attr_reader :memos + + def initialize + @regexp_states = Hash.new { |h,k| h[k] = {} } + @string_states = Hash.new { |h,k| h[k] = {} } + @accepting = {} + @memos = Hash.new { |h,k| h[k] = [] } + end + + def add_accepting(state) + @accepting[state] = true + end + + def accepting_states + @accepting.keys + end + + def accepting?(state) + @accepting[state] + end + + def add_memo(idx, memo) + @memos[idx] << memo + end + + def memo(idx) + @memos[idx] + end + + def eclosure(t) + Array(t) + end + + def move(t, a) + move_string(t, a).concat(move_regexp(t, a)) + end + + def to_json + require 'json' + + simple_regexp = Hash.new { |h,k| h[k] = {} } + + @regexp_states.each do |from, hash| + hash.each do |re, to| + simple_regexp[from][re.source] = to + end + end + + JSON.dump({ + regexp_states: simple_regexp, + string_states: @string_states, + accepting: @accepting + }) + end + + def to_svg + svg = IO.popen('dot -Tsvg', 'w+') { |f| + f.write(to_dot) + f.close_write + f.readlines + } + 3.times { svg.shift } + svg.join.sub(/width="[^"]*"/, '').sub(/height="[^"]*"/, '') + end + + def visualizer(paths, title = 'FSM') + viz_dir = File.join File.dirname(__FILE__), '..', 'visualizer' + fsm_js = File.read File.join(viz_dir, 'fsm.js') + fsm_css = File.read File.join(viz_dir, 'fsm.css') + erb = File.read File.join(viz_dir, 'index.html.erb') + states = "function tt() { return #{to_json}; }" + + fun_routes = paths.shuffle.first(3).map do |ast| + ast.map { |n| + case n + when Nodes::Symbol + case n.left + when ':id' then rand(100).to_s + when ':format' then %w{ xml json }.shuffle.first + else + 'omg' + end + when Nodes::Terminal then n.symbol + else + nil + end + }.compact.join + end + + stylesheets = [fsm_css] + svg = to_svg + javascripts = [states, fsm_js] + + # Annoying hack for 1.9 warnings + fun_routes = fun_routes + stylesheets = stylesheets + svg = svg + javascripts = javascripts + + require 'erb' + template = ERB.new erb + template.result(binding) + end + + def []=(from, to, sym) + case sym + when String + @string_states[from][sym] = to + when Regexp + @regexp_states[from][sym] = to + else + raise ArgumentError, 'unknown symbol: %s' % sym.class + end + end + + def states + ss = @string_states.keys + @string_states.values.map(&:values).flatten + rs = @regexp_states.keys + @regexp_states.values.map(&:values).flatten + (ss + rs).uniq + end + + def transitions + @string_states.map { |from, hash| + hash.map { |s, to| [from, s, to] } + }.flatten(1) + @regexp_states.map { |from, hash| + hash.map { |s, to| [from, s, to] } + }.flatten(1) + end + + private + + def move_regexp(t, a) + return [] if t.empty? + + t.map { |s| + @regexp_states[s].map { |re, v| re === a ? v : nil } + }.flatten.compact.uniq + end + + def move_string(t, a) + return [] if t.empty? + + t.map { |s| @string_states[s][a] }.compact + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/nfa/builder.rb b/actionpack/lib/action_dispatch/journey/nfa/builder.rb new file mode 100644 index 0000000000..ee6494c3e4 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/nfa/builder.rb @@ -0,0 +1,76 @@ +require 'action_dispatch/journey/nfa/transition_table' +require 'action_dispatch/journey/gtg/transition_table' + +module ActionDispatch + module Journey # :nodoc: + module NFA # :nodoc: + class Visitor < Visitors::Visitor # :nodoc: + def initialize(tt) + @tt = tt + @i = -1 + end + + def visit_CAT(node) + left = visit(node.left) + right = visit(node.right) + + @tt.merge(left.last, right.first) + + [left.first, right.last] + end + + def visit_GROUP(node) + from = @i += 1 + left = visit(node.left) + to = @i += 1 + + @tt.accepting = to + + @tt[from, left.first] = nil + @tt[left.last, to] = nil + @tt[from, to] = nil + + [from, to] + end + + def visit_OR(node) + from = @i += 1 + children = node.children.map { |c| visit(c) } + to = @i += 1 + + children.each do |child| + @tt[from, child.first] = nil + @tt[child.last, to] = nil + end + + @tt.accepting = to + + [from, to] + end + + def terminal(node) + from_i = @i += 1 # new state + to_i = @i += 1 # new state + + @tt[from_i, to_i] = node + @tt.accepting = to_i + @tt.add_memo(to_i, node.memo) + + [from_i, to_i] + end + end + + class Builder # :nodoc: + def initialize(ast) + @ast = ast + end + + def transition_table + tt = TransitionTable.new + Visitor.new(tt).accept(@ast) + tt + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/nfa/dot.rb b/actionpack/lib/action_dispatch/journey/nfa/dot.rb new file mode 100644 index 0000000000..5c33a872e5 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/nfa/dot.rb @@ -0,0 +1,36 @@ +# encoding: utf-8 + +module ActionDispatch + module Journey # :nodoc: + module NFA # :nodoc: + module Dot # :nodoc: + def to_dot + edges = transitions.map { |from, sym, to| + " #{from} -> #{to} [label=\"#{sym || 'ε'}\"];" + } + + #memo_nodes = memos.values.flatten.map { |n| + # label = n + # if Journey::Route === n + # label = "#{n.verb.source} #{n.path.spec}" + # end + # " #{n.object_id} [label=\"#{label}\", shape=box];" + #} + #memo_edges = memos.map { |k, memos| + # (memos || []).map { |v| " #{k} -> #{v.object_id};" } + #}.flatten.uniq + + <<-eodot +digraph nfa { + rankdir=LR; + node [shape = doublecircle]; + #{accepting_states.join ' '}; + node [shape = circle]; +#{edges.join "\n"} +} + eodot + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/nfa/simulator.rb b/actionpack/lib/action_dispatch/journey/nfa/simulator.rb new file mode 100644 index 0000000000..5b40da6569 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/nfa/simulator.rb @@ -0,0 +1,47 @@ +require 'strscan' + +module ActionDispatch + module Journey # :nodoc: + module NFA # :nodoc: + class MatchData # :nodoc: + attr_reader :memos + + def initialize(memos) + @memos = memos + end + end + + class Simulator # :nodoc: + attr_reader :tt + + def initialize(transition_table) + @tt = transition_table + end + + def simulate(string) + input = StringScanner.new(string) + state = tt.eclosure(0) + until input.eos? + sym = input.scan(%r([/.?]|[^/.?]+)) + + # FIXME: tt.eclosure is not needed for the GTG + state = tt.eclosure(tt.move(state, sym)) + end + + acceptance_states = state.find_all { |s| + tt.accepting?(tt.eclosure(s).sort.last) + } + + return if acceptance_states.empty? + + memos = acceptance_states.map { |x| tt.memo(x) }.flatten.compact + + MatchData.new(memos) + end + + alias :=~ :simulate + alias :match :simulate + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/nfa/transition_table.rb b/actionpack/lib/action_dispatch/journey/nfa/transition_table.rb new file mode 100644 index 0000000000..a3017aeea1 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/nfa/transition_table.rb @@ -0,0 +1,163 @@ +require 'action_dispatch/journey/nfa/dot' + +module ActionDispatch + module Journey # :nodoc: + module NFA # :nodoc: + class TransitionTable # :nodoc: + include Journey::NFA::Dot + + attr_accessor :accepting + attr_reader :memos + + def initialize + @table = Hash.new { |h,f| h[f] = {} } + @memos = {} + @accepting = nil + @inverted = nil + end + + def accepting?(state) + accepting == state + end + + def accepting_states + [accepting] + end + + def add_memo(idx, memo) + @memos[idx] = memo + end + + def memo(idx) + @memos[idx] + end + + def []=(i, f, s) + @table[f][i] = s + end + + def merge(left, right) + @memos[right] = @memos.delete(left) + @table[right] = @table.delete(left) + end + + def states + (@table.keys + @table.values.map(&:keys).flatten).uniq + end + + # Returns a generalized transition graph with reduced states. The states + # are reduced like a DFA, but the table must be simulated like an NFA. + # + # Edges of the GTG are regular expressions. + def generalized_table + gt = GTG::TransitionTable.new + marked = {} + state_id = Hash.new { |h,k| h[k] = h.length } + alphabet = self.alphabet + + stack = [eclosure(0)] + + until stack.empty? + state = stack.pop + next if marked[state] || state.empty? + + marked[state] = true + + alphabet.each do |alpha| + next_state = eclosure(following_states(state, alpha)) + next if next_state.empty? + + gt[state_id[state], state_id[next_state]] = alpha + stack << next_state + end + end + + final_groups = state_id.keys.find_all { |s| + s.sort.last == accepting + } + + final_groups.each do |states| + id = state_id[states] + + gt.add_accepting(id) + save = states.find { |s| + @memos.key?(s) && eclosure(s).sort.last == accepting + } + + gt.add_memo(id, memo(save)) + end + + gt + end + + # Returns set of NFA states to which there is a transition on ast symbol + # +a+ from some state +s+ in +t+. + def following_states(t, a) + Array(t).map { |s| inverted[s][a] }.flatten.uniq + end + + # Returns set of NFA states to which there is a transition on ast symbol + # +a+ from some state +s+ in +t+. + def move(t, a) + Array(t).map { |s| + inverted[s].keys.compact.find_all { |sym| + sym === a + }.map { |sym| inverted[s][sym] } + }.flatten.uniq + end + + def alphabet + inverted.values.map(&:keys).flatten.compact.uniq.sort_by { |x| x.to_s } + end + + # Returns a set of NFA states reachable from some NFA state +s+ in set + # +t+ on nil-transitions alone. + def eclosure(t) + stack = Array(t) + seen = {} + children = [] + + until stack.empty? + s = stack.pop + next if seen[s] + + seen[s] = true + children << s + + stack.concat(inverted[s][nil]) + end + + children.uniq + end + + def transitions + @table.map { |to, hash| + hash.map { |from, sym| [from, sym, to] } + }.flatten(1) + end + + private + + def inverted + return @inverted if @inverted + + @inverted = Hash.new { |h, from| + h[from] = Hash.new { |j, s| j[s] = [] } + } + + @table.each { |to, hash| + hash.each { |from, sym| + if sym + sym = Nodes::Symbol === sym ? sym.regexp : sym.left + end + + @inverted[from][sym] << to + } + } + + @inverted + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/nodes/node.rb b/actionpack/lib/action_dispatch/journey/nodes/node.rb new file mode 100644 index 0000000000..935442ef66 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/nodes/node.rb @@ -0,0 +1,124 @@ +require 'action_dispatch/journey/visitors' + +module ActionDispatch + module Journey # :nodoc: + module Nodes # :nodoc: + class Node # :nodoc: + include Enumerable + + attr_accessor :left, :memo + + def initialize(left) + @left = left + @memo = nil + end + + def each(&block) + Visitors::Each.new(block).accept(self) + end + + def to_s + Visitors::String.new.accept(self) + end + + def to_dot + Visitors::Dot.new.accept(self) + end + + def to_sym + name.to_sym + end + + def name + left.tr '*:', '' + end + + def type + raise NotImplementedError + end + + def symbol?; false; end + def literal?; false; end + end + + class Terminal < Node # :nodoc: + alias :symbol :left + end + + class Literal < Terminal # :nodoc: + def literal?; true; end + def type; :LITERAL; end + end + + class Dummy < Literal # :nodoc: + def initialize(x = Object.new) + super + end + + def literal?; false; end + end + + %w{ Symbol Slash Dot }.each do |t| + class_eval <<-eoruby, __FILE__, __LINE__ + 1 + class #{t} < Terminal; + def type; :#{t.upcase}; end + end + eoruby + end + + class Symbol < Terminal # :nodoc: + attr_accessor :regexp + alias :symbol :regexp + + DEFAULT_EXP = /[^\.\/\?]+/ + def initialize(left) + super + @regexp = DEFAULT_EXP + end + + def default_regexp? + regexp == DEFAULT_EXP + end + + def symbol?; true; end + end + + class Unary < Node # :nodoc: + def children; [left] end + end + + class Group < Unary # :nodoc: + def type; :GROUP; end + end + + class Star < Unary # :nodoc: + def type; :STAR; end + end + + class Binary < Node # :nodoc: + attr_accessor :right + + def initialize(left, right) + super(left) + @right = right + end + + def children; [left, right] end + end + + class Cat < Binary # :nodoc: + def type; :CAT; end + end + + class Or < Node # :nodoc: + attr_reader :children + + def initialize(children) + @children = children + end + + def type; :OR; end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/parser.rb b/actionpack/lib/action_dispatch/journey/parser.rb new file mode 100644 index 0000000000..bb4cbb00e2 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/parser.rb @@ -0,0 +1,206 @@ +# +# DO NOT MODIFY!!!! +# This file is automatically generated by Racc 1.4.9 +# from Racc grammer file "". +# + +require 'racc/parser.rb' + + +require 'action_dispatch/journey/parser_extras' +module ActionDispatch + module Journey # :nodoc: + class Parser < Racc::Parser # :nodoc: +##### State transition tables begin ### + +racc_action_table = [ + 17, 21, 13, 15, 14, 7, nil, 16, 8, 19, + 13, 15, 14, 7, 23, 16, 8, 19, 13, 15, + 14, 7, nil, 16, 8, 13, 15, 14, 7, nil, + 16, 8, 13, 15, 14, 7, nil, 16, 8 ] + +racc_action_check = [ + 1, 17, 1, 1, 1, 1, nil, 1, 1, 1, + 20, 20, 20, 20, 20, 20, 20, 20, 7, 7, + 7, 7, nil, 7, 7, 19, 19, 19, 19, nil, + 19, 19, 0, 0, 0, 0, nil, 0, 0 ] + +racc_action_pointer = [ + 30, 0, nil, nil, nil, nil, nil, 16, nil, nil, + nil, nil, nil, nil, nil, nil, nil, 1, nil, 23, + 8, nil, nil, nil ] + +racc_action_default = [ + -18, -18, -2, -3, -4, -5, -6, -18, -9, -10, + -11, -12, -13, -14, -15, -16, -17, -18, -1, -18, + -18, 24, -8, -7 ] + +racc_goto_table = [ + 18, 1, nil, nil, nil, nil, nil, nil, 20, nil, + nil, nil, nil, nil, nil, nil, nil, nil, 22, 18 ] + +racc_goto_check = [ + 2, 1, nil, nil, nil, nil, nil, nil, 1, nil, + nil, nil, nil, nil, nil, nil, nil, nil, 2, 2 ] + +racc_goto_pointer = [ + nil, 1, -1, nil, nil, nil, nil, nil, nil, nil, + nil ] + +racc_goto_default = [ + nil, nil, 2, 3, 4, 5, 6, 9, 10, 11, + 12 ] + +racc_reduce_table = [ + 0, 0, :racc_error, + 2, 11, :_reduce_1, + 1, 11, :_reduce_2, + 1, 11, :_reduce_none, + 1, 12, :_reduce_none, + 1, 12, :_reduce_none, + 1, 12, :_reduce_none, + 3, 15, :_reduce_7, + 3, 13, :_reduce_8, + 1, 16, :_reduce_9, + 1, 14, :_reduce_none, + 1, 14, :_reduce_none, + 1, 14, :_reduce_none, + 1, 14, :_reduce_none, + 1, 19, :_reduce_14, + 1, 17, :_reduce_15, + 1, 18, :_reduce_16, + 1, 20, :_reduce_17 ] + +racc_reduce_n = 18 + +racc_shift_n = 24 + +racc_token_table = { + false => 0, + :error => 1, + :SLASH => 2, + :LITERAL => 3, + :SYMBOL => 4, + :LPAREN => 5, + :RPAREN => 6, + :DOT => 7, + :STAR => 8, + :OR => 9 } + +racc_nt_base = 10 + +racc_use_result_var = true + +Racc_arg = [ + racc_action_table, + racc_action_check, + racc_action_default, + racc_action_pointer, + racc_goto_table, + racc_goto_check, + racc_goto_default, + racc_goto_pointer, + racc_nt_base, + racc_reduce_table, + racc_token_table, + racc_shift_n, + racc_reduce_n, + racc_use_result_var ] + +Racc_token_to_s_table = [ + "$end", + "error", + "SLASH", + "LITERAL", + "SYMBOL", + "LPAREN", + "RPAREN", + "DOT", + "STAR", + "OR", + "$start", + "expressions", + "expression", + "or", + "terminal", + "group", + "star", + "symbol", + "literal", + "slash", + "dot" ] + +Racc_debug_parser = false + +##### State transition tables end ##### + +# reduce 0 omitted + +def _reduce_1(val, _values, result) + result = Cat.new(val.first, val.last) + result +end + +def _reduce_2(val, _values, result) + result = val.first + result +end + +# reduce 3 omitted + +# reduce 4 omitted + +# reduce 5 omitted + +# reduce 6 omitted + +def _reduce_7(val, _values, result) + result = Group.new(val[1]) + result +end + +def _reduce_8(val, _values, result) + result = Or.new([val.first, val.last]) + result +end + +def _reduce_9(val, _values, result) + result = Star.new(Symbol.new(val.last)) + result +end + +# reduce 10 omitted + +# reduce 11 omitted + +# reduce 12 omitted + +# reduce 13 omitted + +def _reduce_14(val, _values, result) + result = Slash.new('/') + result +end + +def _reduce_15(val, _values, result) + result = Symbol.new(val.first) + result +end + +def _reduce_16(val, _values, result) + result = Literal.new(val.first) + result +end + +def _reduce_17(val, _values, result) + result = Dot.new(val.first) + result +end + +def _reduce_none(val, _values, result) + val[0] +end + + end # class Parser + end # module Journey + end # module ActionDispatch diff --git a/actionpack/lib/action_dispatch/journey/parser.y b/actionpack/lib/action_dispatch/journey/parser.y new file mode 100644 index 0000000000..a2e1afed32 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/parser.y @@ -0,0 +1,47 @@ +class ActionDispatch::Journey::Parser + +token SLASH LITERAL SYMBOL LPAREN RPAREN DOT STAR OR + +rule + expressions + : expressions expression { result = Cat.new(val.first, val.last) } + | expression { result = val.first } + | or + ; + expression + : terminal + | group + | star + ; + group + : LPAREN expressions RPAREN { result = Group.new(val[1]) } + ; + or + : expressions OR expression { result = Or.new([val.first, val.last]) } + ; + star + : STAR { result = Star.new(Symbol.new(val.last)) } + ; + terminal + : symbol + | literal + | slash + | dot + ; + slash + : SLASH { result = Slash.new('/') } + ; + symbol + : SYMBOL { result = Symbol.new(val.first) } + ; + literal + : LITERAL { result = Literal.new(val.first) } + dot + : DOT { result = Dot.new(val.first) } + ; + +end + +---- header + +require 'action_dispatch/journey/parser_extras' diff --git a/actionpack/lib/action_dispatch/journey/parser_extras.rb b/actionpack/lib/action_dispatch/journey/parser_extras.rb new file mode 100644 index 0000000000..14892f4321 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/parser_extras.rb @@ -0,0 +1,23 @@ +require 'action_dispatch/journey/scanner' +require 'action_dispatch/journey/nodes/node' + +module ActionDispatch + module Journey # :nodoc: + class Parser < Racc::Parser # :nodoc: + include Journey::Nodes + + def initialize + @scanner = Scanner.new + end + + def parse(string) + @scanner.scan_setup(string) + do_parse + end + + def next_token + @scanner.next_token + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/path/pattern.rb b/actionpack/lib/action_dispatch/journey/path/pattern.rb new file mode 100644 index 0000000000..4a571ec546 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/path/pattern.rb @@ -0,0 +1,196 @@ +module ActionDispatch + module Journey # :nodoc: + module Path # :nodoc: + class Pattern # :nodoc: + attr_reader :spec, :requirements, :anchored + + def initialize(strexp) + parser = Journey::Parser.new + + @anchored = true + + case strexp + when String + @spec = parser.parse(strexp) + @requirements = {} + @separators = "/.?" + when Router::Strexp + @spec = parser.parse(strexp.path) + @requirements = strexp.requirements + @separators = strexp.separators.join + @anchored = strexp.anchor + else + raise "wtf bro: #{strexp}" + end + + @names = nil + @optional_names = nil + @required_names = nil + @re = nil + @offsets = nil + end + + def ast + @spec.grep(Nodes::Symbol).each do |node| + re = @requirements[node.to_sym] + node.regexp = re if re + end + + @spec.grep(Nodes::Star).each do |node| + node = node.left + node.regexp = @requirements[node.to_sym] || /(.+)/ + end + + @spec + end + + def names + @names ||= spec.grep(Nodes::Symbol).map { |n| n.name } + end + + def required_names + @required_names ||= names - optional_names + end + + def optional_names + @optional_names ||= spec.grep(Nodes::Group).map { |group| + group.grep(Nodes::Symbol) + }.flatten.map { |n| n.name }.uniq + end + + class RegexpOffsets < Journey::Visitors::Visitor # :nodoc: + attr_reader :offsets + + def initialize(matchers) + @matchers = matchers + @capture_count = [0] + end + + def visit(node) + super + @capture_count + end + + def visit_SYMBOL(node) + node = node.to_sym + + if @matchers.key?(node) + re = /#{@matchers[node]}|/ + @capture_count.push((re.match('').length - 1) + (@capture_count.last || 0)) + else + @capture_count << (@capture_count.last || 0) + end + end + end + + class AnchoredRegexp < Journey::Visitors::Visitor # :nodoc: + def initialize(separator, matchers) + @separator = separator + @matchers = matchers + @separator_re = "([^#{separator}]+)" + super() + end + + def accept(node) + %r{\A#{visit node}\Z} + end + + def visit_CAT(node) + [visit(node.left), visit(node.right)].join + end + + def visit_SYMBOL(node) + node = node.to_sym + + return @separator_re unless @matchers.key?(node) + + re = @matchers[node] + "(#{re})" + end + + def visit_GROUP(node) + "(?:#{visit node.left})?" + end + + def visit_LITERAL(node) + Regexp.escape(node.left) + end + alias :visit_DOT :visit_LITERAL + + def visit_SLASH(node) + node.left + end + + def visit_STAR(node) + re = @matchers[node.left.to_sym] || '.+' + "(#{re})" + end + end + + class UnanchoredRegexp < AnchoredRegexp # :nodoc: + def accept(node) + %r{\A#{visit node}} + end + end + + class MatchData # :nodoc: + attr_reader :names + + def initialize(names, offsets, match) + @names = names + @offsets = offsets + @match = match + end + + def captures + (length - 1).times.map { |i| self[i + 1] } + end + + def [](x) + idx = @offsets[x - 1] + x + @match[idx] + end + + def length + @offsets.length + end + + def post_match + @match.post_match + end + + def to_s + @match.to_s + end + end + + def match(other) + return unless match = to_regexp.match(other) + MatchData.new(names, offsets, match) + end + alias :=~ :match + + def source + to_regexp.source + end + + def to_regexp + @re ||= regexp_visitor.new(@separators, @requirements).accept spec + end + + private + + def regexp_visitor + @anchored ? AnchoredRegexp : UnanchoredRegexp + end + + def offsets + return @offsets if @offsets + + viz = RegexpOffsets.new(@requirements) + @offsets = viz.accept(spec) + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/route.rb b/actionpack/lib/action_dispatch/journey/route.rb new file mode 100644 index 0000000000..d18efd863a --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/route.rb @@ -0,0 +1,94 @@ +module ActionDispatch + module Journey # :nodoc: + class Route # :nodoc: + attr_reader :app, :path, :verb, :defaults, :ip, :name + + attr_reader :constraints + alias :conditions :constraints + + attr_accessor :precedence + + ## + # +path+ is a path constraint. + # +constraints+ is a hash of constraints to be applied to this route. + def initialize(name, app, path, constraints, defaults = {}) + constraints = constraints.dup + @name = name + @app = app + @path = path + @verb = constraints[:request_method] || // + @ip = constraints.delete(:ip) || // + + @constraints = constraints + @constraints.keep_if { |_,v| Regexp === v || String === v } + @defaults = defaults + @required_defaults = nil + @required_parts = nil + @parts = nil + @decorated_ast = nil + @precedence = 0 + end + + def ast + return @decorated_ast if @decorated_ast + + @decorated_ast = path.ast + @decorated_ast.grep(Nodes::Terminal).each { |n| n.memo = self } + @decorated_ast + end + + def requirements # :nodoc: + # needed for rails `rake routes` + path.requirements.merge(@defaults).delete_if { |_,v| + /.+?/ == v + } + end + + def segments + @path.names + end + + def required_keys + path.required_names.map { |x| x.to_sym } + required_defaults.keys + end + + def score(constraints) + required_keys = path.required_names + supplied_keys = constraints.map { |k,v| v && k.to_s }.compact + + return -1 unless (required_keys - supplied_keys).empty? + + score = (supplied_keys & path.names).length + score + (required_defaults.length * 2) + end + + def parts + @parts ||= segments.map { |n| n.to_sym } + end + alias :segment_keys :parts + + def format(path_options) + path_options.delete_if do |key, value| + value.to_s == defaults[key].to_s && !required_parts.include?(key) + end + + Visitors::Formatter.new(path_options).accept(path.spec) + end + + def optional_parts + path.optional_names.map { |n| n.to_sym } + end + + def required_parts + @required_parts ||= path.required_names.map { |n| n.to_sym } + end + + def required_defaults + @required_defaults ||= begin + matches = parts + @defaults.dup.delete_if { |k,_| matches.include?(k) } + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/router.rb b/actionpack/lib/action_dispatch/journey/router.rb new file mode 100644 index 0000000000..1fc45a2109 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/router.rb @@ -0,0 +1,168 @@ +require 'action_dispatch/journey/router/utils' +require 'action_dispatch/journey/router/strexp' +require 'action_dispatch/journey/routes' +require 'action_dispatch/journey/formatter' + +before = $-w +$-w = false +require 'action_dispatch/journey/parser' +$-w = before + +require 'action_dispatch/journey/route' +require 'action_dispatch/journey/path/pattern' + +module ActionDispatch + module Journey # :nodoc: + class Router # :nodoc: + class RoutingError < ::StandardError # :nodoc: + end + + # :nodoc: + VERSION = '2.0.0' + + class NullReq # :nodoc: + attr_reader :env + def initialize(env) + @env = env + end + + def request_method + env['REQUEST_METHOD'] + end + + def path_info + env['PATH_INFO'] + end + + def ip + env['REMOTE_ADDR'] + end + + def [](k); env[k]; end + end + + attr_reader :request_class, :formatter + attr_accessor :routes + + def initialize(routes, options) + @options = options + @params_key = options[:parameters_key] + @request_class = options[:request_class] || NullReq + @routes = routes + end + + def call(env) + env['PATH_INFO'] = Utils.normalize_path(env['PATH_INFO']) + + find_routes(env).each do |match, parameters, route| + script_name, path_info, set_params = env.values_at('SCRIPT_NAME', + 'PATH_INFO', + @params_key) + + unless route.path.anchored + env['SCRIPT_NAME'] = (script_name.to_s + match.to_s).chomp('/') + env['PATH_INFO'] = match.post_match + end + + env[@params_key] = (set_params || {}).merge parameters + + status, headers, body = route.app.call(env) + + if 'pass' == headers['X-Cascade'] + env['SCRIPT_NAME'] = script_name + env['PATH_INFO'] = path_info + env[@params_key] = set_params + next + end + + return [status, headers, body] + end + + return [404, {'X-Cascade' => 'pass'}, ['Not Found']] + end + + def recognize(req) + find_routes(req.env).each do |match, parameters, route| + unless route.path.anchored + req.env['SCRIPT_NAME'] = match.to_s + req.env['PATH_INFO'] = match.post_match.sub(/^([^\/])/, '/\1') + end + + yield(route, nil, parameters) + end + end + + def visualizer + tt = GTG::Builder.new(ast).transition_table + groups = partitioned_routes.first.map(&:ast).group_by { |a| a.to_s } + asts = groups.values.map { |v| v.first } + tt.visualizer(asts) + end + + private + + def partitioned_routes + routes.partitioned_routes + end + + def ast + routes.ast + end + + def simulator + routes.simulator + end + + def custom_routes + partitioned_routes.last + end + + def filter_routes(path) + return [] unless ast + data = simulator.match(path) + data ? data.memos : [] + end + + def find_routes env + req = request_class.new(env) + + routes = filter_routes(req.path_info).concat custom_routes.find_all { |r| + r.path.match(req.path_info) + } + routes.concat get_routes_as_head(routes) + + routes.sort_by!(&:precedence).select! { |r| + r.constraints.all? { |k, v| v === req.send(k) } && + r.verb === req.request_method + } + routes.reject! { |r| req.ip && !(r.ip === req.ip) } + + routes.map! { |r| + match_data = r.path.match(req.path_info) + match_names = match_data.names.map { |n| n.to_sym } + match_values = match_data.captures.map { |v| v && Utils.unescape_uri(v) } + info = Hash[match_names.zip(match_values).find_all { |_, y| y }] + + [match_data, r.defaults.merge(info), r] + } + end + + def get_routes_as_head(routes) + precedence = (routes.map(&:precedence).max || 0) + 1 + routes = routes.select { |r| + r.verb === "GET" && !(r.verb === "HEAD") + }.map! { |r| + Route.new(r.name, + r.app, + r.path, + r.conditions.merge(request_method: "HEAD"), + r.defaults).tap do |route| + route.precedence = r.precedence + precedence + end + } + routes.flatten! + routes + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/router/strexp.rb b/actionpack/lib/action_dispatch/journey/router/strexp.rb new file mode 100644 index 0000000000..f97f1a223e --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/router/strexp.rb @@ -0,0 +1,24 @@ +module ActionDispatch + module Journey # :nodoc: + class Router # :nodoc: + class Strexp # :nodoc: + class << self + alias :compile :new + end + + attr_reader :path, :requirements, :separators, :anchor + + def initialize(path, requirements, separators, anchor = true) + @path = path + @requirements = requirements + @separators = separators + @anchor = anchor + end + + def names + @path.scan(/:\w+/).map { |s| s.tr(':', '') } + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/router/utils.rb b/actionpack/lib/action_dispatch/journey/router/utils.rb new file mode 100644 index 0000000000..462f1a122d --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/router/utils.rb @@ -0,0 +1,54 @@ +require 'uri' + +module ActionDispatch + module Journey # :nodoc: + class Router # :nodoc: + class Utils # :nodoc: + # Normalizes URI path. + # + # Strips off trailing slash and ensures there is a leading slash. + # + # normalize_path("/foo") # => "/foo" + # normalize_path("/foo/") # => "/foo" + # normalize_path("foo") # => "/foo" + # normalize_path("") # => "/" + def self.normalize_path(path) + path = "/#{path}" + path.squeeze!('/') + path.sub!(%r{/+\Z}, '') + path = '/' if path == '' + path + end + + # URI path and fragment escaping + # http://tools.ietf.org/html/rfc3986 + module UriEscape # :nodoc: + # Symbol captures can generate multiple path segments, so include /. + reserved_segment = '/' + reserved_fragment = '/?' + reserved_pchar = ':@&=+$,;%' + + safe_pchar = "#{URI::REGEXP::PATTERN::UNRESERVED}#{reserved_pchar}" + safe_segment = "#{safe_pchar}#{reserved_segment}" + safe_fragment = "#{safe_pchar}#{reserved_fragment}" + UNSAFE_SEGMENT = Regexp.new("[^#{safe_segment}]", false).freeze + UNSAFE_FRAGMENT = Regexp.new("[^#{safe_fragment}]", false).freeze + end + + Parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI + + def self.escape_path(path) + Parser.escape(path.to_s, UriEscape::UNSAFE_SEGMENT) + end + + def self.escape_fragment(fragment) + Parser.escape(fragment.to_s, UriEscape::UNSAFE_FRAGMENT) + end + + def self.unescape_uri(uri) + Parser.unescape(uri) + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/routes.rb b/actionpack/lib/action_dispatch/journey/routes.rb new file mode 100644 index 0000000000..32829a1f20 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/routes.rb @@ -0,0 +1,76 @@ +module ActionDispatch + module Journey # :nodoc: + # The Routing table. Contains all routes for a system. Routes can be + # added to the table by calling Routes#add_route. + class Routes # :nodoc: + include Enumerable + + attr_reader :routes, :named_routes + + def initialize + @routes = [] + @named_routes = {} + @ast = nil + @partitioned_routes = nil + @simulator = nil + end + + def length + @routes.length + end + alias :size :length + + def last + @routes.last + end + + def each(&block) + routes.each(&block) + end + + def clear + routes.clear + end + + def partitioned_routes + @partitioned_routes ||= routes.partition { |r| + r.path.anchored && r.ast.grep(Nodes::Symbol).all? { |n| n.default_regexp? } + } + end + + def ast + return @ast if @ast + return if partitioned_routes.first.empty? + + asts = partitioned_routes.first.map { |r| r.ast } + @ast = Nodes::Or.new(asts) + end + + def simulator + return @simulator if @simulator + + gtg = GTG::Builder.new(ast).transition_table + @simulator = GTG::Simulator.new(gtg) + end + + # Add a route to the routing table. + def add_route(app, path, conditions, defaults, name = nil) + route = Route.new(name, app, path, conditions, defaults) + + route.precedence = routes.length + routes << route + named_routes[name] = route if name && !named_routes[name] + clear_cache! + route + end + + private + + def clear_cache! + @ast = nil + @partitioned_routes = nil + @simulator = nil + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/scanner.rb b/actionpack/lib/action_dispatch/journey/scanner.rb new file mode 100644 index 0000000000..633be11a2d --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/scanner.rb @@ -0,0 +1,61 @@ +require 'strscan' + +module ActionDispatch + module Journey # :nodoc: + class Scanner # :nodoc: + def initialize + @ss = nil + end + + def scan_setup(str) + @ss = StringScanner.new(str) + end + + def eos? + @ss.eos? + end + + def pos + @ss.pos + end + + def pre_match + @ss.pre_match + end + + def next_token + return if @ss.eos? + + until token = scan || @ss.eos?; end + token + end + + private + + def scan + case + # / + when text = @ss.scan(/\//) + [:SLASH, text] + when text = @ss.scan(/\*\w+/) + [:STAR, text] + when text = @ss.scan(/\(/) + [:LPAREN, text] + when text = @ss.scan(/\)/) + [:RPAREN, text] + when text = @ss.scan(/\|/) + [:OR, text] + when text = @ss.scan(/\./) + [:DOT, text] + when text = @ss.scan(/:\w+/) + [:SYMBOL, text] + when text = @ss.scan(/[\w%\-~]+/) + [:LITERAL, text] + # any char + when text = @ss.scan(/./) + [:LITERAL, text] + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/visitors.rb b/actionpack/lib/action_dispatch/journey/visitors.rb new file mode 100644 index 0000000000..46bd58c178 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/visitors.rb @@ -0,0 +1,189 @@ +# encoding: utf-8 +module ActionDispatch + module Journey # :nodoc: + module Visitors # :nodoc: + class Visitor # :nodoc: + DISPATCH_CACHE = Hash.new { |h,k| + h[k] = "visit_#{k}" + } + + def accept(node) + visit(node) + end + + private + + def visit node + send(DISPATCH_CACHE[node.type], node) + end + + def binary(node) + visit(node.left) + visit(node.right) + end + def visit_CAT(n); binary(n); end + + def nary(node) + node.children.each { |c| visit(c) } + end + def visit_OR(n); nary(n); end + + def unary(node) + visit(node.left) + end + def visit_GROUP(n); unary(n); end + def visit_STAR(n); unary(n); end + + def terminal(node); end + %w{ LITERAL SYMBOL SLASH DOT }.each do |t| + class_eval %{ def visit_#{t}(n); terminal(n); end }, __FILE__, __LINE__ + end + end + + # Loop through the requirements AST + class Each < Visitor # :nodoc: + attr_reader :block + + def initialize(block) + @block = block + end + + def visit(node) + super + block.call(node) + end + end + + class String < Visitor # :nodoc: + private + + def binary(node) + [visit(node.left), visit(node.right)].join + end + + def nary(node) + node.children.map { |c| visit(c) }.join '|' + end + + def terminal(node) + node.left + end + + def visit_GROUP(node) + "(#{visit(node.left)})" + end + end + + # Used for formatting urls (url_for) + class Formatter < Visitor # :nodoc: + attr_reader :options, :consumed + + def initialize(options) + @options = options + @consumed = {} + end + + private + + def visit_GROUP(node) + if consumed == options + nil + else + route = visit(node.left) + route.include?("\0") ? nil : route + end + end + + def terminal(node) + node.left + end + + def binary(node) + [visit(node.left), visit(node.right)].join + end + + def nary(node) + node.children.map { |c| visit(c) }.join + end + + def visit_SYMBOL(node) + key = node.to_sym + + if value = options[key] + consumed[key] = value + Router::Utils.escape_path(value) + else + "\0" + end + end + end + + class Dot < Visitor # :nodoc: + def initialize + @nodes = [] + @edges = [] + end + + def accept(node) + super + <<-eodot + digraph parse_tree { + size="8,5" + node [shape = none]; + edge [dir = none]; + #{@nodes.join "\n"} + #{@edges.join("\n")} + } + eodot + end + + private + + def binary(node) + node.children.each do |c| + @edges << "#{node.object_id} -> #{c.object_id};" + end + super + end + + def nary(node) + node.children.each do |c| + @edges << "#{node.object_id} -> #{c.object_id};" + end + super + end + + def unary(node) + @edges << "#{node.object_id} -> #{node.left.object_id};" + super + end + + def visit_GROUP(node) + @nodes << "#{node.object_id} [label=\"()\"];" + super + end + + def visit_CAT(node) + @nodes << "#{node.object_id} [label=\"○\"];" + super + end + + def visit_STAR(node) + @nodes << "#{node.object_id} [label=\"*\"];" + super + end + + def visit_OR(node) + @nodes << "#{node.object_id} [label=\"|\"];" + super + end + + def terminal(node) + value = node.left + + @nodes << "#{node.object_id} [label=\"#{value}\"];" + end + end + end + end +end diff --git a/actionpack/lib/action_dispatch/journey/visualizer/fsm.css b/actionpack/lib/action_dispatch/journey/visualizer/fsm.css new file mode 100644 index 0000000000..50caebaa18 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/visualizer/fsm.css @@ -0,0 +1,34 @@ +body { + font-family: "Helvetica Neue", Helvetica, Arial, Sans-Serif; + margin: 0; +} + +h1 { + font-size: 2.0em; font-weight: bold; text-align: center; + color: white; background-color: black; + padding: 5px 0; + margin: 0 0 20px; +} + +h2 { + text-align: center; + display: none; + font-size: 0.5em; +} + +div#chart-2 { + height: 350px; +} + +.clearfix {display: inline-block; } +.input { overflow: show;} +.instruction { color: #666; padding: 0 30px 20px; font-size: 0.9em} +.instruction p { padding: 0 0 5px; } +.instruction li { padding: 0 10px 5px; } + +.form { background: #EEE; padding: 20px 30px; border-radius: 5px; margin-left: auto; margin-right: auto; width: 500px; margin-bottom: 20px} +.form p, .form form { text-align: center } +.form form {padding: 0 10px 5px; } +.form .fun_routes { font-size: 0.9em;} +.form .fun_routes a { margin: 0 5px 0 0; } + diff --git a/actionpack/lib/action_dispatch/journey/visualizer/fsm.js b/actionpack/lib/action_dispatch/journey/visualizer/fsm.js new file mode 100644 index 0000000000..d9bcaef928 --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/visualizer/fsm.js @@ -0,0 +1,134 @@ +function tokenize(input, callback) { + while(input.length > 0) { + callback(input.match(/^[\/\.\?]|[^\/\.\?]+/)[0]); + input = input.replace(/^[\/\.\?]|[^\/\.\?]+/, ''); + } +} + +var graph = d3.select("#chart-2 svg"); +var svg_edges = {}; +var svg_nodes = {}; + +graph.selectAll("g.edge").each(function() { + var node = d3.select(this); + var index = node.select("title").text().split("->"); + var left = parseInt(index[0]); + var right = parseInt(index[1]); + + if(!svg_edges[left]) { svg_edges[left] = {} } + svg_edges[left][right] = node; +}); + +graph.selectAll("g.node").each(function() { + var node = d3.select(this); + var index = parseInt(node.select("title").text()); + svg_nodes[index] = node; +}); + +function reset_graph() { + for(var key in svg_edges) { + for(var mkey in svg_edges[key]) { + var node = svg_edges[key][mkey]; + var path = node.select("path"); + var arrow = node.select("polygon"); + path.style("stroke", "black"); + arrow.style("stroke", "black").style("fill", "black"); + } + } + + for(var key in svg_nodes) { + var node = svg_nodes[key]; + node.select('ellipse').style("fill", "white"); + node.select('polygon').style("fill", "white"); + } + return false; +} + +function highlight_edge(from, to) { + var node = svg_edges[from][to]; + var path = node.select("path"); + var arrow = node.select("polygon"); + + path + .transition().duration(500) + .style("stroke", "green"); + + arrow + .transition().duration(500) + .style("stroke", "green").style("fill", "green"); +} + +function highlight_state(index, color) { + if(!color) { color = "green"; } + + svg_nodes[index].select('ellipse') + .style("fill", "white") + .transition().duration(500) + .style("fill", color); +} + +function highlight_finish(index) { + svg_nodes[index].select('polygon') + .style("fill", "while") + .transition().duration(500) + .style("fill", "blue"); +} + +function match(input) { + reset_graph(); + var table = tt(); + var states = [0]; + var regexp_states = table['regexp_states']; + var string_states = table['string_states']; + var accepting = table['accepting']; + + highlight_state(0); + + tokenize(input, function(token) { + var new_states = []; + for(var key in states) { + var state = states[key]; + + if(string_states[state] && string_states[state][token]) { + var new_state = string_states[state][token]; + highlight_edge(state, new_state); + highlight_state(new_state); + new_states.push(new_state); + } + + if(regexp_states[state]) { + for(var key in regexp_states[state]) { + var re = new RegExp("^" + key + "$"); + if(re.test(token)) { + var new_state = regexp_states[state][key]; + highlight_edge(state, new_state); + highlight_state(new_state); + new_states.push(new_state); + } + } + } + } + + if(new_states.length == 0) { + return; + } + states = new_states; + }); + + for(var key in states) { + var state = states[key]; + if(accepting[state]) { + for(var mkey in svg_edges[state]) { + if(!regexp_states[mkey] && !string_states[mkey]) { + highlight_edge(state, mkey); + highlight_finish(mkey); + } + } + } else { + highlight_state(state, "red"); + } + } + + return false; +} + diff --git a/actionpack/lib/action_dispatch/journey/visualizer/index.html.erb b/actionpack/lib/action_dispatch/journey/visualizer/index.html.erb new file mode 100644 index 0000000000..6aff10956a --- /dev/null +++ b/actionpack/lib/action_dispatch/journey/visualizer/index.html.erb @@ -0,0 +1,52 @@ +<!DOCTYPE html> +<html> + <head> + <title><%= title %></title> + <link rel="stylesheet" href="https://raw.github.com/gist/1706081/af944401f75ea20515a02ddb3fb43d23ecb8c662/reset.css" type="text/css"> + <style> + <% stylesheets.each do |style| %> + <%= style %> + <% end %> + </style> + <script src="https://raw.github.com/gist/1706081/df464722a05c3c2bec450b7b5c8240d9c31fa52d/d3.min.js" type="text/javascript"></script> + </head> + <body> + <div id="wrapper"> + <h1>Routes FSM with NFA simulation</h1> + <div class="instruction form"> + <p> + Type a route in to the box and click "simulate". + </p> + <form onsubmit="return match(this.route.value);"> + <input type="text" size="30" name="route" value="/articles/new" /> + <button>simulate</button> + <input type="reset" value="reset" onclick="return reset_graph();"/> + </form> + <p class="fun_routes"> + Some fun routes to try: + <% fun_routes.each do |path| %> + <a href="#" onclick="document.forms[0].elements[0].value=this.text.replace(/^\s+|\s+$/g,''); return match(this.text.replace(/^\s+|\s+$/g,''));"> + <%= path %> + </a> + <% end %> + </p> + </div> + <div class='chart' id='chart-2'> + <%= svg %> + </div> + <div class="instruction"> + <p> + This is a FSM for a system that has the following routes: + </p> + <ul> + <% paths.each do |route| %> + <li><%= route %></li> + <% end %> + </ul> + </div> + </div> + <% javascripts.each do |js| %> + <script><%= js %></script> + <% end %> + </body> +</html> diff --git a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb index 0f0589a844..1dc51d62e0 100644 --- a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb @@ -7,7 +7,7 @@ module ActionDispatch # This middleware is responsible for logging exceptions and # showing a debugging page in case the request is local. class DebugExceptions - RESCUES_TEMPLATE_PATH = File.join(File.dirname(__FILE__), 'templates') + RESCUES_TEMPLATE_PATH = File.expand_path('../templates', __FILE__) def initialize(app, routes_app = nil) @app = app @@ -87,7 +87,7 @@ module ActionDispatch return false unless @routes_app.respond_to?(:routes) if exception.is_a?(ActionController::RoutingError) || exception.is_a?(ActionView::Template::Error) inspector = ActionDispatch::Routing::RoutesInspector.new - inspector.format(@routes_app.routes.routes).join("\n") + inspector.collect_routes(@routes_app.routes.routes) end end end diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb index a357a7ba11..6c903d6a17 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/routing_error.erb @@ -20,4 +20,6 @@ Routes match in priority from top to bottom </p> -<p><pre><%= @routes %></pre></p> +<%= render layout: "routes/route_wrapper" do %> + <%= render partial: "routes/route", collection: @routes %> +<% end %> diff --git a/actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb b/actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb new file mode 100644 index 0000000000..400ae97d22 --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/routes/_route.html.erb @@ -0,0 +1,16 @@ +<tr class='route_row' data-helper='path'> + <td data-route-name='<%= route[:name] %>'> + <% if route[:name].present? %> + <%= route[:name] %><span class='helper'>_path</span> + <% end %> + </td> + <td data-route-verb='<%= route[:verb] %>'> + <%= route[:verb] %> + </td> + <td data-route-path='<%= route[:path] %>'> + <%= route[:path] %> + </td> + <td data-route-reqs='<%= route[:reqs] %>'> + <%= route[:reqs] %> + </td> +</tr> diff --git a/actionpack/lib/action_dispatch/middleware/templates/routes/_route_wrapper.html.erb b/actionpack/lib/action_dispatch/middleware/templates/routes/_route_wrapper.html.erb new file mode 100644 index 0000000000..dc17cb77ef --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/templates/routes/_route_wrapper.html.erb @@ -0,0 +1,56 @@ +<style type='text/css'> + #route_table td {padding: 0 30px;} + #route_table {margin: 0 auto 0;} +</style> + +<table id='route_table' class='route_table'> + <thead> + <tr> + <th>Helper<br /> + <%= link_to "Path", "#", 'data-route-helper' => '_path', + title: "Returns a relative path (without the http or domain)" %> / + <%= link_to "Url", "#", 'data-route-helper' => '_url', + title: "Returns an absolute url (with the http and domain)" %> + </th> + <th>HTTP Verb</th> + <th>Path</th> + <th>Controller#Action</th> + </tr> + </thead> + <tbody> + <%= yield %> + </tbody> +</table> + +<script type='text/javascript'> + function each(elems, func) { + if (!elems instanceof Array) { elems = [elems]; } + for (var i = elems.length; i--; ) { + func(elems[i]); + } + } + + function setValOn(elems, val) { + each(elems, function(elem) { + elem.innerHTML = val; + }); + } + + function onClick(elems, func) { + each(elems, function(elem) { + elem.onclick = func; + }); + } + + // Enables functionality to toggle between `_path` and `_url` helper suffixes + function setupRouteToggleHelperLinks() { + var toggleLinks = document.querySelectorAll('#route_table [data-route-helper]'); + onClick(toggleLinks, function(){ + var helperTxt = this.getAttribute("data-route-helper"); + var helperElems = document.querySelectorAll('[data-route-name] span.helper'); + setValOn(helperElems, helperTxt); + }); + } + + setupRouteToggleHelperLinks(); +</script> diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index c18dc94d4f..8d7461ecc3 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -51,7 +51,7 @@ module ActionDispatch end def internal? - path =~ %r{/rails/info.*|^#{Rails.application.config.assets.prefix}} + controller =~ %r{\Arails/(info|welcome)} || path =~ %r{\A#{Rails.application.config.assets.prefix}} end def engine? diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 0f95daa790..b1959e388c 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -1,5 +1,6 @@ -require 'journey' +require 'action_dispatch/journey' require 'forwardable' +require 'thread_safe' require 'active_support/core_ext/object/to_query' require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/module/remove_method' @@ -20,7 +21,7 @@ module ActionDispatch def initialize(options={}) @defaults = options[:defaults] @glob_param = options.delete(:glob) - @controllers = {} + @controller_class_names = ThreadSafe::Cache.new end def call(env) @@ -68,13 +69,8 @@ module ActionDispatch private def controller_reference(controller_param) - controller_name = "#{controller_param.camelize}Controller" - - unless controller = @controllers[controller_param] - controller = @controllers[controller_param] = - ActiveSupport::Dependencies.reference(controller_name) - end - controller.get(controller_name) + const_name = @controller_class_names[controller_param] ||= "#{controller_param.camelize}Controller" + ActiveSupport::Dependencies.constantize(const_name) end def dispatch(controller, action, env) @@ -130,6 +126,12 @@ module ActionDispatch end def clear! + @helpers.each do |helper| + @module.module_eval do + remove_possible_method helper + end + end + @routes.clear @helpers.clear end @@ -288,7 +290,6 @@ module ActionDispatch def clear! @finalized = false - @url_helpers = nil named_routes.clear set.clear formatter.clear diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index 76311c423a..8e19025722 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -130,6 +130,7 @@ module ActionDispatch # * <tt>:port</tt> - Optionally specify the port to connect to. # * <tt>:anchor</tt> - An anchor name to be appended to the path. # * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2009/" + # * <tt>:script_name</tt> - Specifies application path relative to domain root. If provided, prepends application path. # # Any other key (<tt>:controller</tt>, <tt>:action</tt>, etc.) given to # +url_for+ is forwarded to the Routes module. @@ -142,6 +143,10 @@ module ActionDispatch # # => 'http://somehost.org/tasks/testing/' # url_for controller: 'tasks', action: 'testing', host: 'somehost.org', number: '33' # # => 'http://somehost.org/tasks/testing?number=33' + # url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp" + # # => 'http://somehost.org/myapp/tasks/testing' + # url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp", only_path: true + # # => '/myapp/tasks/testing' def url_for(options = nil) case options when nil diff --git a/actionpack/lib/action_view/digestor.rb b/actionpack/lib/action_view/digestor.rb index 1c6eaf36f7..8bc69b9246 100644 --- a/actionpack/lib/action_view/digestor.rb +++ b/actionpack/lib/action_view/digestor.rb @@ -1,4 +1,4 @@ -require 'mutex_m' +require 'thread_safe' module ActionView class Digestor @@ -21,23 +21,12 @@ module ActionView /x cattr_reader(:cache) - @@cache = Hash.new.extend Mutex_m + @@cache = ThreadSafe::Cache.new def self.digest(name, format, finder, options = {}) - cache.synchronize do - unsafe_digest name, format, finder, options - end - end - - ### - # This method is NOT thread safe. DO NOT CALL IT DIRECTLY, instead call - # Digestor.digest - def self.unsafe_digest(name, format, finder, options = {}) # :nodoc: - key = "#{name}.#{format}" - - cache.fetch(key) do + @@cache["#{name}.#{format}"] ||= begin klass = options[:partial] || name.include?("/_") ? PartialDigestor : Digestor - cache[key] = klass.new(name, format, finder).digest + klass.new(name, format, finder).digest end end @@ -93,7 +82,7 @@ module ActionView def dependency_digest dependencies.collect do |template_name| - Digestor.unsafe_digest(template_name, format, finder, partial: true) + Digestor.digest(template_name, format, finder, partial: true) end.join("-") end diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index cf2a117966..11743e36f2 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -53,9 +53,11 @@ module ActionView # def javascript_include_tag(*sources) options = sources.extract_options!.stringify_keys + path_options = options.extract!('protocol').symbolize_keys + sources.uniq.map { |source| tag_options = { - "src" => path_to_javascript(source) + "src" => path_to_javascript(source, path_options) }.merge(options) content_tag(:script, "", tag_options) }.join("\n").html_safe @@ -89,11 +91,13 @@ module ActionView # def stylesheet_link_tag(*sources) options = sources.extract_options!.stringify_keys + path_options = options.extract!('protocol').symbolize_keys + sources.uniq.map { |source| tag_options = { "rel" => "stylesheet", "media" => "screen", - "href" => path_to_stylesheet(source) + "href" => path_to_stylesheet(source, path_options) }.merge(options) tag(:link, tag_options) }.join("\n").html_safe diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb index 8693f4f0e4..995aa10afb 100644 --- a/actionpack/lib/action_view/helpers/cache_helper.rb +++ b/actionpack/lib/action_view/helpers/cache_helper.rb @@ -110,15 +110,8 @@ module ActionView # <%= some_helper_method(person) %> # # Now all you'll have to do is change that timestamp when the helper method changes. - # - # ==== Conditional caching - # - # You can pass :if and :unless options, to conditionally perform or skip the cache. - # - # <%= cache @model, if: some_condition(@model) do %> - # def cache(name = {}, options = nil, &block) - if controller.perform_caching && conditions_match?(options) + if controller.perform_caching safe_concat(fragment_for(cache_fragment_name(name, options), options, &block)) else yield @@ -127,6 +120,32 @@ module ActionView nil end + # Cache fragments of a view if +condition+ is true + # + # <%= cache_if admin?, project do %> + # <b>All the topics on this project</b> + # <%= render project.topics %> + # <% end %> + def cache_if(condition, name = {}, options = nil, &block) + if condition + cache(name, options, &block) + else + yield + end + + nil + end + + # Cache fragments of a view unless +condition+ is true + # + # <%= cache_unless admin?, project do %> + # <b>All the topics on this project</b> + # <%= render project.topics %> + # <% end %> + def cache_unless(condition, name = {}, options = nil, &block) + cache_if !condition, name, options, &block + end + # This helper returns the name of a cache key for a given fragment cache # call. By supplying skip_digest: true to cache, the digestion of cache # fragments can be manually bypassed. This is useful when cache fragments @@ -144,10 +163,6 @@ module ActionView private - def conditions_match?(options) - !(options && (!options.fetch(:if, true) || options.fetch(:unless, false))) - end - def fragment_name_with_digest(name) #:nodoc: if @virtual_path [ diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 5710d1fc02..516492ca30 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1226,6 +1226,255 @@ module ActionView RUBY_EVAL end + # Creates a scope around a specific model object like form_for, but + # doesn't create the form tags themselves. This makes fields_for suitable + # for specifying additional model objects in the same form. + # + # Although the usage and purpose of +field_for+ is similar to +form_for+'s, + # its method signature is slightly different. Like +form_for+, it yields + # a FormBuilder object associated with a particular model object to a block, + # and within the block allows methods to be called on the builder to + # generate fields associated with the model object. Fields may reflect + # a model object in two ways - how they are named (hence how submitted + # values appear within the +params+ hash in the controller) and what + # default values are shown when the form the fields appear in is first + # displayed. In order for both of these features to be specified independently, + # both an object name (represented by either a symbol or string) and the + # object itself can be passed to the method separately - + # + # <%= form_for @person do |person_form| %> + # First name: <%= person_form.text_field :first_name %> + # Last name : <%= person_form.text_field :last_name %> + # + # <%= fields_for :permission, @person.permission do |permission_fields| %> + # Admin? : <%= permission_fields.check_box :admin %> + # <% end %> + # + # <%= f.submit %> + # <% end %> + # + # In this case, the checkbox field will be represented by an HTML +input+ + # tag with the +name+ attribute <tt>permission[admin]</tt>, and the submitted + # value will appear in the controller as <tt>params[:permission][:admin]</tt>. + # If <tt>@person.permission</tt> is an existing record with an attribute + # +admin+, the initial state of the checkbox when first displayed will + # reflect the value of <tt>@person.permission.admin</tt>. + # + # Often this can be simplified by passing just the name of the model + # object to +fields_for+ - + # + # <%= fields_for :permission do |permission_fields| %> + # Admin?: <%= permission_fields.check_box :admin %> + # <% end %> + # + # ...in which case, if <tt>:permission</tt> also happens to be the name of an + # instance variable <tt>@permission</tt>, the initial state of the input + # field will reflect the value of that variable's attribute <tt>@permission.admin</tt>. + # + # Alternatively, you can pass just the model object itself (if the first + # argument isn't a string or symbol +fields_for+ will realize that the + # name has been omitted) - + # + # <%= fields_for @person.permission do |permission_fields| %> + # Admin?: <%= permission_fields.check_box :admin %> + # <% end %> + # + # and +fields_for+ will derive the required name of the field from the + # _class_ of the model object, e.g. if <tt>@person.permission</tt>, is + # of class +Permission+, the field will still be named <tt>permission[admin]</tt>. + # + # Note: This also works for the methods in FormOptionHelper and + # DateHelper that are designed to work with an object as base, like + # FormOptionHelper#collection_select and DateHelper#datetime_select. + # + # === Nested Attributes Examples + # + # When the object belonging to the current scope has a nested attribute + # writer for a certain attribute, fields_for will yield a new scope + # for that attribute. This allows you to create forms that set or change + # the attributes of a parent object and its associations in one go. + # + # Nested attribute writers are normal setter methods named after an + # association. The most common way of defining these writers is either + # with +accepts_nested_attributes_for+ in a model definition or by + # defining a method with the proper name. For example: the attribute + # writer for the association <tt>:address</tt> is called + # <tt>address_attributes=</tt>. + # + # Whether a one-to-one or one-to-many style form builder will be yielded + # depends on whether the normal reader method returns a _single_ object + # or an _array_ of objects. + # + # ==== One-to-one + # + # Consider a Person class which returns a _single_ Address from the + # <tt>address</tt> reader method and responds to the + # <tt>address_attributes=</tt> writer method: + # + # class Person + # def address + # @address + # end + # + # def address_attributes=(attributes) + # # Process the attributes hash + # end + # end + # + # This model can now be used with a nested fields_for, like so: + # + # <%= form_for @person do |person_form| %> + # ... + # <%= person_form.fields_for :address do |address_fields| %> + # Street : <%= address_fields.text_field :street %> + # Zip code: <%= address_fields.text_field :zip_code %> + # <% end %> + # ... + # <% end %> + # + # When address is already an association on a Person you can use + # +accepts_nested_attributes_for+ to define the writer method for you: + # + # class Person < ActiveRecord::Base + # has_one :address + # accepts_nested_attributes_for :address + # end + # + # If you want to destroy the associated model through the form, you have + # to enable it first using the <tt>:allow_destroy</tt> option for + # +accepts_nested_attributes_for+: + # + # class Person < ActiveRecord::Base + # has_one :address + # accepts_nested_attributes_for :address, allow_destroy: true + # end + # + # Now, when you use a form element with the <tt>_destroy</tt> parameter, + # with a value that evaluates to +true+, you will destroy the associated + # model (eg. 1, '1', true, or 'true'): + # + # <%= form_for @person do |person_form| %> + # ... + # <%= person_form.fields_for :address do |address_fields| %> + # ... + # Delete: <%= address_fields.check_box :_destroy %> + # <% end %> + # ... + # <% end %> + # + # ==== One-to-many + # + # Consider a Person class which returns an _array_ of Project instances + # from the <tt>projects</tt> reader method and responds to the + # <tt>projects_attributes=</tt> writer method: + # + # class Person + # def projects + # [@project1, @project2] + # end + # + # def projects_attributes=(attributes) + # # Process the attributes hash + # end + # end + # + # Note that the <tt>projects_attributes=</tt> writer method is in fact + # required for fields_for to correctly identify <tt>:projects</tt> as a + # collection, and the correct indices to be set in the form markup. + # + # When projects is already an association on Person you can use + # +accepts_nested_attributes_for+ to define the writer method for you: + # + # class Person < ActiveRecord::Base + # has_many :projects + # accepts_nested_attributes_for :projects + # end + # + # This model can now be used with a nested fields_for. The block given to + # the nested fields_for call will be repeated for each instance in the + # collection: + # + # <%= form_for @person do |person_form| %> + # ... + # <%= person_form.fields_for :projects do |project_fields| %> + # <% if project_fields.object.active? %> + # Name: <%= project_fields.text_field :name %> + # <% end %> + # <% end %> + # ... + # <% end %> + # + # It's also possible to specify the instance to be used: + # + # <%= form_for @person do |person_form| %> + # ... + # <% @person.projects.each do |project| %> + # <% if project.active? %> + # <%= person_form.fields_for :projects, project do |project_fields| %> + # Name: <%= project_fields.text_field :name %> + # <% end %> + # <% end %> + # <% end %> + # ... + # <% end %> + # + # Or a collection to be used: + # + # <%= form_for @person do |person_form| %> + # ... + # <%= person_form.fields_for :projects, @active_projects do |project_fields| %> + # Name: <%= project_fields.text_field :name %> + # <% end %> + # ... + # <% end %> + # + # When projects is already an association on Person you can use + # +accepts_nested_attributes_for+ to define the writer method for you: + # + # class Person < ActiveRecord::Base + # has_many :projects + # accepts_nested_attributes_for :projects + # end + # + # If you want to destroy any of the associated models through the + # form, you have to enable it first using the <tt>:allow_destroy</tt> + # option for +accepts_nested_attributes_for+: + # + # class Person < ActiveRecord::Base + # has_many :projects + # accepts_nested_attributes_for :projects, allow_destroy: true + # end + # + # This will allow you to specify which models to destroy in the + # attributes hash by adding a form element for the <tt>_destroy</tt> + # parameter with a value that evaluates to +true+ + # (eg. 1, '1', true, or 'true'): + # + # <%= form_for @person do |person_form| %> + # ... + # <%= person_form.fields_for :projects do |project_fields| %> + # Delete: <%= project_fields.check_box :_destroy %> + # <% end %> + # ... + # <% end %> + # + # When a collection is used you might want to know the index of each + # object into the array. For this purpose, the <tt>index</tt> method + # is available in the FormBuilder object. + # + # <%= form_for @person do |person_form| %> + # ... + # <%= person_form.fields_for :projects do |project_fields| %> + # Project #<%= project_fields.index %> + # ... + # <% end %> + # ... + # <% end %> + # + # Note that fields_for will automatically generate a hidden field + # to store the ID of the record. There are circumstances where this + # hidden field is not needed and you can pass <tt>hidden_field_id: false</tt> + # to prevent fields_for from rendering it automatically. def fields_for(record_name, record_object = nil, fields_options = {}, &block) fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options? fields_options[:builder] ||= options[:builder] @@ -1255,23 +1504,186 @@ module ActionView @template.fields_for(record_name, record_object, fields_options, &block) end + # Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object + # assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation + # is found in the current I18n locale (through helpers.label.<modelname>.<attribute>) or you specify it explicitly. + # Additional options on the label tag can be passed as a hash with +options+. These options will be tagged + # onto the HTML as an HTML element attribute as in the example shown, except for the <tt>:value</tt> option, which is designed to + # target labels for radio_button tags (where the value is used in the ID of the input tag). + # + # ==== Examples + # label(:post, :title) + # # => <label for="post_title">Title</label> + # + # You can localize your labels based on model and attribute names. + # For example you can define the following in your locale (e.g. en.yml) + # + # helpers: + # label: + # post: + # body: "Write your entire text here" + # + # Which then will result in + # + # label(:post, :body) + # # => <label for="post_body">Write your entire text here</label> + # + # Localization can also be based purely on the translation of the attribute-name + # (if you are using ActiveRecord): + # + # activerecord: + # attributes: + # post: + # cost: "Total cost" + # + # label(:post, :cost) + # # => <label for="post_cost">Total cost</label> + # + # label(:post, :title, "A short title") + # # => <label for="post_title">A short title</label> + # + # label(:post, :title, "A short title", class: "title_label") + # # => <label for="post_title" class="title_label">A short title</label> + # + # label(:post, :privacy, "Public Post", value: "public") + # # => <label for="post_privacy_public">Public Post</label> + # + # label(:post, :terms) do + # 'Accept <a href="/terms">Terms</a>.'.html_safe + # end def label(method, text = nil, options = {}, &block) @template.label(@object_name, method, text, objectify_options(options), &block) end + # Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object + # assigned to the template (identified by +object+). This object must be an instance object (@object) and not a local object. + # It's intended that +method+ returns an integer and if that integer is above zero, then the checkbox is checked. + # Additional options on the input tag can be passed as a hash with +options+. The +checked_value+ defaults to 1 + # while the default +unchecked_value+ is set to 0 which is convenient for boolean values. + # + # ==== Gotcha + # + # The HTML specification says unchecked check boxes are not successful, and + # thus web browsers do not send them. Unfortunately this introduces a gotcha: + # if an +Invoice+ model has a +paid+ flag, and in the form that edits a paid + # invoice the user unchecks its check box, no +paid+ parameter is sent. So, + # any mass-assignment idiom like + # + # @invoice.update_attributes(params[:invoice]) + # + # wouldn't update the flag. + # + # To prevent this the helper generates an auxiliary hidden field before + # the very check box. The hidden field has the same name and its + # attributes mimic an unchecked check box. + # + # This way, the client either sends only the hidden field (representing + # the check box is unchecked), or both fields. Since the HTML specification + # says key/value pairs have to be sent in the same order they appear in the + # form, and parameters extraction gets the last occurrence of any repeated + # key in the query string, that works for ordinary forms. + # + # Unfortunately that workaround does not work when the check box goes + # within an array-like parameter, as in + # + # <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %> + # <%= form.check_box :paid %> + # ... + # <% end %> + # + # because parameter name repetition is precisely what Rails seeks to distinguish + # the elements of the array. For each item with a checked check box you + # get an extra ghost item with only that attribute, assigned to "0". + # + # In that case it is preferable to either use +check_box_tag+ or to use + # hashes instead of arrays. + # + # # Let's say that @post.validated? is 1: + # check_box("post", "validated") + # # => <input name="post[validated]" type="hidden" value="0" /> + # # <input checked="checked" type="checkbox" id="post_validated" name="post[validated]" value="1" /> + # + # # Let's say that @puppy.gooddog is "no": + # check_box("puppy", "gooddog", {}, "yes", "no") + # # => <input name="puppy[gooddog]" type="hidden" value="no" /> + # # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" /> + # + # check_box("eula", "accepted", { class: 'eula_check' }, "yes", "no") + # # => <input name="eula[accepted]" type="hidden" value="no" /> + # # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" /> def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") @template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value) end + # Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object + # assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the + # radio button will be checked. + # + # To force the radio button to be checked pass <tt>checked: true</tt> in the + # +options+ hash. You may pass HTML options there as well. + # + # # Let's say that @post.category returns "rails": + # radio_button("post", "category", "rails") + # radio_button("post", "category", "java") + # # => <input type="radio" id="post_category_rails" name="post[category]" value="rails" checked="checked" /> + # # <input type="radio" id="post_category_java" name="post[category]" value="java" /> + # + # radio_button("user", "receive_newsletter", "yes") + # radio_button("user", "receive_newsletter", "no") + # # => <input type="radio" id="user_receive_newsletter_yes" name="user[receive_newsletter]" value="yes" /> + # # <input type="radio" id="user_receive_newsletter_no" name="user[receive_newsletter]" value="no" checked="checked" /> def radio_button(method, tag_value, options = {}) @template.radio_button(@object_name, method, tag_value, objectify_options(options)) end + # Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object + # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a + # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example + # shown. + # + # ==== Examples + # hidden_field(:signup, :pass_confirm) + # # => <input type="hidden" id="signup_pass_confirm" name="signup[pass_confirm]" value="#{@signup.pass_confirm}" /> + # + # hidden_field(:post, :tag_list) + # # => <input type="hidden" id="post_tag_list" name="post[tag_list]" value="#{@post.tag_list}" /> + # + # hidden_field(:user, :token) + # # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" /> + # def hidden_field(method, options = {}) @emitted_hidden_id = true if method == :id @template.hidden_field(@object_name, method, objectify_options(options)) end + # Returns a file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object + # assigned to the template (identified by +object+). Additional options on the input tag can be passed as a + # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example + # shown. + # + # Using this method inside a +form_for+ block will set the enclosing form's encoding to <tt>multipart/form-data</tt>. + # + # ==== Options + # * Creates standard HTML attributes for the tag. + # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input. + # * <tt>:multiple</tt> - If set to true, *in most updated browsers* the user will be allowed to select multiple files. + # * <tt>:accept</tt> - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations. + # + # ==== Examples + # file_field(:user, :avatar) + # # => <input type="file" id="user_avatar" name="user[avatar]" /> + # + # file_field(:post, :image, :multiple => true) + # # => <input type="file" id="post_image" name="post[image]" multiple="true" /> + # + # file_field(:post, :attached, accept: 'text/html') + # # => <input accept="text/html" type="file" id="post_attached" name="post[attached]" /> + # + # file_field(:post, :image, accept: 'image/png,image/gif,image/jpeg') + # # => <input type="file" id="post_image" name="post[image]" accept="image/png,image/gif,image/jpeg" /> + # + # file_field(:attachment, :file, class: 'file_input') + # # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" /> def file_field(method, options = {}) self.multipart = true @template.file_field(@object_name, method, objectify_options(options)) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index c0e7ee1f8d..1b5b788a35 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -2,6 +2,7 @@ require 'cgi' require 'erb' require 'action_view/helpers/form_helper' require 'active_support/core_ext/string/output_safety' +require 'active_support/core_ext/array/wrap' module ActionView # = Action View Form Option Helpers diff --git a/actionpack/lib/action_view/helpers/tags/date_select.rb b/actionpack/lib/action_view/helpers/tags/date_select.rb index 5d706087b0..6c400f85cb 100644 --- a/actionpack/lib/action_view/helpers/tags/date_select.rb +++ b/actionpack/lib/action_view/helpers/tags/date_select.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/time/calculations' + module ActionView module Helpers module Tags @@ -58,7 +60,7 @@ module ActionView default[key] ||= time.send(key) end - Time.utc_time( + Time.utc( default[:year], default[:month], default[:day], default[:hour], default[:min], default[:sec] ) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index fa516cf91b..aeee662071 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -415,40 +415,26 @@ module ActionView # also used as the name of the link unless +name+ is specified. Additional # HTML attributes for the link can be passed in +html_options+. # - # +mail_to+ has several methods for hindering email harvesters and customizing - # the email itself by passing special keys to +html_options+. + # +mail_to+ has several methods for customizing the email itself by + # passing special keys to +html_options+. # # ==== Options - # * <tt>:encode</tt> - This key will accept the strings "javascript" or "hex". - # Passing "javascript" will dynamically create and encode the mailto link then - # eval it into the DOM of the page. This method will not show the link on - # the page if the user has JavaScript disabled. Passing "hex" will hex - # encode the +email_address+ before outputting the mailto link. - # * <tt>:replace_at</tt> - When the link +name+ isn't provided, the - # +email_address+ is used for the link label. You can use this option to - # obfuscate the +email_address+ by substituting the @ sign with the string - # given as the value. - # * <tt>:replace_dot</tt> - When the link +name+ isn't provided, the - # +email_address+ is used for the link label. You can use this option to - # obfuscate the +email_address+ by substituting the . in the email with the - # string given as the value. # * <tt>:subject</tt> - Preset the subject line of the email. # * <tt>:body</tt> - Preset the body of the email. # * <tt>:cc</tt> - Carbon Copy additional recipients on the email. # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email. # + # ==== Obfuscation + # Prior to Rails 4.0, +mail_to+ provided options for encoding the address + # in order to hinder email harvesters. To take advantage of these options, + # install the +actionview-encoded_mail_to+ gem. + # # ==== Examples # mail_to "me@domain.com" # # => <a href="mailto:me@domain.com">me@domain.com</a> # - # mail_to "me@domain.com", "My email", encode: "javascript" - # # => <script>eval(decodeURIComponent('%64%6f%63...%27%29%3b'))</script> - # - # mail_to "me@domain.com", "My email", encode: "hex" - # # => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a> - # - # mail_to "me@domain.com", nil, replace_at: "_at_", replace_dot: "_dot_", class: "email" - # # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a> + # mail_to "me@domain.com", "My email" + # # => <a href="mailto:me@domain.com">My email</a> # # mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com", # subject: "This is an example email" @@ -456,43 +442,15 @@ module ActionView def mail_to(email_address, name = nil, html_options = {}) email_address = ERB::Util.html_escape(email_address) - html_options = html_options.stringify_keys - encode = html_options.delete("encode").to_s + html_options.stringify_keys! extras = %w{ cc bcc body subject }.map { |item| option = html_options.delete(item) || next "#{item}=#{Rack::Utils.escape_path(option)}" }.compact extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&')) - - email_address_obfuscated = email_address.to_str - email_address_obfuscated.gsub!(/@/, html_options.delete("replace_at")) if html_options.key?("replace_at") - email_address_obfuscated.gsub!(/\./, html_options.delete("replace_dot")) if html_options.key?("replace_dot") - case encode - when "javascript" - string = '' - html = content_tag("a", name || email_address_obfuscated.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe)) - html = escape_javascript(html.to_str) - "document.write('#{html}');".each_byte do |c| - string << sprintf("%%%x", c) - end - "<script>eval(decodeURIComponent('#{string}'))</script>".html_safe - when "hex" - email_address_encoded = email_address_obfuscated.unpack('C*').map {|c| - sprintf("&#%d;", c) - }.join - - string = 'mailto:'.unpack('C*').map { |c| - sprintf("&#%d;", c) - }.join + email_address.unpack('C*').map { |c| - char = c.chr - char =~ /\w/ ? sprintf("%%%x", c) : char - }.join - - content_tag "a", name || email_address_encoded.html_safe, html_options.merge("href" => "#{string}#{extras}".html_safe) - else - content_tag "a", name || email_address_obfuscated.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe) - end + + content_tag "a", name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe) end # True if the current request URI was generated by the given +options+. diff --git a/actionpack/lib/action_view/lookup_context.rb b/actionpack/lib/action_view/lookup_context.rb index 76f4dea7b8..4e4816d983 100644 --- a/actionpack/lib/action_view/lookup_context.rb +++ b/actionpack/lib/action_view/lookup_context.rb @@ -1,3 +1,4 @@ +require 'thread_safe' require 'active_support/core_ext/module/remove_method' module ActionView @@ -51,7 +52,7 @@ module ActionView alias :object_hash :hash attr_reader :hash - @details_keys = Hash.new + @details_keys = ThreadSafe::Cache.new def self.get(details) @details_keys[details] ||= new diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb index 8fb9b6ff18..37f93a13fc 100644 --- a/actionpack/lib/action_view/renderer/partial_renderer.rb +++ b/actionpack/lib/action_view/renderer/partial_renderer.rb @@ -1,3 +1,5 @@ +require 'thread_safe' + module ActionView # = Action View Partials # @@ -247,7 +249,9 @@ module ActionView # <%- end -%> # <% end %> class PartialRenderer < AbstractRenderer - PREFIXED_PARTIAL_NAMES = Hash.new { |h,k| h[k] = {} } + PREFIXED_PARTIAL_NAMES = ThreadSafe::Cache.new do |h, k| + h[k] = ThreadSafe::Cache.new + end def initialize(*) super diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index fc77c1485d..8b23029bbc 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -3,7 +3,7 @@ require "active_support/core_ext/class" require "active_support/core_ext/class/attribute_accessors" require "action_view/template" require "thread" -require "mutex_m" +require "thread_safe" module ActionView # = Action View Resolver @@ -35,52 +35,51 @@ module ActionView # Threadsafe template cache class Cache #:nodoc: - class CacheEntry - include Mutex_m - - attr_accessor :templates + class SmallCache < ThreadSafe::Cache + def initialize(options = {}) + super(options.merge(:initial_capacity => 2)) + end end + # preallocate all the default blocks for performance/memory consumption reasons + PARTIAL_BLOCK = lambda {|cache, partial| cache[partial] = SmallCache.new} + PREFIX_BLOCK = lambda {|cache, prefix| cache[prefix] = SmallCache.new(&PARTIAL_BLOCK)} + NAME_BLOCK = lambda {|cache, name| cache[name] = SmallCache.new(&PREFIX_BLOCK)} + KEY_BLOCK = lambda {|cache, key| cache[key] = SmallCache.new(&NAME_BLOCK)} + + # usually a majority of template look ups return nothing, use this canonical preallocated array to safe memory + NO_TEMPLATES = [].freeze + def initialize - @data = Hash.new { |h1,k1| h1[k1] = Hash.new { |h2,k2| - h2[k2] = Hash.new { |h3,k3| h3[k3] = Hash.new { |h4,k4| h4[k4] = {} } } } } - @mutex = Mutex.new + @data = SmallCache.new(&KEY_BLOCK) end # Cache the templates returned by the block def cache(key, name, prefix, partial, locals) - cache_entry = nil - - # first obtain a lock on the main data structure to create the cache entry - @mutex.synchronize do - cache_entry = @data[key][name][prefix][partial][locals] ||= CacheEntry.new - end - - # then to avoid a long lasting global lock, obtain a more granular lock - # on the CacheEntry itself - cache_entry.synchronize do - if Resolver.caching? - cache_entry.templates ||= yield + if Resolver.caching? + @data[key][name][prefix][partial][locals] ||= canonical_no_templates(yield) + else + fresh_templates = yield + cached_templates = @data[key][name][prefix][partial][locals] + + if templates_have_changed?(cached_templates, fresh_templates) + @data[key][name][prefix][partial][locals] = canonical_no_templates(fresh_templates) else - fresh_templates = yield - - if templates_have_changed?(cache_entry.templates, fresh_templates) - cache_entry.templates = fresh_templates - else - cache_entry.templates ||= [] - end + cached_templates || NO_TEMPLATES end end end def clear - @mutex.synchronize do - @data.clear - end + @data.clear end private + def canonical_no_templates(templates) + templates.empty? ? NO_TEMPLATES : templates + end + def templates_have_changed?(cached_templates, fresh_templates) # if either the old or new template list is empty, we don't need to (and can't) # compare modification times, and instead just check whether the lists are different diff --git a/actionpack/test/controller/http_token_authentication_test.rb b/actionpack/test/controller/http_token_authentication_test.rb index 8a409d6ed2..faf923e929 100644 --- a/actionpack/test/controller/http_token_authentication_test.rb +++ b/actionpack/test/controller/http_token_authentication_test.rb @@ -104,17 +104,40 @@ class HttpTokenAuthenticationTest < ActionController::TestCase assert_equal 'Token realm="SuperSecret"', @response.headers['WWW-Authenticate'] end - test "authentication request with valid credential" do - @request.env['HTTP_AUTHORIZATION'] = encode_credentials('"quote" pretty', :algorithm => 'test') - get :display + test "token_and_options returns correct token" do + token = "rcHu+HzSFw89Ypyhn/896A==" + actual = ActionController::HttpAuthentication::Token.token_and_options(sample_request(token)).first + expected = token + assert_equal(expected, actual) + end + + test "token_and_options returns correct token" do + token = 'rcHu+=HzSFw89Ypyhn/896A==f34' + actual = ActionController::HttpAuthentication::Token.token_and_options(sample_request(token)).first + expected = token + assert_equal(expected, actual) + end - assert_response :success - assert assigns(:logged_in) - assert_equal 'Definitely Maybe', @response.body + test "token_and_options returns correct token" do + token = 'rcHu+\\\\"/896A' + actual = ActionController::HttpAuthentication::Token.token_and_options(sample_request(token)).first + expected = token + assert_equal(expected, actual) + end + + test "token_and_options returns correct token" do + token = '\"quote\" pretty' + actual = ActionController::HttpAuthentication::Token.token_and_options(sample_request(token)).first + expected = token + assert_equal(expected, actual) end private + def sample_request(token) + @sample_request ||= OpenStruct.new authorization: %{Token token="#{token}"} + end + def encode_credentials(token, options = {}) ActionController::HttpAuthentication::Token.encode_credentials(token, options) end diff --git a/actionpack/test/controller/log_subscriber_test.rb b/actionpack/test/controller/log_subscriber_test.rb index 929545fc10..075347be52 100644 --- a/actionpack/test/controller/log_subscriber_test.rb +++ b/actionpack/test/controller/log_subscriber_test.rb @@ -46,20 +46,20 @@ module Another render :inline => "<%= cache('foo%bar'){ 'Contains % sign in key' } %>" end - def with_fragment_cache_and_if_true_condition - render :inline => "<%= cache('foo', :if => true) { 'bar' } %>" + def with_fragment_cache_if_with_true_condition + render :inline => "<%= cache_if(true, 'foo') { 'bar' } %>" end - def with_fragment_cache_and_if_false_condition - render :inline => "<%= cache('foo', :if => false) { 'bar' } %>" + def with_fragment_cache_if_with_false_condition + render :inline => "<%= cache_if(false, 'foo') { 'bar' } %>" end - def with_fragment_cache_and_unless_false_condition - render :inline => "<%= cache('foo', :unless => false) { 'bar' } %>" + def with_fragment_cache_unless_with_false_condition + render :inline => "<%= cache_unless(false, 'foo') { 'bar' } %>" end - def with_fragment_cache_and_unless_true_condition - render :inline => "<%= cache('foo', :unless => true) { 'bar' } %>" + def with_fragment_cache_unless_with_true_condition + render :inline => "<%= cache_unless(true, 'foo') { 'bar' } %>" end def with_exception @@ -219,9 +219,9 @@ class ACLogSubscriberTest < ActionController::TestCase @controller.config.perform_caching = true end - def test_with_fragment_cache_and_if_true + def test_with_fragment_cache_if_with_true @controller.config.perform_caching = true - get :with_fragment_cache_and_if_true_condition + get :with_fragment_cache_if_with_true_condition wait assert_equal 4, logs.size @@ -231,9 +231,9 @@ class ACLogSubscriberTest < ActionController::TestCase @controller.config.perform_caching = true end - def test_with_fragment_cache_and_if_false + def test_with_fragment_cache_if_with_false @controller.config.perform_caching = true - get :with_fragment_cache_and_if_false_condition + get :with_fragment_cache_if_with_false_condition wait assert_equal 2, logs.size @@ -243,9 +243,9 @@ class ACLogSubscriberTest < ActionController::TestCase @controller.config.perform_caching = true end - def test_with_fragment_cache_and_unless_true + def test_with_fragment_cache_unless_with_true @controller.config.perform_caching = true - get :with_fragment_cache_and_unless_true_condition + get :with_fragment_cache_unless_with_true_condition wait assert_equal 2, logs.size @@ -255,9 +255,9 @@ class ACLogSubscriberTest < ActionController::TestCase @controller.config.perform_caching = true end - def test_with_fragment_cache_and_unless_false + def test_with_fragment_cache_unless_with_false @controller.config.perform_caching = true - get :with_fragment_cache_and_unless_false_condition + get :with_fragment_cache_unless_with_false_condition wait assert_equal 4, logs.size diff --git a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb index 63c5ea26a6..399f15199c 100644 --- a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb @@ -123,6 +123,18 @@ class MultipartParamsParsingTest < ActionDispatch::IntegrationTest end end + # This can happen in Internet Explorer when redirecting after multipart form submit. + test "does not raise EOFError on GET request with multipart content-type" do + with_routing do |set| + set.draw do + get ':action', to: 'multipart_params_parsing_test/test' + end + headers = { "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x" } + get "/parse", {}, headers + assert_response :ok + end + end + private def fixture(name) File.open(File.join(FIXTURE_PATH, name), 'rb') do |file| diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index f2bacf3e20..263853fb6c 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -650,6 +650,13 @@ class RequestTest < ActiveSupport::TestCase assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV]) end + test "raw_post rewinds rack.input if RAW_POST_DATA is nil" do + request = stub_request('rack.input' => StringIO.new("foo"), + 'CONTENT_LENGTH' => 3) + assert_equal "foo", request.raw_post + assert_equal "foo", request.env['rack.input'].read + end + test "process parameter filter" do test_hashes = [ [{'foo'=>'bar'},{'foo'=>'bar'},%w'food'], diff --git a/actionpack/test/dispatch/routing/route_set_test.rb b/actionpack/test/dispatch/routing/route_set_test.rb new file mode 100644 index 0000000000..d57b1a5637 --- /dev/null +++ b/actionpack/test/dispatch/routing/route_set_test.rb @@ -0,0 +1,86 @@ +require 'abstract_unit' + +module ActionDispatch + module Routing + class RouteSetTest < ActiveSupport::TestCase + class SimpleApp + def initialize(response) + @response = response + end + + def call(env) + [ 200, { 'Content-Type' => 'text/plain' }, [response] ] + end + end + + setup do + @set = RouteSet.new + end + + test "url helpers are added when route is added" do + draw do + get 'foo', to: SimpleApp.new('foo#index') + end + + assert_equal '/foo', url_helpers.foo_path + assert_raises NoMethodError do + assert_equal '/bar', url_helpers.bar_path + end + + draw do + get 'foo', to: SimpleApp.new('foo#index') + get 'bar', to: SimpleApp.new('bar#index') + end + + assert_equal '/foo', url_helpers.foo_path + assert_equal '/bar', url_helpers.bar_path + end + + test "url helpers are updated when route is updated" do + draw do + get 'bar', to: SimpleApp.new('bar#index'), as: :bar + end + + assert_equal '/bar', url_helpers.bar_path + + draw do + get 'baz', to: SimpleApp.new('baz#index'), as: :bar + end + + assert_equal '/baz', url_helpers.bar_path + end + + test "url helpers are removed when route is removed" do + draw do + get 'foo', to: SimpleApp.new('foo#index') + get 'bar', to: SimpleApp.new('bar#index') + end + + assert_equal '/foo', url_helpers.foo_path + assert_equal '/bar', url_helpers.bar_path + + draw do + get 'foo', to: SimpleApp.new('foo#index') + end + + assert_equal '/foo', url_helpers.foo_path + assert_raises NoMethodError do + assert_equal '/bar', url_helpers.bar_path + end + end + + private + def clear! + @set.clear! + end + + def draw(&block) + @set.draw(&block) + end + + def url_helpers + @set.url_helpers + end + end + end +end diff --git a/actionpack/test/journey/gtg/builder_test.rb b/actionpack/test/journey/gtg/builder_test.rb new file mode 100644 index 0000000000..a633c3eea6 --- /dev/null +++ b/actionpack/test/journey/gtg/builder_test.rb @@ -0,0 +1,79 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + module GTG + class TestBuilder < MiniTest::Unit::TestCase + def test_following_states_multi + table = tt ['a|a'] + assert_equal 1, table.move([0], 'a').length + end + + def test_following_states_multi_regexp + table = tt [':a|b'] + assert_equal 1, table.move([0], 'fooo').length + assert_equal 2, table.move([0], 'b').length + end + + def test_multi_path + table = tt ['/:a/d', '/b/c'] + + [ + [1, '/'], + [2, 'b'], + [2, '/'], + [1, 'c'], + ].inject([0]) { |state, (exp, sym)| + new = table.move(state, sym) + assert_equal exp, new.length + new + } + end + + def test_match_data_ambiguous + table = tt %w{ + /articles(.:format) + /articles/new(.:format) + /articles/:id/edit(.:format) + /articles/:id(.:format) + } + + sim = NFA::Simulator.new table + + match = sim.match '/articles/new' + assert_equal 2, match.memos.length + end + + ## + # Identical Routes may have different restrictions. + def test_match_same_paths + table = tt %w{ + /articles/new(.:format) + /articles/new(.:format) + } + + sim = NFA::Simulator.new table + + match = sim.match '/articles/new' + assert_equal 2, match.memos.length + end + + private + def ast strings + parser = Journey::Parser.new + asts = strings.map { |string| + memo = Object.new + ast = parser.parse string + ast.each { |n| n.memo = memo } + ast + } + Nodes::Or.new asts + end + + def tt strings + Builder.new(ast(strings)).transition_table + end + end + end + end +end diff --git a/actionpack/test/journey/gtg/transition_table_test.rb b/actionpack/test/journey/gtg/transition_table_test.rb new file mode 100644 index 0000000000..6d81b72c41 --- /dev/null +++ b/actionpack/test/journey/gtg/transition_table_test.rb @@ -0,0 +1,115 @@ +require 'abstract_unit' +require 'json' + +module ActionDispatch + module Journey + module GTG + class TestGeneralizedTable < MiniTest::Unit::TestCase + def test_to_json + table = tt %w{ + /articles(.:format) + /articles/new(.:format) + /articles/:id/edit(.:format) + /articles/:id(.:format) + } + + json = JSON.load table.to_json + assert json['regexp_states'] + assert json['string_states'] + assert json['accepting'] + end + + if system("dot -V 2>/dev/null") + def test_to_svg + table = tt %w{ + /articles(.:format) + /articles/new(.:format) + /articles/:id/edit(.:format) + /articles/:id(.:format) + } + svg = table.to_svg + assert svg + refute_match(/DOCTYPE/, svg) + end + end + + def test_simulate_gt + sim = simulator_for ['/foo', '/bar'] + assert_match sim, '/foo' + end + + def test_simulate_gt_regexp + sim = simulator_for [':foo'] + assert_match sim, 'foo' + end + + def test_simulate_gt_regexp_mix + sim = simulator_for ['/get', '/:method/foo'] + assert_match sim, '/get' + assert_match sim, '/get/foo' + end + + def test_simulate_optional + sim = simulator_for ['/foo(/bar)'] + assert_match sim, '/foo' + assert_match sim, '/foo/bar' + refute_match sim, '/foo/' + end + + def test_match_data + path_asts = asts %w{ /get /:method/foo } + paths = path_asts.dup + + builder = GTG::Builder.new Nodes::Or.new path_asts + tt = builder.transition_table + + sim = GTG::Simulator.new tt + + match = sim.match '/get' + assert_equal [paths.first], match.memos + + match = sim.match '/get/foo' + assert_equal [paths.last], match.memos + end + + def test_match_data_ambiguous + path_asts = asts %w{ + /articles(.:format) + /articles/new(.:format) + /articles/:id/edit(.:format) + /articles/:id(.:format) + } + + paths = path_asts.dup + ast = Nodes::Or.new path_asts + + builder = GTG::Builder.new ast + sim = GTG::Simulator.new builder.transition_table + + match = sim.match '/articles/new' + assert_equal [paths[1], paths[3]], match.memos + end + + private + def asts paths + parser = Journey::Parser.new + paths.map { |x| + ast = parser.parse x + ast.each { |n| n.memo = ast} + ast + } + end + + def tt paths + x = asts paths + builder = GTG::Builder.new Nodes::Or.new x + builder.transition_table + end + + def simulator_for paths + GTG::Simulator.new tt(paths) + end + end + end + end +end diff --git a/actionpack/test/journey/nfa/simulator_test.rb b/actionpack/test/journey/nfa/simulator_test.rb new file mode 100644 index 0000000000..9f89329b57 --- /dev/null +++ b/actionpack/test/journey/nfa/simulator_test.rb @@ -0,0 +1,98 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + module NFA + class TestSimulator < MiniTest::Unit::TestCase + def test_simulate_simple + sim = simulator_for ['/foo'] + assert_match sim, '/foo' + end + + def test_simulate_simple_no_match + sim = simulator_for ['/foo'] + refute_match sim, 'foo' + end + + def test_simulate_simple_no_match_too_long + sim = simulator_for ['/foo'] + refute_match sim, '/foo/bar' + end + + def test_simulate_simple_no_match_wrong_string + sim = simulator_for ['/foo'] + refute_match sim, '/bar' + end + + def test_simulate_regex + sim = simulator_for ['/:foo/bar'] + assert_match sim, '/bar/bar' + assert_match sim, '/foo/bar' + end + + def test_simulate_or + sim = simulator_for ['/foo', '/bar'] + assert_match sim, '/bar' + assert_match sim, '/foo' + refute_match sim, '/baz' + end + + def test_simulate_optional + sim = simulator_for ['/foo(/bar)'] + assert_match sim, '/foo' + assert_match sim, '/foo/bar' + refute_match sim, '/foo/' + end + + def test_matchdata_has_memos + paths = %w{ /foo /bar } + parser = Journey::Parser.new + asts = paths.map { |x| + ast = parser.parse x + ast.each { |n| n.memo = ast} + ast + } + + expected = asts.first + + builder = Builder.new Nodes::Or.new asts + + sim = Simulator.new builder.transition_table + + md = sim.match '/foo' + assert_equal [expected], md.memos + end + + def test_matchdata_memos_on_merge + parser = Journey::Parser.new + routes = [ + '/articles(.:format)', + '/articles/new(.:format)', + '/articles/:id/edit(.:format)', + '/articles/:id(.:format)', + ].map { |path| + ast = parser.parse path + ast.each { |n| n.memo = ast } + ast + } + + asts = routes.dup + + ast = Nodes::Or.new routes + + nfa = Journey::NFA::Builder.new ast + sim = Simulator.new nfa.transition_table + md = sim.match '/articles' + assert_equal [asts.first], md.memos + end + + def simulator_for paths + parser = Journey::Parser.new + asts = paths.map { |x| parser.parse x } + builder = Builder.new Nodes::Or.new asts + Simulator.new builder.transition_table + end + end + end + end +end diff --git a/actionpack/test/journey/nfa/transition_table_test.rb b/actionpack/test/journey/nfa/transition_table_test.rb new file mode 100644 index 0000000000..72cefe42bf --- /dev/null +++ b/actionpack/test/journey/nfa/transition_table_test.rb @@ -0,0 +1,72 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + module NFA + class TestTransitionTable < MiniTest::Unit::TestCase + def setup + @parser = Journey::Parser.new + end + + def test_eclosure + table = tt '/' + assert_equal [0], table.eclosure(0) + + table = tt ':a|:b' + assert_equal 3, table.eclosure(0).length + + table = tt '(:a|:b)' + assert_equal 5, table.eclosure(0).length + assert_equal 5, table.eclosure([0]).length + end + + def test_following_states_one + table = tt '/' + + assert_equal [1], table.following_states(0, '/') + assert_equal [1], table.following_states([0], '/') + end + + def test_following_states_group + table = tt 'a|b' + states = table.eclosure 0 + + assert_equal 1, table.following_states(states, 'a').length + assert_equal 1, table.following_states(states, 'b').length + end + + def test_following_states_multi + table = tt 'a|a' + states = table.eclosure 0 + + assert_equal 2, table.following_states(states, 'a').length + assert_equal 0, table.following_states(states, 'b').length + end + + def test_following_states_regexp + table = tt 'a|:a' + states = table.eclosure 0 + + assert_equal 1, table.following_states(states, 'a').length + assert_equal 1, table.following_states(states, /[^\.\/\?]+/).length + assert_equal 0, table.following_states(states, 'b').length + end + + def test_alphabet + table = tt 'a|:a' + assert_equal [/[^\.\/\?]+/, 'a'], table.alphabet + + table = tt 'a|a' + assert_equal ['a'], table.alphabet + end + + private + def tt string + ast = @parser.parse string + builder = Builder.new ast + builder.transition_table + end + end + end + end +end diff --git a/actionpack/test/journey/nodes/symbol_test.rb b/actionpack/test/journey/nodes/symbol_test.rb new file mode 100644 index 0000000000..f53840274a --- /dev/null +++ b/actionpack/test/journey/nodes/symbol_test.rb @@ -0,0 +1,17 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + module Nodes + class TestSymbol < MiniTest::Unit::TestCase + def test_default_regexp? + sym = Symbol.new nil + assert sym.default_regexp? + + sym.regexp = nil + refute sym.default_regexp? + end + end + end + end +end diff --git a/actionpack/test/journey/path/pattern_test.rb b/actionpack/test/journey/path/pattern_test.rb new file mode 100644 index 0000000000..0f2d0d44c0 --- /dev/null +++ b/actionpack/test/journey/path/pattern_test.rb @@ -0,0 +1,284 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + module Path + class TestPattern < MiniTest::Unit::TestCase + x = /.+/ + { + '/:controller(/:action)' => %r{\A/(#{x})(?:/([^/.?]+))?\Z}, + '/:controller/foo' => %r{\A/(#{x})/foo\Z}, + '/:controller/:action' => %r{\A/(#{x})/([^/.?]+)\Z}, + '/:controller' => %r{\A/(#{x})\Z}, + '/:controller(/:action(/:id))' => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z}, + '/:controller/:action.xml' => %r{\A/(#{x})/([^/.?]+)\.xml\Z}, + '/:controller.:format' => %r{\A/(#{x})\.([^/.?]+)\Z}, + '/:controller(.:format)' => %r{\A/(#{x})(?:\.([^/.?]+))?\Z}, + '/:controller/*foo' => %r{\A/(#{x})/(.+)\Z}, + '/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar\Z}, + }.each do |path, expected| + define_method(:"test_to_regexp_#{path}") do + strexp = Router::Strexp.new( + path, + { :controller => /.+/ }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + assert_equal(expected, path.to_regexp) + end + end + + { + '/:controller(/:action)' => %r{\A/(#{x})(?:/([^/.?]+))?}, + '/:controller/foo' => %r{\A/(#{x})/foo}, + '/:controller/:action' => %r{\A/(#{x})/([^/.?]+)}, + '/:controller' => %r{\A/(#{x})}, + '/:controller(/:action(/:id))' => %r{\A/(#{x})(?:/([^/.?]+)(?:/([^/.?]+))?)?}, + '/:controller/:action.xml' => %r{\A/(#{x})/([^/.?]+)\.xml}, + '/:controller.:format' => %r{\A/(#{x})\.([^/.?]+)}, + '/:controller(.:format)' => %r{\A/(#{x})(?:\.([^/.?]+))?}, + '/:controller/*foo' => %r{\A/(#{x})/(.+)}, + '/:controller/*foo/bar' => %r{\A/(#{x})/(.+)/bar}, + }.each do |path, expected| + define_method(:"test_to_non_anchored_regexp_#{path}") do + strexp = Router::Strexp.new( + path, + { :controller => /.+/ }, + ["/", ".", "?"], + false + ) + path = Pattern.new strexp + assert_equal(expected, path.to_regexp) + end + end + + { + '/:controller(/:action)' => %w{ controller action }, + '/:controller/foo' => %w{ controller }, + '/:controller/:action' => %w{ controller action }, + '/:controller' => %w{ controller }, + '/:controller(/:action(/:id))' => %w{ controller action id }, + '/:controller/:action.xml' => %w{ controller action }, + '/:controller.:format' => %w{ controller format }, + '/:controller(.:format)' => %w{ controller format }, + '/:controller/*foo' => %w{ controller foo }, + '/:controller/*foo/bar' => %w{ controller foo }, + }.each do |path, expected| + define_method(:"test_names_#{path}") do + strexp = Router::Strexp.new( + path, + { :controller => /.+/ }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + assert_equal(expected, path.names) + end + end + + def test_to_regexp_with_extended_group + strexp = Router::Strexp.new( + '/page/:name', + { :name => / + #ROFL + (tender|love + #MAO + )/x }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + assert_match(path, '/page/tender') + assert_match(path, '/page/love') + refute_match(path, '/page/loving') + end + + def test_optional_names + [ + ['/:foo(/:bar(/:baz))', %w{ bar baz }], + ['/:foo(/:bar)', %w{ bar }], + ['/:foo(/:bar)/:lol(/:baz)', %w{ bar baz }], + ].each do |pattern, list| + path = Pattern.new pattern + assert_equal list.sort, path.optional_names.sort + end + end + + def test_to_regexp_match_non_optional + strexp = Router::Strexp.new( + '/:name', + { :name => /\d+/ }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + assert_match(path, '/123') + refute_match(path, '/') + end + + def test_to_regexp_with_group + strexp = Router::Strexp.new( + '/page/:name', + { :name => /(tender|love)/ }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + assert_match(path, '/page/tender') + assert_match(path, '/page/love') + refute_match(path, '/page/loving') + end + + def test_ast_sets_regular_expressions + requirements = { :name => /(tender|love)/, :value => /./ } + strexp = Router::Strexp.new( + '/page/:name/:value', + requirements, + ["/", ".", "?"] + ) + + assert_equal requirements, strexp.requirements + + path = Pattern.new strexp + nodes = path.ast.grep(Nodes::Symbol) + assert_equal 2, nodes.length + nodes.each do |node| + assert_equal requirements[node.to_sym], node.regexp + end + end + + def test_match_data_with_group + strexp = Router::Strexp.new( + '/page/:name', + { :name => /(tender|love)/ }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + match = path.match '/page/tender' + assert_equal 'tender', match[1] + assert_equal 2, match.length + end + + def test_match_data_with_multi_group + strexp = Router::Strexp.new( + '/page/:name/:id', + { :name => /t(((ender|love)))()/ }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + match = path.match '/page/tender/10' + assert_equal 'tender', match[1] + assert_equal '10', match[2] + assert_equal 3, match.length + assert_equal %w{ tender 10 }, match.captures + end + + def test_star_with_custom_re + z = /\d+/ + strexp = Router::Strexp.new( + '/page/*foo', + { :foo => z }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + assert_equal(%r{\A/page/(#{z})\Z}, path.to_regexp) + end + + def test_insensitive_regexp_with_group + strexp = Router::Strexp.new( + '/page/:name/aaron', + { :name => /(tender|love)/i }, + ["/", ".", "?"] + ) + path = Pattern.new strexp + assert_match(path, '/page/TENDER/aaron') + assert_match(path, '/page/loVE/aaron') + refute_match(path, '/page/loVE/AAron') + end + + def test_to_regexp_with_strexp + strexp = Router::Strexp.new('/:controller', { }, ["/", ".", "?"]) + path = Pattern.new strexp + x = %r{\A/([^/.?]+)\Z} + + assert_equal(x.source, path.source) + end + + def test_to_regexp_defaults + path = Pattern.new '/:controller(/:action(/:id))' + expected = %r{\A/([^/.?]+)(?:/([^/.?]+)(?:/([^/.?]+))?)?\Z} + assert_equal expected, path.to_regexp + end + + def test_failed_match + path = Pattern.new '/:controller(/:action(/:id(.:format)))' + uri = 'content' + + refute path =~ uri + end + + def test_match_controller + path = Pattern.new '/:controller(/:action(/:id(.:format)))' + uri = '/content' + + match = path =~ uri + assert_equal %w{ controller action id format }, match.names + assert_equal 'content', match[1] + assert_nil match[2] + assert_nil match[3] + assert_nil match[4] + end + + def test_match_controller_action + path = Pattern.new '/:controller(/:action(/:id(.:format)))' + uri = '/content/list' + + match = path =~ uri + assert_equal %w{ controller action id format }, match.names + assert_equal 'content', match[1] + assert_equal 'list', match[2] + assert_nil match[3] + assert_nil match[4] + end + + def test_match_controller_action_id + path = Pattern.new '/:controller(/:action(/:id(.:format)))' + uri = '/content/list/10' + + match = path =~ uri + assert_equal %w{ controller action id format }, match.names + assert_equal 'content', match[1] + assert_equal 'list', match[2] + assert_equal '10', match[3] + assert_nil match[4] + end + + def test_match_literal + path = Path::Pattern.new "/books(/:action(.:format))" + + uri = '/books' + match = path =~ uri + assert_equal %w{ action format }, match.names + assert_nil match[1] + assert_nil match[2] + end + + def test_match_literal_with_action + path = Path::Pattern.new "/books(/:action(.:format))" + + uri = '/books/list' + match = path =~ uri + assert_equal %w{ action format }, match.names + assert_equal 'list', match[1] + assert_nil match[2] + end + + def test_match_literal_with_action_and_format + path = Path::Pattern.new "/books(/:action(.:format))" + + uri = '/books/list.rss' + match = path =~ uri + assert_equal %w{ action format }, match.names + assert_equal 'list', match[1] + assert_equal 'rss', match[2] + end + end + end + end +end diff --git a/actionpack/test/journey/route/definition/parser_test.rb b/actionpack/test/journey/route/definition/parser_test.rb new file mode 100644 index 0000000000..580235c6a1 --- /dev/null +++ b/actionpack/test/journey/route/definition/parser_test.rb @@ -0,0 +1,110 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + module Definition + class TestParser < MiniTest::Unit::TestCase + def setup + @parser = Parser.new + end + + def test_slash + assert_equal :SLASH, @parser.parse('/').type + assert_round_trip '/' + end + + def test_segment + assert_round_trip '/foo' + end + + def test_segments + assert_round_trip '/foo/bar' + end + + def test_segment_symbol + assert_round_trip '/foo/:id' + end + + def test_symbol + assert_round_trip '/:foo' + end + + def test_group + assert_round_trip '(/:foo)' + end + + def test_groups + assert_round_trip '(/:foo)(/:bar)' + end + + def test_nested_groups + assert_round_trip '(/:foo(/:bar))' + end + + def test_dot_symbol + assert_round_trip('.:format') + end + + def test_dot_literal + assert_round_trip('.xml') + end + + def test_segment_dot + assert_round_trip('/foo.:bar') + end + + def test_segment_group_dot + assert_round_trip('/foo(.:bar)') + end + + def test_segment_group + assert_round_trip('/foo(/:action)') + end + + def test_segment_groups + assert_round_trip('/foo(/:action)(/:bar)') + end + + def test_segment_nested_groups + assert_round_trip('/foo(/:action(/:bar))') + end + + def test_group_followed_by_path + assert_round_trip('/foo(/:action)/:bar') + end + + def test_star + assert_round_trip('*foo') + assert_round_trip('/*foo') + assert_round_trip('/bar/*foo') + assert_round_trip('/bar/(*foo)') + end + + def test_or + assert_round_trip('a|b') + assert_round_trip('a|b|c') + assert_round_trip('(a|b)|c') + assert_round_trip('a|(b|c)') + assert_round_trip('*a|(b|c)') + assert_round_trip('*a|:b|c') + end + + def test_arbitrary + assert_round_trip('/bar/*foo#') + end + + def test_literal_dot_paren + assert_round_trip "/sprockets.js(.:format)" + end + + def test_groups_with_dot + assert_round_trip "/(:locale)(.:format)" + end + + def assert_round_trip str + assert_equal str, @parser.parse(str).to_s + end + end + end + end +end diff --git a/actionpack/test/journey/route/definition/scanner_test.rb b/actionpack/test/journey/route/definition/scanner_test.rb new file mode 100644 index 0000000000..110baf9977 --- /dev/null +++ b/actionpack/test/journey/route/definition/scanner_test.rb @@ -0,0 +1,56 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + module Definition + class TestScanner < MiniTest::Unit::TestCase + def setup + @scanner = Scanner.new + end + + # /page/:id(/:action)(.:format) + def test_tokens + [ + ['/', [[:SLASH, '/']]], + ['*omg', [[:STAR, '*omg']]], + ['/page', [[:SLASH, '/'], [:LITERAL, 'page']]], + ['/~page', [[:SLASH, '/'], [:LITERAL, '~page']]], + ['/pa-ge', [[:SLASH, '/'], [:LITERAL, 'pa-ge']]], + ['/:page', [[:SLASH, '/'], [:SYMBOL, ':page']]], + ['/(:page)', [ + [:SLASH, '/'], + [:LPAREN, '('], + [:SYMBOL, ':page'], + [:RPAREN, ')'], + ]], + ['(/:action)', [ + [:LPAREN, '('], + [:SLASH, '/'], + [:SYMBOL, ':action'], + [:RPAREN, ')'], + ]], + ['(())', [[:LPAREN, '('], + [:LPAREN, '('], [:RPAREN, ')'], [:RPAREN, ')']]], + ['(.:format)', [ + [:LPAREN, '('], + [:DOT, '.'], + [:SYMBOL, ':format'], + [:RPAREN, ')'], + ]], + ].each do |str, expected| + @scanner.scan_setup str + assert_tokens expected, @scanner + end + end + + def assert_tokens tokens, scanner + toks = [] + while tok = scanner.next_token + toks << tok + end + assert_equal tokens, toks + end + end + end + end +end diff --git a/actionpack/test/journey/route_test.rb b/actionpack/test/journey/route_test.rb new file mode 100644 index 0000000000..b205db5fbc --- /dev/null +++ b/actionpack/test/journey/route_test.rb @@ -0,0 +1,103 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + class TestRoute < MiniTest::Unit::TestCase + def test_initialize + app = Object.new + path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))' + defaults = Object.new + route = Route.new("name", app, path, {}, defaults) + + assert_equal app, route.app + assert_equal path, route.path + assert_equal defaults, route.defaults + end + + def test_route_adds_itself_as_memo + app = Object.new + path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))' + defaults = Object.new + route = Route.new("name", app, path, {}, defaults) + + route.ast.grep(Nodes::Terminal).each do |node| + assert_equal route, node.memo + end + end + + def test_ip_address + path = Path::Pattern.new '/messages/:id(.:format)' + route = Route.new("name", nil, path, {:ip => '192.168.1.1'}, + { :controller => 'foo', :action => 'bar' }) + assert_equal '192.168.1.1', route.ip + end + + def test_default_ip + path = Path::Pattern.new '/messages/:id(.:format)' + route = Route.new("name", nil, path, {}, + { :controller => 'foo', :action => 'bar' }) + assert_equal(//, route.ip) + end + + def test_format_with_star + path = Path::Pattern.new '/:controller/*extra' + route = Route.new("name", nil, path, {}, + { :controller => 'foo', :action => 'bar' }) + assert_equal '/foo/himom', route.format({ + :controller => 'foo', + :extra => 'himom', + }) + end + + def test_connects_all_match + path = Path::Pattern.new '/:controller(/:action(/:id(.:format)))' + route = Route.new("name", nil, path, {:action => 'bar'}, { :controller => 'foo' }) + + assert_equal '/foo/bar/10', route.format({ + :controller => 'foo', + :action => 'bar', + :id => 10 + }) + end + + def test_extras_are_not_included_if_optional + path = Path::Pattern.new '/page/:id(/:action)' + route = Route.new("name", nil, path, { }, { :action => 'show' }) + + assert_equal '/page/10', route.format({ :id => 10 }) + end + + def test_extras_are_not_included_if_optional_with_parameter + path = Path::Pattern.new '(/sections/:section)/pages/:id' + route = Route.new("name", nil, path, { }, { :action => 'show' }) + + assert_equal '/pages/10', route.format({:id => 10}) + end + + def test_extras_are_not_included_if_optional_parameter_is_nil + path = Path::Pattern.new '(/sections/:section)/pages/:id' + route = Route.new("name", nil, path, { }, { :action => 'show' }) + + assert_equal '/pages/10', route.format({:id => 10, :section => nil}) + end + + def test_score + path = Path::Pattern.new "/page/:id(/:action)(.:format)" + specific = Route.new "name", nil, path, {}, {:controller=>"pages", :action=>"show"} + + path = Path::Pattern.new "/:controller(/:action(/:id))(.:format)" + generic = Route.new "name", nil, path, {} + + knowledge = {:id=>20, :controller=>"pages", :action=>"show"} + + routes = [specific, generic] + + refute_equal specific.score(knowledge), generic.score(knowledge) + + found = routes.sort_by { |r| r.score(knowledge) }.last + + assert_equal specific, found + end + end + end +end diff --git a/actionpack/test/journey/router/strexp_test.rb b/actionpack/test/journey/router/strexp_test.rb new file mode 100644 index 0000000000..9e0337f144 --- /dev/null +++ b/actionpack/test/journey/router/strexp_test.rb @@ -0,0 +1,32 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + class Router + class TestStrexp < MiniTest::Unit::TestCase + def test_many_names + exp = Strexp.new( + "/:controller(/:action(/:id(.:format)))", + {:controller=>/.+?/}, + ["/", ".", "?"], + true) + + assert_equal ["controller", "action", "id", "format"], exp.names + end + + def test_names + { + "/bar(.:format)" => %w{ format }, + ":format" => %w{ format }, + ":format-" => %w{ format }, + ":format0" => %w{ format0 }, + ":format1,:format2" => %w{ format1 format2 }, + }.each do |string, expected| + exp = Strexp.new(string, {}, ["/", ".", "?"]) + assert_equal expected, exp.names + end + end + end + end + end +end diff --git a/actionpack/test/journey/router/utils_test.rb b/actionpack/test/journey/router/utils_test.rb new file mode 100644 index 0000000000..97a6449c99 --- /dev/null +++ b/actionpack/test/journey/router/utils_test.rb @@ -0,0 +1,21 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + class Router + class TestUtils < MiniTest::Unit::TestCase + def test_path_escape + assert_equal "a/b%20c+d", Utils.escape_path("a/b c+d") + end + + def test_fragment_escape + assert_equal "a/b%20c+d?e", Utils.escape_fragment("a/b c+d?e") + end + + def test_uri_unescape + assert_equal "a/b c+d", Utils.unescape_uri("a%2Fb%20c+d") + end + end + end + end +end diff --git a/actionpack/test/journey/router_test.rb b/actionpack/test/journey/router_test.rb new file mode 100644 index 0000000000..1b64600ba8 --- /dev/null +++ b/actionpack/test/journey/router_test.rb @@ -0,0 +1,575 @@ +# encoding: UTF-8 +require 'abstract_unit' + +module ActionDispatch + module Journey + class TestRouter < MiniTest::Unit::TestCase + attr_reader :routes + + def setup + @routes = Routes.new + @router = Router.new(@routes, {}) + @formatter = Formatter.new(@routes) + end + + def test_request_class_reader + klass = Object.new + router = Router.new(routes, :request_class => klass) + assert_equal klass, router.request_class + end + + class FakeRequestFeeler < Struct.new(:env, :called) + def new env + self.env = env + self + end + + def hello + self.called = true + 'world' + end + + def path_info; env['PATH_INFO']; end + def request_method; env['REQUEST_METHOD']; end + def ip; env['REMOTE_ADDR']; end + end + + def test_dashes + router = Router.new(routes, {}) + + exp = Router::Strexp.new '/foo-bar-baz', {}, ['/.?'] + path = Path::Pattern.new exp + + routes.add_route nil, path, {}, {:id => nil}, {} + + env = rails_env 'PATH_INFO' => '/foo-bar-baz' + called = false + router.recognize(env) do |r, _, params| + called = true + end + assert called + end + + def test_unicode + router = Router.new(routes, {}) + + #match the escaped version of /ほげ + exp = Router::Strexp.new '/%E3%81%BB%E3%81%92', {}, ['/.?'] + path = Path::Pattern.new exp + + routes.add_route nil, path, {}, {:id => nil}, {} + + env = rails_env 'PATH_INFO' => '/%E3%81%BB%E3%81%92' + called = false + router.recognize(env) do |r, _, params| + called = true + end + assert called + end + + def test_request_class_and_requirements_success + klass = FakeRequestFeeler.new nil + router = Router.new(routes, {:request_class => klass }) + + requirements = { :hello => /world/ } + + exp = Router::Strexp.new '/foo(/:id)', {}, ['/.?'] + path = Path::Pattern.new exp + + routes.add_route nil, path, requirements, {:id => nil}, {} + + env = rails_env 'PATH_INFO' => '/foo/10' + router.recognize(env) do |r, _, params| + assert_equal({:id => '10'}, params) + end + + assert klass.called, 'hello should have been called' + assert_equal env.env, klass.env + end + + def test_request_class_and_requirements_fail + klass = FakeRequestFeeler.new nil + router = Router.new(routes, {:request_class => klass }) + + requirements = { :hello => /mom/ } + + exp = Router::Strexp.new '/foo(/:id)', {}, ['/.?'] + path = Path::Pattern.new exp + + router.routes.add_route nil, path, requirements, {:id => nil}, {} + + env = rails_env 'PATH_INFO' => '/foo/10' + router.recognize(env) do |r, _, params| + flunk 'route should not be found' + end + + assert klass.called, 'hello should have been called' + assert_equal env.env, klass.env + end + + class CustomPathRequest < Router::NullReq + def path_info + env['custom.path_info'] + end + end + + def test_request_class_overrides_path_info + router = Router.new(routes, {:request_class => CustomPathRequest }) + + exp = Router::Strexp.new '/bar', {}, ['/.?'] + path = Path::Pattern.new exp + + routes.add_route nil, path, {}, {}, {} + + env = rails_env 'PATH_INFO' => '/foo', 'custom.path_info' => '/bar' + + recognized = false + router.recognize(env) do |r, _, params| + recognized = true + end + + assert recognized, "route should have been recognized" + end + + def test_regexp_first_precedence + add_routes @router, [ + Router::Strexp.new("/whois/:domain", {:domain => /\w+\.[\w\.]+/}, ['/', '.', '?']), + Router::Strexp.new("/whois/:id(.:format)", {}, ['/', '.', '?']) + ] + + env = rails_env 'PATH_INFO' => '/whois/example.com' + + list = [] + @router.recognize(env) do |r, _, params| + list << r + end + assert_equal 2, list.length + + r = list.first + + assert_equal '/whois/:domain', r.path.spec.to_s + end + + def test_required_parts_verified_are_anchored + add_routes @router, [ + Router::Strexp.new("/foo/:id", { :id => /\d/ }, ['/', '.', '?'], false) + ] + + assert_raises(Router::RoutingError) do + @formatter.generate(:path_info, nil, { :id => '10' }, { }) + end + end + + def test_required_parts_are_verified_when_building + add_routes @router, [ + Router::Strexp.new("/foo/:id", { :id => /\d+/ }, ['/', '.', '?'], false) + ] + + path, _ = @formatter.generate(:path_info, nil, { :id => '10' }, { }) + assert_equal '/foo/10', path + + assert_raises(Router::RoutingError) do + @formatter.generate(:path_info, nil, { :id => 'aa' }, { }) + end + end + + def test_only_required_parts_are_verified + add_routes @router, [ + Router::Strexp.new("/foo(/:id)", {:id => /\d/}, ['/', '.', '?'], false) + ] + + path, _ = @formatter.generate(:path_info, nil, { :id => '10' }, { }) + assert_equal '/foo/10', path + + path, _ = @formatter.generate(:path_info, nil, { }, { }) + assert_equal '/foo', path + + path, _ = @formatter.generate(:path_info, nil, { :id => 'aa' }, { }) + assert_equal '/foo/aa', path + end + + def test_knows_what_parts_are_missing_from_named_route + route_name = "gorby_thunderhorse" + pattern = Router::Strexp.new("/foo/:id", { :id => /\d+/ }, ['/', '.', '?'], false) + path = Path::Pattern.new pattern + @router.routes.add_route nil, path, {}, {}, route_name + + error = assert_raises(Router::RoutingError) do + @formatter.generate(:path_info, route_name, { }, { }) + end + + assert_match(/required keys: \[:id\]/, error.message) + end + + def test_X_Cascade + add_routes @router, [ "/messages(.:format)" ] + resp = @router.call({ 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/lol' }) + assert_equal ['Not Found'], resp.last + assert_equal 'pass', resp[1]['X-Cascade'] + assert_equal 404, resp.first + end + + def test_clear_trailing_slash_from_script_name_on_root_unanchored_routes + strexp = Router::Strexp.new("/", {}, ['/', '.', '?'], false) + path = Path::Pattern.new strexp + app = lambda { |env| [200, {}, ['success!']] } + @router.routes.add_route(app, path, {}, {}, {}) + + env = rack_env('SCRIPT_NAME' => '', 'PATH_INFO' => '/weblog') + resp = @router.call(env) + assert_equal ['success!'], resp.last + assert_equal '', env['SCRIPT_NAME'] + end + + def test_defaults_merge_correctly + path = Path::Pattern.new '/foo(/:id)' + @router.routes.add_route nil, path, {}, {:id => nil}, {} + + env = rails_env 'PATH_INFO' => '/foo/10' + @router.recognize(env) do |r, _, params| + assert_equal({:id => '10'}, params) + end + + env = rails_env 'PATH_INFO' => '/foo' + @router.recognize(env) do |r, _, params| + assert_equal({:id => nil}, params) + end + end + + def test_recognize_with_unbound_regexp + add_routes @router, [ + Router::Strexp.new("/foo", { }, ['/', '.', '?'], false) + ] + + env = rails_env 'PATH_INFO' => '/foo/bar' + + @router.recognize(env) { |*_| } + + assert_equal '/foo', env.env['SCRIPT_NAME'] + assert_equal '/bar', env.env['PATH_INFO'] + end + + def test_bound_regexp_keeps_path_info + add_routes @router, [ + Router::Strexp.new("/foo", { }, ['/', '.', '?'], true) + ] + + env = rails_env 'PATH_INFO' => '/foo' + + before = env.env['SCRIPT_NAME'] + + @router.recognize(env) { |*_| } + + assert_equal before, env.env['SCRIPT_NAME'] + assert_equal '/foo', env.env['PATH_INFO'] + end + + def test_path_not_found + add_routes @router, [ + "/messages(.:format)", + "/messages/new(.:format)", + "/messages/:id/edit(.:format)", + "/messages/:id(.:format)" + ] + env = rails_env 'PATH_INFO' => '/messages/unknown/path' + yielded = false + + @router.recognize(env) do |*whatever| + yielded = true + end + refute yielded + end + + def test_required_part_in_recall + add_routes @router, [ "/messages/:a/:b" ] + + path, _ = @formatter.generate(:path_info, nil, { :a => 'a' }, { :b => 'b' }) + assert_equal "/messages/a/b", path + end + + def test_splat_in_recall + add_routes @router, [ "/*path" ] + + path, _ = @formatter.generate(:path_info, nil, { }, { :path => 'b' }) + assert_equal "/b", path + end + + def test_recall_should_be_used_when_scoring + add_routes @router, [ + "/messages/:action(/:id(.:format))", + "/messages/:id(.:format)" + ] + + path, _ = @formatter.generate(:path_info, nil, { :id => 10 }, { :action => 'index' }) + assert_equal "/messages/index/10", path + end + + def test_nil_path_parts_are_ignored + path = Path::Pattern.new "/:controller(/:action(.:format))" + @router.routes.add_route nil, path, {}, {}, {} + + params = { :controller => "tasks", :format => nil } + extras = { :action => 'lol' } + + path, _ = @formatter.generate(:path_info, nil, params, extras) + assert_equal '/tasks', path + end + + def test_generate_slash + params = [ [:controller, "tasks"], + [:action, "show"] ] + str = Router::Strexp.new("/", Hash[params], ['/', '.', '?'], true) + path = Path::Pattern.new str + + @router.routes.add_route nil, path, {}, {}, {} + + path, _ = @formatter.generate(:path_info, nil, Hash[params], {}) + assert_equal '/', path + end + + def test_generate_calls_param_proc + path = Path::Pattern.new '/:controller(/:action)' + @router.routes.add_route nil, path, {}, {}, {} + + parameterized = [] + params = [ [:controller, "tasks"], + [:action, "show"] ] + + @formatter.generate( + :path_info, + nil, + Hash[params], + {}, + lambda { |k,v| parameterized << [k,v]; v }) + + assert_equal params.map(&:to_s).sort, parameterized.map(&:to_s).sort + end + + def test_generate_id + path = Path::Pattern.new '/:controller(/:action)' + @router.routes.add_route nil, path, {}, {}, {} + + path, params = @formatter.generate( + :path_info, nil, {:id=>1, :controller=>"tasks", :action=>"show"}, {}) + assert_equal '/tasks/show', path + assert_equal({:id => 1}, params) + end + + def test_generate_escapes + path = Path::Pattern.new '/:controller(/:action)' + @router.routes.add_route nil, path, {}, {}, {} + + path, _ = @formatter.generate(:path_info, + nil, { :controller => "tasks", + :action => "a/b c+d", + }, {}) + assert_equal '/tasks/a/b%20c+d', path + end + + def test_generate_extra_params + path = Path::Pattern.new '/:controller(/:action)' + @router.routes.add_route nil, path, {}, {}, {} + + path, params = @formatter.generate(:path_info, + nil, { :id => 1, + :controller => "tasks", + :action => "show", + :relative_url_root => nil + }, {}) + assert_equal '/tasks/show', path + assert_equal({:id => 1, :relative_url_root => nil}, params) + end + + def test_generate_uses_recall_if_needed + path = Path::Pattern.new '/:controller(/:action(/:id))' + @router.routes.add_route nil, path, {}, {}, {} + + path, params = @formatter.generate(:path_info, + nil, + {:controller =>"tasks", :id => 10}, + {:action =>"index"}) + assert_equal '/tasks/index/10', path + assert_equal({}, params) + end + + def test_generate_with_name + path = Path::Pattern.new '/:controller(/:action)' + @router.routes.add_route nil, path, {}, {}, {} + + path, params = @formatter.generate(:path_info, + "tasks", + {:controller=>"tasks"}, + {:controller=>"tasks", :action=>"index"}) + assert_equal '/tasks', path + assert_equal({}, params) + end + + { + '/content' => { :controller => 'content' }, + '/content/list' => { :controller => 'content', :action => 'list' }, + '/content/show/10' => { :controller => 'content', :action => 'show', :id => "10" }, + }.each do |request_path, expected| + define_method("test_recognize_#{expected.keys.map(&:to_s).join('_')}") do + path = Path::Pattern.new "/:controller(/:action(/:id))" + app = Object.new + route = @router.routes.add_route(app, path, {}, {}, {}) + + env = rails_env 'PATH_INFO' => request_path + called = false + + @router.recognize(env) do |r, _, params| + assert_equal route, r + assert_equal(expected, params) + called = true + end + + assert called + end + end + + { + :segment => ['/a%2Fb%20c+d/splat', { :segment => 'a/b c+d', :splat => 'splat' }], + :splat => ['/segment/a/b%20c+d', { :segment => 'segment', :splat => 'a/b c+d' }] + }.each do |name, (request_path, expected)| + define_method("test_recognize_#{name}") do + path = Path::Pattern.new '/:segment/*splat' + app = Object.new + route = @router.routes.add_route(app, path, {}, {}, {}) + + env = rails_env 'PATH_INFO' => request_path + called = false + + @router.recognize(env) do |r, _, params| + assert_equal route, r + assert_equal(expected, params) + called = true + end + + assert called + end + end + + def test_namespaced_controller + strexp = Router::Strexp.new( + "/:controller(/:action(/:id))", + { :controller => /.+?/ }, + ["/", ".", "?"] + ) + path = Path::Pattern.new strexp + app = Object.new + route = @router.routes.add_route(app, path, {}, {}, {}) + + env = rails_env 'PATH_INFO' => '/admin/users/show/10' + called = false + expected = { + :controller => 'admin/users', + :action => 'show', + :id => '10' + } + + @router.recognize(env) do |r, _, params| + assert_equal route, r + assert_equal(expected, params) + called = true + end + assert called + end + + def test_recognize_literal + path = Path::Pattern.new "/books(/:action(.:format))" + app = Object.new + route = @router.routes.add_route(app, path, {}, {:controller => 'books'}) + + env = rails_env 'PATH_INFO' => '/books/list.rss' + expected = { :controller => 'books', :action => 'list', :format => 'rss' } + called = false + @router.recognize(env) do |r, _, params| + assert_equal route, r + assert_equal(expected, params) + called = true + end + + assert called + end + + def test_recognize_head_request_as_get_route + path = Path::Pattern.new "/books(/:action(.:format))" + app = Object.new + conditions = { + :request_method => 'GET' + } + @router.routes.add_route(app, path, conditions, {}) + + env = rails_env 'PATH_INFO' => '/books/list.rss', + "REQUEST_METHOD" => "HEAD" + + called = false + @router.recognize(env) do |r, _, params| + called = true + end + + assert called + end + + def test_recognize_cares_about_verbs + path = Path::Pattern.new "/books(/:action(.:format))" + app = Object.new + conditions = { + :request_method => 'GET' + } + @router.routes.add_route(app, path, conditions, {}) + + conditions = conditions.dup + conditions[:request_method] = 'POST' + + post = @router.routes.add_route(app, path, conditions, {}) + + env = rails_env 'PATH_INFO' => '/books/list.rss', + "REQUEST_METHOD" => "POST" + + called = false + @router.recognize(env) do |r, _, params| + assert_equal post, r + called = true + end + + assert called + end + + private + + def add_routes router, paths + paths.each do |path| + path = Path::Pattern.new path + router.routes.add_route nil, path, {}, {}, {} + end + end + + RailsEnv = Struct.new(:env) + + def rails_env env + RailsEnv.new rack_env env + end + + def rack_env env + { + "rack.version" => [1, 1], + "rack.input" => StringIO.new, + "rack.errors" => StringIO.new, + "rack.multithread" => true, + "rack.multiprocess" => true, + "rack.run_once" => false, + "REQUEST_METHOD" => "GET", + "SERVER_NAME" => "example.org", + "SERVER_PORT" => "80", + "QUERY_STRING" => "", + "PATH_INFO" => "/content", + "rack.url_scheme" => "http", + "HTTPS" => "off", + "SCRIPT_NAME" => "", + "CONTENT_LENGTH" => "0" + }.merge env + end + end + end +end diff --git a/actionpack/test/journey/routes_test.rb b/actionpack/test/journey/routes_test.rb new file mode 100644 index 0000000000..3b17bd53b7 --- /dev/null +++ b/actionpack/test/journey/routes_test.rb @@ -0,0 +1,53 @@ +require 'abstract_unit' + +module ActionDispatch + module Journey + class TestRoutes < MiniTest::Unit::TestCase + def test_clear + routes = Routes.new + exp = Router::Strexp.new '/foo(/:id)', {}, ['/.?'] + path = Path::Pattern.new exp + requirements = { :hello => /world/ } + + routes.add_route nil, path, requirements, {:id => nil}, {} + assert_equal 1, routes.length + + routes.clear + assert_equal 0, routes.length + end + + def test_ast + routes = Routes.new + path = Path::Pattern.new '/hello' + + routes.add_route nil, path, {}, {}, {} + ast = routes.ast + routes.add_route nil, path, {}, {}, {} + refute_equal ast, routes.ast + end + + def test_simulator_changes + routes = Routes.new + path = Path::Pattern.new '/hello' + + routes.add_route nil, path, {}, {}, {} + sim = routes.simulator + routes.add_route nil, path, {}, {}, {} + refute_equal sim, routes.simulator + end + + def test_first_name_wins + #def add_route app, path, conditions, defaults, name = nil + routes = Routes.new + + one = Path::Pattern.new '/hello' + two = Path::Pattern.new '/aaron' + + routes.add_route nil, one, {}, {}, 'aaron' + routes.add_route nil, two, {}, {}, 'aaron' + + assert_equal '/hello', routes.named_routes['aaron'].path.spec.to_s + end + end + end +end diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index eb1a54a81f..82c9d383ac 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -358,6 +358,17 @@ class AssetTagHelperTest < ActionView::TestCase assert javascript_include_tag("prototype").html_safe? end + def test_javascript_include_tag_relative_protocol + @controller.config.asset_host = "assets.example.com" + assert_dom_equal %(<script src="//assets.example.com/javascripts/prototype.js"></script>), javascript_include_tag('prototype', protocol: :relative) + end + + def test_javascript_include_tag_default_protocol + @controller.config.asset_host = "assets.example.com" + @controller.config.default_asset_host_protocol = :relative + assert_dom_equal %(<script src="//assets.example.com/javascripts/prototype.js"></script>), javascript_include_tag('prototype') + end + def test_stylesheet_path StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end @@ -398,7 +409,18 @@ class AssetTagHelperTest < ActionView::TestCase end def test_stylesheet_link_tag_should_not_output_the_same_asset_twice - assert_dom_equal %(<link href="/stylesheets/wellington.css" media="screen" rel="stylesheet" />\n<link href="/stylesheets/amsterdam.css" media="screen" rel="stylesheet" />), stylesheet_link_tag('wellington', 'wellington', 'amsterdam') + assert_dom_equal %(<link href="/stylesheets/wellington.css" media="screen" rel="stylesheet" />\n<link href="/stylesheets/amsterdam.css" media="screen" rel="stylesheet" />), stylesheet_link_tag('wellington', 'wellington', 'amsterdam') + end + + def test_stylesheet_link_tag_with_relative_protocol + @controller.config.asset_host = "assets.example.com" + assert_dom_equal %(<link href="//assets.example.com/stylesheets/wellington.css" media="screen" rel="stylesheet" />), stylesheet_link_tag('wellington', protocol: :relative) + end + + def test_stylesheet_link_tag_with_default_protocol + @controller.config.asset_host = "assets.example.com" + @controller.config.default_asset_host_protocol = :relative + assert_dom_equal %(<link href="//assets.example.com/stylesheets/wellington.css" media="screen" rel="stylesheet" />), stylesheet_link_tag('wellington') end def test_image_path diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 1bb625213d..ba65349b6a 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -517,16 +517,6 @@ class UrlHelperTest < ActiveSupport::TestCase mail_to("david@loudthinking.com", "David Heinemeier Hansson", class: "admin") end - def test_mail_to_with_javascript - snippet = mail_to("me@domain.com", "My email", encode: "javascript") - assert_dom_equal "<script>eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%5c%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%5c%22%3e%4d%79%20%65%6d%61%69%6c%3c%5c%2f%61%3e%27%29%3b'))</script>", snippet - end - - def test_mail_to_with_javascript_unicode - snippet = mail_to("unicode@example.com", "únicode", encode: "javascript") - assert_dom_equal "<script>eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%5c%22%6d%61%69%6c%74%6f%3a%75%6e%69%63%6f%64%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%5c%22%3e%c3%ba%6e%69%63%6f%64%65%3c%5c%2f%61%3e%27%29%3b'))</script>", snippet - end - def test_mail_with_options assert_dom_equal( %{<a href="mailto:me@example.com?cc=ccaddress%40example.com&bcc=bccaddress%40example.com&body=This%20is%20the%20body%20of%20the%20message.&subject=This%20is%20an%20example%20email">My email</a>}, @@ -539,54 +529,8 @@ class UrlHelperTest < ActiveSupport::TestCase mail_to('feedback@example.com', '<img src="/feedback.png" />'.html_safe) end - def test_mail_to_with_hex - assert_dom_equal( - %{<a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>}, - mail_to("me@domain.com", "My email", encode: "hex") - ) - - assert_dom_equal( - %{<a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">me@domain.com</a>}, - mail_to("me@domain.com", nil, encode: "hex") - ) - end - - def test_mail_to_with_replace_options - assert_dom_equal( - %{<a href="mailto:wolfgang@stufenlos.net">wolfgang(at)stufenlos(dot)net</a>}, - mail_to("wolfgang@stufenlos.net", nil, replace_at: "(at)", replace_dot: "(dot)") - ) - - assert_dom_equal( - %{<a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">me(at)domain.com</a>}, - mail_to("me@domain.com", nil, encode: "hex", replace_at: "(at)") - ) - - assert_dom_equal( - %{<a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>}, - mail_to("me@domain.com", "My email", encode: "hex", replace_at: "(at)") - ) - - assert_dom_equal( - %{<a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">me(at)domain(dot)com</a>}, - mail_to("me@domain.com", nil, encode: "hex", replace_at: "(at)", replace_dot: "(dot)") - ) - - assert_dom_equal( - %{<script>eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%5c%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%5c%22%3e%4d%79%20%65%6d%61%69%6c%3c%5c%2f%61%3e%27%29%3b'))</script>}, - mail_to("me@domain.com", "My email", encode: "javascript", replace_at: "(at)", replace_dot: "(dot)") - ) - - assert_dom_equal( - %{<script>eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%5c%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%5c%22%3e%6d%65%28%61%74%29%64%6f%6d%61%69%6e%28%64%6f%74%29%63%6f%6d%3c%5c%2f%61%3e%27%29%3b'))</script>}, - mail_to("me@domain.com", nil, encode: "javascript", replace_at: "(at)", replace_dot: "(dot)") - ) - end - def test_mail_to_returns_html_safe_string assert mail_to("david@loudthinking.com").html_safe? - assert mail_to("me@domain.com", "My email", encode: "javascript").html_safe? - assert mail_to("me@domain.com", "My email", encode: "hex").html_safe? end def protect_against_forgery? diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index b955e98835..09e6ede064 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,5 +1,24 @@ ## Rails 4.0.0 (unreleased) ## +* Add `ActiveModel::Validations::AbsenceValidator`, a validator to check the + absence of attributes. + + class Person + include ActiveModel::Validations + + attr_accessor :first_name + validates_absence_of :first_name + end + + person = Person.new + person.first_name = "John" + person.valid? + # => false + person.errors.messages + # => {:first_name=>["must be blank"]} + + *Roberto Vasquez Angel* + * `[attribute]_changed?` now returns `false` after a call to `reset_[attribute]!` *Renato Mascarenhas* @@ -19,7 +38,7 @@ *Yves Senn* -* Use BCrypt's MIN_COST in the test environment for speedier tests when using `has_secure_pasword`. +* Use BCrypt's `MIN_COST` in the test environment for speedier tests when using `has_secure_pasword`. *Brian Cardarella + Jeremy Kemper + Trevor Turk* diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index 018a60ba90..51655fe3da 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'].select { |path| File.file? path } + s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] s.require_path = 'lib' s.add_dependency 'activesupport', version diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index af11da1351..db5759ada9 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -1,3 +1,4 @@ +require 'thread_safe' module ActiveModel # Raised when an attribute is not defined. @@ -337,17 +338,17 @@ module ActiveModel # significantly (in our case our test suite finishes 10% faster with # this cache). def attribute_method_matchers_cache #:nodoc: - @attribute_method_matchers_cache ||= {} + @attribute_method_matchers_cache ||= ThreadSafe::Cache.new(:initial_capacity => 4) end def attribute_method_matcher(method_name) #:nodoc: - attribute_method_matchers_cache.fetch(method_name) do |name| + attribute_method_matchers_cache.compute_if_absent(method_name) do # Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix # will match every time. matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1) match = nil - matchers.detect { |method| match = method.match(name) } - attribute_method_matchers_cache[name] = match + matchers.detect { |method| match = method.match(method_name) } + match end end diff --git a/activemodel/lib/active_model/locale/en.yml b/activemodel/lib/active_model/locale/en.yml index d17848c861..540e8132d3 100644 --- a/activemodel/lib/active_model/locale/en.yml +++ b/activemodel/lib/active_model/locale/en.yml @@ -13,6 +13,7 @@ en: accepted: "must be accepted" empty: "can't be empty" blank: "can't be blank" + present: "must be blank" too_long: "is too long (maximum is %{count} characters)" too_short: "is too short (minimum is %{count} characters)" wrong_length: "is the wrong length (should be %{count} characters)" diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index 4a17a63e20..648ae7ce3d 100755 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -2,6 +2,7 @@ require 'active_support/core_ext/class/attribute_accessors' require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/hash/conversions' require 'active_support/core_ext/hash/slice' +require 'active_support/core_ext/time/acts_like' module ActiveModel module Serializers @@ -20,7 +21,11 @@ module ActiveModel def initialize(name, serializable, value) @name, @serializable = name, serializable - value = value.in_time_zone if value.respond_to?(:in_time_zone) + + if value.acts_like?(:time) && value.respond_to?(:in_time_zone) + value = value.in_time_zone + end + @value = value @type = compute_type end diff --git a/activemodel/lib/active_model/validations/absence.rb b/activemodel/lib/active_model/validations/absence.rb new file mode 100644 index 0000000000..1a1863370b --- /dev/null +++ b/activemodel/lib/active_model/validations/absence.rb @@ -0,0 +1,31 @@ +module ActiveModel + module Validations + # == Active Model Absence Validator + class AbsenceValidator < EachValidator #:nodoc: + def validate_each(record, attr_name, value) + record.errors.add(attr_name, :present, options) if value.present? + end + end + + module HelperMethods + # Validates that the specified attributes are blank (as defined by + # Object#blank?). Happens by default on save. + # + # class Person < ActiveRecord::Base + # validates_absence_of :first_name + # end + # + # The first_name attribute must be in the object and it must be blank. + # + # Configuration options: + # * <tt>:message</tt> - A custom error message (default is: "must be blank"). + # + # There is also a list of default options supported by every validator: + # +:if+, +:unless+, +:on+ and +:strict+. + # See <tt>ActiveModel::Validation#validates</tt> for more information + def validates_absence_of(*attr_names) + validates_with AbsenceValidator, _merge_attributes(attr_names) + end + end + end +end diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index ff3dfa01c8..d51f4d1936 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -110,7 +110,7 @@ module ActiveModel # Return the kind for this validator. # # PresenceValidator.new.kind # => :presence - # UniquenessValidator.new.kind # => :uniqueness + # UniquenessValidator.new.kind # => :uniqueness def kind self.class.kind end diff --git a/activemodel/test/cases/validations/absence_validation_test.rb b/activemodel/test/cases/validations/absence_validation_test.rb new file mode 100644 index 0000000000..c05d71de5a --- /dev/null +++ b/activemodel/test/cases/validations/absence_validation_test.rb @@ -0,0 +1,67 @@ +# encoding: utf-8 +require 'cases/helper' +require 'models/topic' +require 'models/person' +require 'models/custom_reader' + +class AbsenceValidationTest < ActiveModel::TestCase + teardown do + Topic.reset_callbacks(:validate) + Person.reset_callbacks(:validate) + CustomReader.reset_callbacks(:validate) + end + + def test_validate_absences + Topic.validates_absence_of(:title, :content) + t = Topic.new + t.title = "foo" + t.content = "bar" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:title] + assert_equal ["must be blank"], t.errors[:content] + t.title = "" + t.content = "something" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:content] + t.content = "" + assert t.valid? + end + + def test_accepts_array_arguments + Topic.validates_absence_of %w(title content) + t = Topic.new + t.title = "foo" + t.content = "bar" + assert t.invalid? + assert_equal ["must be blank"], t.errors[:title] + assert_equal ["must be blank"], t.errors[:content] + end + + def test_validates_acceptance_of_with_custom_error_using_quotes + Person.validates_absence_of :karma, message: "This string contains 'single' and \"double\" quotes" + p = Person.new + p.karma = "good" + assert p.invalid? + assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last + end + + def test_validates_absence_of_for_ruby_class + Person.validates_absence_of :karma + p = Person.new + p.karma = "good" + assert p.invalid? + assert_equal ["must be blank"], p.errors[:karma] + p.karma = nil + assert p.valid? + end + + def test_validates_absence_of_for_ruby_class_with_custom_reader + CustomReader.validates_absence_of :karma + p = CustomReader.new + p[:karma] = "excellent" + assert p.invalid? + assert_equal ["must be blank"], p.errors[:karma] + p[:karma] = "" + assert p.valid? + end +end diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index ab8c54e4cb..272cef6fcf 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,71 @@ ## Rails 4.0.0 (unreleased) ## +* Improve ways to write `change` migrations, making the old `up` & `down` methods no longer necessary. + + * The methods `drop_table` and `remove_column` are now reversible, as long as the necessary information is given. + The method `remove_column` used to accept multiple column names; instead use `remove_columns` (which is not revertible). + The method `change_table` is also reversible, as long as its block doesn't call `remove`, `change` or `change_default` + + * New method `reversible` makes it possible to specify code to be run when migrating up or down. + See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/migrations.md#using-the-reversible-method) + + * New method `revert` will revert a whole migration or the given block. + If migrating down, the given migration / block is run normally. + See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/migrations.md#reverting-previous-migrations) + + Attempting to revert the methods `execute`, `remove_columns` and `change_column` will now raise an IrreversibleMigration instead of actually executing them without any output. + + *Marc-André Lafortune* + +* Serialized attributes can be serialized in integer columns. + Fix #8575. + + *Rafael Mendonça França* + +* Keep index names when using `alter_table` with sqlite3. + Fix #3489. + + *Yves Senn* + +* Add ability for postgresql adapter to disable user triggers in `disable_referential_integrity`. + Fix #5523. + + *Gary S. Weaver* + +* Added support for `validates_uniqueness_of` in PostgreSQL array columns. + Fixes #8075. + + *Pedro Padron* + +* Allow int4range and int8range columns to be created in PostgreSQL and properly convert to/from database. + + *Alexey Vasiliev aka leopard* + +* Do not log the binding values for binary columns. + + *Matthew M. Boedicker* + +* Fix counter cache columns not updated when replacing `has_many :through` + associations. + + *Matthew Robertson* + +* Recognize migrations placed in directories containing numbers and 'rb'. + Fix #8492 + + *Yves Senn* + +* Add `ActiveRecord::Base.cache_timestamp_format` class attribute to control + the format of the timestamp value in the cache key. + This allows users to improve the precision of the cache key. + Fixes #8195. + + *Rafael Mendonça França* + +* Add `:nsec` date format. This can be used to improve the precision of cache key. + + *Jamie Gaskins* + * Session variables can be set for the `mysql`, `mysql2`, and `postgresql` adapters in the `variables: <hash>` parameter in `database.yml`. The key-value pairs of this hash will be sent in a `SET key = value` query on new database connections. See also: @@ -46,22 +112,6 @@ *Chris Feist* -* Add migration history to `schema.rb` dump. Loading `schema.rb` with full migration - history restores the exact list of migrations that created that schema (including names - and fingerprints). This avoids possible mistakes caused by assuming all migrations with - a lower version have been run when loading `schema.rb`. Old `schema.rb` files without - migration history but with the `:version` setting still work as before. - - *Josh Susser* - -* Add metadata columns to `schema_migrations` table. New columns are: - - * `migrated_at`: timestamp - * `fingerprint`: md5 hash of migration source - * `name`: filename minus version and extension - - *Josh Susser* - * Fix performance problem with `primary_key` method in PostgreSQL adapter when having many schemas. Uses `pg_constraint` table instead of `pg_depend` table which has many records in general. Fix #8414 @@ -75,8 +125,8 @@ *Yves Senn* * Add STI support to init and building associations. - Allows you to do `BaseClass.new(:type => "SubClass")` as well as - `parent.children.build(:type => "SubClass")` or `parent.build_child` + Allows you to do `BaseClass.new(type: "SubClass")` as well as + `parent.children.build(type: "SubClass")` or `parent.build_child` to initialize an STI subclass. Ensures that the class name is a valid class and that it is in the ancestors of the super class that the association is expecting. @@ -107,7 +157,7 @@ * Fix postgresql adapter to handle BC timestamps correctly - HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years) + HistoryEvent.create!(name: "something", occured_at: Date.new(0) - 5.years) *Bogdan Gusiev* @@ -738,7 +788,7 @@ *kennyj* -* Changed validates_presence_of on an association so that children objects +* Changed `validates_presence_of` on an association so that children objects do not validate as being present if they are marked for destruction. This prevents you from saving the parent successfully and thus putting the parent in an invalid state. @@ -755,7 +805,7 @@ def change create_table :foobars do |t| - t.timestamps :precision => 0 + t.timestamps precision: 0 end end diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index 2df874b525..31ddb01123 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'examples/**/*', 'lib/**/*'].select { |path| File.file? path } + s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'examples/**/*', 'lib/**/*'] s.require_path = 'lib' s.extra_rdoc_files = %w(README.rdoc) diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index c7d8a84a7e..c3266f2bb4 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -153,6 +153,11 @@ module ActiveRecord delete_through_records(records) + if source_reflection.options[:counter_cache] + counter = source_reflection.counter_cache_column + klass.decrement_counter counter, records.map(&:id) + end + if through_reflection.macro == :has_many && update_through_counter?(method) update_counter(-count, through_reflection) end diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb index 6c5e2ac05d..ecfa556ab4 100644 --- a/activerecord/lib/active_record/attribute_assignment.rb +++ b/activerecord/lib/active_record/attribute_assignment.rb @@ -132,7 +132,7 @@ module ActiveRecord if object.class.send(:create_time_zone_conversion_attribute?, name, column) Time.zone.local(*set_values) else - Time.time_with_datetime_fallback(object.class.default_timezone, *set_values) + Time.send(object.class.default_timezone, *set_values) end end diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index 47d4a938af..25d62fdb85 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -112,6 +112,14 @@ module ActiveRecord end end + def _field_changed?(attr, old, value) + if self.class.serialized_attributes.include?(attr) + old != value + else + super + end + end + def read_attribute_before_type_cast(attr_name) if self.class.serialized_attributes.include?(attr_name) super.unserialized_value diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb index b5a8011ca4..82d0cf7e2e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -1,4 +1,5 @@ require 'thread' +require 'thread_safe' require 'monitor' require 'set' require 'active_support/deprecation' @@ -236,9 +237,6 @@ module ActiveRecord @spec = spec - # The cache of reserved connections mapped to threads - @reserved_connections = {} - @checkout_timeout = spec.config[:checkout_timeout] || 5 @dead_connection_timeout = spec.config[:dead_connection_timeout] @reaper = Reaper.new self, spec.config[:reaping_frequency] @@ -247,6 +245,9 @@ module ActiveRecord # default max pool size to 5 @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5 + # The cache of reserved connections mapped to threads + @reserved_connections = ThreadSafe::Cache.new(:initial_capacity => @size) + @connections = [] @automatic_reconnect = true @@ -267,7 +268,9 @@ module ActiveRecord # #connection can be called any number of times; the connection is # held in a hash keyed by the thread id. def connection - synchronize do + # this is correctly done double-checked locking + # (ThreadSafe::Cache's lookups have volatile semantics) + @reserved_connections[current_connection_id] || synchronize do @reserved_connections[current_connection_id] ||= checkout end end @@ -310,7 +313,7 @@ module ActiveRecord # Disconnects all connections in the pool, and clears the pool. def disconnect! synchronize do - @reserved_connections = {} + @reserved_connections.clear @connections.each do |conn| checkin conn conn.disconnect! @@ -323,7 +326,7 @@ module ActiveRecord # Clears the cache which maps classes. def clear_reloadable_connections! synchronize do - @reserved_connections = {} + @reserved_connections.clear @connections.each do |conn| checkin conn conn.disconnect! if conn.requires_reloading? @@ -490,11 +493,15 @@ module ActiveRecord # determine the connection pool that they should use. class ConnectionHandler def initialize - # These hashes are keyed by klass.name, NOT klass. Keying them by klass + # These caches are keyed by klass.name, NOT klass. Keying them by klass # alone would lead to memory leaks in development mode as all previous # instances of the class would stay in memory. - @owner_to_pool = Hash.new { |h,k| h[k] = {} } - @class_to_pool = Hash.new { |h,k| h[k] = {} } + @owner_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2) do |h,k| + h[k] = ThreadSafe::Cache.new(:initial_capacity => 2) + end + @class_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2) do |h,k| + h[k] = ThreadSafe::Cache.new + end end def connection_pool_list diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 73012834c9..b1ec33d06c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -423,7 +423,7 @@ module ActiveRecord # t.remove(:qualification) # t.remove(:qualification, :experience) def remove(*column_names) - @base.remove_column(@table_name, *column_names) + @base.remove_columns(@table_name, *column_names) end # Removes the given index from the table. @@ -490,20 +490,8 @@ module ActiveRecord class_eval <<-EOV, __FILE__, __LINE__ + 1 def #{column_type}(*args) # def string(*args) options = args.extract_options! # options = args.extract_options! - column_names = args # column_names = args - type = :'#{column_type}' # type = :string - column_names.each do |name| # column_names.each do |name| - column = ColumnDefinition.new(@base, name.to_s, type) # column = ColumnDefinition.new(@base, name, type) - if options[:limit] # if options[:limit] - column.limit = options[:limit] # column.limit = options[:limit] - elsif native[type].is_a?(Hash) # elsif native[type].is_a?(Hash) - column.limit = native[type][:limit] # column.limit = native[type][:limit] - end # end - column.precision = options[:precision] # column.precision = options[:precision] - column.scale = options[:scale] # column.scale = options[:scale] - column.default = options[:default] # column.default = options[:default] - column.null = options[:null] # column.null = options[:null] - @base.add_column(@table_name, name, column.sql_type, options) # @base.add_column(@table_name, name, column.sql_type, options) + args.each do |name| # column_names.each do |name| + @base.add_column(@table_name, name, :#{column_type}, options) # @base.add_column(@table_name, name, :string, options) end # end end # end EOV diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 1586e84a25..cdc8433185 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -214,6 +214,17 @@ module ActiveRecord end end + # Drops the join table specified by the given arguments. + # See create_join_table for details. + # + # Although this command ignores the block if one is given, it can be helpful + # to provide one in a migration's +change+ method so it can be reverted. + # In that case, the block will be used by create_join_table. + def drop_join_table(table_1, table_2, options = {}) + join_table_name = find_join_table_name(table_1, table_2, options) + drop_table(join_table_name) + end + # A block for changing columns in +table+. # # # change_table() yields a Table instance @@ -294,6 +305,10 @@ module ActiveRecord end # Drops a table from the database. + # + # Although this command ignores +options+ and the block if one is given, it can be helpful + # to provide these in a migration's +change+ method so it can be reverted. + # In that case, +options+ and the block will be used by create_table. def drop_table(table_name, options = {}) execute "DROP TABLE #{quote_table_name(table_name)}" end @@ -306,14 +321,26 @@ module ActiveRecord execute(add_column_sql) end - # Removes the column(s) from the table definition. + # Removes the given columns from the table definition. # - # remove_column(:suppliers, :qualification) # remove_columns(:suppliers, :qualification, :experience) - def remove_column(table_name, *column_names) - columns_for_remove(table_name, *column_names).each {|column_name| execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{column_name}" } + def remove_columns(table_name, *column_names) + raise ArgumentError.new("You must specify at least one column name. Example: remove_columns(:people, :first_name)") if column_names.empty? + column_names.each do |column_name| + remove_column(table_name, column_name) + end + end + + # Removes the column from the table definition. + # + # remove_column(:suppliers, :qualification) + # + # The +type+ and +options+ parameters will be ignored if present. It can be helpful + # to provide these in a migration's +change+ method so it can be reverted. + # In that case, +type+ and +options+ will be used by add_column. + def remove_column(table_name, column_name, type = nil, options = {}) + execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{quote_column_name(column_name)}" end - alias :remove_columns :remove_column # Changes the column's definition according to the new options. # See TableDefinition#column for details of the options you can use. @@ -490,8 +517,8 @@ module ActiveRecord sm_table = ActiveRecord::Migrator.schema_migrations_table_name ActiveRecord::SchemaMigration.order('version').map { |sm| - "INSERT INTO #{sm_table} (version, migrated_at, fingerprint, name) VALUES ('#{sm.version}',CURRENT_TIMESTAMP,'#{sm.fingerprint}','#{sm.name}');" - }.join("\n\n") + "INSERT INTO #{sm_table} (version) VALUES ('#{sm.version}');" + }.join "\n\n" end # Should not be called normally, but this operation is non-destructive. @@ -512,7 +539,7 @@ module ActiveRecord end unless migrated.include?(version) - ActiveRecord::SchemaMigration.create!(:version => version, :migrated_at => Time.now) + execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" end inserted = Set.new @@ -520,7 +547,7 @@ module ActiveRecord if inserted.include?(v) raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict." elsif v < version - ActiveRecord::SchemaMigration.create!(:version => v, :migrated_at => Time.now) + execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" inserted << v end end @@ -662,7 +689,8 @@ module ActiveRecord end def columns_for_remove(table_name, *column_names) - raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.blank? + ActiveSupport::Deprecation.warn("columns_for_remove is deprecated and will be removed in the future") + raise ArgumentError.new("You must specify at least one column name. Example: remove_columns(:people, :first_name)") if column_names.blank? column_names.map {|column_name| quote_column_name(column_name) } end diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 80984f39c9..fd36a5b075 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -126,6 +126,7 @@ module ActiveRecord when :hstore then "#{klass}.string_to_hstore(#{var_name})" when :inet, :cidr then "#{klass}.string_to_cidr(#{var_name})" when :json then "#{klass}.string_to_json(#{var_name})" + when :intrange then "#{klass}.string_to_intrange(#{var_name})" else var_name end end @@ -240,7 +241,7 @@ module ActiveRecord # Treat 0000-00-00 00:00:00 as nil. return nil if year.nil? || (year == 0 && mon == 0 && mday == 0) - Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil + Time.send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil end def fast_string_to_date(string) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb index c04a799b8d..f7d734a2f1 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb @@ -92,6 +92,36 @@ module ActiveRecord parse_pg_array(string).map{|val| oid.type_cast val} end + def string_to_intrange(string) + if string.nil? + nil + elsif "empty" == string + (nil..nil) + elsif String === string && (matches = /^(\(|\[)([0-9]+),(\s?)([0-9]+)(\)|\])$/i.match(string)) + lower_bound = ("(" == matches[1] ? (matches[2].to_i + 1) : matches[2].to_i) + upper_bound = (")" == matches[5] ? (matches[4].to_i - 1) : matches[4].to_i) + (lower_bound..upper_bound) + else + string + end + end + + def intrange_to_string(object) + if object.nil? + nil + elsif Range === object + if [object.first, object.last].all? { |el| Integer === el } + "[#{object.first.to_i},#{object.exclude_end? ? object.last.to_i : object.last.to_i + 1})" + elsif [object.first, object.last].all? { |el| NilClass === el } + "empty" + else + nil + end + else + object + end + end + private HstorePair = begin diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb index 52344f61c0..18ea83ce42 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb @@ -168,6 +168,14 @@ module ActiveRecord end end + class IntRange < Type + def type_cast(value) + return if value.nil? + + ConnectionAdapters::PostgreSQLColumn.string_to_intrange value + end + end + class TypeMap def initialize @mapping = {} @@ -269,6 +277,9 @@ module ActiveRecord register_type 'hstore', OID::Hstore.new register_type 'json', OID::Json.new + register_type 'int4range', OID::IntRange.new + alias_type 'int8range', 'int4range' + register_type 'cidr', OID::Cidr.new alias_type 'inet', 'cidr' end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index 62a4d76928..c2fcef94da 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -31,6 +31,11 @@ module ActiveRecord when 'json' then super(PostgreSQLColumn.json_to_string(value), column) else super end + when Range + case column.sql_type + when 'int4range', 'int8range' then super(PostgreSQLColumn.intrange_to_string(value), column) + else super + end when IPAddr case column.sql_type when 'inet', 'cidr' then super(PostgreSQLColumn.cidr_to_string(value), column) @@ -89,6 +94,11 @@ module ActiveRecord when 'json' then PostgreSQLColumn.json_to_string(value) else super(value, column) end + when Range + case column.sql_type + when 'int4range', 'int8range' then PostgreSQLColumn.intrange_to_string(value) + else super(value, column) + end when IPAddr return super(value, column) unless ['inet','cidr'].include? column.sql_type PostgreSQLColumn.cidr_to_string(value) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb b/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb index 16da3ea732..bc775394a6 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb @@ -7,13 +7,21 @@ module ActiveRecord end def disable_referential_integrity #:nodoc: - if supports_disable_referential_integrity? then - execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";")) + if supports_disable_referential_integrity? + begin + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";")) + rescue + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER USER" }.join(";")) + end end yield ensure - if supports_disable_referential_integrity? then - execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";")) + if supports_disable_referential_integrity? + begin + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";")) + rescue + execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER USER" }.join(";")) + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 18bf14d1fb..e10b562fa4 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -417,6 +417,14 @@ module ActiveRecord when 0..6; "timestamp(#{precision})" else raise(ActiveRecordError, "No timestamp type has precision of #{precision}. The allowed range of precision is from 0 to 6") end + when 'intrange' + return 'int4range' unless limit + + case limit + when 1..4; 'int4range' + when 5..8; 'int8range' + else raise(ActiveRecordError, "No range type has byte size #{limit}. Use a numeric with precision 0 instead.") + end else super end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e24ee1efdd..d62a375470 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -114,6 +114,9 @@ module ActiveRecord # JSON when /\A'(.*)'::json\z/ $1 + # int4range, int8range + when /\A'(.*)'::int(4|8)range\z/ + $1 # Object identifier types when /\A-?\d+\z/ $1 @@ -209,9 +212,12 @@ module ActiveRecord # UUID type when 'uuid' :uuid - # JSON type - when 'json' - :json + # JSON type + when 'json' + :json + # int4range, int8range types + when 'int4range', 'int8range' + :intrange # Small and big integer types when /^(?:small|big)int$/ :integer @@ -289,6 +295,10 @@ module ActiveRecord column(name, 'json', options) end + def intrange(name, options = {}) + column(name, 'intrange', options) + end + def column(name, type = nil, options = {}) super column = self[name] @@ -329,7 +339,8 @@ module ActiveRecord cidr: { name: "cidr" }, macaddr: { name: "macaddr" }, uuid: { name: "uuid" }, - json: { name: "json" } + json: { name: "json" }, + intrange: { name: "int4range" } } include Quoting diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index b89e9a01a8..11e8197293 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -444,15 +444,11 @@ module ActiveRecord end end - def remove_column(table_name, *column_names) #:nodoc: - raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.empty? - column_names.each do |column_name| - alter_table(table_name) do |definition| - definition.columns.delete(definition[column_name]) - end + def remove_column(table_name, column_name, type = nil, options = {}) #:nodoc: + alter_table(table_name) do |definition| + definition.columns.delete(definition[column_name]) end end - alias :remove_columns :remove_column def change_column_default(table_name, column_name, default) #:nodoc: alter_table(table_name) do |definition| @@ -537,7 +533,6 @@ module ActiveRecord end yield @definition if block_given? end - copy_table_indexes(from, to, options[:rename] || {}) copy_table_contents(from, to, @definition.columns.map {|column| column.name}, @@ -560,7 +555,7 @@ module ActiveRecord unless columns.empty? # index name can't be the same - opts = { :name => name.gsub(/_(#{from})_/, "_#{to}_") } + opts = { name: name.gsub(/(^|_)(#{from})_/, "\\1#{to}_") } opts[:unique] = true if index.unique add_index(to, columns, opts) end diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index c5ad14722e..ea3bb8f33f 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -753,9 +753,8 @@ module ActiveRecord def fixtures(*fixture_set_names) if fixture_set_names.first == :all - fixture_set_names = Dir["#{fixture_path}/**/*.yml"].map { |f| - File.basename f, '.yml' - } + fixture_set_names = Dir["#{fixture_path}/**/*.{yml}"] + fixture_set_names.map! { |f| f[(fixture_path.size + 1)..-5] } else fixture_set_names = fixture_set_names.flatten.map { |n| n.to_s } end diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb index 7bdc1bd4c6..7f877a6471 100644 --- a/activerecord/lib/active_record/integration.rb +++ b/activerecord/lib/active_record/integration.rb @@ -1,5 +1,16 @@ module ActiveRecord module Integration + extend ActiveSupport::Concern + + included do + ## + # :singleton-method: + # Indicates the format used to generate the timestamp format in the cache key. + # This is +:number+, by default. + class_attribute :cache_timestamp_format, :instance_writer => false + self.cache_timestamp_format = :nsec + end + # Returns a String, which Action Pack uses for constructing an URL to this # object. The default implementation returns this record's id as a String, # or nil if this record's unsaved. @@ -37,7 +48,7 @@ module ActiveRecord when new_record? "#{self.class.model_name.cache_key}/new" when timestamp = self[:updated_at] - timestamp = timestamp.utc.to_s(:nsec) + timestamp = timestamp.utc.to_s(cache_timestamp_format) "#{self.class.model_name.cache_key}/#{id}-#{timestamp}" else "#{self.class.model_name.cache_key}/#{id}" diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb index ca79950049..2366a91bb5 100644 --- a/activerecord/lib/active_record/log_subscriber.rb +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -1,7 +1,7 @@ module ActiveRecord class LogSubscriber < ActiveSupport::LogSubscriber IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"] - + def self.runtime=(value) Thread.current[:active_record_sql_runtime] = value end @@ -20,6 +20,16 @@ module ActiveRecord @odd_or_even = false end + def render_bind(column, value) + if column.type == :binary + rendered_value = "<#{value.bytesize} bytes of binary data>" + else + rendered_value = value + end + + [column.name, rendered_value] + end + def sql(event) self.class.runtime += event.duration return unless logger.debug? @@ -34,7 +44,7 @@ module ActiveRecord unless (payload[:binds] || []).empty? binds = " " + payload[:binds].map { |col,v| - [col.name, v] + render_bind(col, v) }.inspect end diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 4ce276d4bf..273af32237 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1,6 +1,5 @@ require "active_support/core_ext/class/attribute_accessors" require 'set' -require 'digest/md5' module ActiveRecord # Exception that can be raised to stop migrations from going backwards. @@ -33,7 +32,7 @@ module ActiveRecord class PendingMigrationError < ActiveRecordError#:nodoc: def initialize - super("Migrations are pending; run 'rake db:migrate RAILS_ENV=#{ENV['RAILS_ENV']}' to resolve this issue.") + super("Migrations are pending; run 'rake db:migrate RAILS_ENV=#{Rails.env}' to resolve this issue.") end end @@ -374,22 +373,129 @@ module ActiveRecord @name = name @version = version @connection = nil - @reverting = false end # instantiate the delegate object after initialize is defined self.verbose = true self.delegate = new - def revert - @reverting = true - yield - ensure - @reverting = false + # Reverses the migration commands for the given block and + # the given migrations. + # + # The following migration will remove the table 'horses' + # and create the table 'apples' on the way up, and the reverse + # on the way down. + # + # class FixTLMigration < ActiveRecord::Migration + # def change + # revert do + # create_table(:horses) do |t| + # t.text :content + # t.datetime :remind_at + # end + # end + # create_table(:apples) do |t| + # t.string :variety + # end + # end + # end + # + # Or equivalently, if +TenderloveMigration+ is defined as in the + # documentation for Migration: + # + # require_relative '2012121212_tenderlove_migration' + # + # class FixupTLMigration < ActiveRecord::Migration + # def change + # revert TenderloveMigration + # + # create_table(:apples) do |t| + # t.string :variety + # end + # end + # end + # + # This command can be nested. + def revert(*migration_classes) + run(*migration_classes.reverse, revert: true) unless migration_classes.empty? + if block_given? + if @connection.respond_to? :revert + @connection.revert { yield } + else + recorder = CommandRecorder.new(@connection) + @connection = recorder + suppress_messages do + @connection.revert { yield } + end + @connection = recorder.delegate + recorder.commands.each do |cmd, args, block| + send(cmd, *args, &block) + end + end + end end def reverting? - @reverting + @connection.respond_to?(:reverting) && @connection.reverting + end + + class ReversibleBlockHelper < Struct.new(:reverting) + def up + yield unless reverting + end + + def down + yield if reverting + end + end + + # Used to specify an operation that can be run in one direction or another. + # Call the methods +up+ and +down+ of the yielded object to run a block + # only in one given direction. + # The whole block will be called in the right order within the migration. + # + # In the following example, the looping on users will always be done + # when the three columns 'first_name', 'last_name' and 'full_name' exist, + # even when migrating down: + # + # class SplitNameMigration < ActiveRecord::Migration + # def change + # add_column :users, :first_name, :string + # add_column :users, :last_name, :string + # + # reversible do |dir| + # User.reset_column_information + # User.all.each do |u| + # dir.up { u.first_name, u.last_name = u.full_name.split(' ') } + # dir.down { u.full_name = "#{u.first_name} #{u.last_name}" } + # u.save + # end + # end + # + # revert { add_column :users, :full_name, :string } + # end + # end + def reversible + helper = ReversibleBlockHelper.new(reverting?) + transaction{ yield helper } + end + + # Runs the given migration classes. + # Last argument can specify options: + # - :direction (default is :up) + # - :revert (default is false) + def run(*migration_classes) + opts = migration_classes.extract_options! + dir = opts[:direction] || :up + dir = (dir == :down ? :up : :down) if opts[:revert] + if reverting? + # If in revert and going :up, say, we want to execute :down without reverting, so + revert { run(*migration_classes, direction: dir, revert: true) } + else + migration_classes.each do |migration_class| + migration_class.new.exec_migration(@connection, dir) + end + end end def up @@ -415,29 +521,9 @@ module ActiveRecord time = nil ActiveRecord::Base.connection_pool.with_connection do |conn| - @connection = conn - if respond_to?(:change) - if direction == :down - recorder = CommandRecorder.new(@connection) - suppress_messages do - @connection = recorder - change - end - @connection = conn - time = Benchmark.measure { - self.revert { - recorder.inverse.each do |cmd, args| - send(cmd, *args) - end - } - } - else - time = Benchmark.measure { change } - end - else - time = Benchmark.measure { send(direction) } + time = Benchmark.measure do + exec_migration(conn, direction) end - @connection = nil end case direction @@ -446,6 +532,21 @@ module ActiveRecord end end + def exec_migration(conn, direction) + @connection = conn + if respond_to?(:change) + if direction == :down + revert { change } + else + change + end + else + send(direction) + end + ensure + @connection = nil + end + def write(text="") puts(text) if verbose end @@ -484,7 +585,7 @@ module ActiveRecord arg_list = arguments.map{ |a| a.inspect } * ', ' say_with_time "#{method}(#{arg_list})" do - unless reverting? + unless @connection.respond_to? :revert unless arguments.empty? || method == :execute arguments[0] = Migrator.proper_table_name(arguments.first) arguments[1] = Migrator.proper_table_name(arguments.second) if method == :rename_table @@ -555,10 +656,6 @@ module ActiveRecord delegate :migrate, :announce, :write, :to => :migration - def fingerprint - @fingerprint ||= Digest::MD5.hexdigest(File.read(filename)) - end - private def migration @@ -670,7 +767,7 @@ module ActiveRecord files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }] migrations = files.map do |file| - version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?.rb/).first + version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first raise IllegalMigrationNameError.new(file) unless version version = version.to_i @@ -729,7 +826,7 @@ module ActiveRecord raise UnknownMigrationVersionError.new(@target_version) if target.nil? unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) target.migrate(@direction) - record_version_state_after_migrating(target) + record_version_state_after_migrating(target.version) end end @@ -752,7 +849,7 @@ module ActiveRecord begin ddl_transaction do migration.migrate(@direction) - record_version_state_after_migrating(migration) + record_version_state_after_migrating(migration.version) end rescue => e canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" @@ -810,18 +907,13 @@ module ActiveRecord raise DuplicateMigrationVersionError.new(version) if version end - def record_version_state_after_migrating(target) + def record_version_state_after_migrating(version) if down? - migrated.delete(target.version) - ActiveRecord::SchemaMigration.where(:version => target.version.to_s).delete_all + migrated.delete(version) + ActiveRecord::SchemaMigration.where(:version => version.to_s).delete_all else - migrated << target.version - ActiveRecord::SchemaMigration.create!( - :version => target.version.to_s, - :migrated_at => Time.now, - :fingerprint => target.fingerprint, - :name => File.basename(target.filename,'.rb').gsub(/^\d+_/,'') - ) + migrated << version + ActiveRecord::SchemaMigration.create!(:version => version.to_s) end end diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index 95f4360578..a6377185a8 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -16,69 +16,116 @@ module ActiveRecord class CommandRecorder include JoinTable - attr_accessor :commands, :delegate + attr_accessor :commands, :delegate, :reverting def initialize(delegate = nil) @commands = [] @delegate = delegate + @reverting = false + end + + # While executing the given block, the recorded will be in reverting mode. + # All commands recorded will end up being recorded reverted + # and in reverse order. + # For example: + # + # recorder.revert{ recorder.record(:rename_table, [:old, :new]) } + # # same effect as recorder.record(:rename_table, [:new, :old]) + def revert + @reverting = !@reverting + previous = @commands + @commands = [] + yield + ensure + @commands = previous.concat(@commands.reverse) + @reverting = !@reverting end # record +command+. +command+ should be a method name and arguments. # For example: # # recorder.record(:method_name, [:arg1, :arg2]) - def record(*command) - @commands << command + def record(*command, &block) + if @reverting + @commands << inverse_of(*command, &block) + else + @commands << (command << block) + end end - # Returns a list that represents commands that are the inverse of the - # commands stored in +commands+. For example: + # Returns the inverse of the given command. For example: # - # recorder.record(:rename_table, [:old, :new]) - # recorder.inverse # => [:rename_table, [:new, :old]] + # recorder.inverse_of(:rename_table, [:old, :new]) + # # => [:rename_table, [:new, :old]] # # This method will raise an +IrreversibleMigration+ exception if it cannot - # invert the +commands+. - def inverse - @commands.reverse.map { |name, args| - method = :"invert_#{name}" - raise IrreversibleMigration unless respond_to?(method, true) - send(method, args) - } + # invert the +command+. + def inverse_of(command, args, &block) + method = :"invert_#{command}" + raise IrreversibleMigration unless respond_to?(method, true) + send(method, args, &block) end def respond_to?(*args) # :nodoc: super || delegate.respond_to?(*args) end - [:create_table, :create_join_table, :change_table, :rename_table, :add_column, :remove_column, :rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps, :change_column, :change_column_default, :add_reference, :remove_reference].each do |method| + [:create_table, :create_join_table, :rename_table, :add_column, :remove_column, + :rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps, + :change_column_default, :add_reference, :remove_reference, :transaction, + :drop_join_table, :drop_table, + :change_column, :execute, :remove_columns, # irreversible methods need to be here too + ].each do |method| class_eval <<-EOV, __FILE__, __LINE__ + 1 - def #{method}(*args) # def create_table(*args) - record(:"#{method}", args) # record(:create_table, args) - end # end + def #{method}(*args, &block) # def create_table(*args, &block) + record(:"#{method}", args, &block) # record(:create_table, args, &block) + end # end EOV end alias :add_belongs_to :add_reference alias :remove_belongs_to :remove_reference - private - - def invert_create_table(args) - [:drop_table, [args.first]] + def change_table(table_name, options = {}) + yield ConnectionAdapters::Table.new(table_name, self) end - def invert_create_join_table(args) - table_name = find_join_table_name(*args) + private - [:drop_table, [table_name]] + module StraightReversions + private + { transaction: :transaction, + create_table: :drop_table, + create_join_table: :drop_join_table, + add_column: :remove_column, + add_timestamps: :remove_timestamps, + add_reference: :remove_reference, + }.each do |cmd, inv| + [[inv, cmd], [cmd, inv]].uniq.each do |method, inverse| + class_eval <<-EOV, __FILE__, __LINE__ + 1 + def invert_#{method}(args, &block) # def invert_create_table(args, &block) + [:#{inverse}, args, block] # [:drop_table, args, block] + end # end + EOV + end + end + end + + include StraightReversions + + def invert_drop_table(args, &block) + if args.size == 1 && block == nil + raise ActiveRecord::IrreversibleMigration, "To avoid mistakes, drop_table is only reversible if given options or a block (can be empty)." + end + super end def invert_rename_table(args) [:rename_table, args.reverse] end - def invert_add_column(args) - [:remove_column, args.first(2)] + def invert_remove_column(args) + raise ActiveRecord::IrreversibleMigration, "remove_column is only reversible if given a type." if args.size <= 2 + super end def invert_rename_index(args) @@ -91,27 +138,18 @@ module ActiveRecord def invert_add_index(args) table, columns, options = *args - index_name = options.try(:[], :name) - options_hash = index_name ? {:name => index_name} : {:column => columns} - [:remove_index, [table, options_hash]] + [:remove_index, [table, (options || {}).merge(column: columns)]] end - def invert_remove_timestamps(args) - [:add_timestamps, args] - end + def invert_remove_index(args) + table, options = *args + raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." unless options && options[:column] - def invert_add_timestamps(args) - [:remove_timestamps, args] + options = options.dup + [:add_index, [table, options.delete(:column), options]] end - def invert_add_reference(args) - [:remove_reference, args] - end alias :invert_add_belongs_to :invert_add_reference - - def invert_remove_reference(args) - [:add_reference, args] - end alias :invert_remove_belongs_to :invert_remove_reference # Forwards any missing method call to the \target. diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index 628ab0f566..85fb4be992 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -224,11 +224,10 @@ module ActiveRecord def decorate_columns(columns_hash) # :nodoc: return if columns_hash.empty? - serialized_attributes.each_key do |key| - columns_hash[key] = AttributeMethods::Serialization::Type.new(columns_hash[key]) - end - columns_hash.each do |name, col| + if serialized_attributes.key?(name) + columns_hash[name] = AttributeMethods::Serialization::Type.new(col) + end if create_time_zone_conversion_attribute?(name, col) columns_hash[name] = AttributeMethods::TimeZoneConversion::Type.new(col) end diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 0a9caa25b2..b25c0270c2 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -167,7 +167,7 @@ db_namespace = namespace :db do # desc "Raises an error if there are pending migrations" task :abort_if_pending_migrations => [:environment, :load_config] do - pending_migrations = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations_paths).pending_migrations + pending_migrations = ActiveRecord::Migrator.open(ActiveRecord::Migrator.migrations_paths).pending_migrations if pending_migrations.any? puts "You have #{pending_migrations.size} pending migrations:" diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb index 2184625e22..431d083f21 100644 --- a/activerecord/lib/active_record/relation/delegation.rb +++ b/activerecord/lib/active_record/relation/delegation.rb @@ -1,5 +1,6 @@ require 'active_support/concern' -require 'mutex_m' +require 'thread' +require 'thread_safe' module ActiveRecord module Delegation # :nodoc: @@ -73,8 +74,7 @@ module ActiveRecord end module ClassMethods - # This hash is keyed by klass.name to avoid memory leaks in development mode - @@subclasses = Hash.new { |h, k| h[k] = {} }.extend(Mutex_m) + @@subclasses = ThreadSafe::Cache.new(:initial_capacity => 2) def new(klass, *args) relation = relation_class_for(klass).allocate @@ -82,33 +82,27 @@ module ActiveRecord relation end + # This doesn't have to be thread-safe. relation_class_for guarantees that this will only be + # called exactly once for a given const name. + def const_missing(name) + const_set(name, Class.new(self) { include ClassSpecificRelation }) + end + + private # Cache the constants in @@subclasses because looking them up via const_get # make instantiation significantly slower. def relation_class_for(klass) - if klass && klass.name - if subclass = @@subclasses.synchronize { @@subclasses[self][klass.name] } - subclass - else - subclass = const_get("#{name.gsub('::', '_')}_#{klass.name.gsub('::', '_')}", false) - @@subclasses.synchronize { @@subclasses[self][klass.name] = subclass } - subclass + if klass && (klass_name = klass.name) + my_cache = @@subclasses.compute_if_absent(self) { ThreadSafe::Cache.new } + # This hash is keyed by klass.name to avoid memory leaks in development mode + my_cache.compute_if_absent(klass_name) do + # Cache#compute_if_absent guarantees that the block will only executed once for the given klass_name + const_get("#{name.gsub('::', '_')}_#{klass_name.gsub('::', '_')}", false) end else ActiveRecord::Relation end end - - # Check const_defined? in case another thread has already defined the constant. - # I am not sure whether this is strictly necessary. - def const_missing(name) - @@subclasses.synchronize { - if const_defined?(name) - const_get(name) - else - const_set(name, Class.new(self) { include ClassSpecificRelation }) - end - } - end end def respond_to?(method, include_private = false) diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index 44b7eb424b..3259dbbd80 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -39,45 +39,27 @@ module ActiveRecord end def define(info, &block) # :nodoc: - @using_deprecated_version_setting = info[:version].present? - SchemaMigration.drop_table - initialize_schema_migrations_table - instance_eval(&block) - # handle files from pre-4.0 that used :version option instead of dumping migration table - assume_migrated_upto_version(info[:version], migrations_paths) if @using_deprecated_version_setting + unless info[:version].blank? + initialize_schema_migrations_table + assume_migrated_upto_version(info[:version], migrations_paths) + end end # Eval the given block. All methods available to the current connection # adapter are available within the block, so you can easily use the # database definition DSL to build up your schema (+create_table+, # +add_index+, etc.). - def self.define(info={}, &block) - new.define(info, &block) - end - - # Create schema migration history. Include migration statements in a block to this method. - # - # migrations do - # migration 20121128235959, "44f1397e3b92442ca7488a029068a5ad", "add_horn_color_to_unicorns" - # migration 20121129235959, "4a1eb3965d94406b00002b370854eae8", "add_magic_power_to_unicorns" - # end - def migrations - raise(ArgumentError, "Can't set migrations while using :version option") if @using_deprecated_version_setting - yield - end - - # Add a migration to the ActiveRecord::SchemaMigration table. # - # The +version+ argument is an integer. - # The +fingerprint+ and +name+ arguments are required but may be empty strings. - # The migration's +migrated_at+ attribute is set to the current time, - # instead of being set explicitly as an argument to the method. + # The +info+ hash is optional, and if given is used to define metadata + # about the current schema (currently, only the schema's version): # - # migration 20121129235959, "4a1eb3965d94406b00002b370854eae8", "add_magic_power_to_unicorns" - def migration(version, fingerprint, name) - SchemaMigration.create!(version: version, migrated_at: Time.now, fingerprint: fingerprint, name: name) + # ActiveRecord::Schema.define(version: 20380119000001) do + # ... + # end + def self.define(info={}, &block) + new.define(info, &block) end end end diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 194a77ca16..36bde44e7c 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -24,7 +24,6 @@ module ActiveRecord def dump(stream) header(stream) - migrations(stream) tables(stream) trailer(stream) stream @@ -39,11 +38,13 @@ module ActiveRecord end def header(stream) + define_params = @version ? "version: #{@version}" : "" + if stream.respond_to?(:external_encoding) && stream.external_encoding stream.puts "# encoding: #{stream.external_encoding.name}" end - header_text = <<HEADER_RUBY + stream.puts <<HEADER # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -56,27 +57,15 @@ module ActiveRecord # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define do +ActiveRecord::Schema.define(#{define_params}) do -HEADER_RUBY - stream.puts header_text +HEADER end def trailer(stream) stream.puts "end" end - def migrations(stream) - all_migrations = ActiveRecord::SchemaMigration.all.to_a - if all_migrations.any? - stream.puts(" migrations do") - all_migrations.each do |migration| - stream.puts(migration.schema_line(" ")) - end - stream.puts(" end\n\n") - end - end - def tables(stream) @connection.tables.sort.each do |tbl| next if ['schema_migrations', ignore_tables].flatten.any? do |ignored| diff --git a/activerecord/lib/active_record/schema_migration.rb b/activerecord/lib/active_record/schema_migration.rb index 6c3cd5b6ba..9830abe7d8 100644 --- a/activerecord/lib/active_record/schema_migration.rb +++ b/activerecord/lib/active_record/schema_migration.rb @@ -14,38 +14,17 @@ module ActiveRecord end def self.create_table - if connection.table_exists?(table_name) - cols = connection.columns(table_name).collect { |col| col.name } - unless cols.include?("migrated_at") - connection.add_column(table_name, "migrated_at", :datetime) - q_table_name = connection.quote_table_name(table_name) - q_timestamp = connection.quoted_date(Time.now) - connection.update("UPDATE #{q_table_name} SET migrated_at = '#{q_timestamp}' WHERE migrated_at IS NULL") - connection.change_column(table_name, "migrated_at", :datetime, :null => false) - end - unless cols.include?("fingerprint") - connection.add_column(table_name, "fingerprint", :string, :limit => 32) - end - unless cols.include?("name") - connection.add_column(table_name, "name", :string) - end - else + unless connection.table_exists?(table_name) connection.create_table(table_name, :id => false) do |t| t.column :version, :string, :null => false - t.column :migrated_at, :datetime, :null => false - t.column :fingerprint, :string, :limit => 32 - t.column :name, :string end - connection.add_index(table_name, "version", :unique => true, :name => index_name) + connection.add_index table_name, :version, :unique => true, :name => index_name end - reset_column_information end def self.drop_table if connection.table_exists?(table_name) - if connection.index_exists?(table_name, "version", :unique => true, :name => index_name) - connection.remove_index(table_name, :name => index_name) - end + connection.remove_index table_name, :name => index_name connection.drop_table(table_name) end end @@ -53,17 +32,5 @@ module ActiveRecord def version super.to_i end - - # Construct ruby source to include in schema.rb dump for this migration. - # Pass a string of spaces as +indent+ to allow calling code to control how deeply indented the line is. - # The generated line includes the migration version, fingerprint, and name. Either fingerprint or name - # can be an empty string. - # - # Example output: - # - # migration 20121129235959, "ee4be703f9e6e2fc0f4baddebe6eb8f7", "add_magic_power_to_unicorns" - def schema_line(indent) - %Q(#{indent}migration %s, "%s", "%s") % [version, fingerprint, name] - end end end diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 5fa6a0b892..1427189851 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -1,10 +1,8 @@ -require 'active_support/core_ext/array/prepend_and_append' - module ActiveRecord module Validations class UniquenessValidator < ActiveModel::EachValidator # :nodoc: def initialize(options) - super(options.reverse_merge(:case_sensitive => true)) + super({ case_sensitive: true }.merge!(options)) end # Unfortunately, we have to tie Uniqueness validators to a class. @@ -15,35 +13,19 @@ module ActiveRecord def validate_each(record, attribute, value) finder_class = find_finder_class_for(record) table = finder_class.arel_table - - coder = record.class.serialized_attributes[attribute.to_s] - - if value && coder - value = coder.dump value - end + value = deserialize_attribute(record, attribute, value) relation = build_relation(finder_class, table, attribute, value) - relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted? - - Array(options[:scope]).each do |scope_item| - reflection = record.class.reflect_on_association(scope_item) - if reflection - scope_value = record.send(reflection.foreign_key) - scope_item = reflection.foreign_key - else - scope_value = record.read_attribute(scope_item) - end - relation = relation.and(table[scope_item].eq(scope_value)) - end - + relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id)) if record.persisted? + relation = scope_relation(record, table, relation) relation = finder_class.unscoped.where(relation) - - if options[:conditions] - relation = relation.merge(options[:conditions]) - end + relation.merge!(options[:conditions]) if options[:conditions] if relation.exists? - record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope, :conditions).merge(:value => value)) + error_options = options.except(:case_sensitive, :scope, :conditions) + error_options[:value] = value + + record.errors.add(attribute, :taken, error_options) end end @@ -58,7 +40,7 @@ module ActiveRecord class_hierarchy = [record.class] while class_hierarchy.first != @klass - class_hierarchy.prepend(class_hierarchy.first.superclass) + class_hierarchy.unshift(class_hierarchy.first.superclass) end class_hierarchy.detect { |klass| !klass.abstract_class? } @@ -71,18 +53,37 @@ module ActiveRecord end column = klass.columns_hash[attribute.to_s] - value = column.limit ? value.to_s[0, column.limit] : value.to_s if !value.nil? && column.text? + value = klass.connection.type_cast(value, column) + value = value.to_s[0, column.limit] if value && column.limit && column.text? if !options[:case_sensitive] && value && column.text? # will use SQL LOWER function before comparison, unless it detects a case insensitive collation - relation = klass.connection.case_insensitive_comparison(table, attribute, column, value) + klass.connection.case_insensitive_comparison(table, attribute, column, value) else - value = klass.connection.case_sensitive_modifier(value) unless value.nil? - relation = table[attribute].eq(value) + value = klass.connection.case_sensitive_modifier(value) unless value.nil? + table[attribute].eq(value) + end + end + + def scope_relation(record, table, relation) + Array(options[:scope]).each do |scope_item| + if reflection = record.class.reflect_on_association(scope_item) + scope_value = record.send(reflection.foreign_key) + scope_item = reflection.foreign_key + else + scope_value = record.read_attribute(scope_item) + end + relation = relation.and(table[scope_item].eq(scope_value)) end relation end + + def deserialize_attribute(record, attribute, value) + coder = record.class.serialized_attributes[attribute.to_s] + value = coder.dump value if value && coder + value + end end module ClassMethods 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 d5c07aecd3..ae9c74fd05 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -21,28 +21,16 @@ class <%= migration_class_name %> < ActiveRecord::Migration end end <%- else -%> - def up + def change <% attributes.each do |attribute| -%> <%- if migration_action -%> <%- if attribute.reference? -%> - remove_reference :<%= table_name %>, :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %> + remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %> <%- else -%> - remove_column :<%= table_name %>, :<%= attribute.name %> - <%- end -%> -<%- end -%> -<%- end -%> - end - - def down -<% attributes.reverse.each do |attribute| -%> -<%- 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 %> + remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> <%- end -%> + remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> <%- end -%> <%- end -%> <%- end -%> diff --git a/activerecord/test/cases/adapters/postgresql/intrange_test.rb b/activerecord/test/cases/adapters/postgresql/intrange_test.rb new file mode 100644 index 0000000000..5f6a64619d --- /dev/null +++ b/activerecord/test/cases/adapters/postgresql/intrange_test.rb @@ -0,0 +1,106 @@ +# encoding: utf-8 + +require "cases/helper" +require 'active_record/base' +require 'active_record/connection_adapters/postgresql_adapter' + +class PostgresqlIntrangesTest < ActiveRecord::TestCase + class IntRangeDataType < ActiveRecord::Base + self.table_name = 'intrange_data_type' + end + + def setup + @connection = ActiveRecord::Base.connection + begin + @connection.transaction do + @connection.create_table('intrange_data_type') do |t| + t.intrange 'int_range', :default => (1..10) + t.intrange 'long_int_range', :limit => 8, :default => (1..100) + end + end + rescue ActiveRecord::StatementInvalid + return skip "do not test on PG without ranges" + end + @int_range_column = IntRangeDataType.columns.find { |c| c.name == 'int_range' } + @long_int_range_column = IntRangeDataType.columns.find { |c| c.name == 'long_int_range' } + end + + def teardown + @connection.execute 'drop table if exists intrange_data_type' + end + + def test_columns + assert_equal :intrange, @int_range_column.type + assert_equal :intrange, @long_int_range_column.type + end + + def test_type_cast_intrange + assert @int_range_column + assert_equal(true, @int_range_column.has_default?) + assert_equal((1..10), @int_range_column.default) + assert_equal("int4range", @int_range_column.sql_type) + + data = "[1,10)" + hash = @int_range_column.class.string_to_intrange data + assert_equal((1..9), hash) + assert_equal((1..9), @int_range_column.type_cast(data)) + + assert_equal((nil..nil), @int_range_column.type_cast("empty")) + assert_equal((1..5), @int_range_column.type_cast('[1,5]')) + assert_equal((2..4), @int_range_column.type_cast('(1,5)')) + assert_equal((2..39), @int_range_column.type_cast('[2,40)')) + assert_equal((10..20), @int_range_column.type_cast('(9,20]')) + end + + def test_type_cast_long_intrange + assert @long_int_range_column + assert_equal(true, @long_int_range_column.has_default?) + assert_equal((1..100), @long_int_range_column.default) + assert_equal("int8range", @long_int_range_column.sql_type) + end + + def test_rewrite + @connection.execute "insert into intrange_data_type (int_range) VALUES ('(1, 6)')" + x = IntRangeDataType.first + x.int_range = (1..100) + assert x.save! + end + + def test_select + @connection.execute "insert into intrange_data_type (int_range) VALUES ('(1, 4]')" + x = IntRangeDataType.first + assert_equal((2..4), x.int_range) + end + + def test_empty_range + @connection.execute %q|insert into intrange_data_type (int_range) VALUES('empty')| + x = IntRangeDataType.first + assert_equal((nil..nil), x.int_range) + end + + def test_rewrite_to_nil + @connection.execute %q|insert into intrange_data_type (int_range) VALUES('(1, 4]')| + x = IntRangeDataType.first + x.int_range = nil + assert x.save! + assert_equal(nil, x.int_range) + end + + def test_invalid_intrange + assert IntRangeDataType.create!(int_range: ('a'..'d')) + x = IntRangeDataType.first + assert_equal(nil, x.int_range) + end + + def test_save_empty_range + assert IntRangeDataType.create!(int_range: (nil..nil)) + x = IntRangeDataType.first + assert_equal((nil..nil), x.int_range) + end + + def test_save_invalid_data + assert_raises(ActiveRecord::StatementInvalid) do + IntRangeDataType.create!(int_range: "empty1") + end + end +end diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb index bd47ba8741..b2eac0349b 100644 --- a/activerecord/test/cases/ar_schema_test.rb +++ b/activerecord/test/cases/ar_schema_test.rb @@ -46,55 +46,4 @@ if ActiveRecord::Base.connection.supports_migrations? end end - class ActiveRecordSchemaMigrationsTest < ActiveRecordSchemaTest - def setup - super - ActiveRecord::SchemaMigration.delete_all - end - - def test_migration_adds_row_to_migrations_table - schema = ActiveRecord::Schema.new - schema.migration(1001, "", "") - schema.migration(1002, "123456789012345678901234567890ab", "add_magic_power_to_unicorns") - - migrations = ActiveRecord::SchemaMigration.all.to_a - assert_equal 2, migrations.length - - assert_equal 1001, migrations[0].version - assert_match %r{^2\d\d\d-}, migrations[0].migrated_at.to_s(:db) - assert_equal "", migrations[0].fingerprint - assert_equal "", migrations[0].name - - assert_equal 1002, migrations[1].version - assert_match %r{^2\d\d\d-}, migrations[1].migrated_at.to_s(:db) - assert_equal "123456789012345678901234567890ab", migrations[1].fingerprint - assert_equal "add_magic_power_to_unicorns", migrations[1].name - end - - def test_define_clears_schema_migrations - assert_nothing_raised do - ActiveRecord::Schema.define do - migrations do - migration(123001, "", "") - end - end - ActiveRecord::Schema.define do - migrations do - migration(123001, "", "") - end - end - end - end - - def test_define_raises_if_both_version_and_explicit_migrations - assert_raise(ArgumentError) do - ActiveRecord::Schema.define(version: 123001) do - migrations do - migration(123001, "", "") - end - end - end - end - end - end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 8e52ce1d91..2b96b42032 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -330,6 +330,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end end + def test_update_counter_caches_on_replace_association + post = posts(:welcome) + tag = post.tags.create!(:name => 'doomed') + tag.tagged_posts << posts(:thinking) + + tag.tagged_posts = [] + post.reload + + assert_equal(post.taggings.count, post.taggings_count) + end + def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 8644f2f496..d326ed7863 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -23,6 +23,8 @@ require 'models/edge' require 'models/joke' require 'models/bulb' require 'models/bird' +require 'models/car' +require 'models/bulb' require 'rexml/document' require 'active_support/core_ext/exception' @@ -1442,6 +1444,21 @@ class BasicsTest < ActiveRecord::TestCase assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:nsec)}", dev.cache_key end + def test_cache_key_format_for_existing_record_with_updated_at_and_custom_cache_timestamp_format + dev = CachedDeveloper.first + assert_equal "cached_developers/#{dev.id}-#{dev.updated_at.utc.to_s(:number)}", dev.cache_key + end + + def test_cache_key_changes_when_child_touched + car = Car.create + Bulb.create(car: car) + + key = car.cache_key + car.bulb.touch + car.reload + assert_not_equal key, car.cache_key + end + def test_cache_key_format_for_existing_record_with_nil_updated_at dev = Developer.first dev.update_columns(updated_at: nil) diff --git a/activerecord/test/cases/date_time_test.rb b/activerecord/test/cases/date_time_test.rb index 3deb0dac99..427076bd80 100644 --- a/activerecord/test/cases/date_time_test.rb +++ b/activerecord/test/cases/date_time_test.rb @@ -8,15 +8,15 @@ class DateTimeTest < ActiveRecord::TestCase with_active_record_default_timezone :utc do time_values = [1807, 2, 10, 15, 30, 45] # create DateTime value with local time zone offset - local_offset = Rational(Time.local_time(*time_values).utc_offset, 86400) + local_offset = Rational(Time.local(*time_values).utc_offset, 86400) now = DateTime.civil(*(time_values + [local_offset])) task = Task.new task.starting = now task.save! - # check against Time.local_time, since some platforms will return a Time instead of a DateTime - assert_equal Time.local_time(*time_values), Task.find(task.id).starting + # check against Time.local, since some platforms will return a Time instead of a DateTime + assert_equal Time.local(*time_values), Task.find(task.id).starting end end end diff --git a/activerecord/test/cases/invertible_migration_test.rb b/activerecord/test/cases/invertible_migration_test.rb index 8f1cdd47ea..cf4e39c4ef 100644 --- a/activerecord/test/cases/invertible_migration_test.rb +++ b/activerecord/test/cases/invertible_migration_test.rb @@ -17,6 +17,37 @@ module ActiveRecord end end + class InvertibleRevertMigration < SilentMigration + def change + revert do + create_table("horses") do |t| + t.column :content, :text + t.column :remind_at, :datetime + end + end + end + end + + class InvertibleByPartsMigration < SilentMigration + attr_writer :test + def change + create_table("new_horses") do |t| + t.column :breed, :string + end + reversible do |dir| + @test.yield :both + dir.up { @test.yield :up } + dir.down { @test.yield :down } + end + revert do + create_table("horses") do |t| + t.column :content, :text + t.column :remind_at, :datetime + end + end + end + end + class NonInvertibleMigration < SilentMigration def change create_table("horses") do |t| @@ -40,6 +71,23 @@ module ActiveRecord end end + class RevertWholeMigration < SilentMigration + def initialize(name = self.class.name, version = nil, migration) + @migration = migration + super(name, version) + end + + def change + revert @migration + end + end + + class NestedRevertWholeMigration < RevertWholeMigration + def change + revert { super } + end + end + def teardown if ActiveRecord::Base.connection.table_exists?("horses") ActiveRecord::Base.connection.drop_table("horses") @@ -67,6 +115,83 @@ module ActiveRecord assert !migration.connection.table_exists?("horses") end + def test_migrate_revert + migration = InvertibleMigration.new + revert = InvertibleRevertMigration.new + migration.migrate :up + revert.migrate :up + assert !migration.connection.table_exists?("horses") + revert.migrate :down + assert migration.connection.table_exists?("horses") + migration.migrate :down + assert !migration.connection.table_exists?("horses") + end + + def test_migrate_revert_by_part + InvertibleMigration.new.migrate :up + received = [] + migration = InvertibleByPartsMigration.new + migration.test = ->(dir){ + assert migration.connection.table_exists?("horses") + assert migration.connection.table_exists?("new_horses") + received << dir + } + migration.migrate :up + assert_equal [:both, :up], received + assert !migration.connection.table_exists?("horses") + assert migration.connection.table_exists?("new_horses") + migration.migrate :down + assert_equal [:both, :up, :both, :down], received + assert migration.connection.table_exists?("horses") + assert !migration.connection.table_exists?("new_horses") + end + + def test_migrate_revert_whole_migration + migration = InvertibleMigration.new + [LegacyMigration, InvertibleMigration].each do |klass| + revert = RevertWholeMigration.new(klass) + migration.migrate :up + revert.migrate :up + assert !migration.connection.table_exists?("horses") + revert.migrate :down + assert migration.connection.table_exists?("horses") + migration.migrate :down + assert !migration.connection.table_exists?("horses") + end + end + + def test_migrate_nested_revert_whole_migration + revert = NestedRevertWholeMigration.new(InvertibleRevertMigration) + revert.migrate :down + assert revert.connection.table_exists?("horses") + revert.migrate :up + assert !revert.connection.table_exists?("horses") + end + + def test_revert_order + block = Proc.new{|t| t.string :name } + recorder = ActiveRecord::Migration::CommandRecorder.new(ActiveRecord::Base.connection) + recorder.instance_eval do + create_table("apples", &block) + revert do + create_table("bananas", &block) + revert do + create_table("clementines") + create_table("dates") + end + create_table("elderberries") + end + revert do + create_table("figs") + create_table("grapes") + end + end + assert_equal [[:create_table, ["apples"], block], [:drop_table, ["elderberries"], nil], + [:create_table, ["clementines"], nil], [:create_table, ["dates"], nil], + [:drop_table, ["bananas"], block], [:drop_table, ["grapes"], nil], + [:drop_table, ["figs"], nil]], recorder.commands + end + def test_legacy_up LegacyMigration.migrate :up assert ActiveRecord::Base.connection.table_exists?("horses"), "horses should exist" diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index 70d00aecf9..345e83a102 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require "models/binary" require "models/developer" require "models/post" require "active_support/log_subscriber/test_helper" @@ -100,4 +101,12 @@ class LogSubscriberTest < ActiveRecord::TestCase def test_initializes_runtime Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join end + + def test_binary_data_is_not_logged + skip if current_adapter?(:Mysql2Adapter) + + Binary.create(:data => 'some binary data') + wait + assert_match(/<16 bytes of binary data>/, @logger.logged(:debug).join) + end end diff --git a/activerecord/test/cases/migration/change_table_test.rb b/activerecord/test/cases/migration/change_table_test.rb index 8fb03cdee0..ac1ad176db 100644 --- a/activerecord/test/cases/migration/change_table_test.rb +++ b/activerecord/test/cases/migration/change_table_test.rb @@ -3,21 +3,8 @@ require "cases/migration/helper" module ActiveRecord class Migration class TableTest < ActiveRecord::TestCase - class MockConnection < MiniTest::Mock - def native_database_types - { - :string => 'varchar(255)', - :integer => 'integer', - } - end - - def type_to_sql(type, limit, precision, scale) - native_database_types[type] - end - end - def setup - @connection = MockConnection.new + @connection = MiniTest::Mock.new end def teardown @@ -98,26 +85,18 @@ module ActiveRecord end end - def string_column - @connection.native_database_types[:string] - end - - def integer_column - @connection.native_database_types[:integer] - end - def test_integer_creates_integer_column with_change_table do |t| - @connection.expect :add_column, nil, [:delete_me, :foo, integer_column, {}] - @connection.expect :add_column, nil, [:delete_me, :bar, integer_column, {}] + @connection.expect :add_column, nil, [:delete_me, :foo, :integer, {}] + @connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}] t.integer :foo, :bar end end def test_string_creates_string_column with_change_table do |t| - @connection.expect :add_column, nil, [:delete_me, :foo, string_column, {}] - @connection.expect :add_column, nil, [:delete_me, :bar, string_column, {}] + @connection.expect :add_column, nil, [:delete_me, :foo, :string, {}] + @connection.expect :add_column, nil, [:delete_me, :bar, :string, {}] t.string :foo, :bar end end @@ -194,14 +173,14 @@ module ActiveRecord def test_remove_drops_single_column with_change_table do |t| - @connection.expect :remove_column, nil, [:delete_me, :bar] + @connection.expect :remove_columns, nil, [:delete_me, :bar] t.remove :bar end end def test_remove_drops_multiple_columns with_change_table do |t| - @connection.expect :remove_column, nil, [:delete_me, :bar, :baz] + @connection.expect :remove_columns, nil, [:delete_me, :bar, :baz] t.remove :bar, :baz end end diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb index f2213ee6aa..2cad8a6d96 100644 --- a/activerecord/test/cases/migration/command_recorder_test.rb +++ b/activerecord/test/cases/migration/command_recorder_test.rb @@ -26,7 +26,7 @@ module ActiveRecord }.new) assert recorder.respond_to?(:create_table), 'respond_to? create_table' recorder.send(:create_table, :horses) - assert_equal [[:create_table, [:horses]]], recorder.commands + assert_equal [[:create_table, [:horses], nil]], recorder.commands end def test_unknown_commands_delegate @@ -34,10 +34,15 @@ module ActiveRecord assert_equal 'bar', recorder.foo end - def test_unknown_commands_raise_exception_if_they_cannot_delegate - @recorder.record :execute, ['some sql'] + def test_inverse_of_raise_exception_on_unknown_commands assert_raises(ActiveRecord::IrreversibleMigration) do - @recorder.inverse + @recorder.inverse_of :execute, ['some sql'] + end + end + + def test_irreversible_commands_raise_exception + assert_raises(ActiveRecord::IrreversibleMigration) do + @recorder.revert{ @recorder.execute 'some sql' } end end @@ -46,121 +51,196 @@ module ActiveRecord assert_equal 1, @recorder.commands.length end - def test_inverse - @recorder.record :create_table, [:system_settings] - assert_equal 1, @recorder.inverse.length - - @recorder.record :rename_table, [:old, :new] - assert_equal 2, @recorder.inverse.length + def test_inverted_commands_are_reversed + @recorder.revert do + @recorder.record :create_table, [:hello] + @recorder.record :create_table, [:world] + end + tables = @recorder.commands.map{|_cmd, args, _block| args} + assert_equal [[:world], [:hello]], tables end - def test_inverted_commands_are_reveresed - @recorder.record :create_table, [:hello] - @recorder.record :create_table, [:world] - tables = @recorder.inverse.map(&:last) - assert_equal [[:world], [:hello]], tables + def test_revert_order + block = Proc.new{|t| t.string :name } + @recorder.instance_eval do + create_table("apples", &block) + revert do + create_table("bananas", &block) + revert do + create_table("clementines", &block) + create_table("dates") + end + create_table("elderberries") + end + revert do + create_table("figs", &block) + create_table("grapes") + end + end + assert_equal [[:create_table, ["apples"], block], [:drop_table, ["elderberries"], nil], + [:create_table, ["clementines"], block], [:create_table, ["dates"], nil], + [:drop_table, ["bananas"], block], [:drop_table, ["grapes"], nil], + [:drop_table, ["figs"], block]], @recorder.commands + end + + def test_invert_change_table + @recorder.revert do + @recorder.change_table :fruits do |t| + t.string :name + t.rename :kind, :cultivar + end + end + assert_equal [ + [:rename_column, [:fruits, :cultivar, :kind]], + [:remove_column, [:fruits, :name, :string, {}], nil], + ], @recorder.commands + + assert_raises(ActiveRecord::IrreversibleMigration) do + @recorder.revert do + @recorder.change_table :fruits do |t| + t.remove :kind + end + end + end end def test_invert_create_table - @recorder.record :create_table, [:system_settings] - drop_table = @recorder.inverse.first - assert_equal [:drop_table, [:system_settings]], drop_table + @recorder.revert do + @recorder.record :create_table, [:system_settings] + end + drop_table = @recorder.commands.first + assert_equal [:drop_table, [:system_settings], nil], drop_table + end + + def test_invert_create_table_with_options_and_block + block = Proc.new{} + drop_table = @recorder.inverse_of :create_table, [:people_reminders, id: false], &block + assert_equal [:drop_table, [:people_reminders, id: false], block], drop_table + end + + def test_invert_drop_table + block = Proc.new{} + create_table = @recorder.inverse_of :drop_table, [:people_reminders, id: false], &block + assert_equal [:create_table, [:people_reminders, id: false], block], create_table end - def test_invert_create_table_with_options - @recorder.record :create_table, [:people_reminders, {:id => false}] - drop_table = @recorder.inverse.first - assert_equal [:drop_table, [:people_reminders]], drop_table + def test_invert_drop_table_without_a_block_nor_option + assert_raises(ActiveRecord::IrreversibleMigration) do + @recorder.inverse_of :drop_table, [:people_reminders] + end end def test_invert_create_join_table - @recorder.record :create_join_table, [:musics, :artists] - drop_table = @recorder.inverse.first - assert_equal [:drop_table, [:artists_musics]], drop_table + drop_join_table = @recorder.inverse_of :create_join_table, [:musics, :artists] + assert_equal [:drop_join_table, [:musics, :artists], nil], drop_join_table end def test_invert_create_join_table_with_table_name - @recorder.record :create_join_table, [:musics, :artists, {:table_name => :catalog}] - drop_table = @recorder.inverse.first - assert_equal [:drop_table, [:catalog]], drop_table + drop_join_table = @recorder.inverse_of :create_join_table, [:musics, :artists, table_name: :catalog] + assert_equal [:drop_join_table, [:musics, :artists, table_name: :catalog], nil], drop_join_table + end + + def test_invert_drop_join_table + block = Proc.new{} + create_join_table = @recorder.inverse_of :drop_join_table, [:musics, :artists, table_name: :catalog], &block + assert_equal [:create_join_table, [:musics, :artists, table_name: :catalog], block], create_join_table end def test_invert_rename_table - @recorder.record :rename_table, [:old, :new] - rename = @recorder.inverse.first + rename = @recorder.inverse_of :rename_table, [:old, :new] assert_equal [:rename_table, [:new, :old]], rename end def test_invert_add_column - @recorder.record :add_column, [:table, :column, :type, {}] - remove = @recorder.inverse.first - assert_equal [:remove_column, [:table, :column]], remove + remove = @recorder.inverse_of :add_column, [:table, :column, :type, {}] + assert_equal [:remove_column, [:table, :column, :type, {}], nil], remove + end + + def test_invert_remove_column + add = @recorder.inverse_of :remove_column, [:table, :column, :type, {}] + assert_equal [:add_column, [:table, :column, :type, {}], nil], add + end + + def test_invert_remove_column_without_type + assert_raises(ActiveRecord::IrreversibleMigration) do + @recorder.inverse_of :remove_column, [:table, :column] + end end def test_invert_rename_column - @recorder.record :rename_column, [:table, :old, :new] - rename = @recorder.inverse.first + rename = @recorder.inverse_of :rename_column, [:table, :old, :new] assert_equal [:rename_column, [:table, :new, :old]], rename end def test_invert_add_index - @recorder.record :add_index, [:table, [:one, :two], {:options => true}] - remove = @recorder.inverse.first - assert_equal [:remove_index, [:table, {:column => [:one, :two]}]], remove + remove = @recorder.inverse_of :add_index, [:table, [:one, :two], options: true] + assert_equal [:remove_index, [:table, {column: [:one, :two], options: true}]], remove end def test_invert_add_index_with_name - @recorder.record :add_index, [:table, [:one, :two], {:name => "new_index"}] - remove = @recorder.inverse.first - assert_equal [:remove_index, [:table, {:name => "new_index"}]], remove + remove = @recorder.inverse_of :add_index, [:table, [:one, :two], name: "new_index"] + assert_equal [:remove_index, [:table, {column: [:one, :two], name: "new_index"}]], remove end def test_invert_add_index_with_no_options - @recorder.record :add_index, [:table, [:one, :two]] - remove = @recorder.inverse.first - assert_equal [:remove_index, [:table, {:column => [:one, :two]}]], remove + remove = @recorder.inverse_of :add_index, [:table, [:one, :two]] + assert_equal [:remove_index, [:table, {column: [:one, :two]}]], remove + end + + def test_invert_remove_index + add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two], options: true}] + assert_equal [:add_index, [:table, [:one, :two], options: true]], add + end + + def test_invert_remove_index_with_name + add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two], name: "new_index"}] + assert_equal [:add_index, [:table, [:one, :two], name: "new_index"]], add + end + + def test_invert_remove_index_with_no_special_options + add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two]}] + assert_equal [:add_index, [:table, [:one, :two], {}]], add + end + + def test_invert_remove_index_with_no_column + assert_raises(ActiveRecord::IrreversibleMigration) do + @recorder.inverse_of :remove_index, [:table, name: "new_index"] + end end def test_invert_rename_index - @recorder.record :rename_index, [:table, :old, :new] - rename = @recorder.inverse.first + rename = @recorder.inverse_of :rename_index, [:table, :old, :new] assert_equal [:rename_index, [:table, :new, :old]], rename end def test_invert_add_timestamps - @recorder.record :add_timestamps, [:table] - remove = @recorder.inverse.first - assert_equal [:remove_timestamps, [:table]], remove + remove = @recorder.inverse_of :add_timestamps, [:table] + assert_equal [:remove_timestamps, [:table], nil], remove end def test_invert_remove_timestamps - @recorder.record :remove_timestamps, [:table] - add = @recorder.inverse.first - assert_equal [:add_timestamps, [:table]], add + add = @recorder.inverse_of :remove_timestamps, [:table] + assert_equal [:add_timestamps, [:table], nil], add end def test_invert_add_reference - @recorder.record :add_reference, [:table, :taggable, { polymorphic: true }] - remove = @recorder.inverse.first - assert_equal [:remove_reference, [:table, :taggable, { polymorphic: true }]], remove + remove = @recorder.inverse_of :add_reference, [:table, :taggable, { polymorphic: true }] + assert_equal [:remove_reference, [:table, :taggable, { polymorphic: true }], nil], remove end def test_invert_add_belongs_to_alias - @recorder.record :add_belongs_to, [:table, :user] - remove = @recorder.inverse.first - assert_equal [:remove_reference, [:table, :user]], remove + remove = @recorder.inverse_of :add_belongs_to, [:table, :user] + assert_equal [:remove_reference, [:table, :user], nil], remove end def test_invert_remove_reference - @recorder.record :remove_reference, [:table, :taggable, { polymorphic: true }] - add = @recorder.inverse.first - assert_equal [:add_reference, [:table, :taggable, { polymorphic: true }]], add + add = @recorder.inverse_of :remove_reference, [:table, :taggable, { polymorphic: true }] + assert_equal [:add_reference, [:table, :taggable, { polymorphic: true }], nil], add end def test_invert_remove_belongs_to_alias - @recorder.record :remove_belongs_to, [:table, :user] - add = @recorder.inverse.first - assert_equal [:add_reference, [:table, :user]], add + add = @recorder.inverse_of :remove_belongs_to, [:table, :user] + assert_equal [:add_reference, [:table, :user], nil], add end end end diff --git a/activerecord/test/cases/migration/create_join_table_test.rb b/activerecord/test/cases/migration/create_join_table_test.rb index f262bbaad7..efaec0f823 100644 --- a/activerecord/test/cases/migration/create_join_table_test.rb +++ b/activerecord/test/cases/migration/create_join_table_test.rb @@ -78,6 +78,48 @@ module ActiveRecord assert_equal [%w(artist_id music_id)], connection.indexes(:artists_musics).map(&:columns) end + + def test_drop_join_table + connection.create_join_table :artists, :musics + connection.drop_join_table :artists, :musics + + assert !connection.tables.include?('artists_musics') + end + + def test_drop_join_table_with_strings + connection.create_join_table :artists, :musics + connection.drop_join_table 'artists', 'musics' + + assert !connection.tables.include?('artists_musics') + end + + def test_drop_join_table_with_the_proper_order + connection.create_join_table :videos, :musics + connection.drop_join_table :videos, :musics + + assert !connection.tables.include?('musics_videos') + end + + def test_drop_join_table_with_the_table_name + connection.create_join_table :artists, :musics, table_name: :catalog + connection.drop_join_table :artists, :musics, table_name: :catalog + + assert !connection.tables.include?('catalog') + end + + def test_drop_join_table_with_the_table_name_as_string + connection.create_join_table :artists, :musics, table_name: 'catalog' + connection.drop_join_table :artists, :musics, table_name: 'catalog' + + assert !connection.tables.include?('catalog') + end + + def test_drop_join_table_with_column_options + connection.create_join_table :artists, :musics, column_options: {null: true} + connection.drop_join_table :artists, :musics, column_options: {null: true} + + assert !connection.tables.include?('artists_musics') + end end end end diff --git a/activerecord/test/cases/migration/logger_test.rb b/activerecord/test/cases/migration/logger_test.rb index c2fdc50e52..ee0c20747e 100644 --- a/activerecord/test/cases/migration/logger_test.rb +++ b/activerecord/test/cases/migration/logger_test.rb @@ -10,8 +10,6 @@ module ActiveRecord def migrate direction # do nothing end - def filename; "anon.rb"; end - def fingerprint; "123456789012345678901234567890ab"; end end def setup diff --git a/activerecord/test/cases/migration/rename_column_test.rb b/activerecord/test/cases/migration/rename_column_test.rb index d1a85ee5e4..318d61263a 100644 --- a/activerecord/test/cases/migration/rename_column_test.rb +++ b/activerecord/test/cases/migration/rename_column_test.rb @@ -173,6 +173,16 @@ module ActiveRecord refute TestModel.new.administrator? end + def test_change_column_with_custom_index_name + add_column "test_models", "category", :string + add_index :test_models, :category, name: 'test_models_categories_idx' + + assert_equal ['test_models_categories_idx'], connection.indexes('test_models').map(&:name) + change_column "test_models", "category", :string, null: false, default: 'article' + + assert_equal ['test_models_categories_idx'], connection.indexes('test_models').map(&:name) + end + def test_change_column_default add_column "test_models", "first_name", :string connection.change_column_default "test_models", "first_name", "Tester" diff --git a/activerecord/test/cases/migration/table_and_index_test.rb b/activerecord/test/cases/migration/table_and_index_test.rb new file mode 100644 index 0000000000..8fd770abd1 --- /dev/null +++ b/activerecord/test/cases/migration/table_and_index_test.rb @@ -0,0 +1,24 @@ +require "cases/helper" + +module ActiveRecord + class Migration + class TableAndIndexTest < ActiveRecord::TestCase + def test_add_schema_info_respects_prefix_and_suffix + conn = ActiveRecord::Base.connection + + conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) + # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters + ActiveRecord::Base.table_name_prefix = 'p_' + ActiveRecord::Base.table_name_suffix = '_s' + conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) + + conn.initialize_schema_migrations_table + + assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name] + ensure + ActiveRecord::Base.table_name_prefix = "" + ActiveRecord::Base.table_name_suffix = "" + end + end + end +end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 3a861d887f..c155f29973 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -59,21 +59,12 @@ class MigrationTest < ActiveRecord::TestCase def test_migrator_versions migrations_path = MIGRATIONS_ROOT + "/valid" ActiveRecord::Migrator.migrations_paths = migrations_path - m0_path = File.join(migrations_path, "1_valid_people_have_last_names.rb") - m0_fingerprint = Digest::MD5.hexdigest(File.read(m0_path)) ActiveRecord::Migrator.up(migrations_path) assert_equal 3, ActiveRecord::Migrator.current_version assert_equal 3, ActiveRecord::Migrator.last_version assert_equal false, ActiveRecord::Migrator.needs_migration? - rows = connection.select_all("SELECT * FROM #{connection.quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)}") - assert_equal m0_fingerprint, rows[0]["fingerprint"] - assert_equal "valid_people_have_last_names", rows[0]["name"] - rows.each do |row| - assert_match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, row["migrated_at"].to_s, "missing migrated_at") # sometimes a String, sometimes a Time - end - ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid") assert_equal 0, ActiveRecord::Migrator.current_version assert_equal 3, ActiveRecord::Migrator.last_version @@ -346,7 +337,7 @@ class MigrationTest < ActiveRecord::TestCase assert_nothing_raised { Person.connection.create_table :binary_testings do |t| - t.column :data, :binary, :null => false + t.column "data", :binary, :null => false end } diff --git a/activerecord/test/cases/migrator_test.rb b/activerecord/test/cases/migrator_test.rb index 0f0384382f..199d0c584b 100644 --- a/activerecord/test/cases/migrator_test.rb +++ b/activerecord/test/cases/migrator_test.rb @@ -18,9 +18,6 @@ module ActiveRecord def up; @went_up = true; end def down; @went_down = true; end - # also used in place of a MigrationProxy - def filename; "anon.rb"; end - def fingerprint; "123456789012345678901234567890ab"; end end def setup @@ -87,6 +84,12 @@ module ActiveRecord end end + def test_finds_migrations_in_numbered_directory + migrations = ActiveRecord::Migrator.migrations [MIGRATIONS_ROOT + '/10_urban'] + assert_equal 9, migrations[0].version + assert_equal 'AddExpressions', migrations[0].name + end + def test_deprecated_constructor assert_deprecated do ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid") @@ -105,7 +108,7 @@ module ActiveRecord end def test_finds_pending_migrations - ActiveRecord::SchemaMigration.create!(:version => '1', :name => "anon", :migrated_at => Time.now) + ActiveRecord::SchemaMigration.create!(:version => '1') migration_list = [ Migration.new('foo', 1), Migration.new('bar', 3) ] migrations = ActiveRecord::Migrator.new(:up, migration_list).pending_migrations @@ -155,7 +158,7 @@ module ActiveRecord end def test_current_version - ActiveRecord::SchemaMigration.create!(:version => '1000', :name => "anon", :migrated_at => Time.now) + ActiveRecord::SchemaMigration.create!(:version => '1000') assert_equal 1000, ActiveRecord::Migrator.current_version end @@ -323,7 +326,7 @@ module ActiveRecord def test_only_loads_pending_migrations # migrate up to 1 - ActiveRecord::SchemaMigration.create!(:version => '1', :name => "anon", :migrated_at => Time.now) + ActiveRecord::SchemaMigration.create!(:version => '1') calls, migrator = migrator_class(3) migrator.migrate("valid", nil) diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 264846eedb..7ff0044bd4 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -1,5 +1,6 @@ require "cases/helper" + class SchemaDumperTest < ActiveRecord::TestCase def setup super @@ -17,15 +18,11 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_dump_schema_information_outputs_lexically_ordered_versions versions = %w{ 20100101010101 20100201010101 20100301010101 } versions.reverse.each do |v| - ActiveRecord::SchemaMigration.create!( - :version => v, :migrated_at => Time.now, - :fingerprint => "123456789012345678901234567890ab", :name => "anon") + ActiveRecord::SchemaMigration.create!(:version => v) end schema_info = ActiveRecord::Base.connection.dump_schema_information assert_match(/20100201010101.*20100301010101/m, schema_info) - target_line = %q{INSERT INTO schema_migrations (version, migrated_at, fingerprint, name) VALUES ('20100101010101',CURRENT_TIMESTAMP,'123456789012345678901234567890ab','anon');} - assert_match target_line, schema_info end def test_magic_comment @@ -39,16 +36,6 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_no_match %r{create_table "schema_migrations"}, output end - def test_schema_dump_includes_migrations - ActiveRecord::SchemaMigration.delete_all - ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/always_safe") - - output = standard_dump - assert_match %r{migrations do}, output, "Missing migrations block" - assert_match %r{migration 1001, "[0-9a-f]{32}", "always_safe"}, output, "Missing migration line" - assert_match %r{migration 1002, "[0-9a-f]{32}", "still_safe"}, output, "Missing migration line" - end - def test_schema_dump_excludes_sqlite_sequence output = standard_dump assert_no_match %r{create_table "sqlite_sequence"}, output diff --git a/activerecord/test/cases/schema_migration_test.rb b/activerecord/test/cases/schema_migration_test.rb deleted file mode 100644 index 882067a7d4..0000000000 --- a/activerecord/test/cases/schema_migration_test.rb +++ /dev/null @@ -1,54 +0,0 @@ -require "cases/helper" - -class SchemaMigrationTest < ActiveRecord::TestCase - def sm_table_name - ActiveRecord::SchemaMigration.table_name - end - - def connection - ActiveRecord::Base.connection - end - - def test_add_schema_info_respects_prefix_and_suffix - connection.drop_table(sm_table_name) if connection.table_exists?(sm_table_name) - # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters - ActiveRecord::Base.table_name_prefix = 'p_' - ActiveRecord::Base.table_name_suffix = '_s' - connection.drop_table(sm_table_name) if connection.table_exists?(sm_table_name) - - ActiveRecord::SchemaMigration.create_table - - assert_equal "p_unique_schema_migrations_s", connection.indexes(sm_table_name)[0][:name] - ensure - ActiveRecord::Base.table_name_prefix = "" - ActiveRecord::Base.table_name_suffix = "" - end - - def test_add_metadata_columns_to_exisiting_schema_migrations - # creates the old table schema from pre-Rails4.0, so we can test adding to it below - if connection.table_exists?(sm_table_name) - connection.drop_table(sm_table_name) - end - connection.create_table(sm_table_name, :id => false) do |schema_migrations_table| - schema_migrations_table.column("version", :string, :null => false) - end - - connection.insert "INSERT INTO #{connection.quote_table_name(sm_table_name)} (version) VALUES (100)" - connection.insert "INSERT INTO #{connection.quote_table_name(sm_table_name)} (version) VALUES (200)" - - ActiveRecord::SchemaMigration.create_table - - rows = connection.select_all("SELECT * FROM #{connection.quote_table_name(sm_table_name)}") - assert rows[0].has_key?("migrated_at"), "missing column `migrated_at`" - assert_match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, rows[0]["migrated_at"].to_s) # sometimes a String, sometimes a Time - assert rows[0].has_key?("fingerprint"), "missing column `fingerprint`" - assert rows[0].has_key?("name"), "missing column `name`" - end - - def test_schema_migrations_columns - ActiveRecord::SchemaMigration.create_table - - columns = connection.columns(sm_table_name).collect(&:name) - %w[version migrated_at fingerprint name].each { |col| assert columns.include?(col), "missing column `#{col}`" } - end -end diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 068f3cf3cd..295c7e13fa 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -1,5 +1,6 @@ -require "cases/helper" +require 'cases/helper' require 'models/topic' +require 'models/person' require 'bcrypt' class SerializedAttributeTest < ActiveRecord::TestCase @@ -212,4 +213,25 @@ class SerializedAttributeTest < ActiveRecord::TestCase assert_kind_of BCrypt::Password, topic.content assert_equal(true, topic.content == password, 'password should equal') end + + def test_serialize_attribute_via_select_method_when_time_zone_available + ActiveRecord::Base.time_zone_aware_attributes = true + Topic.serialize(:content, MyObject) + + myobj = MyObject.new('value1', 'value2') + topic = Topic.create(content: myobj) + + assert_equal(myobj, Topic.select(:content).find(topic.id).content) + assert_raise(ActiveModel::MissingAttributeError) { Topic.select(:id).find(topic.id).content } + ensure + ActiveRecord::Base.time_zone_aware_attributes = false + end + + def test_serialize_attribute_can_be_serialized_in_an_integer_column + insures = ['life'] + person = SerializedPerson.new(first_name: 'David', insures: insures) + assert person.save + person = person.reload + assert_equal(insures, person.insures) + end end diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 46212e49b6..46e767af1a 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -30,6 +30,11 @@ class ReplyWithTitleObject < Reply def title; ReplyTitle.new; end end +class Employee < ActiveRecord::Base + self.table_name = 'postgresql_arrays' + validates_uniqueness_of :nicknames +end + class UniquenessValidationTest < ActiveRecord::TestCase fixtures :topics, 'warehouse-things', :developers @@ -341,16 +346,28 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert w6.errors[:city].any?, "Should have errors for city" assert_equal ["has already been taken"], w6.errors[:city], "Should have uniqueness message for city" end - + def test_validate_uniqueness_with_conditions Topic.validates_uniqueness_of(:title, :conditions => Topic.where('approved = ?', true)) Topic.create("title" => "I'm a topic", "approved" => true) Topic.create("title" => "I'm an unapproved topic", "approved" => false) - + t3 = Topic.new("title" => "I'm a topic", "approved" => true) assert !t3.valid?, "t3 shouldn't be valid" - + t4 = Topic.new("title" => "I'm an unapproved topic", "approved" => false) assert t4.valid?, "t4 should be valid" end + + def test_validate_uniqueness_with_array_column + return skip "Uniqueness on arrays has only been tested in PostgreSQL so far." if !current_adapter? :PostgreSQLAdapter + + e1 = Employee.create("nicknames" => ["john", "johnny"], "commission_by_quarter" => [1000, 1200]) + assert e1.persisted?, "Saving e1" + + e2 = Employee.create("nicknames" => ["john", "johnny"], "commission_by_quarter" => [2200]) + assert !e2.persisted?, "e2 shouldn't be valid" + assert e2.errors[:nicknames].any?, "Should have errors for nicknames" + assert_equal ["has already been taken"], e2.errors[:nicknames], "Should have uniqueness message for nicknames" + end end diff --git a/activerecord/test/migrations/10_urban/9_add_expressions.rb b/activerecord/test/migrations/10_urban/9_add_expressions.rb new file mode 100644 index 0000000000..79a342e574 --- /dev/null +++ b/activerecord/test/migrations/10_urban/9_add_expressions.rb @@ -0,0 +1,11 @@ +class AddExpressions < ActiveRecord::Migration + def self.up + create_table("expressions") do |t| + t.column :expression, :string + end + end + + def self.down + drop_table "expressions" + end +end diff --git a/activerecord/test/migrations/always_safe/1001_always_safe.rb b/activerecord/test/migrations/always_safe/1001_always_safe.rb deleted file mode 100644 index 454b972507..0000000000 --- a/activerecord/test/migrations/always_safe/1001_always_safe.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AlwaysSafe < ActiveRecord::Migration - def change - # do nothing to avoid side-effect conflicts from running multiple times - end -end diff --git a/activerecord/test/migrations/always_safe/1002_still_safe.rb b/activerecord/test/migrations/always_safe/1002_still_safe.rb deleted file mode 100644 index 7398ae27a2..0000000000 --- a/activerecord/test/migrations/always_safe/1002_still_safe.rb +++ /dev/null @@ -1,5 +0,0 @@ -class StillSafe < ActiveRecord::Migration - def change - # do nothing to avoid side-effect conflicts from running multiple times - end -end diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb index e4c0278c0d..0109ef4f83 100644 --- a/activerecord/test/models/bulb.rb +++ b/activerecord/test/models/bulb.rb @@ -1,6 +1,6 @@ class Bulb < ActiveRecord::Base default_scope { where(:name => 'defaulty') } - belongs_to :car + belongs_to :car, :touch => true attr_reader :scope_after_initialize, :attributes_after_initialize diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 1c048bb6b4..683cb54a10 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -234,3 +234,8 @@ class ThreadsafeDeveloper < ActiveRecord::Base limit(1) end end + +class CachedDeveloper < ActiveRecord::Base + self.table_name = "developers" + self.cache_timestamp_format = :number +end diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index 6ad0cf6987..c602ca5eac 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -100,3 +100,24 @@ class NestedPerson < ActiveRecord::Base assign_attributes({ :best_friend_attributes => { :first_name => new_name } }) end end + +class Insure + INSURES = %W{life annuality} + + def self.load mask + INSURES.select do |insure| + (1 << INSURES.index(insure)) & mask.to_i > 0 + end + end + + def self.dump insures + numbers = insures.map { |insure| INSURES.index(insure) } + numbers.inject(0) { |sum, n| sum + (1 << n) } + end +end + +class SerializedPerson < ActiveRecord::Base + self.table_name = 'people' + + serialize :insures, Insure +end diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index ab2a63d3ea..96fef3a831 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -1,7 +1,7 @@ ActiveRecord::Schema.define do %w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings postgresql_uuids - postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent postgresql_json_data_type).each do |table_name| + postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent postgresql_json_data_type postgresql_intrange_data_type).each do |table_name| execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" end @@ -97,6 +97,16 @@ _SQL ); _SQL end + + if 't' == select_value("select 'int4range'=ANY(select typname from pg_type)") + execute <<_SQL + CREATE TABLE postgresql_intrange_data_type ( + id SERIAL PRIMARY KEY, + int_range int4range, + int_long_range int8range + ); +_SQL + end execute <<_SQL CREATE TABLE postgresql_moneys ( diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 35778d008a..46219c53db 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -115,6 +115,7 @@ ActiveRecord::Schema.define do t.integer :engines_count t.integer :wheels_count t.column :lock_version, :integer, :null => false, :default => 0 + t.timestamps end create_table :categories, :force => true do |t| @@ -493,6 +494,7 @@ ActiveRecord::Schema.define do t.integer :followers_count, :default => 0 t.references :best_friend t.references :best_friend_of + t.integer :insures, null: false, default: 0 t.timestamps end diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f5f2aa85ef..41fde553fb 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,36 @@ ## Rails 4.0.0 (unreleased) ## +* Prevent `Callbacks#set_callback` from setting the same callback twice. + + before_save :foo, :bar, :foo + + will at first call `bar`, then `foo`. `foo` will no more be called + twice. + + *Dmitriy Kiriyenko* + +* Add ActiveSupport::Logger#silence that works the same as the old Logger#silence extension. + + *DHH* + +* Remove surrogate unicode character encoding from `ActiveSupport::JSON.encode` + The encoding scheme was broken for unicode characters outside the basic multilingual plane; + since json is assumed to be `UTF-8`, and we already force the encoding to `UTF-8`, + simply pass through the un-encoded characters. + + *Brett Carter* + +* Deprecate `Time.time_with_date_fallback`, `Time.utc_time` and `Time.local_time`. + These methods were added to handle the limited range of Ruby's native Time + implementation. Those limitations no longer apply so we are deprecating them in 4.0 + and they will be removed in 4.1. + + *Andrew White* + +* Deprecate `Date#to_time_in_current_zone` and add `Date#in_time_zone`. *Andrew White* + +* Add `String#in_time_zone` method to convert a string to an ActiveSupport::TimeWithZone. *Andrew White* + * Deprecate `ActiveSupport::BasicObject` in favor of `ActiveSupport::ProxyObject`. This class is used for proxy classes. It avoids confusion with Ruby's BasicObject class. @@ -69,7 +100,7 @@ * Hash#extract! returns only those keys that present in the receiver. - {:a => 1, :b => 2}.extract!(:a, :x) # => {:a => 1} + {a: 1, b: 2}.extract!(:a, :x) # => {:a => 1} *Mikhail Dieterle* @@ -147,7 +178,7 @@ You can choose which instance of the deprecator will be used. - deprecate :method_name, :deprecator => deprecator_instance + deprecate :method_name, deprecator: deprecator_instance You can use ActiveSupport::Deprecation in your gem. @@ -165,7 +196,7 @@ def new_method end - deprecate :old_method => :new_method, :deprecator => deprecator + deprecate old_method: :new_method, deprecator: deprecator end MyGem.new.old_method diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index 2c536cbd05..4c9e59dbd2 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'].select { |path| File.file? path } + s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] s.require_path = 'lib' s.rdoc_options.concat ['--encoding', 'UTF-8'] @@ -24,4 +24,5 @@ Gem::Specification.new do |s| s.add_dependency 'multi_json', '~> 1.3' s.add_dependency 'tzinfo', '~> 0.3.33' s.add_dependency 'minitest', '~> 4.1' + s.add_dependency 'thread_safe','~> 0.1' end diff --git a/activesupport/lib/active_support/basic_object.rb b/activesupport/lib/active_support/basic_object.rb index 242b766b58..91aac6db64 100644 --- a/activesupport/lib/active_support/basic_object.rb +++ b/activesupport/lib/active_support/basic_object.rb @@ -1,7 +1,11 @@ require 'active_support/deprecation' +require 'active_support/proxy_object' module ActiveSupport - # :nodoc: - # Deprecated in favor of ActiveSupport::ProxyObject - BasicObject = Deprecation::DeprecatedConstantProxy.new('ActiveSupport::BasicObject', 'ActiveSupport::ProxyObject') + class BasicObject < ProxyObject # :nodoc: + def self.inherited(*) + ::ActiveSupport::Deprecation.warn 'ActiveSupport::BasicObject is deprecated! Use ActiveSupport::ProxyObject instead.' + super + end + end end diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 3a8353857e..e3e1845868 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -1,3 +1,4 @@ +require 'thread_safe' require 'active_support/concern' require 'active_support/descendants_tracker' require 'active_support/core_ext/class/attribute' @@ -133,6 +134,10 @@ module ActiveSupport @kind == _kind && @filter == _filter end + def duplicates?(other) + matches?(other.kind, other.filter) + end + def _update_filter(filter_options, new_options) filter_options[:if].concat(Array(new_options[:unless])) if new_options.key?(:unless) filter_options[:unless].concat(Array(new_options[:if])) if new_options.key?(:if) @@ -327,6 +332,30 @@ module ActiveSupport method.join("\n") end + def append(*callbacks) + callbacks.each { |c| append_one(c) } + end + + def prepend(*callbacks) + callbacks.each { |c| prepend_one(c) } + end + + private + + def append_one(callback) + remove_duplicates(callback) + push(callback) + end + + def prepend_one(callback) + remove_duplicates(callback) + unshift(callback) + end + + def remove_duplicates(callback) + delete_if { |c| callback.duplicates?(c) } + end + end module ClassMethods @@ -351,10 +380,18 @@ module ActiveSupport undef_method(name) if method_defined?(name) end - def __callback_runner_name(kind) + def __callback_runner_name_cache + @__callback_runner_name_cache ||= ThreadSafe::Cache.new {|cache, kind| cache[kind] = __generate_callback_runner_name(kind) } + end + + def __generate_callback_runner_name(kind) "_run__#{self.name.hash.abs}__#{kind}__callbacks" end + def __callback_runner_name(kind) + __callback_runner_name_cache[kind] + end + # This is used internally to append, prepend and skip callbacks to the # CallbackChain. def __update_callbacks(name, filters = [], block = nil) #:nodoc: @@ -412,11 +449,7 @@ module ActiveSupport Callback.new(chain, filter, type, options.dup, self) end - filters.each do |filter| - chain.delete_if {|c| c.matches?(type, filter) } - end - - options[:prepend] ? chain.unshift(*(mapped.reverse)) : chain.push(*mapped) + options[:prepend] ? chain.prepend(*mapped) : chain.append(*mapped) target.send("_#{name}_callbacks=", chain) end diff --git a/activesupport/lib/active_support/core_ext/array/wrap.rb b/activesupport/lib/active_support/core_ext/array/wrap.rb index 05b09a4c7f..1245768870 100644 --- a/activesupport/lib/active_support/core_ext/array/wrap.rb +++ b/activesupport/lib/active_support/core_ext/array/wrap.rb @@ -29,8 +29,7 @@ class Array # # [*object] # - # which for +nil+ returns <tt>[nil]</tt> (Ruby 1.8.7) or <tt>[]</tt> (Ruby - # 1.9), and calls to <tt>Array(object)</tt> otherwise. + # which for +nil+ returns <tt>[]</tt>, and calls to <tt>Array(object)</tt> otherwise. # # Thus, in this case the behavior may be different for +nil+, and the differences with # <tt>Kernel#Array</tt> explained above apply to the rest of <tt>object</tt>s. diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 439d380af7..421aa12100 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -53,19 +53,19 @@ class Date # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # and then subtracts the specified number of seconds. def ago(seconds) - to_time_in_current_zone.since(-seconds) + in_time_zone.since(-seconds) end # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # and then adds the specified number of seconds def since(seconds) - to_time_in_current_zone.since(seconds) + in_time_zone.since(seconds) end alias :in :since # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) def beginning_of_day - to_time_in_current_zone + in_time_zone end alias :midnight :beginning_of_day alias :at_midnight :beginning_of_day @@ -73,8 +73,9 @@ class Date # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59) def end_of_day - to_time_in_current_zone.end_of_day + in_time_zone.end_of_day end + alias :at_end_of_day :end_of_day def plus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 9120b0ba49..fe08ade7e0 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -75,10 +75,10 @@ class Date # # date.to_time(:utc) # => Sat Nov 10 00:00:00 UTC 2007 def to_time(form = :local) - ::Time.send("#{form}_time", year, month, day) + ::Time.send(form, year, month, day) end def xmlschema - to_time_in_current_zone.xmlschema + in_time_zone.xmlschema end end diff --git a/activesupport/lib/active_support/core_ext/date/zones.rb b/activesupport/lib/active_support/core_ext/date/zones.rb index c1b3934722..b4548671bf 100644 --- a/activesupport/lib/active_support/core_ext/date/zones.rb +++ b/activesupport/lib/active_support/core_ext/date/zones.rb @@ -2,14 +2,36 @@ require 'date' require 'active_support/core_ext/time/zones' class Date + # *DEPRECATED*: Use +Date#in_time_zone+ instead. + # # Converts Date to a TimeWithZone in the current zone if <tt>Time.zone</tt> or # <tt>Time.zone_default</tt> is set, otherwise converts Date to a Time via # Date#to_time. def to_time_in_current_zone + ActiveSupport::Deprecation.warn 'Date#to_time_in_current_zone is deprecated. Use Date#in_time_zone instead', caller + if ::Time.zone ::Time.zone.local(year, month, day) else to_time end end + + # Converts Date to a TimeWithZone in the current zone if Time.zone or Time.zone_default + # is set, otherwise converts Date to a Time via Date#to_time + # + # Time.zone = 'Hawaii' # => 'Hawaii' + # Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00 + # + # You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument, + # and the conversion will be based on that zone instead of <tt>Time.zone</tt>. + # + # Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00 + def in_time_zone(zone = ::Time.zone) + if zone + ::Time.find_zone!(zone).local(year, month, day) + else + to_time + end + end end diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index f77d444479..fca5d4d679 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -110,6 +110,7 @@ class DateTime def end_of_day change(:hour => 23, :min => 59, :sec => 59) end + alias :at_end_of_day :end_of_day # Returns a new DateTime representing the start of the hour (hh:00:00). def beginning_of_hour @@ -121,6 +122,7 @@ class DateTime def end_of_hour change(:min => 59, :sec => 59) end + alias :at_end_of_hour :end_of_hour # Adjusts DateTime to UTC by adding its offset value; offset is set to 0. # diff --git a/activesupport/lib/active_support/core_ext/integer/time.rb b/activesupport/lib/active_support/core_ext/integer/time.rb index 9fb4f6b73a..82080ffe51 100644 --- a/activesupport/lib/active_support/core_ext/integer/time.rb +++ b/activesupport/lib/active_support/core_ext/integer/time.rb @@ -1,3 +1,6 @@ +require 'active_support/duration' +require 'active_support/core_ext/numeric/time' + class Integer # Enables the use of time calculations and declarations, like <tt>45.minutes + # 2.hours + 4.years</tt>. diff --git a/activesupport/lib/active_support/core_ext/logger.rb b/activesupport/lib/active_support/core_ext/logger.rb index 16fce81445..34de766331 100644 --- a/activesupport/lib/active_support/core_ext/logger.rb +++ b/activesupport/lib/active_support/core_ext/logger.rb @@ -1,5 +1,6 @@ require 'active_support/core_ext/class/attribute_accessors' require 'active_support/deprecation' +require 'active_support/logger_silence' ActiveSupport::Deprecation.warn 'this file is deprecated and will be removed' @@ -31,27 +32,9 @@ require 'logger' # # logger.datetime_format = "%Y-%m-%d" # -# Note: This logger is deprecated in favor of ActiveSupport::BufferedLogger +# Note: This logger is deprecated in favor of ActiveSupport::Logger class Logger - ## - # :singleton-method: - # Set to false to disable the silencer - cattr_accessor :silencer - self.silencer = true - - # Silences the logger for the duration of the block. - def silence(temporary_level = Logger::ERROR) - if silencer - begin - old_logger_level, self.level = level, temporary_level - yield self - ensure - self.level = old_logger_level - end - else - yield self - end - end + include LoggerSilence alias :old_datetime_format= :datetime_format= # Logging date-time format (string passed to +strftime+). Ignored if the formatter diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index ad864765a3..5d7cb81e38 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -11,3 +11,4 @@ require 'active_support/core_ext/string/exclude' require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/inquiry' require 'active_support/core_ext/string/indent' +require 'active_support/core_ext/string/zones' diff --git a/activesupport/lib/active_support/core_ext/string/conversions.rb b/activesupport/lib/active_support/core_ext/string/conversions.rb index 9b9d83932e..9d3b81cf38 100644 --- a/activesupport/lib/active_support/core_ext/string/conversions.rb +++ b/activesupport/lib/active_support/core_ext/string/conversions.rb @@ -21,7 +21,7 @@ class String date_values[6] *= 1000000 offset = date_values.pop - ::Time.send("#{form}_time", *date_values) - offset + ::Time.send(form, *date_values) - offset end end diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 341e2deec9..6522145572 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -193,8 +193,8 @@ class String # Capitalizes the first word, turns underscores into spaces, and strips '_id'. # Like +titleize+, this is meant for creating pretty output. # - # 'employee_salary' # => "Employee salary" - # 'author_id' # => "Author" + # 'employee_salary'.humanize # => "Employee salary" + # 'author_id'.humanize # => "Author" def humanize ActiveSupport::Inflector.humanize(self) end diff --git a/activesupport/lib/active_support/core_ext/string/zones.rb b/activesupport/lib/active_support/core_ext/string/zones.rb new file mode 100644 index 0000000000..e3f20eee29 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/zones.rb @@ -0,0 +1,13 @@ +require 'active_support/core_ext/time/zones' + +class String + # Converts String to a TimeWithZone in the current zone if Time.zone or Time.zone_default + # is set, otherwise converts String to a Time via String#to_time + def in_time_zone(zone = ::Time.zone) + if zone + ::Time.find_zone!(zone).parse(self) + else + to_time + end + end +end diff --git a/activesupport/lib/active_support/core_ext/thread.rb b/activesupport/lib/active_support/core_ext/thread.rb new file mode 100644 index 0000000000..6ad0b2d69c --- /dev/null +++ b/activesupport/lib/active_support/core_ext/thread.rb @@ -0,0 +1,70 @@ +class Thread + LOCK = Mutex.new # :nodoc: + + # Returns the value of a thread local variable that has been set. Note that + # these are different than fiber local values. + # + # Thread local values are carried along with threads, and do not respect + # fibers. For example: + # + # Thread.new { + # Thread.current.thread_variable_set("foo", "bar") # set a thread local + # Thread.current["foo"] = "bar" # set a fiber local + # + # Fiber.new { + # Fiber.yield [ + # Thread.current.thread_variable_get("foo"), # get the thread local + # Thread.current["foo"], # get the fiber local + # ] + # }.resume + # }.join.value # => ['bar', nil] + # + # The value <tt>"bar"</tt> is returned for the thread local, where +nil+ is returned + # for the fiber local. The fiber is executed in the same thread, so the + # thread local values are available. + def thread_variable_get(key) + locals[key.to_sym] + end + + # Sets a thread local with +key+ to +value+. Note that these are local to + # threads, and not to fibers. Please see Thread#thread_variable_get for + # more information. + def thread_variable_set(key, value) + locals[key.to_sym] = value + end + + # Returns an an array of the names of the thread-local variables (as Symbols). + # + # thr = Thread.new do + # Thread.current.thread_variable_set(:cat, 'meow') + # Thread.current.thread_variable_set("dog", 'woof') + # end + # thr.join #=> #<Thread:0x401b3f10 dead> + # thr.thread_variables #=> [:dog, :cat] + # + # Note that these are not fiber local variables. Please see Thread#thread_variable_get + # for more details. + def thread_variables + locals.keys + end + + # Returns <tt>true</tt> if the given string (or symbol) exists as a + # thread-local variable. + # + # me = Thread.current + # me.thread_variable_set(:oliver, "a") + # me.thread_variable?(:oliver) #=> true + # me.thread_variable?(:stanley) #=> false + # + # Note that these are not fiber local variables. Please see Thread#thread_variable_get + # for more details. + def thread_variable?(key) + locals.has_key?(key.to_sym) + end + + private + + def locals + @locals || LOCK.synchronize { @locals ||= {} } + end +end unless Thread.instance_methods.include?(:thread_variable_set) diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 46c9f05c15..1f95f62229 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -3,6 +3,7 @@ require 'active_support/core_ext/time/conversions' require 'active_support/time_with_zone' require 'active_support/core_ext/time/zones' require 'active_support/core_ext/date_and_time/calculations' +require 'active_support/deprecation' class Time include DateAndTime::Calculations @@ -25,10 +26,13 @@ class Time end end + # *DEPRECATED*: Use +Time#utc+ or +Time#local+ instead. + # # Returns a new Time if requested year can be accommodated by Ruby's Time class # (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); # otherwise returns a DateTime. def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) + ActiveSupport::Deprecation.warn 'time_with_datetime_fallback is deprecated. Use Time#utc or Time#local instead', caller time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138. @@ -41,13 +45,19 @@ class Time ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec) end + # *DEPRECATED*: Use +Time#utc+ instead. + # # Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:utc</tt>. def utc_time(*args) + ActiveSupport::Deprecation.warn 'utc_time is deprecated. Use Time#utc instead', caller time_with_datetime_fallback(:utc, *args) end + # *DEPRECATED*: Use +Time#local+ instead. + # # Wraps class method +time_with_datetime_fallback+ with +utc_or_local+ set to <tt>:local</tt>. def local_time(*args) + ActiveSupport::Deprecation.warn 'local_time is deprecated. Use Time#local instead', caller time_with_datetime_fallback(:local, *args) end @@ -160,6 +170,7 @@ class Time :usec => Rational(999999999, 1000) ) end + alias :at_end_of_day :end_of_day # Returns a new Time representing the start of the hour (x:00) def beginning_of_hour @@ -175,6 +186,7 @@ class Time :usec => Rational(999999999, 1000) ) end + alias :at_end_of_hour :end_of_hour # Returns a Range representing the whole day of the current time. def all_day diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index efd351d741..fff4c776a9 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -1,5 +1,6 @@ require 'set' require 'thread' +require 'thread_safe' require 'pathname' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/attribute_accessors' @@ -517,7 +518,7 @@ module ActiveSupport #:nodoc: class ClassCache def initialize - @store = Hash.new + @store = ThreadSafe::Cache.new end def empty? diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index 6f259a093b..9cf4b2b2ba 100644 --- a/activesupport/lib/active_support/inflector/inflections.rb +++ b/activesupport/lib/active_support/inflector/inflections.rb @@ -1,3 +1,4 @@ +require 'thread_safe' require 'active_support/core_ext/array/prepend_and_append' require 'active_support/i18n' @@ -24,9 +25,10 @@ module ActiveSupport # singularization rules that is runs. This guarantees that your rules run # before any of the rules that may already have been loaded. class Inflections + @__instance__ = ThreadSafe::Cache.new + def self.instance(locale = :en) - @__instance__ ||= Hash.new { |h, k| h[k] = new } - @__instance__[locale] + @__instance__[locale] ||= new end attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms, :acronym_regex diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index 7a5c351ca8..832d1ce6d5 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -129,13 +129,7 @@ module ActiveSupport def escape(string) string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY) - json = string. - gsub(escape_regex) { |s| ESCAPED_CHARS[s] }. - gsub(/([\xC0-\xDF][\x80-\xBF]| - [\xE0-\xEF][\x80-\xBF]{2}| - [\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s| - s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&') - } + json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] } json = %("#{json}") json.force_encoding(::Encoding::UTF_8) json diff --git a/activesupport/lib/active_support/key_generator.rb b/activesupport/lib/active_support/key_generator.rb index 6beb2b6afa..71654dbb87 100644 --- a/activesupport/lib/active_support/key_generator.rb +++ b/activesupport/lib/active_support/key_generator.rb @@ -1,4 +1,4 @@ -require 'mutex_m' +require 'thread_safe' require 'openssl' module ActiveSupport @@ -28,16 +28,14 @@ module ActiveSupport class CachingKeyGenerator def initialize(key_generator) @key_generator = key_generator - @cache_keys = {}.extend(Mutex_m) + @cache_keys = ThreadSafe::Cache.new end # Returns a derived key suitable for use. The default key_size is chosen # to be compatible with the default settings of ActiveSupport::MessageVerifier. # i.e. OpenSSL::Digest::SHA1#block_length def generate_key(salt, key_size=64) - @cache_keys.synchronize do - @cache_keys["#{salt}#{key_size}"] ||= @key_generator.generate_key(salt, key_size) - end + @cache_keys["#{salt}#{key_size}"] ||= @key_generator.generate_key(salt, key_size) end end diff --git a/activesupport/lib/active_support/logger.rb b/activesupport/lib/active_support/logger.rb index 65202f99fc..4a55bbb350 100644 --- a/activesupport/lib/active_support/logger.rb +++ b/activesupport/lib/active_support/logger.rb @@ -1,7 +1,11 @@ +require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/logger_silence' require 'logger' module ActiveSupport class Logger < ::Logger + include LoggerSilence + # Broadcasts logs to multiple loggers. def self.broadcast(logger) # :nodoc: Module.new do diff --git a/activesupport/lib/active_support/logger_silence.rb b/activesupport/lib/active_support/logger_silence.rb new file mode 100644 index 0000000000..a8efdef944 --- /dev/null +++ b/activesupport/lib/active_support/logger_silence.rb @@ -0,0 +1,24 @@ +require 'active_support/concern' + +module LoggerSilence + extend ActiveSupport::Concern + + included do + cattr_accessor :silencer + self.silencer = true + end + + # Silences the logger for the duration of the block. + def silence(temporary_level = Logger::ERROR) + if silencer + begin + old_logger_level, self.level = level, temporary_level + yield self + ensure + self.level = old_logger_level + end + else + yield self + end + end +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index 2e5bcf4639..7588fdb67c 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -1,4 +1,5 @@ require 'mutex_m' +require 'thread_safe' module ActiveSupport module Notifications @@ -11,7 +12,7 @@ module ActiveSupport def initialize @subscribers = [] - @listeners_for = {} + @listeners_for = ThreadSafe::Cache.new super end @@ -44,7 +45,9 @@ module ActiveSupport end def listeners_for(name) - synchronize do + # this is correctly done double-checked locking (ThreadSafe::Cache's lookups have volatile semantics) + @listeners_for[name] || synchronize do + # use synchronisation when accessing @subscribers @listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) } end end diff --git a/activesupport/lib/active_support/time.rb b/activesupport/lib/active_support/time.rb index bcd5d78b54..92a593965e 100644 --- a/activesupport/lib/active_support/time.rb +++ b/activesupport/lib/active_support/time.rb @@ -9,21 +9,12 @@ end require 'date' require 'time' -require 'active_support/core_ext/time/marshal' -require 'active_support/core_ext/time/acts_like' -require 'active_support/core_ext/time/calculations' -require 'active_support/core_ext/time/conversions' -require 'active_support/core_ext/time/zones' - -require 'active_support/core_ext/date/acts_like' -require 'active_support/core_ext/date/calculations' -require 'active_support/core_ext/date/conversions' -require 'active_support/core_ext/date/zones' - -require 'active_support/core_ext/date_time/acts_like' -require 'active_support/core_ext/date_time/calculations' -require 'active_support/core_ext/date_time/conversions' -require 'active_support/core_ext/date_time/zones' +require 'active_support/core_ext/time' +require 'active_support/core_ext/date' +require 'active_support/core_ext/date_time' require 'active_support/core_ext/integer/time' require 'active_support/core_ext/numeric/time' + +require 'active_support/core_ext/string/conversions' +require 'active_support/core_ext/string/zones' diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index b931de3fac..0dbc198ea2 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -344,7 +344,7 @@ module ActiveSupport end def transfer_time_values_to_utc_constructor(time) - ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:nsec) ? Rational(time.nsec, 1000) : 0) + ::Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec, Rational(time.nsec, 1000)) end def duration_of_variable_length?(obj) diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index d087955587..c5fbddcb5f 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -252,7 +252,7 @@ module ActiveSupport # Time.zone = 'Hawaii' # => "Hawaii" # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00 def local(*args) - time = Time.utc_time(*args) + time = Time.utc(*args) ActiveSupport::TimeWithZone.new(nil, self, time) end diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 8810302f40..13f2e3cdaf 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -297,7 +297,7 @@ module CallbacksTest end end end - + class AroundPersonResult < MySuper attr_reader :result @@ -308,7 +308,7 @@ module CallbacksTest def tweedle_dum @result = yield end - + def tweedle_1 :tweedle_1 end @@ -316,7 +316,7 @@ module CallbacksTest def tweedle_2 :tweedle_2 end - + def save run_callbacks :save do :running @@ -410,7 +410,7 @@ module CallbacksTest ], around.history end end - + class AroundCallbackResultTest < ActiveSupport::TestCase def test_save_around around = AroundPersonResult.new @@ -607,6 +607,45 @@ module CallbacksTest end end + class OneTwoThreeSave + include ActiveSupport::Callbacks + + define_callbacks :save + + attr_accessor :record + + def initialize + @record = [] + end + + def save + run_callbacks :save do + @record << "yielded" + end + end + + def first + @record << "one" + end + + def second + @record << "two" + end + + def third + @record << "three" + end + end + + class DuplicatingCallbacks < OneTwoThreeSave + set_callback :save, :before, :first, :second + set_callback :save, :before, :first, :third + end + + class DuplicatingCallbacksInSameCall < OneTwoThreeSave + set_callback :save, :before, :first, :second, :first, :third + end + class UsingObjectTest < ActiveSupport::TestCase def test_before_object u = UsingObjectBefore.new @@ -709,5 +748,18 @@ module CallbacksTest end end end - + + class ExcludingDuplicatesCallbackTest < ActiveSupport::TestCase + def test_excludes_duplicates_in_separate_calls + model = DuplicatingCallbacks.new + model.save + assert_equal ["two", "one", "three", "yielded"], model.record + end + + def test_excludes_duplicates_in_one_call + model = DuplicatingCallbacksInSameCall.new + model.save + assert_equal ["two", "one", "three", "yielded"], model.record + end + end end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 7ae1f67785..5e89a6e00b 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -34,7 +34,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase def test_to_time assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time - assert_equal Time.local_time(2039, 2, 21), Date.new(2039, 2, 21).to_time + assert_equal Time.local(2039, 2, 21), Date.new(2039, 2, 21).to_time silence_warnings do 0.upto(138) do |year| [:utc, :local].each do |format| @@ -354,3 +354,11 @@ class DateExtBehaviorTest < ActiveSupport::TestCase end end end + +class DateExtConversionsTest < ActiveSupport::TestCase + def test_to_time_in_current_zone_is_deprecated + assert_deprecated(/to_time_in_current_zone/) do + Date.new(2012,6,7).to_time_in_current_zone + end + end +end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 3353465c1c..dfad3a90f8 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -42,7 +42,7 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase def test_to_time assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time - assert_equal Time.utc_time(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0).to_time + assert_equal Time.utc(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0).to_time # DateTimes with offsets other than 0 are returned unaltered assert_equal DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).to_time # Fractional seconds are preserved diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 6720ed42f0..fa8839bcb3 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -161,30 +161,6 @@ class StringInflectionsTest < ActiveSupport::TestCase assert_equal 97, 'abc'.ord end - def test_string_to_time - assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time - assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local) - assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time - assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local) - assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time - assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local) - assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time - assert_nil "".to_time - end - - def test_string_to_datetime - assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime - assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset - assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value - assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime - assert_nil "".to_datetime - end - - def test_string_to_date - assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date - assert_nil "".to_date - end - def test_access s = "hello" assert_equal "h", s.at(0) @@ -308,6 +284,32 @@ class StringInflectionsTest < ActiveSupport::TestCase end end +class StringConversionsTest < ActiveSupport::TestCase + def test_string_to_time + assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time + assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local) + assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time + assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local) + assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time + assert_equal Time.local(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local) + assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time + assert_nil "".to_time + end + + def test_string_to_datetime + assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime + assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset + assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value + assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime + assert_nil "".to_datetime + end + + def test_string_to_date + assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date + assert_nil "".to_date + end +end + class StringBehaviourTest < ActiveSupport::TestCase def test_acts_like_string assert 'Bambi'.acts_like_string? diff --git a/activesupport/test/core_ext/thread_test.rb b/activesupport/test/core_ext/thread_test.rb new file mode 100644 index 0000000000..b58f59a8d4 --- /dev/null +++ b/activesupport/test/core_ext/thread_test.rb @@ -0,0 +1,77 @@ +require 'abstract_unit' +require 'active_support/core_ext/thread' + +class ThreadExt < ActiveSupport::TestCase + def test_main_thread_variable_in_enumerator + assert_equal Thread.main, Thread.current + + Thread.current.thread_variable_set :foo, "bar" + + thread, value = Fiber.new { + Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)] + }.resume + + assert_equal Thread.current, thread + assert_equal Thread.current.thread_variable_get(:foo), value + end + + def test_thread_variable_in_enumerator + Thread.new { + Thread.current.thread_variable_set :foo, "bar" + + thread, value = Fiber.new { + Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)] + }.resume + + assert_equal Thread.current, thread + assert_equal Thread.current.thread_variable_get(:foo), value + }.join + end + + def test_thread_variables + assert_equal [], Thread.new { Thread.current.thread_variables }.join.value + + t = Thread.new { + Thread.current.thread_variable_set(:foo, "bar") + Thread.current.thread_variables + } + assert_equal [:foo], t.join.value + end + + def test_thread_variable? + refute Thread.new { Thread.current.thread_variable?("foo") }.join.value + t = Thread.new { + Thread.current.thread_variable_set("foo", "bar") + }.join + + assert t.thread_variable?("foo") + assert t.thread_variable?(:foo) + refute t.thread_variable?(:bar) + end + + def test_thread_variable_strings_and_symbols_are_the_same_key + t = Thread.new {}.join + t.thread_variable_set("foo", "bar") + assert_equal "bar", t.thread_variable_get(:foo) + end + + def test_thread_variable_frozen + t = Thread.new { }.join + t.freeze + assert_raises(RuntimeError) do + t.thread_variable_set(:foo, "bar") + end + end + + def test_thread_variable_security + t = Thread.new { sleep } + + assert_raises(SecurityError) do + Thread.new { $SAFE = 4; t.thread_variable_get(:foo) }.join + end + + assert_raises(SecurityError) do + Thread.new { $SAFE = 4; t.thread_variable_set(:foo, :baz) }.join + end + end +end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 0e75104fc6..70e07cefd1 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -567,45 +567,57 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end def test_time_with_datetime_fallback - assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30) - assert_equal Time.time_with_datetime_fallback(:local, 2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30) - assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0) - assert_equal Time.time_with_datetime_fallback(:local, 2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30) - assert_equal Time.time_with_datetime_fallback(:utc, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, 0) - assert_equal Time.time_with_datetime_fallback(:utc, 2005), Time.utc(2005) - assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 0, 0, 0) - assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30, 1), Time.utc(2005, 2, 21, 17, 44, 30, 1) #with usec - # This won't overflow on 64bit linux - unless time_is_64bits? - assert_equal Time.time_with_datetime_fallback(:local, 1900, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1900, 2, 21, 17, 44, 30) - assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1), - DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0) - assert_equal ::Date::ITALY, Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1).start # use Ruby's default start value - end - silence_warnings do - 0.upto(138) do |year| - [:utc, :local].each do |format| - assert_equal year, Time.time_with_datetime_fallback(format, year).year + ActiveSupport::Deprecation.silence do + assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30) + assert_equal Time.time_with_datetime_fallback(:local, 2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30) + assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0) + assert_equal Time.time_with_datetime_fallback(:local, 2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30) + assert_equal Time.time_with_datetime_fallback(:utc, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, 0) + assert_equal Time.time_with_datetime_fallback(:utc, 2005), Time.utc(2005) + assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 0, 0, 0) + assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30, 1), Time.utc(2005, 2, 21, 17, 44, 30, 1) #with usec + # This won't overflow on 64bit linux + unless time_is_64bits? + assert_equal Time.time_with_datetime_fallback(:local, 1900, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1900, 2, 21, 17, 44, 30) + assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1), + DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0) + assert_equal ::Date::ITALY, Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1).start # use Ruby's default start value + end + silence_warnings do + 0.upto(138) do |year| + [:utc, :local].each do |format| + assert_equal year, Time.time_with_datetime_fallback(format, year).year + end end end end end def test_utc_time - assert_equal Time.utc_time(2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30) - assert_equal Time.utc_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0) - assert_equal Time.utc_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, 0) + ActiveSupport::Deprecation.silence do + assert_equal Time.utc_time(2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30) + assert_equal Time.utc_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0) + assert_equal Time.utc_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, 0) + end end def test_local_time - assert_equal Time.local_time(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30) - assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30) + ActiveSupport::Deprecation.silence do + assert_equal Time.local_time(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30) + assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 2039, 2, 21, 17, 44, 30) - unless time_is_64bits? - assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1901, 2, 21, 17, 44, 30) + unless time_is_64bits? + assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil_from_format(:local, 1901, 2, 21, 17, 44, 30) + end end end + def test_time_with_datetime_fallback_deprecations + assert_deprecated(/time_with_datetime_fallback/) { Time.time_with_datetime_fallback(:utc, 2012, 6, 7) } + assert_deprecated(/utc_time/) { Time.utc_time(2012, 6, 7) } + assert_deprecated(/local_time/) { Time.local_time(2012, 6, 7) } + end + def test_last_month_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 1293f104e5..56020da035 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -953,3 +953,131 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < ActiveSupport::TestCase old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') end end + +class TimeWithZoneMethodsForDate < ActiveSupport::TestCase + def setup + @d = Date.civil(2000) + end + + def teardown + Time.zone = nil + end + + def test_in_time_zone + with_tz_default 'Alaska' do + assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @d.in_time_zone.inspect + end + with_tz_default 'Hawaii' do + assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @d.in_time_zone.inspect + end + with_tz_default nil do + assert_equal @d.to_time, @d.in_time_zone + end + end + + def test_nil_time_zone + with_tz_default nil do + assert !@d.in_time_zone.respond_to?(:period), 'no period method' + end + end + + def test_in_time_zone_with_argument + with_tz_default 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) + assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @d.in_time_zone('Alaska').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @d.in_time_zone('Hawaii').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @d.in_time_zone('UTC').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @d.in_time_zone(-9.hours).inspect + end + end + + def test_in_time_zone_with_invalid_argument + assert_raise(ArgumentError) { @d.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @d.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @d.in_time_zone(Object.new) } + end + + protected + def with_tz_default(tz = nil) + old_tz = Time.zone + Time.zone = tz + yield + ensure + Time.zone = old_tz + end +end + +class TimeWithZoneMethodsForString < ActiveSupport::TestCase + def setup + @s = "Sat, 01 Jan 2000 00:00:00" + @u = "Sat, 01 Jan 2000 00:00:00 UTC +00:00" + @z = "Fri, 31 Dec 1999 19:00:00 EST -05:00" + end + + def teardown + Time.zone = nil + end + + def test_in_time_zone + with_tz_default 'Alaska' do + assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @s.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @u.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @z.in_time_zone.inspect + end + with_tz_default 'Hawaii' do + assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @s.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @u.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @z.in_time_zone.inspect + end + with_tz_default nil do + assert_equal @s.to_time, @s.in_time_zone + assert_equal @u.to_time, @u.in_time_zone + assert_equal @z.to_time, @z.in_time_zone + end + end + + def test_nil_time_zone + with_tz_default nil do + assert !@s.in_time_zone.respond_to?(:period), 'no period method' + assert !@u.in_time_zone.respond_to?(:period), 'no period method' + assert !@z.in_time_zone.respond_to?(:period), 'no period method' + end + end + + def test_in_time_zone_with_argument + with_tz_default 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) + assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @s.in_time_zone('Alaska').inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @u.in_time_zone('Alaska').inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @z.in_time_zone('Alaska').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @s.in_time_zone('Hawaii').inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @u.in_time_zone('Hawaii').inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @z.in_time_zone('Hawaii').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @s.in_time_zone('UTC').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @u.in_time_zone('UTC').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @z.in_time_zone('UTC').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @s.in_time_zone(-9.hours).inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @u.in_time_zone(-9.hours).inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @z.in_time_zone(-9.hours).inspect + end + end + + def test_in_time_zone_with_invalid_argument + assert_raise(ArgumentError) { @s.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @u.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @z.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @s.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @u.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @z.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @s.in_time_zone(Object.new) } + assert_raise(ArgumentError) { @u.in_time_zone(Object.new) } + assert_raise(ArgumentError) { @z.in_time_zone(Object.new) } + end + + protected + def with_tz_default(tz = nil) + old_tz = Time.zone + Time.zone = tz + yield + ensure + Time.zone = old_tz + end +end diff --git a/activesupport/test/deprecation/basic_object_test.rb b/activesupport/test/deprecation/basic_object_test.rb new file mode 100644 index 0000000000..4b5bed9eb1 --- /dev/null +++ b/activesupport/test/deprecation/basic_object_test.rb @@ -0,0 +1,12 @@ +require 'abstract_unit' +require 'active_support/deprecation' +require 'active_support/basic_object' + + +class BasicObjectTest < ActiveSupport::TestCase + test 'BasicObject warns about deprecation when inherited from' do + warn = 'ActiveSupport::BasicObject is deprecated! Use ActiveSupport::ProxyObject instead.' + ActiveSupport::Deprecation.expects(:warn).with(warn).once + Class.new(ActiveSupport::BasicObject) + end +end
\ No newline at end of file diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 5bb2a45c87..b6e2cd4529 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -112,21 +112,34 @@ class TestJSONEncoding < ActiveSupport::TestCase def test_utf8_string_encoded_properly result = ActiveSupport::JSON.encode('€2.99') - assert_equal '"\\u20ac2.99"', result + assert_equal '"€2.99"', result assert_equal(Encoding::UTF_8, result.encoding) result = ActiveSupport::JSON.encode('✎☺') - assert_equal '"\\u270e\\u263a"', result + assert_equal '"✎☺"', result assert_equal(Encoding::UTF_8, result.encoding) end def test_non_utf8_string_transcodes s = '二'.encode('Shift_JIS') result = ActiveSupport::JSON.encode(s) - assert_equal '"\\u4e8c"', result + assert_equal '"二"', result assert_equal Encoding::UTF_8, result.encoding end + def test_wide_utf8_chars + w = '𠜎' + result = ActiveSupport::JSON.encode(w) + assert_equal '"𠜎"', result + end + + def test_wide_utf8_roundtrip + hash = { string: "𐒑" } + json = ActiveSupport::JSON.encode(hash) + decoded_hash = ActiveSupport::JSON.decode(json) + assert_equal "𐒑", decoded_hash['string'] + end + def test_exception_raised_when_encoding_circular_reference_in_array a = [1] a << a diff --git a/activesupport/test/logger_test.rb b/activesupport/test/logger_test.rb index eedeca30a8..d2801849ca 100644 --- a/activesupport/test/logger_test.rb +++ b/activesupport/test/logger_test.rb @@ -120,4 +120,14 @@ class LoggerTest < ActiveSupport::TestCase byte_string.force_encoding("ASCII-8BIT") assert byte_string.include?(BYTE_STRING) end + + def test_silencing_everything_but_errors + @logger.silence do + @logger.debug "NOT THERE" + @logger.error "THIS IS HERE" + end + + assert !@output.string.include?("NOT THERE") + assert @output.string.include?("THIS IS HERE") + end end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 9516556844..d6821135ea 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -122,12 +122,12 @@ end # Setup and teardown callbacks. class SetupAndTeardownTest < ActiveSupport::TestCase setup :reset_callback_record, :foo - teardown :foo, :sentinel, :foo + teardown :foo, :sentinel def test_inherited_setup_callbacks assert_equal [:reset_callback_record, :foo], self.class._setup_callbacks.map(&:raw_filter) assert_equal [:foo], @called_back - assert_equal [:foo, :sentinel, :foo], self.class._teardown_callbacks.map(&:raw_filter) + assert_equal [:foo, :sentinel], self.class._teardown_callbacks.map(&:raw_filter) end def setup @@ -147,7 +147,7 @@ class SetupAndTeardownTest < ActiveSupport::TestCase end def sentinel - assert_equal [:foo, :foo], @called_back + assert_equal [:foo], @called_back end end @@ -159,7 +159,7 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest def test_inherited_setup_callbacks assert_equal [:reset_callback_record, :foo, :bar], self.class._setup_callbacks.map(&:raw_filter) assert_equal [:foo, :bar], @called_back - assert_equal [:foo, :sentinel, :foo, :bar], self.class._teardown_callbacks.map(&:raw_filter) + assert_equal [:foo, :sentinel, :bar], self.class._teardown_callbacks.map(&:raw_filter) end protected @@ -168,7 +168,7 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest end def sentinel - assert_equal [:foo, :bar, :bar, :foo], @called_back + assert_equal [:foo, :bar, :bar], @called_back end end diff --git a/guides/source/4_0_release_notes.md b/guides/source/4_0_release_notes.md index dd57787111..55ac4bca87 100644 --- a/guides/source/4_0_release_notes.md +++ b/guides/source/4_0_release_notes.md @@ -165,6 +165,19 @@ Please refer to the [Changelog](https://github.com/rails/rails/blob/master/railt ### Notable changes +* Improve ways to write `change` migrations, making the old `up` & `down` methods no longer necessary. + + * The methods `drop_table` and `remove_column` are now reversible, as long as the necessary information is given. + The method `remove_column` used to accept multiple column names; instead use `remove_columns` (which is not revertible). + The method `change_table` is also reversible, as long as its block doesn't call `remove`, `change` or `change_default` + + * New method `reversible` makes it possible to specify code to be run when migrating up or down. + See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/migrations.md#using-the-reversible-method) + + * New method `revert` will revert a whole migration or the given block. + If migrating down, the given migration / block is run normally. + See the [Guide on Migration](https://github.com/rails/rails/blob/master/guides/source/migrations.md#reverting-previous-migrations) + * Adds some metadata columns to `schema_migrations` table. * `migrated_at` diff --git a/guides/source/action_mailer_basics.md b/guides/source/action_mailer_basics.md index aaf04f4256..6bb3439e5e 100644 --- a/guides/source/action_mailer_basics.md +++ b/guides/source/action_mailer_basics.md @@ -516,7 +516,6 @@ The following configuration options are best made in one of the environment file |`perform_deliveries`|Determines whether deliveries are actually carried out when the `deliver` method is invoked on the Mail message. By default they are, but this can be turned off to help functional testing.| |`deliveries`|Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.| |`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).| -|`async`|Setting this flag will turn on asynchronous message sending, message rendering and delivery will be pushed to `Rails.queue` for processing.| ### Example Action Mailer Configuration diff --git a/guides/source/action_view_overview.md b/guides/source/action_view_overview.md index 6c2871d478..4cdac43a7e 100644 --- a/guides/source/action_view_overview.md +++ b/guides/source/action_view_overview.md @@ -1264,7 +1264,7 @@ Creates a field set for grouping HTML form elements. Creates a file upload field. ```html+erb -<%= form_tag {action: "post"} do %> +<%= form_tag {action: "post"}, {multipart: true} do %> <label for="file">File to Upload</label> <%= file_field_tag "file" %> <%= submit_tag %> <% end %> diff --git a/guides/source/active_record_basics.md b/guides/source/active_record_basics.md index 68c6416e89..883c2dda4a 100644 --- a/guides/source/active_record_basics.md +++ b/guides/source/active_record_basics.md @@ -5,9 +5,11 @@ This guide is an introduction to Active Record. After reading this guide, you will know: -* What Object Relational Mapping and Active Record are and how they are used in Rails. +* What Object Relational Mapping and Active Record are and how they are used in + Rails. * How Active Record fits into the Model-View-Controller paradigm. -* How to use Active Record models to manipulate data stored in a relational database. +* How to use Active Record models to manipulate data stored in a relational + database. * Active Record schema naming conventions. * The concepts of database migrations, validations and callbacks. @@ -16,19 +18,34 @@ After reading this guide, you will know: What is Active Record? ---------------------- -Active Record is the M in [MVC](getting_started.html#the-mvc-architecture) - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system. +Active Record is the M in [MVC](getting_started.html#the-mvc-architecture) - the +model - which is the layer of the system responsible for representing business +data and logic. Active Record facilitates the creation and use of business +objects whose data requires persistent storage to a database. It is an +implementation of the Active Record pattern which itself is a description of an +Object Relational Mapping system. ### The Active Record Pattern -Active Record was described by Martin Fowler in his book _Patterns of Enterprise Application Architecture_. In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic is part of the object will educate users of that object on how to write to and read from the database. +Active Record was described by Martin Fowler in his book _Patterns of Enterprise +Application Architecture_. In Active Record, objects carry both persistent data +and behavior which operates on that data. Active Record takes the opinion that +ensuring data access logic is part of the object will educate users of that +object on how to write to and read from the database. ### Object Relational Mapping -Object-Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system. Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code. +Object-Relational Mapping, commonly referred to as its abbreviation ORM, is +a technique that connects the rich objects of an application to tables in +a relational database management system. Using ORM, the properties and +relationships of the objects in an application can be easily stored and +retrieved from a database without writing SQL statements directly and with less +overall database access code. ### Active Record as an ORM Framework -Active Record gives us several mechanisms, the most important being the ability to: +Active Record gives us several mechanisms, the most important being the ability +to: * Represent models and their data * Represent associations between these models @@ -39,14 +56,30 @@ Active Record gives us several mechanisms, the most important being the ability Convention over Configuration in Active Record ---------------------------------------------- -When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly true for ORM frameworks in general. However, if you follow the conventions adopted by Rails, you'll need to write very little configuration (in some case no configuration at all) when creating Active Record models. The idea is that if you configure your applications in the very same way most of the times then this should be the default way. In this cases, explicit configuration would be needed only in those cases where you can't follow the conventions for any reason. +When writing applications using other programming languages or frameworks, it +may be necessary to write a lot of configuration code. This is particularly true +for ORM frameworks in general. However, if you follow the conventions adopted by +Rails, you'll need to write very little configuration (in some case no +configuration at all) when creating Active Record models. The idea is that if +you configure your applications in the very same way most of the times then this +should be the default way. In this cases, explicit configuration would be needed +only in those cases where you can't follow the conventions for any reason. ### Naming Conventions -By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class `Book`, you should have a database table called **books**. The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the CamelCase form, while the table name must contain the words separated by underscores. Examples: +By default, Active Record uses some naming conventions to find out how the +mapping between models and database tables should be created. Rails will +pluralize your class names to find the respective database table. So, for +a class `Book`, you should have a database table called **books**. The Rails +pluralization mechanisms are very powerful, being capable to pluralize (and +singularize) both regular and irregular words. When using class names composed +of two or more words, the model class name should follow the Ruby conventions, +using the CamelCase form, while the table name must contain the words separated +by underscores. Examples: * Database Table - Plural with underscores separating words (e.g., `book_clubs`) -* Model Class - Singular with the first letter of each word capitalized (e.g., `BookClub`) +* Model Class - Singular with the first letter of each word capitalized (e.g., +`BookClub`) | Model / Class | Table / Schema | | ------------- | -------------- | @@ -59,34 +92,52 @@ By default, Active Record uses some naming conventions to find out how the mappi ### Schema Conventions -Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns. - -* **Foreign keys** - These fields should be named following the pattern `singularized_table_name_id` (e.g., `item_id`, `order_id`). These are the fields that Active Record will look for when you create associations between your models. -* **Primary keys** - By default, Active Record will use an integer column named `id` as the table's primary key. When using [Rails Migrations](migrations.html) to create your tables, this column will be automatically created. - -There are also some optional column names that will create additional features to Active Record instances: - -* `created_at` - Automatically gets set to the current date and time when the record is first created. -* `created_on` - Automatically gets set to the current date when the record is first created. -* `updated_at` - Automatically gets set to the current date and time whenever the record is updated. -* `updated_on` - Automatically gets set to the current date whenever the record is updated. -* `lock_version` - Adds [optimistic locking](http://api.rubyonrails.org/classes/ActiveRecord/Locking.html) to a model. -* `type` - Specifies that the model uses [Single Table Inheritance](http://api.rubyonrails.org/classes/ActiveRecord/Base.html) -* `(table_name)_count` - Used to cache the number of belonging objects on associations. For example, a `comments_count` column in a `Post` class that has many instances of `Comment` will cache the number of existent comments for each post. +Active Record uses naming conventions for the columns in database tables, +depending on the purpose of these columns. + +* **Foreign keys** - These fields should be named following the pattern + `singularized_table_name_id` (e.g., `item_id`, `order_id`). These are the + fields that Active Record will look for when you create associations between + your models. +* **Primary keys** - By default, Active Record will use an integer column named + `id` as the table's primary key. When using [Rails + Migrations](migrations.html) to create your tables, this column will be + automatically created. + +There are also some optional column names that will create additional features +to Active Record instances: + +* `created_at` - Automatically gets set to the current date and time when the + record is first created. +* `updated_at` - Automatically gets set to the current date and time whenever + the record is updated. +* `lock_version` - Adds [optimistic + locking](http://api.rubyonrails.org/classes/ActiveRecord/Locking.html) to + a model. +* `type` - Specifies that the model uses [Single Table + Inheritance](http://api.rubyonrails.org/classes/ActiveRecord/Base.html) +* `(table_name)_count` - Used to cache the number of belonging objects on + associations. For example, a `comments_count` column in a `Post` class that + has many instances of `Comment` will cache the number of existent comments + for each post. NOTE: While these column names are optional, they are in fact reserved by Active Record. Steer clear of reserved keywords unless you want the extra functionality. For example, `type` is a reserved keyword used to designate a table using Single Table Inheritance (STI). If you are not using STI, try an analogous keyword like "context", that may still accurately describe the data you are modeling. Creating Active Record Models ----------------------------- -It is very easy to create Active Record models. All you have to do is to subclass the `ActiveRecord::Base` class and you're good to go: +It is very easy to create Active Record models. All you have to do is to +subclass the `ActiveRecord::Base` class and you're good to go: ```ruby class Product < ActiveRecord::Base end ``` -This will create a `Product` model, mapped to a `products` table at the database. By doing this you'll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. Suppose that the `products` table was created using an SQL sentence like: +This will create a `Product` model, mapped to a `products` table at the +database. By doing this you'll also have the ability to map the columns of each +row in that table with the attributes of the instances of your model. Suppose +that the `products` table was created using an SQL sentence like: ```sql CREATE TABLE products ( @@ -96,7 +147,8 @@ CREATE TABLE products ( ); ``` -Following the table schema above, you would be able to write code like the following: +Following the table schema above, you would be able to write code like the +following: ```ruby p = Product.new @@ -107,9 +159,12 @@ puts p.name # "Some Book" Overriding the Naming Conventions --------------------------------- -What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions. +What if you need to follow a different naming convention or need to use your +Rails application with a legacy database? No problem, you can easily override +the default conventions. -You can use the `ActiveRecord::Base.table_name=` method to specify the table name that should be used: +You can use the `ActiveRecord::Base.table_name=` method to specify the table +name that should be used: ```ruby class Product < ActiveRecord::Base @@ -117,7 +172,9 @@ class Product < ActiveRecord::Base end ``` -If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the `set_fixture_class` method in your test definition: +If you do so, you will have to define manually the class name that is hosting +the fixtures (class_name.yml) using the `set_fixture_class` method in your test +definition: ```ruby class FunnyJoke < ActiveSupport::TestCase @@ -127,7 +184,8 @@ class FunnyJoke < ActiveSupport::TestCase end ``` -It's also possible to override the column that should be used as the table's primary key using the `ActiveRecord::Base.set_primary_key` method: +It's also possible to override the column that should be used as the table's +primary key using the `ActiveRecord::Base.set_primary_key` method: ```ruby class Product < ActiveRecord::Base @@ -138,19 +196,24 @@ end CRUD: Reading and Writing Data ------------------------------ -CRUD is an acronym for the four verbs we use to operate on data: **C**reate, **R**ead, **U**pdate and **D**elete. Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables. +CRUD is an acronym for the four verbs we use to operate on data: **C**reate, +**R**ead, **U**pdate and **D**elete. Active Record automatically creates methods +to allow an application to read and manipulate data stored within its tables. ### Create -Active Record objects can be created from a hash, a block or have their attributes manually set after creation. The `new` method will return a new object while `create` will return the object and save it to the database. +Active Record objects can be created from a hash, a block or have their +attributes manually set after creation. The `new` method will return a new +object while `create` will return the object and save it to the database. -For example, given a model `User` with attributes of `name` and `occupation`, the `create` method call will create and save a new record into the database: +For example, given a model `User` with attributes of `name` and `occupation`, +the `create` method call will create and save a new record into the database: ```ruby user = User.create(name: "David", occupation: "Code Artist") ``` -Using the `new` method, an object can be created without being saved: +Using the `new` method, an object can be instantiated without being saved: ```ruby user = User.new @@ -160,7 +223,8 @@ user.occupation = "Code Artist" A call to `user.save` will commit the record to the database. -Finally, if a block is provided, both `create` and `new` will yield the new object to that block for initialization: +Finally, if a block is provided, both `create` and `new` will yield the new +object to that block for initialization: ```ruby user = User.new do |u| @@ -171,7 +235,8 @@ end ### Read -Active Record provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by Active Record. +Active Record provides a rich API for accessing data within a database. Below +are a few examples of different data access methods provided by Active Record. ```ruby # return array with all records @@ -193,11 +258,13 @@ david = User.find_by_name('David') users = User.where(name: 'David', occupation: 'Code Artist').order('created_at DESC') ``` -You can learn more about querying an Active Record model in the [Active Record Query Interface](active_record_querying.html) guide. +You can learn more about querying an Active Record model in the [Active Record +Query Interface](active_record_querying.html) guide. ### Update -Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database. +Once an Active Record object has been retrieved, its attributes can be modified +and it can be saved to the database. ```ruby user = User.find_by_name('David') @@ -205,9 +272,26 @@ user.name = 'Dave' user.save ``` +A shorthand for this is to use a hash mapping attribute names to the desired +value, like so: + +```ruby +user = User.find_by_name('David') +user.update_attributes(name: 'Dave') +``` + +This is most useful when updating several attributes at once. If, on the other +hand, you'd like to update several records in bulk, you may find the +`update_all` class method useful: + +```ruby +User.update_all "max_login_attempts = 3, must_change_password = 'true'" +``` + ### Delete -Likewise, once retrieved an Active Record object can be destroyed which removes it from the database. +Likewise, once retrieved an Active Record object can be destroyed which removes +it from the database. ```ruby user = User.find_by_name('David') @@ -217,14 +301,70 @@ user.destroy Validations ----------- -Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format and many more. You can learn more about validations in the [Active Record Validations and Callbacks guide](active_record_validations_callbacks.html#validations-overview). +Active Record allows you to validate the state of a model before it gets written +into the database. There are several methods that you can use to check your +models and validate that an attribute value is not empty, is unique and not +already in the database, follows a specific format and many more. + +Validation is a very important issue to consider when persisting to database, so +the methods `create`, `save` and `update_attributes` take it into account when +running: they return `false` when validation fails and they didn't actually +perform any operation on database. All of these have a bang counterpart (that +is, `create!`, `save!` and `update_attributes!`), which are stricter in that +they raise the exception `ActiveRecord::RecordInvalid` if validation fails. +A quick example to illustrate: + +```ruby +class User < ActiveRecord::Base + validates_presence_of :name +end + +User.create # => false +User.create! # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank +``` + +You can learn more about validations in the [Active Record Validations +guide](active_record_validations.html). Callbacks --------- -Active Record callbacks allow you to attach code to certain events in the life-cycle of your models. This enables you to add behavior to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on. You can learn more about callbacks in the [Active Record Validations and Callbacks guide](active_record_validations_callbacks.html#callbacks-overview). +Active Record callbacks allow you to attach code to certain events in the +life-cycle of your models. This enables you to add behavior to your models by +transparently executing code when those events occur, like when you create a new +record, update it, destroy it and so on. You can learn more about callbacks in +the [Active Record Callbacks guide](active_record_callbacks.html). Migrations ---------- -Rails provides a domain-specific language for managing a database schema called migrations. Migrations are stored in files which are executed against any database that Active Record support using rake. Rails keeps track of which files have been committed to the database and provides rollback features. You can learn more about migrations in the [Active Record Migrations guide](migrations.html) +Rails provides a domain-specific language for managing a database schema called +migrations. Migrations are stored in files which are executed against any +database that Active Record support using `rake`. Here's a migration that +creates a table: + +```ruby +class CreatePublications < ActiveRecord::Migration + def change + create_table :publications do |t| + t.string :title + t.text :description + t.references :publication_type + t.integer :publisher_id + t.string :publisher_type + t.boolean :single_issue + + t.timestamps + end + add_index :publications, :publication_type_id + end +end +``` + +Rails keeps track of which files have been committed to the database and +provides rollback features. To actually create the table, you'd run `rake db:migrate` +and to roll it back, `rake db:rollback`. + +Note that the above code is database-agnostic: it will run in MySQL, postgresql, +Oracle and others. You can learn more about migrations in the [Active Record +Migrations guide](migrations.html) diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md index 971c1cdb25..0a93f61f6d 100644 --- a/guides/source/active_record_callbacks.md +++ b/guides/source/active_record_callbacks.md @@ -150,6 +150,7 @@ The following methods trigger callbacks: * `create!` * `decrement!` * `destroy` +* `destroy!` * `destroy_all` * `increment!` * `save` diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md index 2e2f0e4ea9..822d12aa3a 100644 --- a/guides/source/active_record_validations.md +++ b/guides/source/active_record_validations.md @@ -503,8 +503,8 @@ end ``` If you want to be sure that an association is present, you'll need to test -whether the foreign key used to map the association is present, and not the -associated object itself. +whether the associated object itself is present, and not the foreign key used +to map the association. ```ruby class LineItem < ActiveRecord::Base @@ -513,7 +513,8 @@ class LineItem < ActiveRecord::Base end ``` -You should also be sure to have a proper `:inverse_of` as well: +In order to validate associated records whose presence is required, you must +specify the `:inverse_of` option for the association: ```ruby class Order < ActiveRecord::Base @@ -908,6 +909,146 @@ class Invoice < ActiveRecord::Base end ``` +Working with Validation Errors +------------------------------ + +In addition to the `valid?` and `invalid?` methods covered earlier, Rails provides a number of methods for working with the `errors` collection and inquiring about the validity of objects. + +The following is a list of the most commonly used methods. Please refer to the `ActiveModel::Errors` documentation for a list of all the available methods. + +### `errors` + +Returns an instance of the class `ActiveModel::Errors` containing all errors. Each key is the attribute name and the value is an array of strings with all errors. + +```ruby +class Person < ActiveRecord::Base + validates :name, presence: true, length: { minimum: 3 } +end + +person = Person.new +person.valid? # => false +person.errors + # => {:name=>["can't be blank", "is too short (minimum is 3 characters)"]} + +person = Person.new(name: "John Doe") +person.valid? # => true +person.errors # => [] +``` + +### `errors[]` + +`errors[]` is used when you want to check the error messages for a specific attribute. It returns an array of strings with all error messages for the given attribute, each string with one error message. If there are no errors related to the attribute, it returns an empty array. + +```ruby +class Person < ActiveRecord::Base + validates :name, presence: true, length: { minimum: 3 } +end + +person = Person.new(name: "John Doe") +person.valid? # => true +person.errors[:name] # => [] + +person = Person.new(name: "JD") +person.valid? # => false +person.errors[:name] # => ["is too short (minimum is 3 characters)"] + +person = Person.new +person.valid? # => false +person.errors[:name] + # => ["can't be blank", "is too short (minimum is 3 characters)"] +``` + +### `errors.add` + +The `add` method lets you manually add messages that are related to particular attributes. You can use the `errors.full_messages` or `errors.to_a` methods to view the messages in the form they might be displayed to a user. Those particular messages get the attribute name prepended (and capitalized). `add` receives the name of the attribute you want to add the message to, and the message itself. + +```ruby +class Person < ActiveRecord::Base + def a_method_used_for_validation_purposes + errors.add(:name, "cannot contain the characters !@#%*()_-+=") + end +end + +person = Person.create(name: "!@#") + +person.errors[:name] + # => ["cannot contain the characters !@#%*()_-+="] + +person.errors.full_messages + # => ["Name cannot contain the characters !@#%*()_-+="] +``` + +Another way to do this is using `[]=` setter + +```ruby + class Person < ActiveRecord::Base + def a_method_used_for_validation_purposes + errors[:name] = "cannot contain the characters !@#%*()_-+=" + end + end + + person = Person.create(name: "!@#") + + person.errors[:name] + # => ["cannot contain the characters !@#%*()_-+="] + + person.errors.to_a + # => ["Name cannot contain the characters !@#%*()_-+="] +``` + +### `errors[:base]` + +You can add error messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of its attributes. Since `errors[:base]` is an array, you can simply add a string to it and it will be used as an error message. + +```ruby +class Person < ActiveRecord::Base + def a_method_used_for_validation_purposes + errors[:base] << "This person is invalid because ..." + end +end +``` + +### `errors.clear` + +The `clear` method is used when you intentionally want to clear all the messages in the `errors` collection. Of course, calling `errors.clear` upon an invalid object won't actually make it valid: the `errors` collection will now be empty, but the next time you call `valid?` or any method that tries to save this object to the database, the validations will run again. If any of the validations fail, the `errors` collection will be filled again. + +```ruby +class Person < ActiveRecord::Base + validates :name, presence: true, length: { minimum: 3 } +end + +person = Person.new +person.valid? # => false +person.errors[:name] + # => ["can't be blank", "is too short (minimum is 3 characters)"] + +person.errors.clear +person.errors.empty? # => true + +p.save # => false + +p.errors[:name] +# => ["can't be blank", "is too short (minimum is 3 characters)"] +``` + +### `errors.size` + +The `size` method returns the total number of error messages for the object. + +```ruby +class Person < ActiveRecord::Base + validates :name, presence: true, length: { minimum: 3 } +end + +person = Person.new +person.valid? # => false +person.errors.size # => 2 + +person = Person.new(name: "Andrea", email: "andrea@example.com") +person.valid? # => true +person.errors.size # => 0 +``` + Displaying Validation Errors in Views ------------------------------------- diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 1d79ee2565..23736020ec 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -2678,13 +2678,6 @@ If the receiver responds to `convert_key`, the method is called on each of the a {a: 1}.with_indifferent_access.except("a") # => {} ``` -The method `except` may come in handy for example when you want to protect some parameter that can't be globally protected with `attr_protected`: - -```ruby -params[:account] = params[:account].except(:plan_id) unless admin? -@account.update_attributes(params[:account]) -``` - There's also the bang variant `except!` that removes keys in the very receiver. NOTE: Defined in `active_support/core_ext/hash/except.rb`. @@ -3599,7 +3592,7 @@ Time.zone_default # => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...> # In Barcelona, 2010/03/28 02:00 +0100 becomes 2010/03/28 03:00 +0200 due to DST. -t = Time.local_time(2010, 3, 28, 1, 59, 59) +t = Time.local(2010, 3, 28, 1, 59, 59) # => Sun Mar 28 01:59:59 +0100 2010 t.advance(seconds: 1) # => Sun Mar 28 03:00:00 +0200 2010 @@ -3654,26 +3647,6 @@ Time.current Analogously to `DateTime`, the predicates `past?`, and `future?` are relative to `Time.current`. -Use the `local_time` class method to create time objects honoring the user time zone: - -```ruby -Time.zone_default -# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...> -Time.local_time(2010, 8, 15) -# => Sun Aug 15 00:00:00 +0200 2010 -``` - -The `utc_time` class method returns a time in UTC: - -```ruby -Time.zone_default -# => #<ActiveSupport::TimeZone:0x7f73654d4f38 @utc_offset=nil, @name="Madrid", ...> -Time.utc_time(2010, 8, 15) -# => Sun Aug 15 00:00:00 UTC 2010 -``` - -Both `local_time` and `utc_time` accept up to seven positional arguments: year, month, day, hour, min, sec, usec. Year is mandatory, month and day default to 1, and the rest default to 0. - If the time to be constructed lies beyond the range supported by `Time` in the runtime platform, usecs are discarded and a `DateTime` object is returned instead. #### Durations @@ -3692,7 +3665,7 @@ now - 1.week They translate to calls to `since` or `advance`. For example here we get the correct jump in the calendar reform: ```ruby -Time.utc_time(1582, 10, 3) + 5.days +Time.utc(1582, 10, 3) + 5.days # => Mon Oct 18 00:00:00 UTC 1582 ``` diff --git a/guides/source/asset_pipeline.md b/guides/source/asset_pipeline.md index ff6787dae5..b302ef76c6 100644 --- a/guides/source/asset_pipeline.md +++ b/guides/source/asset_pipeline.md @@ -96,28 +96,25 @@ Assets can still be placed in the `public` hierarchy. Any assets under `public` In production, Rails precompiles these files to `public/assets` by default. The precompiled copies are then served as static assets by the web server. The files in `app/assets` are never served directly in production. +### Controller Specific Assets + When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the `coffee-rails` gem is in the `Gemfile`) and a Cascading Style Sheet file (or SCSS file if `sass-rails` is in the `Gemfile`) for that controller. -For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Note that you have to set `config.assets.precompile` in `config/environments/production.rb` if you want to precomepile them and use in production mode. You can append them one by one or do something like this: - - # config/environments/production.rb - config.assets.precompile << Proc.new { |path| - if path =~ /\.(css|js)\z/ - full_path = Rails.application.assets.resolve(path).to_path - app_assets_path = Rails.root.join('app', 'assets').to_path - if full_path.starts_with? app_assets_path - puts "including asset: " + full_path - true - else - puts "excluding asset: " + full_path - false - end - else - false - end - } - -NOTE: You must have an [ExecJS](https://github.com/sstephenson/execjs#readme) supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes. +For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. By default these files will be ready to use by your application immediately using the `require_tree` directive. See [Manifest Files and Directives](#manifest-files-and-directives) for more details on require_tree. + +You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following: `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Ensure that you are not using the `require_tree` directive though, as this will result in your assets being included more than once. + +WARNING: When using asset precompilation (the production default), you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. This will result in false positives during development as these files will work just fine since assets will be compiled on the fly. When running in production however, you will see 500 errors since live compilation is turned off by default. See [Precompiling Assets](#precompiling-assets) for more information on how precompiling works. + +NOTE: You must have an ExecJS supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes. + +You can also disable the generation of asset files when generating a controller by adding the following to your `config/application.rb` configuration: + +```ruby +config.generators do |g| + g.assets false +end +``` ### Asset Organization @@ -272,7 +269,8 @@ $('#logo').attr src: "<%= asset_path('logo.png') %>" ### Manifest Files and Directives -Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ — instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if `Rails.application.config.assets.compress` is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests. +Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ — instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if `Rails.application.config.assets.compress` is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests. Compression also reduces the file size enabling the browser to download it faster. + For example, a new Rails application includes a default `app/assets/javascripts/application.js` file which contains the following lines: @@ -458,6 +456,27 @@ If you have other manifests or individual stylesheets and JavaScript files to in config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js'] ``` +Or you can opt to precompile all assets with something like this: + +```ruby +# config/environments/production.rb +config.assets.precompile << Proc.new { |path| + if path =~ /\.(css|js)\z/ + full_path = Rails.application.assets.resolve(path).to_path + app_assets_path = Rails.root.join('app', 'assets').to_path + if full_path.starts_with? app_assets_path + puts "including asset: " + full_path + true + else + puts "excluding asset: " + full_path + false + end + else + false + end +} +``` + NOTE. Always specify an expected compiled filename that ends with js or css, even if you want to add Sass or CoffeeScript files to the precompile array. The rake task also generates a `manifest.yml` that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like: diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 95adb4ff0a..dd59e2a8df 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -94,6 +94,25 @@ end NOTE: `belongs_to` associations _must_ use the singular term. If you used the pluralized form in the above example for the `customer` association in the `Order` model, you would be told that there was an "uninitialized constant Order::Customers". This is because Rails automatically infers the class name from the association name. If the association name is wrongly pluralized, then the inferred class will be wrongly pluralized too. +The corresponding migration might look like this: + +```ruby +class CreateOrders < ActiveRecord::Migration + def change + create_table :customers do |t| + t.string :name + t.timestamps + end + + create_table :orders do |t| + t.belongs_to :customer + t.datetime :order_date + t.timestamps + end + end +end +``` + ### The `has_one` Association A `has_one` association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you'd declare the supplier model like this: @@ -106,6 +125,25 @@ end ![has_one Association Diagram](images/has_one.png) +The corresponding migration might look like this: + +```ruby +class CreateSuppliers < ActiveRecord::Migration + def change + create_table :suppliers do |t| + t.string :name + t.timestamps + end + + create_table :accounts do |t| + t.belongs_to :supplier + t.string :account_number + t.timestamps + end + end +end +``` + ### The `has_many` Association A `has_many` association indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a `belongs_to` association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this: @@ -120,6 +158,25 @@ NOTE: The name of the other model is pluralized when declaring a `has_many` asso ![has_many Association Diagram](images/has_many.png) +The corresponding migration might look like this: + +```ruby +class CreateCustomers < ActiveRecord::Migration + def change + create_table :customers do |t| + t.string :name + t.timestamps + end + + create_table :orders do |t| + t.belongs_to :customer + t.datetime :order_date + t.timestamps + end + end +end +``` + ### The `has_many :through` Association A `has_many :through` association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding _through_ a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this: @@ -143,6 +200,31 @@ end ![has_many :through Association Diagram](images/has_many_through.png) +The corresponding migration might look like this: + +```ruby +class CreateAppointments < ActiveRecord::Migration + def change + create_table :physicians do |t| + t.string :name + t.timestamps + end + + create_table :patients do |t| + t.string :name + t.timestamps + end + + create_table :appointments do |t| + t.belongs_to :physician + t.belongs_to :patient + t.datetime :appointment_date + t.timestamps + end + end +end +``` + The collection of join models can be managed via the API. For example, if you assign ```ruby @@ -199,6 +281,31 @@ end ![has_one :through Association Diagram](images/has_one_through.png) +The corresponding migration might look like this: + +```ruby +class CreateAccountHistories < ActiveRecord::Migration + def change + create_table :suppliers do |t| + t.string :name + t.timestamps + end + + create_table :accounts do |t| + t.belongs_to :supplier + t.string :account_number + t.timestamps + end + + create_table :account_histories do |t| + t.belongs_to :account + t.integer :credit_rating + t.timestamps + end + end +end +``` + ### The `has_and_belongs_to_many` Association A `has_and_belongs_to_many` association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way: @@ -215,6 +322,29 @@ end ![has_and_belongs_to_many Association Diagram](images/habtm.png) +The corresponding migration might look like this: + +```ruby +class CreateAssembliesAndParts < ActiveRecord::Migration + def change + create_table :assemblies do |t| + t.string :name + t.timestamps + end + + create_table :parts do |t| + t.string :part_number + t.timestamps + end + + create_table :assemblies_parts do |t| + t.belongs_to :assembly + t.belongs_to :part + end + end +end +``` + ### Choosing Between `belongs_to` and `has_one` If you want to set up a one-to-one relationship between two models, you'll need to add `belongs_to` to one, and `has_one` to the other. How do you know which is which? diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 446f767f0c..a8e33a2956 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -276,6 +276,8 @@ config.middleware.delete ActionDispatch::BestStandardsSupport * `config.active_record.auto_explain_threshold_in_seconds` configures the threshold for automatic EXPLAINs (`nil` disables this feature). Queries exceeding the threshold get their query plan logged. Default is 0.5 in development mode. +* +config.active_record.cache_timestamp_format+ controls the format of the timestamp value in the cache key. Default is +:number+. + The MySQL adapter adds one additional configuration option: * `ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns in a MySQL database to be booleans and is true by default. diff --git a/guides/source/documents.yaml b/guides/source/documents.yaml index d7c648681d..e779407fab 100644 --- a/guides/source/documents.yaml +++ b/guides/source/documents.yaml @@ -9,6 +9,10 @@ name: Models documents: - + name: Active Record Basics + url: active_record_basics.html + description: This guide will get you started with models, persistence to database and the Active Record pattern and library. + - name: Rails Database Migrations url: migrations.html description: This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner. diff --git a/guides/source/form_helpers.md b/guides/source/form_helpers.md index ee563e72d5..8ab44ea0bb 100644 --- a/guides/source/form_helpers.md +++ b/guides/source/form_helpers.md @@ -458,7 +458,7 @@ As with other helpers, if you were to use the `select` helper on a form builder <%= f.select(:city_id, ...) %> ``` -WARNING: If you are using `select` (or similar helpers such as `collection_select`, `select_tag`) to set a `belongs_to` association you must pass the name of the foreign key (in the example above `city_id`), not the name of association itself. If you specify `city` instead of `city_id` Active Record will raise an error along the lines of ` ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) ` when you pass the `params` hash to `Person.new` or `update_attributes`. Another way of looking at this is that form helpers only edit attributes. You should also be aware of the potential security ramifications of allowing users to edit foreign keys directly. You may wish to consider the use of `attr_protected` and `attr_accessible`. For further details on this, see the [Ruby On Rails Security Guide](security.html#mass-assignment). +WARNING: If you are using `select` (or similar helpers such as `collection_select`, `select_tag`) to set a `belongs_to` association you must pass the name of the foreign key (in the example above `city_id`), not the name of association itself. If you specify `city` instead of `city_id` Active Record will raise an error along the lines of ` ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) ` when you pass the `params` hash to `Person.new` or `update_attributes`. Another way of looking at this is that form helpers only edit attributes. You should also be aware of the potential security ramifications of allowing users to edit foreign keys directly. ### Option Tags from a Collection of Arbitrary Objects diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 54200768e6..02ec024e5b 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -215,11 +215,7 @@ Open the `app/views/welcome/index.html.erb` file in your text editor and edit it ### Setting the Application Home Page -Now that we have made the controller and view, we need to tell Rails when we want Hello Rails! to show up. In our case, we want it to show up when we navigate to the root URL of our site, <http://localhost:3000>. At the moment, however, the "Welcome Aboard" smoke test is occupying that spot. - -To fix this, delete the `index.html` file located inside the `public` directory of the application. - -You need to do this because Rails will serve any static file in the `public` directory that matches a route in preference to any dynamic content you generate from the controllers. The `index.html` file is special: it will be served if a request comes in at the root route, e.g. <http://localhost:3000>. If another request such as <http://localhost:3000/welcome> happened, a static file at `public/welcome.html` would be served first, but only if it existed. +Now that we have made the controller and view, we need to tell Rails when we want Hello Rails! to show up. In our case, we want it to show up when we navigate to the root URL of our site, <http://localhost:3000>. At the moment, "Welcome Aboard" is occupying that spot. Next, you have to tell Rails where your actual home page is located. @@ -233,7 +229,6 @@ Blog::Application.routes.draw do # first created -> highest priority. # ... # You can have the root of your site routed with "root" - # just remember to delete public/index.html. # root to: "welcome#index" ``` @@ -558,7 +553,7 @@ parameter, which in our case will be the id of the post. Note that this time we had to specify the actual mapping, `posts#show` because otherwise Rails would not know which action to render. -As we did before, we need to add the `show` action in +As we did before, we need to add the `show` action in `app/controllers/posts_controller.rb` and its respective view. ```ruby diff --git a/guides/source/i18n.md b/guides/source/i18n.md index 399a4963d7..2e61bea5ea 100644 --- a/guides/source/i18n.md +++ b/guides/source/i18n.md @@ -96,7 +96,7 @@ This means, that in the `:en` locale, the key _hello_ will map to the _Hello wor The I18n library will use **English** as a **default locale**, i.e. if you don't set a different locale, `:en` will be used for looking up translations. -NOTE: The i18n library takes a **pragmatic approach** to locale keys (after [some discussion](http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en), including only the _locale_ ("language") part, like `:en`, `:pl`, not the _region_ part, like `:en-US` or `:en-GB`, which are traditionally used for separating "languages" and "regional setting" or "dialects". Many international applications use only the "language" element of a locale such as `:cs`, `:th` or `:es` (for Czech, Thai and Spanish). However, there are also regional differences within different language groups that may be important. For instance, in the `:en-US` locale you would have $ as a currency symbol, while in `:en-GB`, you would have £. Nothing stops you from separating regional and other settings in this way: you just have to provide full "English - United Kingdom" locale in a `:en-GB` dictionary. Various [Rails I18n plugins](http://rails-i18n.org/wiki) such as [Globalize2](https://github.com/joshmh/globalize2/tree/master) may help you implement it. +NOTE: The i18n library takes a **pragmatic approach** to locale keys (after [some discussion](http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en), including only the _locale_ ("language") part, like `:en`, `:pl`, not the _region_ part, like `:en-US` or `:en-GB`, which are traditionally used for separating "languages" and "regional setting" or "dialects". Many international applications use only the "language" element of a locale such as `:cs`, `:th` or `:es` (for Czech, Thai and Spanish). However, there are also regional differences within different language groups that may be important. For instance, in the `:en-US` locale you would have $ as a currency symbol, while in `:en-GB`, you would have £. Nothing stops you from separating regional and other settings in this way: you just have to provide full "English - United Kingdom" locale in a `:en-GB` dictionary. Various [Rails I18n plugins](http://rails-i18n.org/wiki) such as [Globalize3](https://github.com/svenfuchs/globalize3) may help you implement it. The **translations load path** (`I18n.load_path`) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you. diff --git a/guides/source/migrations.md b/guides/source/migrations.md index 9840e7694f..62b70b5571 100644 --- a/guides/source/migrations.md +++ b/guides/source/migrations.md @@ -56,25 +56,40 @@ Before this migration is run, there will be no table. After, the table will exist. Active Record knows how to reverse this migration as well: if we roll this migration back, it will remove the table. -On databases that support transactions with statements that change the schema , +On databases that support transactions with statements that change the schema, migrations are wrapped in a transaction. If the database does not support this then when a migration fails the parts of it that succeeded will not be rolled back. You will have to rollback the changes that were made by hand. If you wish for a migration to do something that Active Record doesn't know how -to reverse, you can use `up` and `down` instead of `change`: +to reverse, you can use `reversible`: ```ruby class ChangeProductsPrice < ActiveRecord::Migration + def change + reversible do |dir| + change_table :products do |t| + dir.up { t.change :price, :string } + dir.down { t.change :price, :integer } + end + end + end +end +``` + +Alternatively, you can use `up` and `down` instead of `change`: + +``ruby +class ChangeProductsPrice < ActiveRecord::Migration def up change_table :products do |t| - t.string :price, null: false + t.change :price, :string end end - + def down change_table :products do |t| - t.integer :price, null: false + t.change :price, :integer end end end @@ -93,7 +108,7 @@ of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example `20080906120000_create_products.rb` should define class `CreateProducts` and `20080906120001_add_details_to_products.rb` should define -`AddDetailsToProducts`. +`AddDetailsToProducts`. Of course, calculating timestamps is no fun, so Active Record provides a generator to handle making it for you: @@ -139,12 +154,8 @@ generates ```ruby class RemovePartNumberFromProducts < ActiveRecord::Migration - def up - remove_column :products, :part_number - end - - def down - add_column :products, :part_number, :string + def change + remove_column :products, :part_number, :string end end ``` @@ -170,10 +181,6 @@ As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit by editing the `db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb` file. -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 @@ -346,7 +353,7 @@ Products.connection.execute('UPDATE `products` SET `price`=`free` WHERE 1') For more details and examples of individual methods, check the API documentation. In particular the documentation for [`ActiveRecord::ConnectionAdapters::SchemaStatements`](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html) -(which provides the methods available in the `up` and `down` methods), +(which provides the methods available in the `change`, `up` and `down` methods), [`ActiveRecord::ConnectionAdapters::TableDefinition`](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html) (which provides the methods available on the object yielded by `create_table`) and @@ -362,25 +369,82 @@ definitions: * `add_column` * `add_index` +* `add_reference` * `add_timestamps` * `create_table` +* `create_join_table` +* `drop_table` (must supply a block) +* `drop_join_table` (must supply a block) * `remove_timestamps` * `rename_column` * `rename_index` +* `remove_reference` * `rename_table` -If you're going to need to use any other methods, you'll have to write the -`up` and `down` methods instead of using the `change` method. +`change_table` is also reversible, as long as the block does not call `change`, +`change_default` or `remove`. + +If you're going to need to use any other methods, you should use `reversible` +or write the `up` and `down` methods instead of using the `change` method. + +### Using `reversible` + +Complex migrations may require processing that Active Record doesn't know how +to reverse. You can use `reversible` to specify what to do when running a +migration what else to do when reverting it. For example, + +```ruby +class ExampleMigration < ActiveRecord::Migration + def change + create_table :products do |t| + t.references :category + end + + reversible do |dir| + dir.up do + #add a foreign key + execute <<-SQL + ALTER TABLE products + ADD CONSTRAINT fk_products_categories + FOREIGN KEY (category_id) + REFERENCES categories(id) + SQL + end + dir.down do + execute <<-SQL + ALTER TABLE products + DROP FOREIGN KEY fk_products_categories + SQL + end + end + + add_column :users, :home_page_url, :string + rename_column :users, :email, :email_address + end +``` + +Using `reversible` will insure that the instructions are executed in the +right order too. If the previous example migration is reverted, +the `down` block will be run after the `home_page_url` column is removed and +right before the table `products` is dropped. + +Sometimes your migration will do something which is just plain irreversible; for +example, it might destroy some data. In such cases, you can raise +`ActiveRecord::IrreversibleMigration` in your `down` block. If someone tries +to revert your migration, an error message will be displayed saying that it +can't be done. ### Using the `up`/`down` Methods +You can also use the old style of migration using `up` and `down` methods +instead of the `change` method. The `up` method should describe the transformation you'd like to make to your schema, and the `down` method of your migration should revert the transformations done by the `up` method. In other words, the database schema should be unchanged if you do an `up` followed by a `down`. For example, if you create a table in the `up` method, you should drop it in the `down` method. It is wise to reverse the transformations in precisely the reverse order they were -made in the `up` method. For example, +made in the `up` method. The example in the `reversible` section is equivalent to: ```ruby class ExampleMigration < ActiveRecord::Migration @@ -415,19 +479,92 @@ class ExampleMigration < ActiveRecord::Migration end ``` -Sometimes your migration will do something which is just plain irreversible; for -example, it might destroy some data. In such cases, you can raise +If your migration is irreversible, you should raise `ActiveRecord::IrreversibleMigration` from your `down` method. If someone tries to revert your migration, an error message will be displayed saying that it can't be done. +### Reverting Previous Migrations + +You can use Active Record's ability to rollback migrations using the `revert` method: + +```ruby +require_relative '2012121212_example_migration' + +class FixupExampleMigration < ActiveRecord::Migration + def change + revert ExampleMigration + + create_table(:apples) do |t| + t.string :variety + end + end +end +``` + +The `revert` method also accepts a block of instructions to reverse. +This could be useful to revert selected parts of previous migrations. +For example, let's imagine that `ExampleMigration` is committed and it +is later decided it would be best to serialize the product list instead. +One could write: + +```ruby +class SerializeProductListMigration < ActiveRecord::Migration + def change + add_column :categories, :product_list + + reversible do |dir| + dir.up do + # transfer data from Products to Category#product_list + end + dir.down do + # create Products from Category#product_list + end + end + + revert do + # copy-pasted code from ExampleMigration + create_table :products do |t| + t.references :category + end + + reversible do |dir| + dir.up do + #add a foreign key + execute <<-SQL + ALTER TABLE products + ADD CONSTRAINT fk_products_categories + FOREIGN KEY (category_id) + REFERENCES categories(id) + SQL + end + dir.down do + execute <<-SQL + ALTER TABLE products + DROP FOREIGN KEY fk_products_categories + SQL + end + end + + # The rest of the migration was ok + end + end +end +``` + +The same migration could also have been written without using `revert` +but this would have involved a few more steps: reversing the order +of `create_table` and `reversible`, replacing `create_table` +by `drop_table`, and finally replacing `up` by `down` and vice-versa. +This is all taken care of by `revert`. + Running Migrations ------------------ Rails provides a set of Rake tasks to run certain sets of migrations. The very first migration related Rake task you will use will probably be -`rake db:migrate`. In its most basic form it just runs the `up` or `change` +`rake db:migrate`. In its most basic form it just runs the `change` or `up` method for all the migrations that have not yet been run. If there are no such migrations, it exits. It will run these migrations in order based on the date of the migration. @@ -436,7 +573,7 @@ Note that running the `db:migrate` also invokes the `db:schema:dump` task, which will update your `db/schema.rb` file to match the structure of your database. If you specify a target version, Active Record will run the required migrations -(up, down or change) until it has reached the specified version. The version +(change, up, down) until it has reached the specified version. The version is the numerical prefix on the migration's filename. For example, to migrate to version 20080906120000 run @@ -445,7 +582,8 @@ $ rake db:migrate VERSION=20080906120000 ``` If version 20080906120000 is greater than the current version (i.e., it is -migrating upwards), this will run the `up` method on all migrations up to and +migrating upwards), this will run the `change` (or `up`) method +on all migrations up to and including 20080906120000, and will not execute any later migrations. If migrating downwards, this will run the `down` method on all the migrations down to, but not including, 20080906120000. @@ -460,14 +598,15 @@ number associated with the previous migration you can run $ rake db:rollback ``` -This will run the `down` method from the latest migration. If you need to undo +This will rollback the latest migration, either by reverting the `change` +method or by running the `down` method. If you need to undo several migrations you can provide a `STEP` parameter: ```bash $ rake db:rollback STEP=3 ``` -will run the `down` method from the last 3 migrations. +will revert the last 3 migrations. The `db:migrate:redo` task is a shortcut for doing a rollback and then migrating back up again. As with the `db:rollback` task, you can use the `STEP` parameter @@ -495,14 +634,15 @@ contents of the current schema.rb file. If a migration can't be rolled back, If you need to run a specific migration up or down, the `db:migrate:up` and `db:migrate:down` tasks will do that. Just specify the appropriate version and -the corresponding migration will have its `up` or `down` method invoked, for -example, +the corresponding migration will have its `change`, `up` or `down` method +invoked, for example, ```bash $ rake db:migrate:up VERSION=20080906120000 ``` -will run the `up` method from the 20080906120000 migration. This task will +will run the 20080906120000 migration by running the `change` method (or the +`up` method). This task will first check whether the migration is already performed and will do nothing if Active Record believes that it has already been run. @@ -596,6 +736,10 @@ you require. Editing a freshly generated migration that has not yet been committed to source control (or, more generally, which has not been propagated beyond your development machine) is relatively harmless. +The `revert` method can be helpful when writing a new migration to undo +previous migrations in whole or in part +(see [Reverting Previous Migrations](#reverting-previous-migrations) above). + Using Models in Your Migrations ------------------------------- @@ -622,6 +766,9 @@ column. class AddFlagToProduct < ActiveRecord::Migration def change add_column :products, :flag, :boolean + reversible do |dir| + dir.up { Product.update_all flag: false } + end Product.update_all flag: false end end @@ -645,7 +792,9 @@ column. class AddFuzzToProduct < ActiveRecord::Migration def change add_column :products, :fuzz, :string - Product.update_all fuzz: 'fuzzy' + reversible do |dir| + dir.up { Product.update_all fuzz: 'fuzzy' } + end end end ``` @@ -697,7 +846,9 @@ class AddFlagToProduct < ActiveRecord::Migration def change add_column :products, :flag, :boolean Product.reset_column_information - Product.update_all flag: false + reversible do |dir| + dir.up { Product.update_all flag: false } + end end end ``` @@ -712,7 +863,9 @@ class AddFuzzToProduct < ActiveRecord::Migration def change add_column :products, :fuzz, :string Product.reset_column_information - Product.update_all fuzz: 'fuzzy' + reversible do |dir| + dir.up { Product.update_all fuzz: 'fuzzy' } + end end end ``` @@ -810,9 +963,9 @@ Rake task) into `db/structure.sql`. For example, for PostgreSQL, the `pg_dump` utility is used. For MySQL, this file will contain the output of `SHOW CREATE TABLE` for the various tables. -Loading these schemas is simply a question of executing the SQL statements they -contain. By definition, this will create a perfect copy of the database's -structure. Using the `:sql` schema format will, however, prevent loading the +Loading these schemas is simply a question of executing the SQL statements they +contain. By definition, this will create a perfect copy of the database's +structure. Using the `:sql` schema format will, however, prevent loading the schema into a RDBMS other than the one used to create it. ### Schema Dumps and Source Control diff --git a/guides/source/routing.md b/guides/source/routing.md index 241a7cfec6..14f23d4020 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -471,7 +471,7 @@ resources :photos do end ``` -This will recognize `/photos/1/preview` with GET, and route to the `preview` action of `PhotosController`. It will also create the `preview_photo_url` and `preview_photo_path` helpers. +This will recognize `/photos/1/preview` with GET, and route to the `preview` action of `PhotosController`, with the resource id value passed in `params[:id]`. It will also create the `preview_photo_url` and `preview_photo_path` helpers. Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use `get`, `patch`, `put`, `post`, or `delete` here. If you don't have multiple `member` routes, you can also pass `:on` to a route, eliminating the block: @@ -481,6 +481,8 @@ resources :photos do end ``` +You can leave out the `:on` option, this will create the same member route except that the resource id value will be available in `params[:photo_id]` instead of `params[:id]`. + #### Adding Collection Routes To add a route to the collection: @@ -791,7 +793,7 @@ root to: 'pages#main' root 'pages#main' # shortcut for the above ``` -You should put the `root` route at the top of the file, because it is the most popular route and should be matched first. You also need to delete the `public/index.html` file for the root route to take effect. +You should put the `root` route at the top of the file, because it is the most popular route and should be matched first. NOTE: The `root` route only routes `GET` requests to the action. diff --git a/guides/source/security.md b/guides/source/security.md index 532a1ae5cc..0b0cfe69c4 100644 --- a/guides/source/security.md +++ b/guides/source/security.md @@ -94,16 +94,15 @@ Rails 2 introduced a new default session storage, CookieStore. CookieStore saves * The client can see everything you store in a session, because it is stored in clear-text (actually Base64-encoded, so not encrypted). So, of course, _you don't want to store any secrets here_. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie. -That means the security of this storage depends on this secret (and on the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So _don't use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters_. Put the secret in your environment.rb: +That means the security of this storage depends on this secret (and on the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So _don't use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters_. -```ruby -config.action_dispatch.session = { - key: '_app_session', - secret: '0x0dkfj3927dkc7djdh36rkckdfzsg...' -} -``` +`config.secret_key_base` is used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get `config.secret_key_base` initialized to a random key in `config/initializers/secret_token.rb`, e.g.: + + YourApp::Application.config.secret_key_base = '49d3f3de9ed86c74b94ad6bd0...' -There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it. +Older versions of Rails use CookieStore, which uses `secret_token` instead of `secret_key_base` that is used by EncryptedCookieStore. Read the upgrade documentation for more information. + +If you have received an application where the secret was exposed (e.g. an application whose source was shared), strongly consider changing the secret. ### Replay Attacks for CookieStore Sessions @@ -374,141 +373,6 @@ The common admin interface works like this: it's located at www.example.com/admi * _Put the admin interface to a special sub-domain_ such as admin.application.com and make it a separate application with its own user management. This makes stealing an admin cookie from the usual domain, www.application.com, impossible. This is because of the same origin policy in your browser: An injected (XSS) script on www.application.com may not read the cookie for admin.application.com and vice-versa. -Mass Assignment ---------------- - -WARNING: _Without any precautions `Model.new(params[:model]`) allows attackers to set -any database column's value._ - -The mass-assignment feature may become a problem, as it allows an attacker to set -any model's attributes by manipulating the hash passed to a model's `new()` method: - -```ruby -def signup - params[:user] # => {name:"ow3ned", admin:true} - @user = User.new(params[:user]) -end -``` - -Mass-assignment saves you much work, because you don't have to set each value -individually. Simply pass a hash to the `new` method, or `assign_attributes=` -a hash value, to set the model's attributes to the values in the hash. The -problem is that it is often used in conjunction with the parameters (params) -hash available in the controller, which may be manipulated by an attacker. -He may do so by changing the URL like this: - -``` -http://www.example.com/user/signup?user[name]=ow3ned&user[admin]=1 -``` - -This will set the following parameters in the controller: - -```ruby -params[:user] # => {name:"ow3ned", admin:true} -``` - -So if you create a new user using mass-assignment, it may be too easy to become -an administrator. - -Note that this vulnerability is not restricted to database columns. Any setter -method, unless explicitly protected, is accessible via the `attributes=` method. -In fact, this vulnerability is extended even further with the introduction of -nested mass assignment (and nested object forms) in Rails 2.3. The -`accepts_nested_attributes_for` declaration provides us the ability to extend -mass assignment to model associations (`has_many`, `has_one`, -`has_and_belongs_to_many`). For example: - -```ruby - class Person < ActiveRecord::Base - has_many :children - - accepts_nested_attributes_for :children - end - - class Child < ActiveRecord::Base - belongs_to :person - end -``` - -As a result, the vulnerability is extended beyond simply exposing column -assignment, allowing attackers the ability to create entirely new records -in referenced tables (children in this case). - -### Countermeasures - -To avoid this, Rails provides an interface for protecting attributes from -end-user assignment called Strong Parameters. This makes Action Controller -parameters forbidden until they have been whitelisted, so you will have to -make a conscious choice about which attributes to allow for mass assignment -and thus prevent accidentally exposing that which shouldn’t be exposed. - -NOTE. Before Strong Parameters arrived, mass-assignment protection was a -model's task provided by Active Model. This has been extracted to the -[ProtectedAttributes](https://github.com/rails/protected_attributes) -gem. In order to use `attr_accessible` and `attr_protected` helpers in -your models, you should add `protected_attributes` to your Gemfile. - -Why we moved mass-assignment protection out of the model and into -the controller? The whole point of the controller is to control the -flow between user and application, including authentication, authorization, -and, as part of that, access control. - -Strong Parameters provides two methods to the `params` hash to control -access to your attributes: `require` and `permit`. The former is used -to mark parameters as required and the latter limits which attributes -should be allowed for mass updating using the slice pattern. For example: - -```ruby -def signup - params[:user] - # => {name:"ow3ned", admin:true} - permitted_params = params.require(:user).permit(:name) - # => {name:"ow3ned"} - - @user = User.new(permitted_params) -end -``` - -In the example above, `require` is checking whether a `user` key is present or not -in the parameters, if it's not present, it'll raise an `ActionController::MissingParameter` -exception, which will be caught by `ActionController::Base` and turned into a -400 Bad Request reply. Then `permit` whitelists the attributes that should be -allowed for mass assignment. - -A good pattern to encapsulate the permissible parameters is to use a private method -since you'll be able to reuse the same permit list between different actions. - -```ruby -def signup - @user = User.new(user_params) - # ... -end - -def update - @user = User.find(params[:id] - @user.update_attributes!(user_params) - # ... -end - -private - def user_params - params.require(:user).permit(:name) - end -``` - -Also, you can specialize this method with per-user checking of permissible -attributes. - -```ruby -def user_params - if current_user.admin? - params.require(:user).permit(:name, :admin) - else - params.require(:user).permit(:name) - end -end -``` - User Management --------------- @@ -689,7 +553,6 @@ NOTE: _When sanitizing, protecting or verifying something, whitelists over black A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although sometimes it is not possible to create a whitelist (in a SPAM filter, for example), _prefer to use whitelist approaches_: * Use before_action only: [...] instead of except: [...]. This way you don't forget to turn it off for newly added actions. -* Use attr_accessible instead of attr_protected. See the mass-assignment section for details * Allow <strong> instead of removing <script> against Cross-Site Scripting (XSS). See below for details. * Don't try to correct user input by blacklists: * This will make the attack work: "<sc<script>ript>".gsub("<script>", "") @@ -1095,6 +958,11 @@ Used to control which sites are allowed to bypass same origin policies and send * Strict-Transport-Security [Used to control if the browser is allowed to only access a site over a secure connection](http://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security) +Environmental Security +---------------------- + +It is beyond the scope of this guide to inform you on how to secure your application code and environments. However, please secure your database configuration, e.g. `config/database.yml`, and your server-side secret, e.g. stored in `config/initializers/secret_token.rb`. You may want to further restrict access, using environment-specific versions of these files and any others that may contain sensitive information. + Additional Resources -------------------- diff --git a/guides/source/upgrading_ruby_on_rails.md b/guides/source/upgrading_ruby_on_rails.md index bdb4730cdc..b4a59fe3da 100644 --- a/guides/source/upgrading_ruby_on_rails.md +++ b/guides/source/upgrading_ruby_on_rails.md @@ -3,10 +3,6 @@ A Guide for Upgrading Ruby on Rails This guide provides steps to be followed when you upgrade your applications to a newer version of Ruby on Rails. These steps are also available in individual release guides. -After reading this guide, you will know: - --------------------------------------------------------------------------------- - General Advice -------------- @@ -31,7 +27,7 @@ Upgrading from Rails 3.2 to Rails 4.0 NOTE: This section is a work in progress. -If your application is currently on any version of Rails older than 3.2.x, you should upgrade to Rails 3.2 before attempting an update to Rails 4.0. +If your application is currently on any version of Rails older than 3.2.x, you should upgrade to Rails 3.2 before attempting one to Rails 4.0. The following changes are meant for upgrading your application to Rails 4.0. @@ -39,28 +35,21 @@ The following changes are meant for upgrading your application to Rails 4.0. Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must replace any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`. -### Identity Map - -Rails 4.0 has removed the identity map from Active Record, due to [some inconsistencies with associations](https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6). If you have manually enabled it in your application, you will have to remove the following config that has no effect anymore: `config.active_record.identity_map`. - ### Active Record -The `delete` method in collection associations can now receive `Fixnum` or `String` arguments as record ids, besides records, pretty much like the `destroy` method does. Previously it raised `ActiveRecord::AssociationTypeMismatch` for such arguments. From Rails 4.0 on `delete` automatically tries to find the records matching the given ids before deleting them. +* Rails 4.0 has removed the identity map from Active Record, due to [some inconsistencies with associations](https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6). If you have manually enabled it in your application, you will have to remove the following config that has no effect anymore: `config.active_record.identity_map`. + +* The `delete` method in collection associations can now receive `Fixnum` or `String` arguments as record ids, besides records, pretty much like the `destroy` method does. Previously it raised `ActiveRecord::AssociationTypeMismatch` for such arguments. From Rails 4.0 on `delete` automatically tries to find the records matching the given ids before deleting them. -Rails 4.0 has changed how orders get stacked in `ActiveRecord::Relation`. In previous versions of rails new order was applied after previous defined order. But this is no long true. Check [Active Record Query guide](active_record_querying.html#ordering) for more information. +* Rails 4.0 has changed how orders get stacked in `ActiveRecord::Relation`. In previous versions of Rails, the new order was applied after the previously defined order. But this is no longer true. Check [Active Record Query guide](active_record_querying.html#ordering) for more information. -Rails 4.0 has changed `serialized_attributes` and `attr_readonly` to class methods only. Now you shouldn't use instance methods, it's deprecated. You must change them, e.g. `self.serialized_attributes` to `self.class.serialized_attributes`. +* Rails 4.0 has changed `serialized_attributes` and `attr_readonly` to class methods only. Now you shouldn't use instance methods, it's deprecated. You must change them, e.g. `self.serialized_attributes` to `self.class.serialized_attributes`. ### Active Model -Rails 4.0 has changed how errors attach with the `ActiveModel::Validations::ConfirmationValidator`. -Now when confirmation validations fail the error will be attached to -`:#{attribute}_confirmation` instead of `attribute`. +* Rails 4.0 has changed how errors attach with the `ActiveModel::Validations::ConfirmationValidator`. Now when confirmation validations fail the error will be attached to `:#{attribute}_confirmation` instead of `attribute`. -Rails 4.0 has changed `ActiveModel::Serializers::JSON.include_root_in_json` default -value to `false`. Now, Active Model Serializers and Active Record objects have the -same default behaviour. This means that you can comment or remove the following option -in the `config/initializers/wrap_parameters.rb` file: +* Rails 4.0 has changed `ActiveModel::Serializers::JSON.include_root_in_json` default value to `false`. Now, Active Model Serializers and Active Record objects have the same default behaviour. This means that you can comment or remove the following option in the `config/initializers/wrap_parameters.rb` file: ```ruby # Disable root element in JSON by default. @@ -71,20 +60,17 @@ in the `config/initializers/wrap_parameters.rb` file: ### Action Pack -There is an upgrading cookie store `UpgradeSignatureToEncryptionCookieStore` which helps you upgrading apps that use `CookieStore` to the new default `EncryptedCookieStore`. To use this CookieStore set `Myapp::Application.config.session_store :upgrade_signature_to_encryption_cookie_store, key: '_myapp_session'` in `config/initializers/session_store.rb`. Additionally, add `Myapp::Application.config.secret_key_base = 'some secret'` in config/initializers/secret_token.rb (use `rake secret` to generate a value). Do not remove `Myapp::Application.config.secret_token = 'some secret'`. +* There is an upgrading cookie store `UpgradeSignatureToEncryptionCookieStore` which helps you upgrading apps that use `CookieStore` to the new default `EncryptedCookieStore`. To use this `CookieStore` set `Myapp::Application.config.session_store :upgrade_signature_to_encryption_cookie_store, key: '_myapp_session'` in `config/initializers/session_store.rb`. Additionally, add `Myapp::Application.config.secret_key_base = 'some secret'` in `config/initializers/secret_token.rb`. Do not remove `Myapp::Application.config.secret_token = 'some secret'`. -Rails 4.0 removed the `ActionController::Base.asset_path` option. Use the assets pipeline feature. +* Rails 4.0 removed the `ActionController::Base.asset_path` option. Use the assets pipeline feature. -Rails 4.0 has deprecated `ActionController::Base.page_cache_extension` option. Use -`ActionController::Base.default_static_extension` instead. +* Rails 4.0 has deprecated `ActionController::Base.page_cache_extension` option. Use `ActionController::Base.default_static_extension` instead. -Rails 4.0 has removed Action and Page caching from Action Pack. You will need to -add the `actionpack-action_caching` gem in order to use `caches_action` and -the `actionpack-page_caching` to use `caches_pages` in your controllers. +* Rails 4.0 has removed Action and Page caching from Action Pack. You will need to add the `actionpack-action_caching` gem in order to use `caches_action` and the `actionpack-page_caching` to use `caches_pages` in your controllers. -Rails 4.0 changed how `assert_generates`, `assert_recognizes`, and `assert_routing` work. Now all these assertions raise `Assertion` instead of `ActionController::RoutingError`. +* Rails 4.0 changed how `assert_generates`, `assert_recognizes`, and `assert_routing` work. Now all these assertions raise `Assertion` instead of `ActionController::RoutingError`. -Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, for example: +* Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, for example: ```ruby get Rack::Utils.escape('こんにちは'), controller: 'welcome', action: 'index' @@ -98,11 +84,11 @@ get 'こんにちは', controller: 'welcome', action: 'index' ### Active Support -Rails 4.0 Removed the `j` alias for `ERB::Util#json_escape` since `j` is already used for `ActionView::Helpers::JavaScriptHelper#escape_javascript`. +Rails 4.0 removes the `j` alias for `ERB::Util#json_escape` since `j` is already used for `ActionView::Helpers::JavaScriptHelper#escape_javascript`. ### Helpers Loading Order -The loading order of helpers from more than one directory has changed in Rails 4.0. Previously, helpers from all directories were gathered and then sorted alphabetically. After upgrade to Rails 4.0 helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use `helpers_path` parameter, this change will only impact the way of loading helpers from engines. If you rely on the fact that particular helper from engine loads before or after another helper from application or another engine, you should check if correct methods are available after upgrade. If you would like to change order in which engines are loaded, you can use `config.railties_order=` method. +The order in which helpers from more than one directory are loaded has changed in Rails 4.0. Previously, they were gathered and then sorted alphabetically. After upgrading to Rails 4.0, helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use the `helpers_path` parameter, this change will only impact the way of loading helpers from engines. If you rely on the ordering, you should check if correct methods are available after upgrade. If you would like to change the order in which engines are loaded, you can use `config.railties_order=` method. Upgrading from Rails 3.1 to Rails 3.2 ------------------------------------- diff --git a/rails.gemspec b/rails.gemspec index d7a1e10459..ba94354bf1 100644 --- a/rails.gemspec +++ b/rails.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.bindir = 'bin' s.executables = [] - s.files = Dir['guides/**/*'].select { |path| File.file? path } + s.files = Dir['guides/**/*'] s.add_dependency 'activesupport', version s.add_dependency 'actionpack', version diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 01dd86c23e..e60c26d132 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,9 +1,27 @@ ## Rails 4.0.0 (unreleased) ## -* Add ENV['RACK_ENV'] support to `rails runner/console/server`. +* Generated migrations now always use the `change` method. + + *Marc-André Lafortune* + +* Add `app/models/concerns` and `app/controllers/concerns` to the default directory structure and load path. + See http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns for usage instructions. + + *DHH* + +* The `rails/info/routes` now correctly formats routing output as an html table. + + *Richard Schneeman* + +* The `public/index.html` is no longer generated for new projects. + Page is replaced by internal `welcome_controller` inside of railties. + + *Richard Schneeman* + +* Add `ENV['RACK_ENV']` support to `rails runner/console/server`. *kennyj* - + * Add `db` to list of folders included by `rake notes` and `rake notes:custom`. *Antonio Cangiano* * Engines with a dummy app include the rake tasks of dependencies in the app namespace. @@ -11,22 +29,18 @@ *Yves Senn* -* Add sqlserver.yml template file to satisfy '-d sqlserver' being passed to 'rails new'. +* Add `sqlserver.yml` template file to satisfy `-d sqlserver` being passed to `rails new`. Fix #6882 - *Robert Nesius* + *Robert Nesius* * Rake test:uncommitted finds git directory in ancestors *Nicolas Despres* -* Add dummy app Rake tasks when --skip-test-unit and --dummy-path is passed to the plugin generator. +* Add dummy app Rake tasks when `--skip-test-unit` and `--dummy-path` is passed to the plugin generator. Fix #8121 *Yves Senn* -* Ensure that RAILS_ENV is set when accessing Rails.env *Steve Klabnik* - -* Don't eager-load app/assets and app/views *Elia Schito* - * Add `.rake` to list of file extensions included by `rake notes` and `rake notes:custom`. *Brent J. Nordquist* * New test locations `test/models`, `test/helpers`, `test/controllers`, and diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index b6a9eccdb6..ac8e84e366 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -21,7 +21,8 @@ end module Rails autoload :Info, 'rails/info' - autoload :InfoController, 'rails/info_controller' + autoload :InfoController, 'rails/info_controller' + autoload :WelcomeController, 'rails/welcome_controller' class << self attr_accessor :application, :cache, :logger @@ -71,10 +72,7 @@ module Rails end def env - @_env ||= begin - ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development" - ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"]) - end + @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development") end def env=(environment) diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 2d87b8594a..09902ad597 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -25,6 +25,7 @@ module Rails get '/rails/info/properties' => "rails/info#properties" get '/rails/info/routes' => "rails/info#routes" get '/rails/info' => "rails/info#index" + get '/' => "rails/welcome#index" end end end diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index 22e885a3a6..10d1821709 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -38,6 +38,7 @@ module Rails def paths @paths ||= begin paths = Rails::Paths::Root.new(@root) + paths.add "app", eager_load: true, glob: "*" paths.add "app/assets", glob: "*" paths.add "app/controllers", eager_load: true @@ -45,19 +46,27 @@ module Rails paths.add "app/models", eager_load: true paths.add "app/mailers", eager_load: true paths.add "app/views" + + paths.add "app/controllers/concerns", eager_load: true + paths.add "app/models/concerns", eager_load: true + paths.add "lib", load_path: true paths.add "lib/assets", glob: "*" paths.add "lib/tasks", glob: "**/*.rake" + paths.add "config" paths.add "config/environments", glob: "#{Rails.env}.rb" paths.add "config/initializers", glob: "**/*.rb" paths.add "config/locales", glob: "*.{rb,yml}" paths.add "config/routes.rb" + paths.add "db" paths.add "db/migrate" paths.add "db/seeds.rb" + paths.add "vendor", load_path: true paths.add "vendor/assets", glob: "*" + paths end end diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index de3127f43e..edd1e8cbdf 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -52,9 +52,6 @@ module Rails class_option :skip_javascript, type: :boolean, aliases: '-J', default: false, desc: 'Skip JavaScript files' - class_option :skip_index_html, type: :boolean, aliases: '-I', default: false, - desc: 'Skip public/index.html and app/assets/images/rails.png files' - class_option :dev, type: :boolean, default: false, desc: "Setup the #{name} with Gemfile pointing to your Rails checkout" @@ -141,14 +138,12 @@ module Rails if options.dev? <<-GEMFILE.strip_heredoc gem 'rails', path: '#{Rails::Generators::RAILS_DEV_PATH}' - gem 'journey', github: 'rails/journey' gem 'arel', github: 'rails/arel' gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders' GEMFILE elsif options.edge? <<-GEMFILE.strip_heredoc gem 'rails', github: 'rails/rails' - gem 'journey', github: 'rails/journey' gem 'arel', github: 'rails/arel' gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders' GEMFILE diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index d8a4f15b4b..4ae8756ed0 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -99,13 +99,17 @@ module Rails end def index_name - @index_name ||= if reference? - polymorphic? ? %w(id type).map { |t| "#{name}_#{t}" } : "#{name}_id" + @index_name ||= if polymorphic? + %w(id type).map { |t| "#{name}_#{t}" } else - name + column_name end end + def column_name + @column_name ||= reference? ? "#{name}_id" : name + end + def foreign_key? !!(name =~ /_id$/) end diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index cc10fd9177..9965db98de 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -160,6 +160,13 @@ module Rails end end + def attributes_names + @attributes_names ||= attributes.each_with_object([]) do |a, names| + names << a.column_name + names << "#{a.name}_type" if a.polymorphic? + end + end + def pluralize_table_names? !defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names end diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 18637451ac..372790df59 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -55,8 +55,12 @@ module Rails def app directory 'app' + keep_file 'app/mailers' keep_file 'app/models' + + keep_file 'app/controllers/concerns' + keep_file 'app/models/concerns' end def config @@ -97,11 +101,6 @@ module Rails def public_directory directory "public", "public", recursive: false - if options[:skip_index_html] - remove_file "public/index.html" - remove_file 'app/assets/images/rails.png' - keep_file 'app/assets/images' - end end def script diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 5b7a653a09..c4846b2c11 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -21,5 +21,7 @@ source 'https://rubygems.org' # Deploy with Capistrano # gem 'capistrano', group: :development +<% unless defined?(JRUBY_VERSION) -%> # To use debugger # gem 'debugger' +<% end -%> diff --git a/railties/lib/rails/generators/rails/app/templates/config/routes.rb b/railties/lib/rails/generators/rails/app/templates/config/routes.rb index 631543c705..22a6aeb5fe 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/routes.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/routes.rb @@ -2,7 +2,7 @@ # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". - # You can have the root of your site routed with "root" just remember to delete public/index.html. + # You can have the root of your site routed with "root" # root to: 'welcome#index' # Example of regular route: diff --git a/railties/lib/rails/generators/rails/migration/USAGE b/railties/lib/rails/generators/rails/migration/USAGE index af74963b01..f340ed97f2 100644 --- a/railties/lib/rails/generators/rails/migration/USAGE +++ b/railties/lib/rails/generators/rails/migration/USAGE @@ -15,15 +15,8 @@ Example: `rails generate migration AddTitleBodyToPost title:string body:text published:boolean` - This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with - this in the Up migration: + This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with this in the Change migration: add_column :posts, :title, :string add_column :posts, :body, :text add_column :posts, :published, :boolean - - And this in the Down migration: - - remove_column :posts, :published - remove_column :posts, :body - remove_column :posts, :title diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index 08fe8a08e3..4d08b01e60 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -91,10 +91,10 @@ class <%= controller_class_name %>Controller < ApplicationController # Use this method to whitelist the permissible parameters. Example: params.require(:person).permit(:name, :age) # Also, you can specialize this method with per-user checking of permissible attributes. def <%= "#{singular_table_name}_params" %> - <%- if attributes.empty? -%> + <%- if attributes_names.empty? -%> params[<%= ":#{singular_table_name}" %>] <%- else -%> - params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes.map {|a| ":#{a.name}" }.join(', ') %>) + params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) <%- end -%> end end diff --git a/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml b/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml index 5c8780aa64..7625ff975c 100644 --- a/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml +++ b/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml @@ -3,12 +3,18 @@ <% unless attributes.empty? -%> one: <% attributes.each do |attribute| -%> - <%= attribute.name %>: <%= attribute.default %> + <%= attribute.column_name %>: <%= attribute.default %> + <%- if attribute.polymorphic? -%> + <%= "#{attribute.name}_type: #{attribute.human_name}" %> + <%- end -%> <% end -%> two: <% attributes.each do |attribute| -%> - <%= attribute.name %>: <%= attribute.default %> + <%= attribute.column_name %>: <%= attribute.default %> + <%- if attribute.polymorphic? -%> + <%= "#{attribute.name}_type: #{attribute.human_name}" %> + <%- end -%> <% end -%> <% else -%> # This model initially had no columns defined. If you add columns to the diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index 3b4fec2e83..8f3ecaadea 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -18,17 +18,12 @@ module TestUnit # :nodoc: private def attributes_hash - return if accessible_attributes.empty? + return if attributes_names.empty? - accessible_attributes.map do |a| - name = a.name + attributes_names.map do |name| "#{name}: @#{singular_table_name}.#{name}" end.sort.join(', ') end - - def accessible_attributes - attributes.reject(&:reference?) - end end end end diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb index e94c6a2030..e650f58d20 100644 --- a/railties/lib/rails/info_controller.rb +++ b/railties/lib/rails/info_controller.rb @@ -1,7 +1,8 @@ require 'action_dispatch/routing/inspector' -class Rails::InfoController < ActionController::Base - self.view_paths = File.join(File.dirname(__FILE__), 'templates') +class Rails::InfoController < ActionController::Base # :nodoc: + self.view_paths = File.expand_path('../templates', __FILE__) + prepend_view_path ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH layout 'application' before_filter :require_local! @@ -15,8 +16,7 @@ class Rails::InfoController < ActionController::Base end def routes - inspector = ActionDispatch::Routing::RoutesInspector.new - @info = inspector.format(_routes.routes).join("\n") + @routes = ActionDispatch::Routing::RoutesInspector.new.collect_routes(_routes.routes) end protected diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index cfdb15a14e..8af4130e87 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -99,15 +99,14 @@ module Rails protected def filter_by(constraint) - yes = [] - no = [] - + all = [] all_paths.each do |path| - paths = path.existent + path.existent_base_paths - path.send(constraint) ? yes.concat(paths) : no.concat(paths) + if path.send(constraint) + paths = path.existent + paths -= path.children.map { |p| p.send(constraint) ? [] : p.existent }.flatten + all.concat(paths) + end end - - all = yes - no all.uniq! all end @@ -135,7 +134,6 @@ module Rails keys.delete(@current) @root.values_at(*keys.sort) end - deprecate :children def first expanded.first @@ -212,10 +210,6 @@ module Rails expanded.select { |d| File.directory?(d) } end - def existent_base_paths - map { |p| File.expand_path(p, @root.path) }.select{ |f| File.exist? f } - end - alias to_a expanded end end diff --git a/railties/lib/rails/templates/rails/info/routes.html.erb b/railties/lib/rails/templates/rails/info/routes.html.erb index 890f6f5b03..1ea387c63f 100644 --- a/railties/lib/rails/templates/rails/info/routes.html.erb +++ b/railties/lib/rails/templates/rails/info/routes.html.erb @@ -6,4 +6,7 @@ Routes match in priority from top to bottom </p> -<p><pre><%= @info %></pre></p>
\ No newline at end of file +<%# actionpack/lib/action_dispatch/middleware/templates %> +<%= render layout: "routes/route_wrapper" do %> + <%= render partial: "routes/route", collection: @routes %> +<% end %> diff --git a/railties/lib/rails/generators/rails/app/templates/public/index.html b/railties/lib/rails/templates/rails/welcome/index.html.erb index dd09a96de9..9a62d206dc 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/index.html +++ b/railties/lib/rails/templates/rails/welcome/index.html.erb @@ -223,7 +223,8 @@ </li> <li> - <h2>Set up a default route and remove <span class="filename">public/index.html</span></h2> + <h2>Set up a root route to replace this page</h2> + <p>You're seeing this page because you're running in development mode and you haven't set a root route yet.</p> <p>Routes are set up in <span class="filename">config/routes.rb</span>.</p> </li> diff --git a/railties/lib/rails/welcome_controller.rb b/railties/lib/rails/welcome_controller.rb new file mode 100644 index 0000000000..45b764fa6b --- /dev/null +++ b/railties/lib/rails/welcome_controller.rb @@ -0,0 +1,7 @@ +class Rails::WelcomeController < ActionController::Base # :nodoc: + self.view_paths = File.expand_path('../templates', __FILE__) + layout nil + + def index + end +end diff --git a/railties/railties.gemspec b/railties/railties.gemspec index ec0f4266c0..e39430560f 100644 --- a/railties/railties.gemspec +++ b/railties/railties.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG.md', 'README.rdoc', 'bin/**/*', 'lib/**/{*,.[a-z]*}'].select { |path| File.file? path } + s.files = Dir['CHANGELOG.md', 'README.rdoc', 'bin/**/*', 'lib/**/{*,.[a-z]*}'] s.require_path = 'lib' s.bindir = 'bin' diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index ae1127b509..53109cb041 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1,5 +1,6 @@ require "isolation/abstract_unit" require 'rack/test' +require 'env_helpers' class ::MyMailInterceptor def self.delivering_email(email); email; end @@ -17,6 +18,7 @@ module ApplicationTests class ConfigurationTest < ActiveSupport::TestCase include ActiveSupport::Testing::Isolation include Rack::Test::Methods + include EnvHelpers def new_app File.expand_path("#{app_path}/../new_app") @@ -41,6 +43,16 @@ module ApplicationTests FileUtils.rm_rf(new_app) if File.directory?(new_app) end + test "Rails.env does not set the RAILS_ENV environment variable which would leak out into rake tasks" do + require "rails" + + switch_env "RAILS_ENV", nil do + Rails.env = "development" + assert_equal "development", Rails.env + assert_nil ENV['RAILS_ENV'] + end + end + test "a renders exception on pending migration" do add_to_config <<-RUBY config.active_record.migration_error = :page_load diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb index 2265d220ab..4029984ce9 100644 --- a/railties/test/application/paths_test.rb +++ b/railties/test/application/paths_test.rb @@ -59,8 +59,6 @@ module ApplicationTests assert eager_load.include?(root("app/controllers")) assert eager_load.include?(root("app/helpers")) assert eager_load.include?(root("app/models")) - assert !eager_load.include?(root("app/views")), "expected to not be in the eager_load_path" - assert !eager_load.include?(root("app/assets")), "expected to not be in the eager_load_path" end test "environments has a glob equal to the current environment" do @@ -75,18 +73,11 @@ module ApplicationTests assert_in_load_path "vendor" assert_not_in_load_path "app", "views" - assert_not_in_load_path "app", "assets" assert_not_in_load_path "config" assert_not_in_load_path "config", "locales" assert_not_in_load_path "config", "environments" assert_not_in_load_path "tmp" assert_not_in_load_path "tmp", "cache" end - - test "deprecated children method" do - assert_deprecated "children is deprecated and will be removed from Rails 4.1." do - @paths["app/assets"].children - end - end end end diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index f2234ab111..a8275a2e76 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -195,6 +195,16 @@ module ApplicationTests assert_no_match(/Errors running/, output) end + def test_scaffold_with_references_columns_tests_pass_by_default + output = Dir.chdir(app_path) do + `rails generate scaffold LineItems product:references cart:belongs_to; + bundle exec rake db:migrate db:test:clone test` + end + + assert_match(/7 tests, 13 assertions, 0 failures, 0 errors/, output) + assert_no_match(/Errors running/, output) + end + def test_db_test_clone_when_using_sql_format add_to_config "config.active_record.schema_format = :sql" output = Dir.chdir(app_path) do diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index ffcdeac7f0..22de640236 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -15,6 +15,12 @@ module ApplicationTests teardown_app end + test "rails/welcome in development" do + app("development") + get "/" + assert_equal 200, last_response.status + end + test "rails/info/routes in development" do app("development") get "/rails/info/routes" @@ -27,6 +33,36 @@ module ApplicationTests assert_equal 200, last_response.status end + test "root takes precedence over internal welcome controller" do + app("development") + + get '/' + assert_match %r{<h1>Getting started</h1>} , last_response.body + + controller :foo, <<-RUBY + class FooController < ApplicationController + def index + render text: "foo" + end + end + RUBY + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + root to: "foo#index" + end + RUBY + + get '/' + assert_equal 'foo', last_response.body + end + + test "rails/welcome in production" do + app("production") + get "/" + assert_equal 404, last_response.status + end + test "rails/info/routes in production" do app("production") get "/rails/info/routes" @@ -241,6 +277,77 @@ module ApplicationTests end end + test 'routes are added and removed when reloading' do + app('development') + + controller :foo, <<-RUBY + class FooController < ApplicationController + def index + render text: "foo" + end + end + RUBY + + controller :bar, <<-RUBY + class BarController < ApplicationController + def index + render text: "bar" + end + end + RUBY + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + get 'foo', to: 'foo#index' + end + RUBY + + get '/foo' + assert_equal 'foo', last_response.body + assert_equal '/foo', Rails.application.routes.url_helpers.foo_path + + get '/bar' + assert_equal 404, last_response.status + assert_raises NoMethodError do + assert_equal '/bar', Rails.application.routes.url_helpers.bar_path + end + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + get 'foo', to: 'foo#index' + get 'bar', to: 'bar#index' + end + RUBY + + Rails.application.reload_routes! + + get '/foo' + assert_equal 'foo', last_response.body + assert_equal '/foo', Rails.application.routes.url_helpers.foo_path + + get '/bar' + assert_equal 'bar', last_response.body + assert_equal '/bar', Rails.application.routes.url_helpers.bar_path + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + get 'foo', to: 'foo#index' + end + RUBY + + Rails.application.reload_routes! + + get '/foo' + assert_equal 'foo', last_response.body + assert_equal '/foo', Rails.application.routes.url_helpers.foo_path + + get '/bar' + assert_equal 404, last_response.status + assert_raises NoMethodError do + assert_equal '/bar', Rails.application.routes.url_helpers.bar_path + end + end + test 'resource routing with irregular inflection' do app_file 'config/initializers/inflection.rb', <<-RUBY ActiveSupport::Inflector.inflections do |inflect| diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 5ea31f2e0f..bd0f9d6a32 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -11,9 +11,11 @@ DEFAULT_APP_FILES = %w( app/assets/stylesheets app/assets/images app/controllers + app/controllers/concerns app/helpers app/mailers app/models + app/models/concerns app/views/layouts config/environments config/initializers @@ -55,7 +57,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "app/views/layouts/application.html.erb", /javascript_include_tag\s+"application"/ assert_file "app/assets/stylesheets/application.css" assert_file "config/application.rb", /config\.assets\.enabled = true/ - assert_file "public/index.html", /url\("assets\/rails.png"\);/ end def test_invalid_application_name_raises_an_error @@ -251,13 +252,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_generator_if_skip_index_html_is_given - run_generator [destination_root, '--skip-index-html'] - assert_no_file 'public/index.html' - assert_no_file 'app/assets/images/rails.png' - assert_file 'app/assets/images/.keep' - end - def test_creation_of_a_test_directory run_generator assert_file 'test' diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 6ab1cd58c7..c48bc20899 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -117,13 +117,13 @@ class GeneratedAttributeTest < Rails::Generators::TestCase assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? end end - + def test_polymorphic_reference_is_false %w(foo bar baz).each do |attribute_type| assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? end end - + def test_blank_type_defaults_to_string_raises_exception assert_equal :string, create_generated_attribute(nil, 'title').type assert_equal :string, create_generated_attribute("", 'title').type @@ -132,6 +132,13 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_handles_index_names_for_references assert_equal "post", create_generated_attribute('string', 'post').index_name assert_equal "post_id", create_generated_attribute('references', 'post').index_name + assert_equal "post_id", create_generated_attribute('belongs_to', 'post').index_name assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name end + + def test_handles_column_names_for_references + assert_equal "post", create_generated_attribute('string', 'post').column_name + assert_equal "post_id", create_generated_attribute('references', 'post').column_name + assert_equal "post_id", create_generated_attribute('belongs_to', 'post').column_name + end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 15e5a0b92b..62d9d1f06a 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -28,7 +28,7 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration] assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/ end - + def test_migration_with_invalid_file_name migration = "add_something:datetime" assert_raise ActiveRecord::IllegalMigrationNameError do @@ -41,9 +41,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string", "body:text"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :posts, :title, :string/, up) - assert_match(/add_column :posts, :body, :text/, up) + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) end end end @@ -53,15 +53,10 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string:index", "body:text"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :up, content do |up| - assert_match(/remove_column :posts, :title/, up) - assert_match(/remove_column :posts, :body/, up) - end - - assert_method :down, content do |down| - assert_match(/add_column :posts, :title, :string/, down) - assert_match(/add_column :posts, :body, :text/, down) - assert_match(/add_index :posts, :title/, down) + assert_method :change, content do |change| + assert_match(/remove_column :posts, :title, :string/, change) + assert_match(/remove_column :posts, :body, :text/, change) + assert_match(/remove_index :posts, :title/, change) end end end @@ -71,14 +66,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string", "body:text"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :up, content do |up| - assert_match(/remove_column :posts, :title/, up) - assert_match(/remove_column :posts, :body/, up) - end - - assert_method :down, content do |down| - assert_match(/add_column :posts, :title, :string/, down) - assert_match(/add_column :posts, :body, :text/, down) + assert_method :change, content do |change| + assert_match(/remove_column :posts, :title, :string/, change) + assert_match(/remove_column :posts, :body, :text/, change) end end end @@ -88,14 +78,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase 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) + assert_method :change, content do |change| + assert_match(/remove_reference :books, :author, index: true/, change) + assert_match(/remove_reference :books, :distributor, polymorphic: true, index: true/, change) end end end @@ -105,13 +90,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :posts, :title, :string/, up) - assert_match(/add_column :posts, :body, :text/, up) - assert_match(/add_column :posts, :user_id, :integer/, up) + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) + assert_match(/add_column :posts, :user_id, :integer/, change) + assert_match(/add_index :posts, :title/, change) + assert_match(/add_index :posts, :user_id, unique: true/, change) end - assert_match(/add_index :posts, :title/, content) - assert_match(/add_index :posts, :user_id, unique: true/, content) end end @@ -120,10 +105,10 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string:inex", "content:text", "user_id:integer:unik"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :books, :title, :string/, up) - assert_match(/add_column :books, :content, :text/, up) - assert_match(/add_column :books, :user_id, :integer/, up) + assert_method :change, content do |change| + assert_match(/add_column :books, :title, :string/, change) + assert_match(/add_column :books, :content, :text/, change) + assert_match(/add_column :books, :user_id, :integer/, change) end assert_no_match(/add_index :books, :title/, content) assert_no_match(/add_index :books, :user_id/, content) @@ -135,13 +120,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:index", "body:text", "user_uuid:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :posts, :title, :string/, up) - assert_match(/add_column :posts, :body, :text/, up) - assert_match(/add_column :posts, :user_uuid, :string/, up) + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) + assert_match(/add_column :posts, :user_uuid, :string/, change) + assert_match(/add_index :posts, :title/, change) + assert_match(/add_index :posts, :user_uuid, unique: true/, change) end - assert_match(/add_index :posts, :title/, content) - assert_match(/add_index :posts, :user_uuid, unique: true/, content) end end @@ -150,11 +135,11 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string{40}:index", "content:string{255}", "price:decimal{1,2}:index", "discount:decimal{3.4}:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :books, :title, :string, limit: 40/, up) - assert_match(/add_column :books, :content, :string, limit: 255/, up) - assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, up) - assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, up) + assert_method :change, content do |change| + assert_match(/add_column :books, :title, :string, limit: 40/, change) + assert_match(/add_column :books, :content, :string, limit: 255/, change) + assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, change) + assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, change) end assert_match(/add_index :books, :title/, content) assert_match(/add_index :books, :price/, content) @@ -167,9 +152,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase 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) + assert_method :change, content do |change| + assert_match(/add_reference :books, :author, index: true/, change) + assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, change) end end end @@ -179,10 +164,10 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "artist_id", "musics:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/create_join_table :artists, :musics/, up) - assert_match(/# t.index \[:artist_id, :music_id\]/, up) - assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, up) + assert_method :change, content do |change| + assert_match(/create_join_table :artists, :musics/, change) + assert_match(/# t.index \[:artist_id, :music_id\]/, change) + assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, change) end end end @@ -192,12 +177,8 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string", "content:text"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :up, content do |up| - assert_match(/^\s*$/, up) - end - - assert_method :down, content do |down| - assert_match(/^\s*$/, down) + assert_method :change, content do |change| + assert_match(/^\s*$/, change) end end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 0c7ff0ebe7..70e080a8ab 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -273,6 +273,16 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/ end + def test_fixtures_use_the_references_ids + run_generator ["LineItem", "product:references", "cart:belongs_to"] + assert_file "test/fixtures/line_items.yml", /product_id: \n cart_id: / + end + + def test_fixtures_use_the_references_ids_and_type + run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"] + assert_file "test/fixtures/line_items.yml", /product_id: \n product_type: Product\n cart_id: / + end + def test_fixture_is_skipped run_generator ["account", "--skip-fixture"] assert_no_file "test/fixtures/accounts.yml" diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 2a88dac635..57a12d0457 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -52,6 +52,33 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end end + def test_dont_use_require_or_permit_if_there_are_no_attributes + run_generator ["User"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/def user_params/, content) + assert_match(/params\[:user\]/, content) + end + end + + def test_controller_permit_references_attributes + run_generator ["LineItem", "product:references", "cart:belongs_to"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :cart_id\)/, content) + end + end + + def test_controller_permit_polymorphic_references_attributes + run_generator ["LineItem", "product:references{polymorphic}"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :product_type\)/, content) + end + end + def test_helper_are_invoked_with_a_pluralized_name run_generator assert_file "app/helpers/users_helper.rb", /module UsersHelper/ @@ -68,13 +95,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end def test_functional_tests - run_generator + run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}"] assert_file "test/controllers/users_controller_test.rb" do |content| assert_match(/class UsersControllerTest < ActionController::TestCase/, content) assert_match(/test "should get index"/, content) - assert_match(/post :create, user: \{ age: @user.age, name: @user.name \}/, content) - assert_match(/put :update, id: @user, user: \{ age: @user.age, name: @user.name \}/, content) + assert_match(/post :create, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content) + assert_match(/put :update, id: @user, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content) end end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 7fcc0a7409..de62fdb1ea 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -60,8 +60,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_file "test/controllers/product_lines_controller_test.rb" do |test| assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test) - assert_match(/post :create, product_line: \{ title: @product_line.title \}/, test) - assert_match(/put :update, id: @product_line, product_line: \{ title: @product_line.title \}/, test) + assert_match(/post :create, product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \}/, test) + assert_match(/put :update, id: @product_line, product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \}/, test) end # Views @@ -199,7 +199,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase run_generator [ "admin/role" ], :behavior => :revoke # Model - assert_file "app/models/admin.rb" # ( should not be remove ) + assert_file "app/models/admin.rb" # ( should not be remove ) assert_no_file "app/models/admin/role.rb" assert_no_file "test/models/admin/role_test.rb" assert_no_file "test/fixtures/admin/roles.yml" diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb index 08fcddd4bf..a9b237d0a5 100644 --- a/railties/test/rails_info_controller_test.rb +++ b/railties/test/rails_info_controller_test.rb @@ -50,7 +50,7 @@ class InfoControllerTest < ActionController::TestCase test "info controller renders with routes" do get :routes - assert_select 'pre' + assert_response :success end end |