diff options
750 files changed, 7053 insertions, 12195 deletions
diff --git a/.gitignore b/.gitignore index 52a431867c..854fdbf450 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,6 @@ debug.log /railties/test/fixtures/tmp /railties/test/initializer/root/log /railties/doc -/railties/guides/output /railties/tmp +/guides/output /RDOC_MAIN.rdoc diff --git a/.travis.yml b/.travis.yml index 68d5c594ae..c6f868498d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,11 @@ script: 'ci/travis.rb' +before_install: + - gem install bundler rvm: - 1.9.3 env: - "GEM=railties" - - "GEM=ap,am,amo,ares,as" + - "GEM=ap,am,amo,as" - "GEM=ar:mysql" - "GEM=ar:mysql2" - "GEM=ar:sqlite3" @@ -8,7 +8,7 @@ else gem 'arel' end -gem 'rack-test', :git => "https://github.com/brynary/rack-test.git" +gem 'rack-test', :git => "git://github.com/brynary/rack-test.git" gem 'bcrypt-ruby', '~> 3.0.0' gem 'jquery-rails' diff --git a/README.rdoc b/README.rdoc index 78640b39aa..c9fb595efb 100644 --- a/README.rdoc +++ b/README.rdoc @@ -47,7 +47,7 @@ can read more about Action Pack in its {README}[link:/rails/rails/blob/master/ac cd myapp; rails server - Run with <tt>--help</tt> for options. + Run with <tt>--help</tt> or <tt>-h</tt> for options. 4. Go to http://localhost:3000 and you'll see: @@ -13,7 +13,7 @@ task :build => "all:build" desc "Release all gems to gemcutter and create a tag" task :release => "all:release" -PROJECTS = %w(activesupport activemodel actionpack actionmailer activeresource activerecord railties) +PROJECTS = %w(activesupport activemodel actionpack actionmailer activerecord railties) desc 'Run all tests by default' task :default => %w(test test:isolated) @@ -109,11 +109,6 @@ RDoc::Task.new do |rdoc| rdoc.rdoc_files.include('activerecord/lib/active_record/**/*.rb') rdoc.rdoc_files.exclude('activerecord/lib/active_record/vendor/*') - rdoc.rdoc_files.include('activeresource/README.rdoc') - rdoc.rdoc_files.include('activeresource/CHANGELOG.md') - rdoc.rdoc_files.include('activeresource/lib/active_resource.rb') - rdoc.rdoc_files.include('activeresource/lib/active_resource/*') - rdoc.rdoc_files.include('actionpack/README.rdoc') rdoc.rdoc_files.include('actionpack/CHANGELOG.md') rdoc.rdoc_files.include('actionpack/lib/abstract_controller/**/*.rb') @@ -157,7 +152,6 @@ task :update_versions do "activemodel" => "ActiveModel", "actionpack" => "ActionPack", "actionmailer" => "ActionMailer", - "activeresource" => "ActiveResource", "activerecord" => "ActiveRecord", "railties" => "Rails" } diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index da5d5c4086..be7b82eecc 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,6 +1,11 @@ -## Rails 4.0.0 (unreleased) ## +## Rails 3.2.3 (unreleased) ## -* No changes +* Upgrade mail version to 2.4.3 *ML* + + +## Rails 3.2.2 (March 1, 2012) ## + +* No changes. ## Rails 3.2.1 (January 26, 2012) ## @@ -128,6 +133,7 @@ * Mail does not have "quoted_body", "quoted_subject" etc. All of these are accessed via body.encoded, subject.encoded etc * Every object in a Mail object returns an object, never a string. So Mail.body returns a Mail::Body class object, need to call #encoded or #decoded to get the string you want. + * Mail::Message#set_content_type does not exist, it is simply Mail::Message#content_type * Every mail message gets a unique message_id unless you specify one, had to change all the tests that check for equality with expected.encoded == actual.encoded to first replace their message_ids with control values diff --git a/actionmailer/actionmailer.gemspec b/actionmailer/actionmailer.gemspec index c605f1ff04..90503edc20 100644 --- a/actionmailer/actionmailer.gemspec +++ b/actionmailer/actionmailer.gemspec @@ -17,5 +17,5 @@ Gem::Specification.new do |s| s.requirements << 'none' s.add_dependency('actionpack', version) - s.add_dependency('mail', '~> 2.4.1') + s.add_dependency('mail', '~> 2.4.4') end diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 1800ff5839..4af2d0a4a8 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -14,15 +14,15 @@ module ActionMailer #:nodoc: # # $ rails generate mailer Notifier # - # The generated model inherits from <tt>ActionMailer::Base</tt>. Emails are defined by creating methods - # within the model which are then used to set variables to be used in the mail template, to - # change options on the mail, or to add attachments. + # The generated model inherits from <tt>ActionMailer::Base</tt>. A mailer model defines methods + # used to generate an email message. In these methods, you can setup variables to be used in + # the mailer views, options on the mail itself such as the <tt>:from</tt> address, and attachments. # # Examples: # # class Notifier < ActionMailer::Base # default :from => 'no-reply@example.com', - # :return_path => 'system@example.com' + # :return_path => 'system@example.com' # # def welcome(recipient) # @account = recipient @@ -267,6 +267,33 @@ module ActionMailer #:nodoc: # set something in the defaults using a proc, and then set the same thing inside of your # mailer method, it will get over written by the mailer method. # + # = Callbacks + # + # You can specify callbacks using before_filter and after_filter for configuring your messages. + # This may be useful, for example, when you want to add default inline attachments for all + # messages sent out by a certain mailer class: + # + # class Notifier < ActionMailer::Base + # before_filter :add_inline_attachment! + # + # def welcome + # mail + # end + # + # private + # + # def add_inline_attachment! + # attachments.inline["footer.jpg"] = File.read('/path/to/filename.jpg') + # end + # end + # + # Callbacks in ActionMailer are implemented using AbstractController::Callbacks, so you + # can define and configure callbacks in the same manner that you would use callbacks in + # classes that inherit from ActionController::Base. + # + # Note that unless you have a specific reason to do so, you should prefer using before_filter + # rather than after_filter in your ActionMailer classes so that headers are parsed properly. + # # = Configuration options # # These options are specified on the class level, like @@ -330,6 +357,7 @@ module ActionMailer #:nodoc: include AbstractController::Helpers include AbstractController::Translation include AbstractController::AssetPaths + include AbstractController::Callbacks self.protected_instance_variables = [:@_action_has_layout] diff --git a/actionmailer/lib/rails/generators/mailer/templates/mailer.rb b/actionmailer/lib/rails/generators/mailer/templates/mailer.rb index aaa1f79732..f6ed7cf8ac 100644 --- a/actionmailer/lib/rails/generators/mailer/templates/mailer.rb +++ b/actionmailer/lib/rails/generators/mailer/templates/mailer.rb @@ -1,6 +1,6 @@ <% module_namespacing do -%> class <%= class_name %> < ActionMailer::Base - default <%= key_value :from, '"from@example.com"' %> + default from: "from@example.com" <% actions.each do |action| -%> # Subject can be set in your I18n file at config/locales/en.yml @@ -11,7 +11,7 @@ class <%= class_name %> < ActionMailer::Base def <%= action %> @greeting = "Hi" - mail <%= key_value :to, '"to@example.org"' %> + mail to: "to@example.org" end <% end -%> end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 65550ab505..aed3ee1874 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -1,5 +1,7 @@ # encoding: utf-8 require 'abstract_unit' +require 'set' + require 'active_support/time' require 'mailers/base_mailer' @@ -550,6 +552,52 @@ class BaseTest < ActiveSupport::TestCase assert_equal("Thanks for signing up this afternoon", mail.subject) end + test "modifying the mail message with a before_filter" do + class BeforeFilterMailer < ActionMailer::Base + before_filter :add_special_header! + + def welcome ; mail ; end + + private + def add_special_header! + headers('X-Special-Header' => 'Wow, so special') + end + end + + assert_equal('Wow, so special', BeforeFilterMailer.welcome['X-Special-Header'].to_s) + end + + test "modifying the mail message with an after_filter" do + class AfterFilterMailer < ActionMailer::Base + after_filter :add_special_header! + + def welcome ; mail ; end + + private + def add_special_header! + headers('X-Special-Header' => 'Testing') + end + end + + assert_equal('Testing', AfterFilterMailer.welcome['X-Special-Header'].to_s) + end + + test "adding an inline attachment using a before_filter" do + class DefaultInlineAttachmentMailer < ActionMailer::Base + before_filter :add_inline_attachment! + + def welcome ; mail ; end + + private + def add_inline_attachment! + attachments.inline["footer.jpg"] = 'hey there' + end + end + + mail = DefaultInlineAttachmentMailer.welcome + assert_equal('image/jpeg; filename=footer.jpg', mail.attachments.inline.first['Content-Type'].to_s) + end + test "action methods should be refreshed after defining new method" do class FooMailer < ActionMailer::Base # this triggers action_methods @@ -559,7 +607,7 @@ class BaseTest < ActiveSupport::TestCase end end - assert_equal ["notify"], FooMailer.action_methods + assert_equal Set.new(["notify"]), FooMailer.action_methods end protected diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 74a8d9f78a..a4f5116a5d 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,8 +1,27 @@ ## Rails 4.0.0 (unreleased) ## -* Forms of persisted records use always PATCH (via the `_method` hack). *fxn* +* Remove the leading \n added by textarea on assert_select. *Santiago Pastorino* -* For resources, both PATCH and PUT are routed to the `update` action. *fxn* +* 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 + +* 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* + +* Removed default `size` option from the `text_field`, `search_field`, `telephone_field`, `url_field`, `email_field` helpers. *Philip Arndt* + +* Removed default `cols` and `rows` options from the `text_area` helper. *Philip Arndt* + +* Adds support for layouts when rendering a partial with a given collection. *serabe* + +* Allows the route helper `root` to take a string argument. For example, `root 'pages#main'`. *bcardarella* + +* Forms of persisted records use always PATCH (via the `_method` hack). *fxn* + +* For resources, both PATCH and PUT are routed to the `update` action. *fxn* * Don't ignore `force_ssl` in development. This is a change of behavior - use a `:if` condition to recreate the old behavior. @@ -79,6 +98,9 @@ * 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* * Add `:format` option to number_to_percentage *Rodrigo Flores* @@ -107,11 +129,30 @@ * `favicon_link_tag` helper will now use the favicon in app/assets by default. *Lucas Caton* -* `ActionView::Helpers::TextHelper#highlight` now defaults to the - HTML5 `mark` element. *Brian Cardarella* +* `ActionView::Helpers::TextHelper#highlight` now defaults to the + HTML5 `mark` element. *Brian Cardarella* + + +## Rails 3.2.3 (unreleased) ## + +* Add `config.action_view.embed_authenticity_token_in_remote_forms` (defaults to true) which allows to set if authenticity token will be included by default in remote forms. If you change it to false, you can still force authenticity token by passing `:authenticity_token => true` in form options *Piotr Sarnacki* +* Do not include the authenticity token in forms where remote: true as ajax forms use the meta-tag value *DHH* -## Rails 3.2.2 (unreleased) ## +* Upgrade rack-cache to 1.2. *José Valim* + +* ActionController::SessionManagement is removed. *Santiago Pastorino* + +* Since the router holds references to many parts of the system like engines, controllers and the application itself, inspecting the route set can actually be really slow, therefore we default alias inspect to to_s. *José Valim* + +* Add a new line after the textarea opening tag. Closes #393 *Rafael Mendonça França* + +* Always pass a respond block from to responder. We should let the responder decide what to do with the given overridden response block, and not short circuit it. *Prem Sichanugrist* + +* Fixes layout rendering regression from 3.2.2. *José Valim* + + +## Rails 3.2.2 (March 1, 2012) ## * Format lookup for partials is derived from the format in which the template is being rendered. Closes #5025 part 2 *Santiago Pastorino* @@ -301,7 +342,8 @@ returned by the class method attribute_names will be wrapped. This fixes the wrapping of nested attributes by adding them to attr_accessible. -## Rails 3.1.4 (unreleased) ## + +## Rails 3.1.4 (March 1, 2012) ## * Skip assets group in Gemfile and all assets configurations options when the application is generated with --skip-sprockets option. @@ -317,6 +359,7 @@ * Assets should use the request protocol by default or default to relative if no request is available *Jonathan del Strother* + ## Rails 3.1.3 (November 20, 2011) ## * Downgrade sprockets to ~> 2.0.3. Using 2.1.0 caused regressions. @@ -326,6 +369,7 @@ *Jon Leighton* + ## Rails 3.1.2 (November 18, 2011) ## * Fix XSS security vulnerability in the `translate` helper method. When using interpolation @@ -367,6 +411,7 @@ * Ensure users upgrading from 3.0.x to 3.1.x will properly upgrade their flash object in session (issues #3298 and #2509) + ## Rails 3.1.1 (October 07, 2011) ## * javascript_path and stylesheet_path now refer to /assets if asset pipelining @@ -603,7 +648,7 @@ * Add Rack::Cache to the default stack. Create a Rails store that delegates to the Rails cache, so by default, whatever caching layer you are using will be used for HTTP caching. Note that Rack::Cache will be used if you use #expires_in, #fresh_when or #stale with :public => true. Otherwise, the caching rules will apply to the browser only. *Yehuda Katz, Carl Lerche* -## Rails 3.0.12 (unreleased) ## +## Rails 3.0.12 (March 1, 2012) ## * Fix using `tranlate` helper with a html translation which uses the `:count` option for pluralization. diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index b205acdb79..589a67dc02 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -18,12 +18,11 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', version) s.add_dependency('activemodel', version) - s.add_dependency('rack-cache', '~> 1.1') + s.add_dependency('rack-cache', '~> 1.2') s.add_dependency('builder', '~> 3.0.0') s.add_dependency('rack', '~> 1.4.1') s.add_dependency('rack-test', '~> 0.6.1') s.add_dependency('journey', '~> 1.0.1') - s.add_dependency('sprockets', '~> 2.2.0') s.add_dependency('erubis', '~> 2.7.0') s.add_development_dependency('tzinfo', '~> 0.3.29') diff --git a/actionpack/lib/abstract_controller/base.rb b/actionpack/lib/abstract_controller/base.rb index 43cea3b79e..b068846a13 100644 --- a/actionpack/lib/abstract_controller/base.rb +++ b/actionpack/lib/abstract_controller/base.rb @@ -1,4 +1,5 @@ require 'erubis' +require 'set' require 'active_support/configurable' require 'active_support/descendants_tracker' require 'active_support/core_ext/module/anonymous' @@ -59,7 +60,7 @@ module AbstractController # itself. Finally, #hidden_actions are removed. # # ==== Returns - # * <tt>array</tt> - A list of all methods that should be considered actions. + # * <tt>set</tt> - A set of all methods that should be considered actions. def action_methods @action_methods ||= begin # All public instance methods of this class, including ancestors @@ -72,7 +73,7 @@ module AbstractController hidden_actions.to_a # Clear out AS callback method pollution - methods.reject { |method| method =~ /_one_time_conditions/ } + Set.new(methods.reject { |method| method =~ /_one_time_conditions/ }) end end diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index aa39e21ba9..bc9f6fc3e8 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -89,7 +89,7 @@ module AbstractController # class TillController < BankController # layout false # - # In these examples, we have three implicit lookup scenrios: + # In these examples, we have three implicit lookup scenarios: # * The BankController uses the "bank" layout. # * The ExchangeController uses the "exchange" layout. # * The CurrencyController inherits the layout from BankController. @@ -120,6 +120,7 @@ module AbstractController # def writers_and_readers # logged_in? ? "writer_layout" : "reader_layout" # end + # end # # Now when a new request for the index action is processed, the layout will vary depending on whether the person accessing # is logged in or not. @@ -127,7 +128,14 @@ module AbstractController # If you want to use an inline method, such as a proc, do something like this: # # class WeblogController < ActionController::Base - # layout proc{ |controller| controller.logged_in? ? "writer_layout" : "reader_layout" } + # layout proc { |controller| controller.logged_in? ? "writer_layout" : "reader_layout" } + # end + # + # If an argument isn't given to the proc, it's evaluated in the context of + # the current controller anyway. + # + # class WeblogController < ActionController::Base + # layout proc { logged_in? ? "writer_layout" : "reader_layout" } # end # # Of course, the most common way of specifying a layout is still just as a plain template name: @@ -136,8 +144,8 @@ module AbstractController # layout "weblog_standard" # end # - # If no directory is specified for the template name, the template will by default be looked for in <tt>app/views/layouts/</tt>. - # Otherwise, it will be looked up relative to the template root. + # The template will be looked always in <tt>app/views/layouts/</tt> folder. But you can point + # <tt>layouts</tt> folder direct also. <tt>layout "layouts/demo"</tt> is the same as <tt>layout "demo"</tt>. # # Setting the layout to nil forces it to be looked up in the filesystem and fallbacks to the parent behavior if none exists. # Setting it to nil is useful to re-enable template lookup overriding a previous configuration set in the parent: @@ -238,10 +246,10 @@ module AbstractController # # If the specified layout is a: # String:: the String is the template name - # Symbol:: call the method specified by the symbol, which will return - # the template name + # Symbol:: call the method specified by the symbol, which will return the template name # false:: There is no layout # true:: raise an ArgumentError + # nil:: Force default layout behavior with inheritance # # ==== Parameters # * <tt>layout</tt> - The layout to use. @@ -280,6 +288,10 @@ module AbstractController <<-RUBY lookup_context.find_all("#{_implied_layout_name}", #{prefixes.inspect}).first || super RUBY + else + <<-RUBY + super + RUBY end layout_definition = case _layout @@ -295,12 +307,12 @@ module AbstractController end RUBY when Proc - define_method :_layout_from_proc, &_layout - "_layout_from_proc(self)" + define_method :_layout_from_proc, &_layout + _layout.arity == 0 ? "_layout_from_proc" : "_layout_from_proc(self)" when false nil when true - raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" + raise ArgumentError, "Layouts must be specified as a String, Symbol, Proc, false, or nil" when nil name_clause end @@ -360,7 +372,7 @@ module AbstractController when false, nil then nil else raise ArgumentError, - "String, true, or false, expected for `layout'; you passed #{name.inspect}" + "String, Proc, :default, true, or false, expected for `layout'; you passed #{name.inspect}" end end diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index ddc93464cd..7d73c6af8d 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -105,6 +105,7 @@ module AbstractController # Find and renders a template based on the options given. # :api: private def _render_template(options) #:nodoc: + lookup_context.rendered_format = nil if options[:formats] view_renderer.render(view_context, options) end diff --git a/actionpack/lib/abstract_controller/url_for.rb b/actionpack/lib/abstract_controller/url_for.rb index e4261068d8..4a95e1f276 100644 --- a/actionpack/lib/abstract_controller/url_for.rb +++ b/actionpack/lib/abstract_controller/url_for.rb @@ -1,10 +1,10 @@ -# Includes +url_for+ into the host class (e.g. an abstract controller or mailer). The class -# has to provide a +RouteSet+ by implementing the <tt>_routes</tt> methods. Otherwise, an -# exception will be raised. -# -# Note that this module is completely decoupled from HTTP - the only requirement is a valid -# <tt>_routes</tt> implementation. module AbstractController + # Includes +url_for+ into the host class (e.g. an abstract controller or mailer). The class + # has to provide a +RouteSet+ by implementing the <tt>_routes</tt> methods. Otherwise, an + # exception will be raised. + # + # Note that this module is completely decoupled from HTTP - the only requirement is a valid + # <tt>_routes</tt> implementation. module UrlFor extend ActiveSupport::Concern include ActionDispatch::Routing::UrlFor diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb index a0c54dbd84..7c10fcbb8a 100644 --- a/actionpack/lib/action_controller.rb +++ b/actionpack/lib/action_controller.rb @@ -31,7 +31,6 @@ module ActionController autoload :RequestForgeryProtection autoload :Rescue autoload :Responder - autoload :SessionManagement autoload :Streaming autoload :Testing autoload :UrlFor diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 3b82231b15..71425cd542 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -171,6 +171,16 @@ module ActionController class Base < Metal abstract! + # Shortcut helper that returns all the ActionController::Base modules except the ones passed in the argument: + # + # class MetalController + # ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left| + # include left + # end + # end + # + # This gives better control over what you want to exclude and makes it easier + # to create a bare controller class, instead of listing the modules required manually. def self.without_modules(*modules) modules = modules.map do |m| m.is_a?(Symbol) ? ActionController.const_get(m) : m @@ -192,7 +202,6 @@ module ActionController Renderers::All, ConditionalGet, RackDelegation, - SessionManagement, Caching, MimeResponds, ImplicitRender, diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index ba96735e56..59ec197347 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -40,7 +40,7 @@ module ActionController #:nodoc: # # You can modify the default action cache path by passing a # <tt>:cache_path</tt> option. This will be passed directly to - # <tt>ActionCachePath.path_for</tt>. This is handy for actions with + # <tt>ActionCachePath.new</tt>. This is handy for actions with # multiple possible routes that should be cached differently. If a # block is given, it is called with the current controller instance. # @@ -170,14 +170,14 @@ module ActionController #:nodoc: options.reverse_merge!(:format => @extension) if options.is_a?(Hash) end - path = controller.url_for(options).split(%r{://}).last + path = controller.url_for(options).split('://', 2).last @path = normalize!(path) end private def normalize!(path) path << 'index' if path[-1] == ?/ - path << ".#{extension}" if extension and !path.split('?').first.ends_with?(".#{extension}") + path << ".#{extension}" if extension and !path.split('?', 2).first.ends_with?(".#{extension}") URI.parser.unescape(path) end end diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index 159f718029..307594d54a 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -99,7 +99,7 @@ module ActionController #:nodoc: # caches_page :index # # # cache the index action except for JSON requests - # caches_page :index, :if => Proc.new { |c| !c.request.format.json? } + # caches_page :index, :if => Proc.new { !request.format.json? } # # # don't gzip images # caches_page :image, :gzip => false diff --git a/actionpack/lib/action_controller/caching/sweeping.rb b/actionpack/lib/action_controller/caching/sweeping.rb index 49cf70ec21..bb176ca3f9 100644 --- a/actionpack/lib/action_controller/caching/sweeping.rb +++ b/actionpack/lib/action_controller/caching/sweeping.rb @@ -54,6 +54,11 @@ module ActionController #:nodoc: class Sweeper < ActiveRecord::Observer #:nodoc: attr_accessor :controller + def initialize(*args) + super + @controller = nil + end + def before(controller) self.controller = controller callback(:before) if controller.perform_caching @@ -88,7 +93,7 @@ module ActionController #:nodoc: end def method_missing(method, *arguments, &block) - return unless @controller + super unless @controller @controller.__send__(method, *arguments, &block) end end diff --git a/actionpack/lib/action_controller/metal/conditional_get.rb b/actionpack/lib/action_controller/metal/conditional_get.rb index 2a8e4c575e..5b25a0d303 100644 --- a/actionpack/lib/action_controller/metal/conditional_get.rb +++ b/actionpack/lib/action_controller/metal/conditional_get.rb @@ -110,8 +110,8 @@ module ActionController # # Examples: # expires_in 20.minutes + # expires_in 3.hours, :public => true # expires_in 3.hours, :public => true, :must_revalidate => true - # expires_in 3.hours, 'max-stale' => 5.hours, :public => true # # This method will overwrite an existing Cache-Control header. # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities. diff --git a/actionpack/lib/action_controller/metal/exceptions.rb b/actionpack/lib/action_controller/metal/exceptions.rb index ece9ba3725..9a9db0fe5f 100644 --- a/actionpack/lib/action_controller/metal/exceptions.rb +++ b/actionpack/lib/action_controller/metal/exceptions.rb @@ -14,8 +14,6 @@ module ActionController end class MethodNotAllowed < ActionControllerError #:nodoc: - attr_reader :allowed_methods - def initialize(*allowed_methods) super("Only #{allowed_methods.to_sentence(:locale => :en)} requests are allowed.") end @@ -30,9 +28,6 @@ module ActionController class MissingFile < ActionControllerError #:nodoc: end - class RenderError < ActionControllerError #:nodoc: - end - class SessionOverflowError < ActionControllerError #:nodoc: DEFAULT_MESSAGE = 'Your session data is larger than the data column in which it is to be stored. You must increase the size of your data column if you intend to store large data.' diff --git a/actionpack/lib/action_controller/metal/force_ssl.rb b/actionpack/lib/action_controller/metal/force_ssl.rb index a1e40fc4e0..ac12cbb625 100644 --- a/actionpack/lib/action_controller/metal/force_ssl.rb +++ b/actionpack/lib/action_controller/metal/force_ssl.rb @@ -44,7 +44,7 @@ module ActionController redirect_options = {:protocol => 'https://', :status => :moved_permanently} redirect_options.merge!(:host => host) if host redirect_options.merge!(:params => request.query_parameters) - flash.keep + flash.keep if respond_to?(:flash) redirect_to redirect_options end end diff --git a/actionpack/lib/action_controller/metal/helpers.rb b/actionpack/lib/action_controller/metal/helpers.rb index d070eaae5d..1a4bca12d2 100644 --- a/actionpack/lib/action_controller/metal/helpers.rb +++ b/actionpack/lib/action_controller/metal/helpers.rb @@ -52,6 +52,7 @@ module ActionController module Helpers extend ActiveSupport::Concern + class << self; attr_accessor :helpers_path; end include AbstractController::Helpers included do diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index b8b43ea9ef..44d2f740e6 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -263,7 +263,7 @@ module ActionController # The quality of the implementation depends on a good choice. # A nonce might, for example, be constructed as the base 64 encoding of # - # => time-stamp H(time-stamp ":" ETag ":" private-key) + # time-stamp H(time-stamp ":" ETag ":" private-key) # # where time-stamp is a server-generated time or other non-repeating value, # ETag is the value of the HTTP ETag header associated with the requested entity, diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index 55de7e7d8e..f467b74256 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -6,8 +6,6 @@ module ActionController #:nodoc: module MimeResponds extend ActiveSupport::Concern - include ActionController::ImplicitRender - included do class_attribute :responder, :mimes_for_respond_to self.responder = ActionController::Responder @@ -58,7 +56,7 @@ module ActionController #:nodoc: # Clear all mime types in <tt>respond_to</tt>. # def clear_respond_to - self.mimes_for_respond_to = ActiveSupport::OrderedHash.new.freeze + self.mimes_for_respond_to = Hash.new.freeze end end @@ -76,7 +74,7 @@ module ActionController #:nodoc: # # respond_to do |format| # format.html - # format.xml { render :xml => @people.to_xml } + # format.xml { render :xml => @people } # end # end # @@ -193,24 +191,110 @@ module ActionController #:nodoc: if collector = retrieve_collector_from_mimes(mimes, &block) response = collector.response - response ? response.call : default_render({}) + response ? response.call : render({}) end end - # respond_with wraps a resource around a responder for default representation. - # First it invokes respond_to, if a response cannot be found (ie. no block - # for the request was given and template was not available), it instantiates - # an ActionController::Responder with the controller and resource. + # For a given controller action, respond_with generates an appropriate + # response based on the mime-type requested by the client. # - # ==== Example + # If the method is called with just a resource, as in this example - # - # def index - # @users = User.all - # respond_with(@users) + # class PeopleController < ApplicationController + # respond_to :html, :xml, :json + # + # def index + # @people = Person.all + # respond_with @people + # end + # end + # + # then the mime-type of the response is typically selected based on the + # request's Accept header and the set of available formats declared + # by previous calls to the controller's class method +respond_to+. Alternatively + # the mime-type can be selected by explicitly setting <tt>request.format</tt> in + # the controller. + # + # If an acceptable format is not identified, the application returns a + # '406 - not acceptable' status. Otherwise, the default response is to render + # a template named after the current action and the selected format, + # e.g. <tt>index.html.erb</tt>. If no template is available, the behavior + # depends on the selected format: + # + # * for an html response - if the request method is +get+, an exception + # is raised but for other requests such as +post+ the response + # depends on whether the resource has any validation errors (i.e. + # assuming that an attempt has been made to save the resource, + # e.g. by a +create+ action) - + # 1. If there are no errors, i.e. the resource + # was saved successfully, the response +redirect+'s to the resource + # i.e. its +show+ action. + # 2. If there are validation errors, the response + # renders a default action, which is <tt>:new</tt> for a + # +post+ request or <tt>:edit</tt> for +put+. + # Thus an example like this - + # + # respond_to :html, :xml + # + # def create + # @user = User.new(params[:user]) + # flash[:notice] = 'User was successfully created.' if @user.save + # respond_with(@user) + # end + # + # is equivalent, in the absence of <tt>create.html.erb</tt>, to - + # + # def create + # @user = User.new(params[:user]) + # respond_to do |format| + # if @user.save + # flash[:notice] = 'User was successfully created.' + # format.html { redirect_to(@user) } + # format.xml { render :xml => @user } + # else + # format.html { render :action => "new" } + # format.xml { render :xml => @user } + # end + # end + # end + # + # * for a javascript request - if the template isn't found, an exception is + # raised. + # * for other requests - i.e. data formats such as xml, json, csv etc, if + # the resource passed to +respond_with+ responds to <code>to_<format></code>, + # the method attempts to render the resource in the requested format + # directly, e.g. for an xml request, the response is equivalent to calling + # <code>render :xml => resource</code>. + # + # === Nested resources + # + # As outlined above, the +resources+ argument passed to +respond_with+ + # can play two roles. It can be used to generate the redirect url + # for successful html requests (e.g. for +create+ actions when + # no template exists), while for formats other than html and javascript + # it is the object that gets rendered, by being converted directly to the + # required format (again assuming no template exists). + # + # For redirecting successful html requests, +respond_with+ also supports + # the use of nested resources, which are supplied in the same way as + # in <code>form_for</code> and <code>polymorphic_url</code>. For example - + # + # def create + # @project = Project.find(params[:project_id]) + # @task = @project.comments.build(params[:task]) + # flash[:notice] = 'Task was successfully created.' if @task.save + # respond_with(@project, @task) # end # - # It also accepts a block to be given. It's used to overwrite a default - # response: + # This would cause +respond_with+ to redirect to <code>project_task_url</code> + # instead of <code>task_url</code>. For request formats other than html or + # javascript, if multiple resources are passed in this way, it is the last + # one specified that is rendered. + # + # === Customizing response behavior + # + # Like +respond_to+, +respond_with+ may also be called with a block that + # can be used to overwrite any of the default responses, e.g. - # # def create # @user = User.new(params[:user]) @@ -221,13 +305,24 @@ module ActionController #:nodoc: # end # end # - # All options given to respond_with are sent to the underlying responder, - # except for the option :responder itself. Since the responder interface - # is quite simple (it just needs to respond to call), you can even give - # a proc to it. - # - # In order to use respond_with, first you need to declare the formats your - # controller responds to in the class level with a call to <tt>respond_to</tt>. + # The argument passed to the block is an ActionController::MimeResponds::Collector + # object which stores the responses for the formats defined within the + # block. Note that formats with responses defined explicitly in this way + # do not have to first be declared using the class method +respond_to+. + # + # Also, a hash passed to +respond_with+ immediately after the specified + # resource(s) is interpreted as a set of options relevant to all + # formats. Any option accepted by +render+ can be used, e.g. + # respond_with @people, :status => 200 + # However, note that these options are ignored after an unsuccessful attempt + # to save a resource, e.g. when automatically rendering <tt>:new</tt> + # after a post request. + # + # Two additional options are relevant specifically to +respond_with+ - + # 1. <tt>:location</tt> - overwrites the default redirect location used after + # a successful html +post+ request. + # 2. <tt>:action</tt> - overwrites the default render action used after an + # unsuccessful html +post+ request. # def respond_with(*resources, &block) raise "In order to use respond_with, first you need to declare the formats your " << @@ -235,16 +330,8 @@ module ActionController #:nodoc: if collector = retrieve_collector_from_mimes(&block) options = resources.size == 1 ? {} : resources.extract_options! - - if defined_response = collector.response - if action = options.delete(:action) - render :action => action - else - defined_response.call - end - else - (options.delete(:responder) || self.class.responder).call(self, resources, options) - end + options[:default_response] = collector.response + (options.delete(:responder) || self.class.responder).call(self, resources, options) end end @@ -269,8 +356,12 @@ module ActionController #:nodoc: end end - # Collects mimes and return the response for the negotiated format. Returns - # nil if :not_acceptable was sent to the client. + # Returns a Collector object containing the appropriate mime-type response + # for the current request, based on the available responses defined by a block. + # In typical usage this is the block passed to +respond_with+ or +respond_to+. + # + # Sends :not_acceptable to the client and returns nil if no suitable format + # is available. # def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc: mimes ||= collect_mimes_from_class_level @@ -281,6 +372,7 @@ module ActionController #:nodoc: if format self.content_type ||= format.to_s lookup_context.formats = [format.to_sym] + lookup_context.rendered_format = lookup_context.formats.first collector else head :not_acceptable @@ -288,7 +380,30 @@ module ActionController #:nodoc: end end - class Collector #:nodoc: + # A container for responses available from the current controller for + # requests for different mime-types sent to a particular action. + # + # The public controller methods +respond_with+ and +respond_to+ may be called + # with a block that is used to define responses to different mime-types, e.g. + # for +respond_to+ : + # + # respond_to do |format| + # format.html + # format.xml { render :xml => @people } + # end + # + # In this usage, the argument passed to the block (+format+ above) is an + # instance of the ActionController::MimeResponds::Collector class. This + # object serves as a container in which available responses can be stored by + # calling any of the dynamically generated, mime-type-specific methods such + # as +html+, +xml+ etc on the Collector. Each response is represented by a + # corresponding block if present. + # + # A subsequent call to #negotiate_format(request) will enable the Collector + # to determine which specific mime-type it should respond with for the current + # request, with this response then being accessible by calling #response. + # + class Collector include AbstractController::Collector attr_accessor :order, :format diff --git a/actionpack/lib/action_controller/metal/redirecting.rb b/actionpack/lib/action_controller/metal/redirecting.rb index b07742e0e1..3ffb7ef426 100644 --- a/actionpack/lib/action_controller/metal/redirecting.rb +++ b/actionpack/lib/action_controller/metal/redirecting.rb @@ -93,7 +93,7 @@ module ActionController _compute_redirect_to_location options.call else url_for(options) - end.gsub(/[\r\n]/, '') + end.gsub(/[\0\r\n]/, '') end end end diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index afa9243f02..0bff1825d9 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -17,7 +17,6 @@ module ActionController #:nodoc: # CSRF protection is turned on with the <tt>protect_from_forgery</tt> method, # which checks the token and resets the session if it doesn't match what was expected. # A call to this method is generated for new \Rails applications by default. - # You can customize the error message by editing public/422.html. # # The token parameter is named <tt>authenticity_token</tt> by default. The name and # value of this token must be added to every layout that renders forms by including @@ -37,6 +36,10 @@ module ActionController #:nodoc: config_accessor :request_forgery_protection_token self.request_forgery_protection_token ||= :authenticity_token + # Controls how unverified request will be handled + config_accessor :request_forgery_protection_method + self.request_forgery_protection_method ||= :reset_session + # Controls whether request forgery protection is turned on or not. Turned off by default only in test mode. config_accessor :allow_forgery_protection self.allow_forgery_protection = true if allow_forgery_protection.nil? @@ -64,8 +67,10 @@ module ActionController #:nodoc: # Valid Options: # # * <tt>:only/:except</tt> - Passed to the <tt>before_filter</tt> call. Set which actions are verified. + # * <tt>:with</tt> - Set the method to handle unverified request. Valid values: <tt>:exception</tt> and <tt>:reset_session</tt> (default). def protect_from_forgery(options = {}) self.request_forgery_protection_token ||= :authenticity_token + self.request_forgery_protection_method = options.delete(:with) if options.key?(:with) prepend_before_filter :verify_authenticity_token, options end end @@ -80,9 +85,19 @@ module ActionController #:nodoc: end # This is the method that defines the application behavior when a request is found to be unverified. - # By default, \Rails resets the session when it finds an unverified request. + # By default, \Rails uses <tt>request_forgery_protection_method</tt> when it finds an unverified request: + # + # * <tt>:reset_session</tt> - Resets the session. + # * <tt>:exception</tt>: - Raises ActionController::InvalidAuthenticityToken exception. def handle_unverified_request - reset_session + case request_forgery_protection_method + when :exception + raise ActionController::InvalidAuthenticityToken + when :reset_session + reset_session + else + raise ArgumentError, 'Invalid request forgery protection method, use :exception or :reset_session' + end end # Returns true or false if a request is verified. Checks: diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb index ccda01ed44..83407846dc 100644 --- a/actionpack/lib/action_controller/metal/responder.rb +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -63,7 +63,7 @@ module ActionController #:nodoc: # # def create # @project = Project.find(params[:project_id]) - # @task = @project.comments.build(params[:task]) + # @task = @project.tasks.build(params[:task]) # flash[:notice] = 'Task was successfully created.' if @task.save # respond_with(@project, @task) # end @@ -130,6 +130,7 @@ module ActionController #:nodoc: @resources = resources @options = options @action = options.delete(:action) + @default_response = options.delete(:default_response) end delegate :head, :render, :redirect_to, :to => :controller @@ -172,7 +173,7 @@ module ActionController #:nodoc: # responds to :to_format and display it. # def to_format - if get? || !has_errors? + if get? || !has_errors? || response_overridden? default_render else display_errors @@ -226,7 +227,11 @@ module ActionController #:nodoc: # controller. # def default_render - controller.default_render(options) + if @default_response + @default_response.call(options) + else + controller.default_render(options) + end end # Display is just a shortcut to render a resource with the current format. @@ -274,5 +279,9 @@ module ActionController #:nodoc: def json_resource_errors {:errors => resource.errors} end + + def response_overridden? + @default_response.present? + end end end diff --git a/actionpack/lib/action_controller/metal/session_management.rb b/actionpack/lib/action_controller/metal/session_management.rb deleted file mode 100644 index 91d89ff9a4..0000000000 --- a/actionpack/lib/action_controller/metal/session_management.rb +++ /dev/null @@ -1,9 +0,0 @@ -module ActionController #:nodoc: - module SessionManagement #:nodoc: - extend ActiveSupport::Concern - - module ClassMethods - - end - end -end diff --git a/actionpack/lib/action_controller/metal/url_for.rb b/actionpack/lib/action_controller/metal/url_for.rb index 0b40b1fc4c..8e7b56dbcc 100644 --- a/actionpack/lib/action_controller/metal/url_for.rb +++ b/actionpack/lib/action_controller/metal/url_for.rb @@ -1,25 +1,25 @@ -# Includes +url_for+ into the host class. The class has to provide a +RouteSet+ by implementing -# the <tt>_routes</tt> method. Otherwise, an exception will be raised. -# -# In addition to <tt>AbstractController::UrlFor</tt>, this module accesses the HTTP layer to define -# url options like the +host+. In order to do so, this module requires the host class -# to implement +env+ and +request+, which need to be a Rack-compatible. -# -# Example: -# -# class RootUrl -# include ActionController::UrlFor -# include Rails.application.routes.url_helpers -# -# delegate :env, :request, :to => :controller -# -# def initialize(controller) -# @controller = controller -# @url = root_path # named route from the application. -# end -# end -# module ActionController + # Includes +url_for+ into the host class. The class has to provide a +RouteSet+ by implementing + # the <tt>_routes</tt> method. Otherwise, an exception will be raised. + # + # In addition to <tt>AbstractController::UrlFor</tt>, this module accesses the HTTP layer to define + # url options like the +host+. In order to do so, this module requires the host class + # to implement +env+ and +request+, which need to be a Rack-compatible. + # + # Example: + # + # class RootUrl + # include ActionController::UrlFor + # include Rails.application.routes.url_helpers + # + # delegate :env, :request, :to => :controller + # + # def initialize(controller) + # @controller = controller + # @url = root_path # named route from the application. + # end + # end + # module UrlFor extend ActiveSupport::Concern @@ -42,6 +42,5 @@ module ActionController @_url_options end end - end end diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index a288e69649..851a2c4aee 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -3,33 +3,32 @@ require "action_controller" require "action_dispatch/railtie" require "action_view/railtie" require "abstract_controller/railties/routes_helpers" -require "action_controller/railties/paths" +require "action_controller/railties/helpers" module ActionController - class Railtie < Rails::Railtie + class Railtie < Rails::Railtie #:nodoc: config.action_controller = ActiveSupport::OrderedOptions.new - initializer "action_controller.logger" do - ActiveSupport.on_load(:action_controller) { self.logger ||= Rails.logger } - end - - initializer "action_controller.initialize_framework_caches" do - ActiveSupport.on_load(:action_controller) { self.cache_store ||= Rails.cache } - end - initializer "action_controller.assets_config", :group => :all do |app| app.config.action_controller.assets_dir ||= app.config.paths["public"].first end + initializer "action_controller.set_helpers_path" do |app| + ActionController::Helpers.helpers_path = app.helpers_paths + end + initializer "action_controller.set_configs" do |app| paths = app.config.paths options = app.config.action_controller + options.logger ||= Rails.logger + options.cache_store ||= Rails.cache + options.javascripts_dir ||= paths["public/javascripts"].first options.stylesheets_dir ||= paths["public/stylesheets"].first options.page_cache_directory ||= paths["public"].first - # make sure readers methods get compiled + # Ensure readers methods get compiled options.asset_path ||= app.config.asset_path options.asset_host ||= app.config.asset_host options.relative_url_root ||= app.config.relative_url_root @@ -37,8 +36,16 @@ module ActionController ActiveSupport.on_load(:action_controller) do include app.routes.mounted_helpers extend ::AbstractController::Railties::RoutesHelpers.with(app.routes) - extend ::ActionController::Railties::Paths.with(app) - options.each { |k,v| send("#{k}=", v) } + extend ::ActionController::Railties::Helpers + + options.each do |k,v| + k = "#{k}=" + if respond_to?(k) + send(k, v) + elsif !Base.respond_to?(k) + raise "Invalid option key: #{k}" + end + end end end diff --git a/actionpack/lib/action_controller/railties/helpers.rb b/actionpack/lib/action_controller/railties/helpers.rb new file mode 100644 index 0000000000..3985c6b273 --- /dev/null +++ b/actionpack/lib/action_controller/railties/helpers.rb @@ -0,0 +1,22 @@ +module ActionController + module Railties + module Helpers + def inherited(klass) + super + return unless klass.respond_to?(:helpers_path=) + + if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_helpers_paths) } + paths = namespace.railtie_helpers_paths + else + paths = ActionController::Helpers.helpers_path + end + + klass.helpers_path = paths + + if klass.superclass == ActionController::Base && ActionController::Base.include_all_helpers + klass.helper :all + end + end + end + end +end diff --git a/actionpack/lib/action_controller/railties/paths.rb b/actionpack/lib/action_controller/railties/paths.rb deleted file mode 100644 index bbe63149ad..0000000000 --- a/actionpack/lib/action_controller/railties/paths.rb +++ /dev/null @@ -1,25 +0,0 @@ -module ActionController - module Railties - module Paths - def self.with(app) - Module.new do - define_method(:inherited) do |klass| - super(klass) - - if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_helpers_paths) } - paths = namespace.railtie_helpers_paths - else - paths = app.helpers_paths - end - - klass.helpers_path = paths - - if klass.superclass == ActionController::Base && ActionController::Base.include_all_helpers - klass.helper :all - end - end - end - end - end - end -end diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 9c38ff44d8..18dda978b3 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -2,8 +2,8 @@ require 'active_support/core_ext/module' module ActionController # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or - # Active Resources or pretty much any other model type that has an id. These patterns are then used to try elevate - # the view actions to a higher logical level. Example: + # pretty much any other model type that has an id. These patterns are then used to try elevate the view actions to + # a higher logical level. Example: # # # routes # resources :posts diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 3509e74d5e..9bd2e622ad 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -56,6 +56,9 @@ module ActionController # # assert that the "new" view template was rendered # assert_template "new" # + # # assert that the exact template "admin/posts/new" was rendered + # assert_template %r{\Aadmin/posts/new\Z} + # # # assert that the "_customer" partial was rendered twice # assert_template :partial => '_customer', :count => 2 # @@ -69,12 +72,16 @@ module ActionController # assert_template :partial => '_customer', :locals => { :customer => @customer } # def assert_template(options = {}, message = nil) + # Force body to be read in case the + # template is being streamed + response.body + case options - when NilClass, String, Symbol + when NilClass, String, Symbol, Regexp options = options.to_s if Symbol === options rendered = @templates msg = message || sprintf("expecting <%s> but rendering with <%s>", - options, rendered.keys) + options.inspect, rendered.keys) assert_block(msg) do if options rendered.any? { |t,num| t.match(options) } @@ -117,6 +124,8 @@ module ActionController assert @partials.empty?, "Expected no partials to be rendered" end + else + raise ArgumentError, "assert_template only accepts a String, Symbol, Hash, Regexp, or nil" end end end @@ -470,7 +479,6 @@ module ActionController @request.session["flash"].sweep @controller.request = @request - @controller.params.merge!(parameters) build_request_uri(action, parameters) @controller.class.class_eval { include Testing } @controller.recycle! @@ -496,11 +504,6 @@ module ActionController end end - # Cause the action to be rescued according to the regular rules for rescue_action when the visitor is not local - def rescue_action_in_public! - @request.remote_addr = '208.77.188.166' # example.com - end - included do include ActionController::TemplateAssertions include ActionDispatch::Assertions diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb index 24ffc28710..e9b50ff8ce 100644 --- a/actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +++ b/actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb @@ -5,6 +5,7 @@ require 'active_support/core_ext/class/attribute' module HTML class Sanitizer def sanitize(text, options = {}) + validate_options(options) return text unless sanitizeable?(text) tokenize(text, options).join end @@ -27,6 +28,16 @@ module HTML def process_node(node, result, options) result << node.to_s end + + def validate_options(options) + if options[:tags] && !options[:tags].is_a?(Enumerable) + raise ArgumentError, "You should pass :tags as an Enumerable" + end + + if options[:attributes] && !options[:attributes].is_a?(Enumerable) + raise ArgumentError, "You should pass :attributes as an Enumerable" + end + end end class FullSanitizer < Sanitizer diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index a9542a7d1b..e3b04ac097 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -61,6 +61,7 @@ module ActionDispatch autoload :Reloader autoload :RemoteIp autoload :ShowExceptions + autoload :SSL autoload :Static end diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index 2152351703..e039eb1288 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -82,7 +82,7 @@ module Mime class << self TRAILING_STAR_REGEXP = /(text|application)\/\*/ - Q_SEPARATOR_REGEXP = /;\s*q=/ + PARAMETER_SEPARATOR_REGEXP = /;\s*\w+="?\w+"?/ def lookup(string) LOOKUP[string] @@ -109,7 +109,7 @@ module Mime def parse(accept_header) if accept_header !~ /,/ - accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first + accept_header = accept_header.split(PARAMETER_SEPARATOR_REGEXP).first if accept_header =~ TRAILING_STAR_REGEXP parse_data_with_trailing_star($1) else @@ -119,7 +119,7 @@ module Mime # keep track of creation order to keep the subsequent sort stable list, index = [], 0 accept_header.split(/,/).each do |header| - params, q = header.split(Q_SEPARATOR_REGEXP) + params, q = header.split(PARAMETER_SEPARATOR_REGEXP) if params.present? params.strip! diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index de014a9c00..796e0dbc45 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -17,7 +17,8 @@ module ActionDispatch include ActionDispatch::Http::Upload include ActionDispatch::Http::URL - LOCALHOST = [/^127\.0\.0\.\d{1,3}$/, "::1", /^0:0:0:0:0:0:0:1(%.*)?$/].freeze + LOCALHOST = Regexp.union [/^127\.0\.0\.\d{1,3}$/, /^::1$/, /^0:0:0:0:0:0:0:1(%.*)?$/] + ENV_METHODS = %w[ AUTH_TYPE GATEWAY_INTERFACE PATH_TRANSLATED REMOTE_HOST REMOTE_IDENT REMOTE_USER REMOTE_ADDR @@ -250,7 +251,7 @@ module ActionDispatch # True if the request came from localhost, 127.0.0.1. def local? - LOCALHOST.any? { |local_ip| local_ip === remote_addr && local_ip === remote_ip } + LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip end private diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 84732085f0..078229efd2 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -51,12 +51,13 @@ module ActionDispatch # :nodoc: # If a character set has been defined for this response (see charset=) then # the character set information will also be included in the content type # information. - attr_accessor :charset, :content_type + attr_accessor :charset + attr_reader :content_type CONTENT_TYPE = "Content-Type".freeze SET_COOKIE = "Set-Cookie".freeze LOCATION = "Location".freeze - + cattr_accessor(:default_charset) { "utf-8" } include Rack::Response::Helpers @@ -83,6 +84,10 @@ module ActionDispatch # :nodoc: @status = Rack::Utils.status_code(status) end + def content_type=(content_type) + @content_type = content_type.to_s + end + # The response code of the request def response_code @status diff --git a/actionpack/lib/action_dispatch/http/upload.rb b/actionpack/lib/action_dispatch/http/upload.rb index 5ab99d1061..ce8c2729e9 100644 --- a/actionpack/lib/action_dispatch/http/upload.rb +++ b/actionpack/lib/action_dispatch/http/upload.rb @@ -4,11 +4,12 @@ module ActionDispatch attr_accessor :original_filename, :content_type, :tempfile, :headers def initialize(hash) + @tempfile = hash[:tempfile] + raise(ArgumentError, ':tempfile is required') unless @tempfile + @original_filename = encode_filename(hash[:filename]) @content_type = hash[:type] @headers = hash[:head] - @tempfile = hash[:tempfile] - raise(ArgumentError, ':tempfile is required') unless @tempfile end def read(*args) @@ -16,18 +17,15 @@ module ActionDispatch end # Delegate these methods to the tempfile. - [:open, :path, :rewind, :size].each do |method| + [:open, :path, :rewind, :size, :eof?].each do |method| class_eval "def #{method}; @tempfile.#{method}; end" end - + private + def encode_filename(filename) # Encode the filename in the utf8 encoding, unless it is nil - if filename - filename.force_encoding("UTF-8").encode! - else - filename - end + filename.force_encoding("UTF-8").encode! if filename end end diff --git a/actionpack/lib/action_dispatch/http/url.rb b/actionpack/lib/action_dispatch/http/url.rb index 80ffbe575b..f9dae5dad7 100644 --- a/actionpack/lib/action_dispatch/http/url.rb +++ b/actionpack/lib/action_dispatch/http/url.rb @@ -40,7 +40,9 @@ module ActionDispatch rewritten_url << ":#{options.delete(:port)}" if options[:port] end - path = options.delete(:path) || '' + path = "" + path << options.delete(:script_name).to_s.chomp("/") + path << options.delete(:path).to_s params = options[:params] || {} params.reject! {|k,v| v.to_param.nil? } diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 25f1db8228..0c717c8503 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -273,10 +273,6 @@ module ActionDispatch @parent_jar[key] = options end - def signed - @signed ||= SignedCookieJar.new(self, @secret) - end - def method_missing(method, *arguments, &block) @parent_jar.send(method, *arguments, &block) end diff --git a/actionpack/lib/action_dispatch/middleware/request_id.rb b/actionpack/lib/action_dispatch/middleware/request_id.rb index d5a0b80fd5..6fff94707c 100644 --- a/actionpack/lib/action_dispatch/middleware/request_id.rb +++ b/actionpack/lib/action_dispatch/middleware/request_id.rb @@ -19,10 +19,7 @@ module ActionDispatch def call(env) env["action_dispatch.request_id"] = external_request_id(env) || internal_request_id - status, headers, body = @app.call(env) - - headers["X-Request-Id"] = env["action_dispatch.request_id"] - [ status, headers, body ] + @app.call(env).tap { |status, headers, body| headers["X-Request-Id"] = env["action_dispatch.request_id"] } end private diff --git a/actionpack/lib/action_dispatch/middleware/session/cache_store.rb b/actionpack/lib/action_dispatch/middleware/session/cache_store.rb index d3b6fd12fa..1db6194271 100644 --- a/actionpack/lib/action_dispatch/middleware/session/cache_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/cache_store.rb @@ -1,5 +1,4 @@ require 'action_dispatch/middleware/session/abstract_store' -require 'rack/session/memcache' module ActionDispatch module Session diff --git a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb index 8ebf870b95..a4866f5a8f 100644 --- a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb @@ -59,7 +59,8 @@ module ActionDispatch end def set_session(env, sid, session_data, options) - session_data.merge("session_id" => sid) + session_data["session_id"] = sid + session_data end def set_cookie(env, session_id, cookie) diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb index 836136eb95..ab740a0190 100644 --- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb @@ -9,7 +9,7 @@ module ActionDispatch # of ShowExceptions. Everytime there is an exception, ShowExceptions will # store the exception in env["action_dispatch.exception"], rewrite the # PATH_INFO to the exception status code and call the rack app. - # + # # If the application returns a "X-Cascade" pass response, this middleware # will send an empty response as result with the correct status code. # If any exception happens inside the exceptions app, this middleware diff --git a/actionpack/lib/action_dispatch/middleware/ssl.rb b/actionpack/lib/action_dispatch/middleware/ssl.rb new file mode 100644 index 0000000000..9098f4e170 --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/ssl.rb @@ -0,0 +1,70 @@ +module ActionDispatch + class SSL + YEAR = 31536000 + + def self.default_hsts_options + { :expires => YEAR, :subdomains => false } + end + + def initialize(app, options = {}) + @app = app + + @hsts = options.fetch(:hsts, {}) + @hsts = {} if @hsts == true + @hsts = self.class.default_hsts_options.merge(@hsts) if @hsts + + @host = options[:host] + @port = options[:port] + end + + def call(env) + request = Request.new(env) + + if request.ssl? + status, headers, body = @app.call(env) + headers = hsts_headers.merge(headers) + flag_cookies_as_secure!(headers) + [status, headers, body] + else + redirect_to_https(request) + end + end + + private + def redirect_to_https(request) + url = URI(request.url) + url.scheme = "https" + url.host = @host if @host + url.port = @port if @port + headers = hsts_headers.merge('Content-Type' => 'text/html', + 'Location' => url.to_s) + + [301, headers, []] + end + + # http://tools.ietf.org/html/draft-hodges-strict-transport-sec-02 + def hsts_headers + if @hsts + value = "max-age=#{@hsts[:expires]}" + value += "; includeSubDomains" if @hsts[:subdomains] + { 'Strict-Transport-Security' => value } + else + {} + end + end + + def flag_cookies_as_secure!(headers) + if cookies = headers['Set-Cookie'] + cookies = cookies.split("\n") + + headers['Set-Cookie'] = cookies.map { |cookie| + if cookie !~ /;\s+secure(;|$)/ + "#{cookie}; secure" + else + cookie + end + }.join("\n") + end + end + end +end diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 63b7422287..9073e6582d 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -39,6 +39,7 @@ module ActionDispatch end def escape_glob_chars(path) + path.force_encoding('binary') if path.respond_to? :force_encoding path.gsub(/[*?{}\[\]]/, "\\\\\\&") end end diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb index 35f901c575..62f906219c 100644 --- a/actionpack/lib/action_dispatch/railtie.rb +++ b/actionpack/lib/action_dispatch/railtie.rb @@ -16,7 +16,7 @@ module ActionDispatch config.action_dispatch.rack_cache = { :metastore => "rails:/", :entitystore => "rails:/", - :verbose => true + :verbose => false } initializer "action_dispatch.configure" do |app| diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 02a27110e4..fccbff1749 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1,6 +1,6 @@ require 'active_support/core_ext/hash/except' require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/object/inclusion' +require 'active_support/core_ext/enumerable' require 'active_support/inflector' require 'action_dispatch/routing/redirection' @@ -254,10 +254,15 @@ module ActionDispatch # # For options, see +match+, as +root+ uses it internally. # + # You can also pass a string which will expand + # + # root 'pages#main' + # # You should put the root route at the top of <tt>config/routes.rb</tt>, # because this means it will be matched first. As this is the most popular route # of most Rails applications, this is beneficial. def root(options = {}) + options = { :to => options } if options.is_a?(String) match '/', { :as => :root }.merge(options) end @@ -446,7 +451,11 @@ module ActionDispatch _route = @set.named_routes.routes[name.to_sym] _routes = @set app.routes.define_mounted_helper(name) - app.routes.class_eval do + app.routes.singleton_class.class_eval do + define_method :mounted? do + true + end + define_method :_generate_prefix do |options| prefix_options = options.slice(*_route.segment_keys) # we must actually delete prefix segment keys to avoid passing them to next url_for @@ -871,17 +880,18 @@ module ActionDispatch # CANONICAL_ACTIONS holds all actions that does not need a prefix or # a path appended since they fit properly in their scope level. VALID_ON_OPTIONS = [:new, :collection, :member] - RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except] + RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except, :param] CANONICAL_ACTIONS = %w(index create new show update destroy) class Resource #:nodoc: - attr_reader :controller, :path, :options + attr_reader :controller, :path, :options, :param def initialize(entities, options = {}) @name = entities.to_s @path = (options[:path] || @name).to_s @controller = (options[:controller] || @name).to_s @as = options[:as] + @param = options[:param] || :id @options = options end @@ -926,7 +936,7 @@ module ActionDispatch alias :collection_scope :path def member_scope - "#{path}/:id" + "#{path}/:#{param}" end def new_scope(new_path) @@ -934,7 +944,7 @@ module ActionDispatch end def nested_scope - "#{path}/:#{singular}_id" + "#{path}/:#{singular}_#{param}" end end @@ -1125,6 +1135,25 @@ module ActionDispatch # comment PATCH/PUT /sekret/comments/:id(.:format) # comment DELETE /sekret/comments/:id(.:format) # + # [:shallow_prefix] + # Prefixes nested shallow route names with specified prefix. + # + # scope :shallow_prefix => "sekret" do + # resources :posts do + # resources :comments, :shallow => true + # end + # end + # + # The +comments+ resource here will have the following routes generated for it: + # + # post_comments GET /posts/:post_id/comments(.:format) + # post_comments POST /posts/:post_id/comments(.:format) + # new_post_comment GET /posts/:post_id/comments/new(.:format) + # edit_sekret_comment GET /comments/:id/edit(.:format) + # sekret_comment GET /comments/:id(.:format) + # sekret_comment PATCH/PUT /comments/:id(.:format) + # sekret_comment DELETE /comments/:id(.:format) + # # === Examples # # # routes call <tt>Admin::PostsController</tt> diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 57c6972078..30e9e5634b 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -191,14 +191,50 @@ module ActionDispatch selector = url_helper_name(name, kind) hash_access_method = hash_access_name(name, kind) - @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 - remove_possible_method :#{selector} - def #{selector}(*args) - url_for(#{hash_access_method}(*args)) - end - END_EVAL + if optimize_helper?(route) + @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 + remove_possible_method :#{selector} + def #{selector}(*args) + if args.size == #{route.required_parts.size} && !args.last.is_a?(Hash) && optimize_routes_generation? + options = #{options.inspect}.merge!(url_options) + options[:path] = "#{optimized_helper(route)}" + ActionDispatch::Http::URL.url_for(options) + else + url_for(#{hash_access_method}(*args)) + end + end + END_EVAL + else + @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 + remove_possible_method :#{selector} + def #{selector}(*args) + url_for(#{hash_access_method}(*args)) + end + END_EVAL + end + helpers << selector end + + # Clause check about when we need to generate an optimized helper. + def optimize_helper?(route) #:nodoc: + route.ast.grep(Journey::Nodes::Star).empty? && route.requirements.except(:controller, :action).empty? + end + + # Generates the interpolation to be used in the optimized helper. + def optimized_helper(route) + string_route = route.ast.to_s + + while string_route.gsub!(/\([^\)]*\)/, "") + true + end + + route.required_parts.each_with_index do |part, i| + string_route.gsub!(part.inspect, "\#{Journey::Router::Utils.escape_fragment(args[#{i}].to_param)}") + end + + string_route + end end attr_accessor :formatter, :set, :named_routes, :default_scope, :router @@ -323,7 +359,7 @@ module ActionDispatch # Rails.application.routes.url_helpers.url_for(args) @_routes = routes class << self - delegate :url_for, :to => '@_routes' + delegate :url_for, :optimize_routes_generation?, :to => '@_routes' end # Make named_routes available in the module singleton @@ -557,6 +593,14 @@ module ActionDispatch RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length, :trailing_slash, :anchor, :params, :only_path, :script_name] + def mounted? + false + end + + def optimize_routes_generation? + !mounted? && default_url_options.empty? + end + def _generate_prefix(options = {}) nil end @@ -568,19 +612,17 @@ module ActionDispatch user, password = extract_authentication(options) path_segments = options.delete(:_path_segments) - script_name = options.delete(:script_name) - - path = (script_name.blank? ? _generate_prefix(options) : script_name.chomp('/')).to_s + script_name = options.delete(:script_name).presence || _generate_prefix(options) path_options = options.except(*RESERVED_OPTIONS) path_options = yield(path_options) if block_given? - path_addition, params = generate(path_options, path_segments || {}) - path << path_addition + path, params = generate(path_options, path_segments || {}) params.merge!(options[:params] || {}) ActionDispatch::Http::URL.url_for(options.merge!({ :path => path, + :script_name => script_name, :params => params, :user => user, :password => password @@ -594,6 +636,7 @@ module ActionDispatch def recognize_path(path, environment = {}) method = (environment[:method] || "GET").to_s.upcase path = Journey::Router::Utils.normalize_path(path) unless path =~ %r{://} + extras = environment[:extras] || {} begin env = Rack::MockRequest.env_for(path, {:method => method}) @@ -603,6 +646,7 @@ module ActionDispatch req = @request_class.new(env) @router.recognize(req) do |route, matches, params| + params.merge!(extras) params.each do |key, value| if value.is_a?(String) value = value.dup.force_encoding(Encoding::BINARY) diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index ee6616c5d3..94db36ce1f 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -102,6 +102,9 @@ module ActionDispatch super end + # Hook overriden in controller to add request information + # with `default_url_options`. Application logic should not + # go into url_options. def url_options default_url_options end @@ -152,6 +155,11 @@ module ActionDispatch protected + def optimize_routes_generation? + return @_optimized_routes if defined?(@_optimized_routes) + @_optimized_routes = _routes.optimize_routes_generation? && default_url_options.empty? + end + def _with_routes(routes) old_routes, @_routes = @_routes, routes yield diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index 094cfbfc76..33f8f526b5 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -4,11 +4,9 @@ module ActionDispatch module Assertions # A small suite of assertions that test responses from \Rails applications. module ResponseAssertions - extend ActiveSupport::Concern - # Asserts that the response is one of the following types: # - # * <tt>:success</tt> - Status code was 200 + # * <tt>:success</tt> - Status code was in the 200-299 range # * <tt>:redirect</tt> - Status code was in the 300-399 range # * <tt>:missing</tt> - Status code was 404 # * <tt>:error</tt> - Status code was in the 500-599 range @@ -83,7 +81,7 @@ module ActionDispatch refer else @controller.url_for(fragment) - end.gsub(/[\r\n]/, '') + end.gsub(/[\0\r\n]/, '') end end end diff --git a/actionpack/lib/action_dispatch/testing/assertions/routing.rb b/actionpack/lib/action_dispatch/testing/assertions/routing.rb index 1552676fbb..1f4b905d18 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/routing.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/routing.rb @@ -39,10 +39,9 @@ module ActionDispatch # # Test a custom route # assert_recognizes({:controller => 'items', :action => 'show', :id => '1'}, 'view/item1') def assert_recognizes(expected_options, path, extras={}, message=nil) - request = recognized_request_for(path) + request = recognized_request_for(path, extras) expected_options = expected_options.clone - extras.each_key { |key| expected_options.delete key } unless extras.nil? expected_options.stringify_keys! @@ -181,7 +180,7 @@ module ActionDispatch private # Recognizes the route for a given path. - def recognized_request_for(path) + def recognized_request_for(path, extras = {}) if path.is_a?(Hash) method = path[:method] path = path[:path] @@ -209,7 +208,7 @@ module ActionDispatch request.request_method = method if method - params = @routes.recognize_path(path, { :method => method }) + params = @routes.recognize_path(path, { :method => method, :extras => extras }) request.path_parameters = params.with_indifferent_access request diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb index 8eed85bce2..ea1ed20f3c 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb @@ -269,6 +269,7 @@ module ActionDispatch end end text.strip! unless NO_STRIP.include?(match.name) + text.sub!(/\A\n/, '') if match.name == "textarea" unless match_with.is_a?(Regexp) ? (text =~ match_with) : (text == match_with.to_s) content_mismatch ||= sprintf("<%s> expected but was\n<%s>.", match_with, text) true diff --git a/actionpack/lib/action_dispatch/testing/test_request.rb b/actionpack/lib/action_dispatch/testing/test_request.rb index 7280e9a93b..d04be2099c 100644 --- a/actionpack/lib/action_dispatch/testing/test_request.rb +++ b/actionpack/lib/action_dispatch/testing/test_request.rb @@ -1,6 +1,5 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/hash/indifferent_access' -require 'active_support/core_ext/hash/reverse_merge' require 'rack/utils' module ActionDispatch diff --git a/actionpack/lib/action_view/asset_paths.rb b/actionpack/lib/action_view/asset_paths.rb index 2a28e780bf..add8d94b70 100644 --- a/actionpack/lib/action_view/asset_paths.rb +++ b/actionpack/lib/action_view/asset_paths.rb @@ -117,7 +117,7 @@ module ActionView end def relative_url_root - config.relative_url_root + config.relative_url_root || current_request.try(:script_name) end def asset_host_config diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 23329d7f35..f3315948b4 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -141,6 +141,12 @@ module ActionView #:nodoc: cattr_accessor :streaming_completion_on_exception @@streaming_completion_on_exception = %("><script type="text/javascript">window.location = "/500.html"</script></html>) + # Specify whether rendering within namespaced controllers should prefix + # the partial paths for ActiveModel objects with the namespace. + # (e.g., an Admin::PostsController would render @post using /admin/posts/_post.erb) + cattr_accessor :prefix_partial_path_with_controller_namespace + @@prefix_partial_path_with_controller_namespace = true + class_attribute :helpers class_attribute :_routes class_attribute :logger @@ -194,7 +200,7 @@ module ActionView #:nodoc: # TODO Provide a new API for AV::Base and deprecate this one. if context.is_a?(ActionView::Renderer) @view_renderer = context - elsif + else lookup_context = context.is_a?(ActionView::LookupContext) ? context : ActionView::LookupContext.new(context) lookup_context.formats = formats if formats diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index 662adbe183..2f8b28c82b 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -24,9 +24,10 @@ module ActionView # server by setting ActionController::Base.asset_host in the application # configuration, typically in <tt>config/environments/production.rb</tt>. # For example, you'd define <tt>assets.example.com</tt> to be your asset - # host this way: + # host this way, inside the <tt>configure</tt> block of your environment-specific + # configuration files or <tt>config/application.rb</tt>: # - # ActionController::Base.asset_host = "assets.example.com" + # config.action_controller.asset_host = "assets.example.com" # # Helpers take that into account: # @@ -384,7 +385,7 @@ module ActionView # image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # => # <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" /> def image_tag(source, options={}) - options = options.dup.symbolize_keys! + options = options.symbolize_keys src = options[:src] = path_to_image(source) @@ -477,7 +478,7 @@ module ActionView end def multiple_sources_tag(type, sources) - options = sources.extract_options!.dup.symbolize_keys! + options = sources.extract_options!.symbolize_keys sources.flatten! yield options if block_given? diff --git a/actionpack/lib/action_view/helpers/atom_feed_helper.rb b/actionpack/lib/action_view/helpers/atom_feed_helper.rb index 73824dc1f8..70164217de 100644 --- a/actionpack/lib/action_view/helpers/atom_feed_helper.rb +++ b/actionpack/lib/action_view/helpers/atom_feed_helper.rb @@ -176,6 +176,7 @@ module ActionView # * <tt>:updated</tt>: Time of update. Defaults to the updated_at attribute on the record if one such exists. # * <tt>:url</tt>: The URL for this entry. Defaults to the polymorphic_url for the record. # * <tt>:id</tt>: The ID for this entry. Defaults to "tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}" + # * <tt>:type</tt>: The TYPE for this entry. Defaults to "text/html". def entry(record, options = {}) @xml.entry do @xml.id(options[:id] || "tag:#{@view.request.host},#{@feed_options[:schema_date]}:#{record.class}/#{record.id}") @@ -188,7 +189,9 @@ module ActionView @xml.updated((options[:updated] || record.updated_at).xmlschema) end - @xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:url] || @view.polymorphic_url(record)) + type = options.fetch(:type) { 'text/html' } + + @xml.link(:rel => 'alternate', :type => type, :href => options[:url] || @view.polymorphic_url(record)) yield AtomBuilder.new(@xml) end diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index 17bbfe2efd..278139cadb 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -215,7 +215,7 @@ module ActionView def flush_output_buffer #:nodoc: if output_buffer && !output_buffer.empty? response.body_parts << output_buffer - self.output_buffer = output_buffer[0,0] + self.output_buffer = output_buffer.respond_to?(:clone_empty) ? output_buffer.clone_empty : output_buffer[0, 0] nil end end diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 2d37923825..cb46f26d99 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -625,13 +625,18 @@ module ActionView # time_tag Date.today, :pubdate => true # => # <time datetime="2010-11-04" pubdate="pubdate">November 04, 2010</time> # - def time_tag(date_or_time, *args) + # <%= time_tag Time.now do %> + # <span>Right now</span> + # <% end %> + # # => <time datetime="2010-11-04T17:55:45+01:00"><span>Right now</span></time> + # + def time_tag(date_or_time, *args, &block) options = args.extract_options! format = options.delete(:format) || :long content = args.first || I18n.l(date_or_time, :format => format) datetime = date_or_time.acts_like?(:time) ? date_or_time.xmlschema : date_or_time.rfc3339 - content_tag(:time, content, options.reverse_merge(:datetime => datetime)) + content_tag(:time, content, options.reverse_merge(:datetime => datetime), &block) end end @@ -669,11 +674,7 @@ module ActionView @options[:discard_minute] ||= true if @options[:discard_hour] @options[:discard_second] ||= true unless @options[:include_seconds] && !@options[:discard_minute] - # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are - # valid (otherwise it could be 31 and February wouldn't be a valid date) - if @datetime && @options[:discard_day] && !@options[:discard_month] - @datetime = @datetime.change(:day => 1) - end + set_day_if_discarded if @options[:tag] && @options[:ignore_date] select_time @@ -696,11 +697,7 @@ module ActionView @options[:discard_month] ||= true unless order.include?(:month) @options[:discard_day] ||= true if @options[:discard_month] || !order.include?(:day) - # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are - # valid (otherwise it could be 31 and February wouldn't be a valid date) - if @datetime && @options[:discard_day] && !@options[:discard_month] - @datetime = @datetime.change(:day => 1) - end + set_day_if_discarded [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) } @@ -798,7 +795,15 @@ module ActionView private %w( sec min hour day month year ).each do |method| define_method(method) do - @datetime.kind_of?(Fixnum) ? @datetime : @datetime.send(method) if @datetime + @datetime.kind_of?(Numeric) ? @datetime : @datetime.send(method) if @datetime + end + end + + # If the day is hidden, the day should be set to the 1st so all month and year choices are + # valid. Otherwise, February 31st or February 29th, 2011 can be selected, which are invalid. + def set_day_if_discarded + if @datetime && @options[:discard_day] + @datetime = @datetime.change(:day => 1) end end @@ -972,7 +977,10 @@ module ActionView # Returns the id attribute for the input tag. # => "post_written_on_1i" def input_id_from_type(type) - input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '') + id = input_name_from_type(type).gsub(/([\[\(])|(\]\[)/, '_').gsub(/[\]\)]/, '') + id = @options[:namespace] + '_' + id if @options[:namespace] + + id end # Given an ordering of datetime components, create the selection HTML diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 53ae8b66da..6219a7a924 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -9,6 +9,7 @@ require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/string/output_safety' require 'active_support/core_ext/array/extract_options' +require 'active_support/deprecation' module ActionView # = Action View Form Helpers @@ -16,17 +17,28 @@ module ActionView # Form helpers are designed to make working with resources much easier # compared to using vanilla HTML. # - # Forms for models are created with +form_for+. That method yields a form - # builder that knows the model the form is about. The form builder is thus - # able to generate default values for input fields that correspond to model - # attributes, and also convenient names, IDs, endpoints, etc. + # Typically, a form designed to create or update a resource reflects the + # identity of the resource in several ways: (i) the url that the form is + # sent to (the form element's +action+ attribute) should result in a request + # being routed to the appropriate controller action (with the appropriate <tt>:id</tt> + # parameter in the case of an existing resource), (ii) input fields should + # be named in such a way that in the controller their values appear in the + # appropriate places within the +params+ hash, and (iii) for an existing record, + # when the form is initially displayed, input fields corresponding to attributes + # of the resource should show the current values of those attributes. # - # Conventions in the generated field names allow controllers to receive form - # data nicely structured in +params+ with no effort on your side. + # In Rails, this is usually achieved by creating the form using +form_for+ and + # a number of related helper methods. +form_for+ generates an appropriate <tt>form</tt> + # tag and yields a form builder object that knows the model the form is about. + # Input fields are created by calling methods defined on the form builder, which + # means they are able to generate the appropriate names and default values + # corresponding to the model attributes, as well as convenient IDs, etc. + # Conventions in the generated field names allow controllers to receive form data + # nicely structured in +params+ with no effort on your side. # # For example, to create a new person you typically set up a new instance of # +Person+ in the <tt>PeopleController#new</tt> action, <tt>@person</tt>, and - # pass it to +form_for+: + # in the view template pass that object to +form_for+: # # <%= form_for @person do |f| %> # <%= f.label :first_name %>: @@ -45,10 +57,10 @@ module ActionView # <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" /> # </div> # <label for="person_first_name">First name</label>: - # <input id="person_first_name" name="person[first_name]" size="30" type="text" /><br /> + # <input id="person_first_name" name="person[first_name]" type="text" /><br /> # # <label for="person_last_name">Last name</label>: - # <input id="person_last_name" name="person[last_name]" size="30" type="text" /><br /> + # <input id="person_last_name" name="person[last_name]" type="text" /><br /> # # <input name="commit" type="submit" value="Create Person" /> # </form> @@ -76,10 +88,10 @@ module ActionView # <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" /> # </div> # <label for="person_first_name">First name</label>: - # <input id="person_first_name" name="person[first_name]" size="30" type="text" value="John" /><br /> + # <input id="person_first_name" name="person[first_name]" type="text" value="John" /><br /> # # <label for="person_last_name">Last name</label>: - # <input id="person_last_name" name="person[last_name]" size="30" type="text" value="Smith" /><br /> + # <input id="person_last_name" name="person[last_name]" type="text" value="Smith" /><br /> # # <input name="commit" type="submit" value="Update Person" /> # </form> @@ -109,29 +121,14 @@ module ActionView object.respond_to?(:to_model) ? object.to_model : object end - # Creates a form and a scope around a specific model object that is used - # as a base for questioning about values for the fields. + # Creates a form that allows the user to create or update the attributes + # of a specific model object. # - # Rails provides succinct resource-oriented form generation with +form_for+ - # like this: - # - # <%= form_for @offer do |f| %> - # <%= f.label :version, 'Version' %>: - # <%= f.text_field :version %><br /> - # <%= f.label :author, 'Author' %>: - # <%= f.text_field :author %><br /> - # <%= f.submit %> - # <% end %> - # - # There, +form_for+ is able to generate the rest of RESTful form - # parameters based on introspection on the record, but to understand what - # it does we need to dig first into the alternative generic usage it is - # based upon. - # - # === Generic form_for - # - # The generic way to call +form_for+ yields a form builder around a - # model: + # The method can be used in several slightly different ways, depending on + # how much you wish to rely on Rails to infer automatically from the model + # how the form should be constructed. For a generic model object, a form + # can be created by passing +form_for+ a string or symbol representing + # the object we are concerned with: # # <%= form_for :person do |f| %> # First name: <%= f.text_field :first_name %><br /> @@ -141,24 +138,39 @@ module ActionView # <%= f.submit %> # <% end %> # - # There, the argument is a symbol or string with the name of the - # object the form is about. - # - # The form builder acts as a regular form helper that somehow carries the - # model. Thus, the idea is that + # The variable +f+ yielded to the block is a FormBuilder object that + # incorporates the knowledge about the model object represented by + # <tt>:person</tt> passed to +form_for+. Methods defined on the FormBuilder + # are used to generate fields bound to this model. Thus, for example, # # <%= f.text_field :first_name %> # - # gets expanded to + # will get expanded to # # <%= text_field :person, :first_name %> + # which results in an html <tt><input></tt> tag whose +name+ attribute is + # <tt>person[first_name]</tt>. This means that when the form is submitted, + # the value entered by the user will be available in the controller as + # <tt>params[:person][:first_name]</tt>. + # + # For fields generated in this way using the FormBuilder, + # if <tt>:person</tt> also happens to be the name of an instance variable + # <tt>@person</tt>, the default value of the field shown when the form is + # initially displayed (e.g. in the situation where you are editing an + # existing record) will be the value of the corresponding attribute of + # <tt>@person</tt>. # # The rightmost argument to +form_for+ is an - # optional hash of options: - # - # * <tt>:url</tt> - The URL the form is submitted to. It takes the same - # fields you pass to +url_for+ or +link_to+. In particular you may pass - # here a named route directly as well. Defaults to the current action. + # optional hash of options - + # + # * <tt>:url</tt> - The URL the form is to be submitted to. This may be + # represented in the same way as values passed to +url_for+ or +link_to+. + # So for example you may use a named route directly. When the model is + # represented by a string or symbol, as in the example above, if the + # <tt>:url</tt> option is not specified, by default the form will be + # sent back to the current url (We will describe below an alternative + # resource-oriented usage of +form_for+ in which the URL does not need + # to be specified explicitly). # * <tt>:namespace</tt> - A namespace for your form to ensure uniqueness of # id attributes on form elements. The namespace attribute will be prefixed # with underscore on the generated HTML id. @@ -168,7 +180,7 @@ module ActionView # possible to use both the stand-alone FormHelper methods and methods # from FormTagHelper. For example: # - # <%= form_for @person do |f| %> + # <%= form_for :person do |f| %> # First name: <%= f.text_field :first_name %> # Last name : <%= f.text_field :last_name %> # Biography : <%= text_area :person, :biography %> @@ -180,26 +192,65 @@ module ActionView # are designed to work with an object as base, like # FormOptionHelper#collection_select and DateHelper#datetime_select. # - # === Resource-oriented style + # === #form_for with a model object + # + # In the examples above, the object to be created or edited was + # represented by a symbol passed to +form_for+, and we noted that + # a string can also be used equivalently. It is also possible, however, + # to pass a model object itself to +form_for+. For example, if <tt>@post</tt> + # is an existing record you wish to edit, you can create the form using + # + # <%= form_for @post do |f| %> + # ... + # <% end %> + # + # This behaves in almost the same way as outlined previously, with a + # couple of small exceptions. First, the prefix used to name the input + # elements within the form (hence the key that denotes them in the +params+ + # hash) is actually derived from the object's _class_, e.g. <tt>params[:post]</tt> + # if the object's class is +Post+. However, this can be overwritten using + # the <tt>:as</tt> option, e.g. - + # + # <%= form_for(@person, :as => :client) do |f| %> + # ... + # <% end %> # - # As we said above, in addition to manually configuring the +form_for+ - # call, you can rely on automated resource identification, which will use - # the conventions and named routes of that approach. This is the - # preferred way to use +form_for+ nowadays. + # would result in <tt>params[:client]</tt>. # - # For example, if <tt>@post</tt> is an existing record you want to edit + # Secondly, the field values shown when the form is initially displayed + # are taken from the attributes of the object passed to +form_for+, + # regardless of whether the object is an instance + # variable. So, for example, if we had a _local_ variable +post+ + # representing an existing record, + # + # <%= form_for post do |f| %> + # ... + # <% end %> + # + # would produce a form with fields whose initial state reflect the current + # values of the attributes of +post+. + # + # === Resource-oriented style + # + # In the examples just shown, although not indicated explicitly, we still + # need to use the <tt>:url</tt> option in order to specify where the + # form is going to be sent. However, further simplification is possible + # if the record passed to +form_for+ is a _resource_, i.e. it corresponds + # to a set of RESTful routes, e.g. defined using the +resources+ method + # in <tt>config/routes.rb</tt>. In this case Rails will simply infer the + # appropriate URL from the record itself. For example, # # <%= form_for @post do |f| %> # ... # <% end %> # - # is equivalent to something like: + # is then equivalent to something like: # # <%= form_for @post, :as => :post, :url => post_path(@post), :method => :put, :html => { :class => "edit_post", :id => "edit_post_45" } do |f| %> # ... # <% end %> # - # And for new records + # And for a new record # # <%= form_for(Post.new) do |f| %> # ... @@ -211,7 +262,7 @@ module ActionView # ... # <% end %> # - # You can also overwrite the individual conventions, like this: + # However you can still overwrite individual conventions, such as: # # <%= form_for(@post, :url => super_posts_path) do |f| %> # ... @@ -223,13 +274,6 @@ module ActionView # ... # <% end %> # - # If you have an object that needs to be represented as a different - # parameter, like a Person that acts as a Client: - # - # <%= form_for(@person, :as => :client) do |f| %> - # ... - # <% end %> - # # For namespaced routes, like +admin_post_url+: # # <%= form_for([:admin, @post]) do |f| %> @@ -252,9 +296,9 @@ module ActionView # # :method => (:get|:post|:patch|:put|:delete) # - # in the options hash. If the verb is not GET or POST, which are natively supported by HTML forms, the - # form will be set to POST and a hidden input called _method will carry the intended verb for the server - # to interpret. + # in the options hash. If the verb is not GET or POST, which are natively + # supported by HTML forms, the form will be set to POST and a hidden input + # called _method will carry the intended verb for the server to interpret. # # === Unobtrusive JavaScript # @@ -402,30 +446,59 @@ module ActionView # # === Generic Examples # + # 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 @person.permission do |permission_fields| %> + # <%= fields_for :permission, @person.permission do |permission_fields| %> # Admin? : <%= permission_fields.check_box :admin %> # <% end %> # # <%= f.submit %> # <% end %> # - # ...or if you have an object that needs to be represented as a different - # parameter, like a Client that acts as a Person: + # 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 :person, @client do |permission_fields| %> + # <%= fields_for :permission do |permission_fields| %> # Admin?: <%= permission_fields.check_box :admin %> # <% end %> # - # ...or if you don't have an object, just a name of the parameter: + # ...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>. # - # <%= fields_for :person do |permission_fields| %> + # 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. @@ -860,20 +933,20 @@ module ActionView # ==== Examples # # search_field(:user, :name) - # # => <input id="user_name" name="user[name]" size="30" type="search" /> + # # => <input id="user_name" name="user[name]" type="search" /> # search_field(:user, :name, :autosave => false) - # # => <input autosave="false" id="user_name" name="user[name]" size="30" type="search" /> + # # => <input autosave="false" id="user_name" name="user[name]" type="search" /> # search_field(:user, :name, :results => 3) - # # => <input id="user_name" name="user[name]" results="3" size="30" type="search" /> + # # => <input id="user_name" name="user[name]" results="3" type="search" /> # # Assume request.host returns "www.example.com" # search_field(:user, :name, :autosave => true) - # # => <input autosave="com.example.www" id="user_name" name="user[name]" results="10" size="30" type="search" /> + # # => <input autosave="com.example.www" id="user_name" name="user[name]" results="10" type="search" /> # search_field(:user, :name, :onsearch => true) - # # => <input id="user_name" incremental="true" name="user[name]" onsearch="true" size="30" type="search" /> + # # => <input id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" /> # search_field(:user, :name, :autosave => false, :onsearch => true) - # # => <input autosave="false" id="user_name" incremental="true" name="user[name]" onsearch="true" size="30" type="search" /> + # # => <input autosave="false" id="user_name" incremental="true" name="user[name]" onsearch="true" type="search" /> # search_field(:user, :name, :autosave => true, :onsearch => true) - # # => <input autosave="com.example.www" id="user_name" incremental="true" name="user[name]" onsearch="true" results="10" size="30" type="search" /> + # # => <input autosave="com.example.www" id="user_name" incremental="true" name="user[name]" onsearch="true" results="10" type="search" /> # def search_field(object_name, method, options = {}) Tags::SearchField.new(object_name, method, self, options).render @@ -882,7 +955,7 @@ module ActionView # Returns a text_field of type "tel". # # telephone_field("user", "phone") - # # => <input id="user_phone" name="user[phone]" size="30" type="tel" /> + # # => <input id="user_phone" name="user[phone]" type="tel" /> # def telephone_field(object_name, method, options = {}) Tags::TelField.new(object_name, method, self, options).render @@ -910,7 +983,7 @@ module ActionView # Returns a text_field of type "url". # # url_field("user", "homepage") - # # => <input id="user_homepage" size="30" name="user[homepage]" type="url" /> + # # => <input id="user_homepage" name="user[homepage]" type="url" /> # def url_field(object_name, method, options = {}) Tags::UrlField.new(object_name, method, self, options).render @@ -919,7 +992,7 @@ module ActionView # Returns a text_field of type "email". # # email_field("user", "address") - # # => <input id="user_address" size="30" name="user[address]" type="email" /> + # # => <input id="user_address" name="user[address]" type="email" /> # def email_field(object_name, method, options = {}) Tags::EmailField.new(object_name, method, self, options).render @@ -985,7 +1058,12 @@ module ActionView self end - def initialize(object_name, object, template, options) + def initialize(object_name, object, template, options, block=nil) + if block + ActiveSupport::Deprecation.warn( + "Giving a block to FormBuilder is deprecated and has no effect anymore.") + end + @nested_child_index = {} @object_name, @object, @template, @options = object_name, object, template, options @parent_builder = options[:parent_builder] @@ -1016,7 +1094,7 @@ module ActionView fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options? fields_options[:builder] ||= options[:builder] fields_options[:parent_builder] = self - fields_options[:namespace] = fields_options[:parent_builder].options[:namespace] + fields_options[:namespace] = options[:namespace] case record_name when String, Symbol diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index f73ca220fb..d61c2bbee2 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -153,6 +153,8 @@ module ActionView # form, and parameters extraction gets the last occurrence of any repeated # key in the query string, that works for ordinary forms. # + # In case if you don't want the helper to generate this hidden field you can specify <tt>:include_blank => false</tt> option. + # def select(object, method, choices, options = {}, html_options = {}) Tags::Select.new(object, method, self, choices, options, html_options).render end @@ -578,9 +580,9 @@ module ActionView # b.label(:class => "radio_button") { b.radio_button(:class => "radio_button") } # end # - # There are also two special methods available: <tt>text</tt> and - # <tt>value</tt>, which are the current text and value methods for the - # item being rendered, respectively. You can use them like this: + # There are also three special methods available: <tt>object</tt>, <tt>text</tt> and + # <tt>value</tt>, which are the current item being rendered, its text and value methods, + # respectively. You can use them like this: # collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial) do |b| # b.label(:"data-value" => b.value) { b.radio_button + b.text } # end @@ -641,9 +643,9 @@ module ActionView # b.label(:class => "check_box") { b.check_box(:class => "check_box") } # end # - # There are also two special methods available: <tt>text</tt> and - # <tt>value</tt>, which are the current text and value methods for the - # item being rendered, respectively. You can use them like this: + # There are also three special methods available: <tt>object</tt>, <tt>text</tt> and + # <tt>value</tt>, which are the current item being rendered, its text and value methods, + # respectively. You can use them like this: # collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial) do |b| # b.label(:"data-value" => b.value) { b.check_box + b.text } # end diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 2cf1c9055c..e403d6cf5e 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -2,6 +2,7 @@ require 'cgi' require 'action_view/helpers/tag_helper' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/string/output_safety' +require 'active_support/core_ext/module/attribute_accessors' module ActionView # = Action View Form Tag Helpers @@ -17,6 +18,9 @@ module ActionView include UrlHelper include TextHelper + mattr_accessor :embed_authenticity_token_in_remote_forms + self.embed_authenticity_token_in_remote_forms = false + # Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like # ActionController::Base#url_for. The method for the form defaults to POST. # @@ -27,7 +31,11 @@ module ActionView # is added to simulate the verb over post. # * <tt>:authenticity_token</tt> - Authenticity token to use in the form. Use only if you need to # pass custom authenticity token string, or to not add authenticity_token field at all - # (by passing <tt>false</tt>). + # (by passing <tt>false</tt>). Remote forms may omit the embedded authenticity token + # by setting <tt>config.action_view.embed_authenticity_token_in_remote_forms = false</tt>. + # This is helpful when you're fragment-caching the form. Remote forms get the + # authenticity from the <tt>meta</tt> tag, so embedding is unnecessary unless you + # support browsers without JavaScript. # * A list of parameters to feed to the URL the form will be posted to. # * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the # submit behavior. By default this behavior is an ajax submit. @@ -93,7 +101,7 @@ module ActionView # # => <select id="colors" multiple="multiple" name="colors[]"><option>Red</option> # # <option>Green</option><option>Blue</option></select> # - # select_tag "locations", "<option>Home</option><option selected="selected">Work</option><option>Out</option>".html_safe + # select_tag "locations", "<option>Home</option><option selected='selected'>Work</option><option>Out</option>".html_safe # # => <select id="locations" name="locations"><option>Home</option><option selected='selected'>Work</option> # # <option>Out</option></select> # @@ -525,10 +533,9 @@ module ActionView # <% end %> # # => <fieldset class="format"><p><input id="name" name="name" type="text" /></p></fieldset> def field_set_tag(legend = nil, options = nil, &block) - content = capture(&block) output = tag(:fieldset, options, true) output.safe_concat(content_tag(:legend, legend)) unless legend.blank? - output.concat(content) + output.concat(capture(&block)) if block_given? output.safe_concat("</fieldset>") end @@ -617,8 +624,19 @@ module ActionView # responsibility of the caller to escape all the values. html_options["action"] = url_for(url_for_options) html_options["accept-charset"] = "UTF-8" + html_options["data-remote"] = true if html_options.delete("remote") - html_options["authenticity_token"] = html_options.delete("authenticity_token") if html_options.has_key?("authenticity_token") + + if html_options["data-remote"] && + !embed_authenticity_token_in_remote_forms && + html_options["authenticity_token"].blank? + # The authenticity token is taken from the meta tag in this case + html_options["authenticity_token"] = false + elsif html_options["authenticity_token"] == true + # Include the default authenticity_token, which is only generated when its set to nil, + # but we needed the true value to override the default of no authenticity_token on data-remote. + html_options["authenticity_token"] = nil + end end end diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index ac9e530f01..d88f5babb9 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -14,6 +14,8 @@ module ActionView } JS_ESCAPE_MAP["\342\200\250".force_encoding('UTF-8').encode!] = '
' + JS_ESCAPE_MAP["\342\200\251".force_encoding('UTF-8').encode!] = '
' + # Escapes carriage returns and single and double quotes for JavaScript segments. # @@ -22,7 +24,7 @@ module ActionView # $('some_element').replaceWith('<%=j render 'some/element_template' %>'); def escape_javascript(javascript) if javascript - result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] } + result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] } javascript.html_safe? ? result.html_safe : result else '' diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index b0860f87c4..485a9357be 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -292,6 +292,7 @@ module ActionView precision = precision > 0 ? precision : 0 #don't let it be negative else rounded_number = BigDecimal.new(number.to_s).round(precision).to_f + rounded_number = rounded_number.zero? ? rounded_number.abs : rounded_number #prevent showing negative zeros end formatted_number = number_with_delimiter("%01.#{precision}f" % rounded_number, options) if strip_insignificant_zeros diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index ecd26891d6..71be2955a8 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -17,6 +17,10 @@ module ActionView autofocus novalidate formnovalidate open pubdate).to_set BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map {|attribute| attribute.to_sym }) + PRE_CONTENT_STRINGS = { + :textarea => "\n" + } + # Returns an empty HTML tag of type +name+ which by default is XHTML # compliant. Set +open+ to true to create an open tag compatible # with HTML 4.0 and below. Add HTML attributes by passing an attributes @@ -126,7 +130,7 @@ module ActionView def content_tag_string(name, content, options, escape = true) tag_options = tag_options(options, escape) if options content = ERB::Util.h(content) if escape - "<#{name}#{tag_options}>#{content}</#{name}>".html_safe + "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name.to_sym]}#{content}</#{name}>".html_safe end def tag_options(options, escape = true) diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index c9c891daa1..7c2f12d8e7 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -5,8 +5,6 @@ module ActionView include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper include FormOptionsHelper - DEFAULT_FIELD_OPTIONS = { "size" => 30 } - attr_reader :object def initialize(object_name, method_name, template_object, options = {}) @@ -124,7 +122,8 @@ module ActionView html_options = html_options.stringify_keys add_default_name_and_id(html_options) select = content_tag("select", add_options(option_tags, options, value(object)), html_options) - if html_options["multiple"] + + if html_options["multiple"] && options.fetch(:include_hidden) { true } tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select else select @@ -133,12 +132,11 @@ module ActionView def add_options(option_tags, options, value = nil) if options[:include_blank] - include_blank = options[:include_blank] if options[:include_blank].kind_of?(String) - option_tags = content_tag(:option, include_blank, :value => '').safe_concat("\n").safe_concat(option_tags) + option_tags = content_tag('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags end if value.blank? && options[:prompt] prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select') - option_tags = content_tag(:option, prompt, :value => '').safe_concat("\n").safe_concat(option_tags) + option_tags = content_tag('option', prompt, :value => '') + "\n" + option_tags end option_tags end diff --git a/actionpack/lib/action_view/helpers/tags/check_box.rb b/actionpack/lib/action_view/helpers/tags/check_box.rb index 579cdb9fc9..1a4aebb936 100644 --- a/actionpack/lib/action_view/helpers/tags/check_box.rb +++ b/actionpack/lib/action_view/helpers/tags/check_box.rb @@ -25,9 +25,15 @@ module ActionView add_default_name_and_id(options) end - hidden = hidden_field_for_checkbox(options) + include_hidden = options.delete("include_hidden") { true } checkbox = tag("input", options) - hidden + checkbox + + if include_hidden + hidden = hidden_field_for_checkbox(options) + hidden + checkbox + else + checkbox + end end private diff --git a/actionpack/lib/action_view/helpers/tags/collection_helpers.rb b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb index 6f950e552a..6a1479069f 100644 --- a/actionpack/lib/action_view/helpers/tags/collection_helpers.rb +++ b/actionpack/lib/action_view/helpers/tags/collection_helpers.rb @@ -59,6 +59,7 @@ module ActionView end end + html_options[:object] = @object html_options end diff --git a/actionpack/lib/action_view/helpers/tags/label.rb b/actionpack/lib/action_view/helpers/tags/label.rb index 1bd71c2778..16135fcd5a 100644 --- a/actionpack/lib/action_view/helpers/tags/label.rb +++ b/actionpack/lib/action_view/helpers/tags/label.rb @@ -3,16 +3,16 @@ module ActionView module Tags class Label < Base #:nodoc: def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil) + options ||= {} + content_is_options = content_or_options.is_a?(Hash) if content_is_options - options = content_or_options + options.merge! content_or_options @content = nil else @content = content_or_options end - options ||= {} - super(object_name, method_name, template_object, options) end @@ -33,7 +33,7 @@ module ActionView options["for"] = name_and_id["id"] unless options.key?("for") if block_given? - @template_object.label_tag(name_and_id["id"], options, &block) + content = @template_object.capture(&block) else content = if @content.blank? @object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') @@ -55,9 +55,9 @@ module ActionView end content ||= @method_name.humanize - - label_tag(name_and_id["id"], content, options) end + + label_tag(name_and_id["id"], content, options) end end end diff --git a/actionpack/lib/action_view/helpers/tags/text_area.rb b/actionpack/lib/action_view/helpers/tags/text_area.rb index a7db8eb437..f74652c5e7 100644 --- a/actionpack/lib/action_view/helpers/tags/text_area.rb +++ b/actionpack/lib/action_view/helpers/tags/text_area.rb @@ -2,17 +2,15 @@ module ActionView module Helpers module Tags class TextArea < Base #:nodoc: - DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 20 } - def render - options = DEFAULT_TEXT_AREA_OPTIONS.merge(@options.stringify_keys) + options = @options.stringify_keys add_default_name_and_id(options) if size = options.delete("size") options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split) end - content_tag("textarea", ERB::Util.html_escape(options.delete('value') || value_before_type_cast(object)), options) + content_tag("textarea", options.delete('value') || value_before_type_cast(object), options) end end end diff --git a/actionpack/lib/action_view/helpers/tags/text_field.rb b/actionpack/lib/action_view/helpers/tags/text_field.rb index ce5182d20f..024a1a8af2 100644 --- a/actionpack/lib/action_view/helpers/tags/text_field.rb +++ b/actionpack/lib/action_view/helpers/tags/text_field.rb @@ -4,8 +4,7 @@ module ActionView class TextField < Base #:nodoc: def render options = @options.stringify_keys - options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") - options = DEFAULT_FIELD_OPTIONS.merge(options) + options["size"] = options["maxlength"] unless options.key?("size") options["type"] ||= field_type options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file" options["value"] &&= ERB::Util.html_escape(options["value"]) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 8d7417809b..4a641fada3 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -23,20 +23,25 @@ module ActionView include ActionDispatch::Routing::UrlFor include TagHelper - def _routes_context - controller - end + # We need to override url_optoins, _routes_context + # and optimize_routes_generation? to consider the controller. - # Need to map default url options to controller one. - # def default_url_options(*args) #:nodoc: - # controller.send(:default_url_options, *args) - # end - # - def url_options + def url_options #:nodoc: return super unless controller.respond_to?(:url_options) controller.url_options end + def _routes_context #:nodoc: + controller + end + protected :_routes_context + + def optimize_routes_generation? #:nodoc: + controller.respond_to?(:optimize_routes_generation?) ? + controller.optimize_routes_generation? : super + end + protected :optimize_routes_generation? + # Returns the URL for the set of +options+ provided. This takes the # same options as +url_for+ in Action Controller (see the # documentation for <tt>ActionController::Base#url_for</tt>). Note that by default @@ -55,7 +60,7 @@ module ActionView # # ==== Relying on named routes # - # Passing a record (like an Active Record or Active Resource) instead of a Hash as the options parameter will + # Passing a record (like an Active Record) instead of a Hash as the options parameter will # trigger the named route for that record. The lookup will happen on the name of the class. So passing a # Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as # +admin_workshop_path+ you'll have to call that explicitly (it's impossible for +url_for+ to guess that route). @@ -329,7 +334,7 @@ module ActionView remote = html_options.delete('remote') method = html_options.delete('method').to_s - method_tag = %w{patch put delete}.include?(method) ? method_tag(method) : "" + method_tag = %w{patch put delete}.include?(method) ? method_tag(method) : ''.html_safe form_method = method == 'get' ? 'get' : 'post' form_options = html_options.delete('form') || {} @@ -342,7 +347,8 @@ module ActionView html_options = convert_options_to_data_attributes(options, html_options) html_options.merge!("type" => "submit", "value" => name || url) - "#{tag(:form, form_options, true)}<div>#{method_tag}#{tag("input", html_options)}#{request_token_tag}</div></form>".html_safe + inner_tags = method_tag.safe_concat tag('input', html_options).safe_concat request_token_tag + content_tag('form', content_tag('div', inner_tags), form_options) end diff --git a/actionpack/lib/action_view/railtie.rb b/actionpack/lib/action_view/railtie.rb index 43371a1c49..9f5e3be454 100644 --- a/actionpack/lib/action_view/railtie.rb +++ b/actionpack/lib/action_view/railtie.rb @@ -7,6 +7,14 @@ module ActionView config.action_view = ActiveSupport::OrderedOptions.new config.action_view.stylesheet_expansions = {} config.action_view.javascript_expansions = { :defaults => %w(jquery jquery_ujs) } + config.action_view.embed_authenticity_token_in_remote_forms = false + + initializer "action_view.embed_authenticity_token_in_remote_forms" do |app| + ActiveSupport.on_load(:action_view) do + ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = + app.config.action_view.delete(:embed_authenticity_token_in_remote_forms) + end + end initializer "action_view.logger" do ActiveSupport.on_load(:action_view) { self.logger ||= Rails.logger } diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb index 3033294883..8b53867aea 100644 --- a/actionpack/lib/action_view/renderer/partial_renderer.rb +++ b/actionpack/lib/action_view/renderer/partial_renderer.rb @@ -32,7 +32,7 @@ module ActionView # # <%= render :partial => "account", :object => @buyer %> # - # would provide the +@buyer+ object to the partial, available under the local variable +account+ and is + # would provide the <tt>@buyer</tt> object to the partial, available under the local variable +account+ and is # equivalent to: # # <%= render :partial => "account", :locals => { :account => @buyer } %> @@ -158,6 +158,43 @@ module ActionView # Name: <%= user.name %> # </div> # + # If a collection is given, the layout will be rendered once for each item in the collection. Just think + # these two snippets have the same output: + # + # <%# app/views/users/_user.html.erb %> + # Name: <%= user.name %> + # + # <%# app/views/users/index.html.erb %> + # <%# This does not use layouts %> + # <ul> + # <% users.each do |user| -%> + # <li> + # <%= render :partial => "user", :locals => { :user => user } %> + # </li> + # <% end -%> + # </ul> + # + # <%# app/views/users/_li_layout.html.erb %> + # <li> + # <%= yield %> + # </li> + # + # <%# app/views/users/index.html.erb %> + # <ul> + # <%= render :partial => "user", :layout => "li_layout", :collection => users %> + # </ul> + # + # Given two users whose names are Alice and Bob, these snippets return: + # + # <ul> + # <li> + # Name: Alice + # </li> + # <li> + # Name: Bob + # </li> + # </ul> + # # You can also apply a layout to a block within any template: # # <%# app/views/users/_chief.html.erb &> @@ -208,18 +245,25 @@ module ActionView # <%- end -%> # <% end %> class PartialRenderer < AbstractRenderer - PARTIAL_NAMES = Hash.new { |h,k| h[k] = {} } + PREFIXED_PARTIAL_NAMES = Hash.new { |h,k| h[k] = {} } def initialize(*) super @context_prefix = @lookup_context.prefixes.first - @partial_names = PARTIAL_NAMES[@context_prefix] end def render(context, options, block) setup(context, options, block) identifier = (@template = find_partial) ? @template.identifier : @path + @lookup_context.rendered_format ||= begin + if @template && @template.formats.present? + @template.formats.first + else + formats.first + end + end + if @collection instrument(:collection, :identifier => identifier || "collection", :count => @collection.size) do render_collection @@ -238,7 +282,14 @@ module ActionView spacer = find_template(@options[:spacer_template]).render(@view, @locals) end + if layout = @options[:layout] + layout = find_template(layout) + end + result = @template ? collection_with_template : collection_without_template + + result.map!{|content| layout.render(@view, @locals) { content } } if layout + result.join(spacer).html_safe end @@ -340,9 +391,10 @@ module ActionView locals[as] = object segments << template.render(@view, locals) end - + segments end + def collection_without_template segments, locals, collection_data = [], @locals, @collection_data @@ -370,7 +422,15 @@ module ActionView raise ArgumentError.new("'#{object.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.") end - @partial_names[path] ||= merge_prefix_into_object_path(@context_prefix, path.dup) + if @view.prefix_partial_path_with_controller_namespace + prefixed_partial_names[path] ||= merge_prefix_into_object_path(@context_prefix, path.dup) + else + path + end + end + + def prefixed_partial_names + @prefixed_partial_names ||= PREFIXED_PARTIAL_NAMES[@context_prefix] end def merge_prefix_into_object_path(prefix, object_path) diff --git a/actionpack/lib/action_view/renderer/template_renderer.rb b/actionpack/lib/action_view/renderer/template_renderer.rb index 20f75fba45..ae923de24e 100644 --- a/actionpack/lib/action_view/renderer/template_renderer.rb +++ b/actionpack/lib/action_view/renderer/template_renderer.rb @@ -6,8 +6,13 @@ module ActionView @view = context @details = extract_details(options) template = determine_template(options) - @lookup_context.rendered_format ||= template.formats.first - @lookup_context.formats = template.formats + context = @lookup_context + + unless context.rendered_format + context.formats = template.formats unless template.formats.empty? + context.rendered_format = context.formats.first + end + render_template(template, options[:layout], options[:locals]) end diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 7fa86866a7..8ea2e5bfe4 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -176,7 +176,7 @@ module ActionView end end - # A resolver that loads files from the filesystem. It allows to set your own + # A resolver that loads files from the filesystem. It allows setting your own # resolving pattern. Such pattern can be a glob string supported by some variables. # # ==== Examples @@ -192,7 +192,7 @@ module ActionView # # FileSystemResolver.new("/path/to/views", ":prefix/{:formats/,}:action{.:locale,}{.:formats,}{.:handlers,}") # - # If you don't specify pattern then the default will be used. + # If you don't specify a pattern then the default will be used. # # In order to use any of the customized resolvers above in a Rails application, you just need # to configure ActionController::Base.view_paths in an initializer, for example: @@ -204,10 +204,10 @@ module ActionView # # ==== Pattern format and variables # - # Pattern have to be a valid glob string, and it allows you to use the + # Pattern has to be a valid glob string, and it allows you to use the # following variables: # - # * <tt>:prefix</tt> - usualy the controller path + # * <tt>:prefix</tt> - usually the controller path # * <tt>:action</tt> - name of the action # * <tt>:locale</tt> - possible locale versions # * <tt>:formats</tt> - possible request formats (for example html, json, xml...) diff --git a/actionpack/lib/sprockets/assets.rake b/actionpack/lib/sprockets/assets.rake deleted file mode 100644 index 2e92fe416b..0000000000 --- a/actionpack/lib/sprockets/assets.rake +++ /dev/null @@ -1,105 +0,0 @@ -require "fileutils" - -namespace :assets do - def ruby_rake_task(task, fork = true) - env = ENV['RAILS_ENV'] || 'production' - groups = ENV['RAILS_GROUPS'] || 'assets' - args = [$0, task,"RAILS_ENV=#{env}","RAILS_GROUPS=#{groups}"] - args << "--trace" if Rake.application.options.trace - if $0 =~ /rake\.bat\Z/i - Kernel.exec $0, *args - else - fork ? ruby(*args) : Kernel.exec(FileUtils::RUBY, *args) - end - end - - # We are currently running with no explicit bundler group - # and/or no explicit environment - we have to reinvoke rake to - # execute this task. - def invoke_or_reboot_rake_task(task) - if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty? - ruby_rake_task task - else - Rake::Task[task].invoke - end - end - - desc "Compile all the assets named in config.assets.precompile" - task :precompile do - invoke_or_reboot_rake_task "assets:precompile:all" - end - - namespace :precompile do - def internal_precompile(digest=nil) - unless Rails.application.config.assets.enabled - warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true" - exit - end - - # Ensure that action view is loaded and the appropriate - # sprockets hooks get executed - _ = ActionView::Base - - config = Rails.application.config - config.assets.compile = true - config.assets.digest = digest unless digest.nil? - config.assets.digests = {} - - env = Rails.application.assets - target = File.join(Rails.public_path, config.assets.prefix) - compiler = Sprockets::StaticCompiler.new(env, - target, - config.assets.precompile, - :manifest_path => config.assets.manifest, - :digest => config.assets.digest, - :manifest => digest.nil?) - compiler.compile - end - - task :all do - Rake::Task["assets:precompile:primary"].invoke - # We need to reinvoke in order to run the secondary digestless - # asset compilation run - a fresh Sprockets environment is - # required in order to compile digestless assets as the - # environment has already cached the assets on the primary - # run. - ruby_rake_task("assets:precompile:nondigest", false) if Rails.application.config.assets.digest - end - - task :primary => ["assets:cache:clean"] do - internal_precompile - end - - task :nondigest => ["assets:cache:clean"] do - internal_precompile(false) - end - end - - desc "Remove compiled assets" - task :clean do - invoke_or_reboot_rake_task "assets:clean:all" - end - - namespace :clean do - task :all => ["assets:cache:clean"] do - config = Rails.application.config - public_asset_path = File.join(Rails.public_path, config.assets.prefix) - rm_rf public_asset_path, :secure => true - end - end - - namespace :cache do - task :clean => ["assets:environment"] do - Rails.application.assets.cache.clear - end - end - - task :environment do - if Rails.application.config.assets.initialize_on_precompile - Rake::Task["environment"].invoke - else - Rails.application.initialize!(:assets) - Sprockets::Bootstrap.new(Rails.application).run - end - end -end diff --git a/actionpack/lib/sprockets/bootstrap.rb b/actionpack/lib/sprockets/bootstrap.rb deleted file mode 100644 index 395b264fe7..0000000000 --- a/actionpack/lib/sprockets/bootstrap.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Sprockets - class Bootstrap - def initialize(app) - @app = app - end - - # TODO: Get rid of config.assets.enabled - def run - app, config = @app, @app.config - return unless app.assets - - config.assets.paths.each { |path| app.assets.append_path(path) } - - if config.assets.compress - # temporarily hardcode default JS compressor to uglify. Soon, it will work - # the same as SCSS, where a default plugin sets the default. - unless config.assets.js_compressor == false - app.assets.js_compressor = LazyCompressor.new { Sprockets::Compressors.registered_js_compressor(config.assets.js_compressor || :uglifier) } - end - - unless config.assets.css_compressor == false - app.assets.css_compressor = LazyCompressor.new { Sprockets::Compressors.registered_css_compressor(config.assets.css_compressor) } - end - end - - if config.assets.compile - app.routes.prepend do - mount app.assets => config.assets.prefix - end - end - - if config.assets.digest - app.assets = app.assets.index - end - end - end -end diff --git a/actionpack/lib/sprockets/compressors.rb b/actionpack/lib/sprockets/compressors.rb deleted file mode 100644 index 8b728d6570..0000000000 --- a/actionpack/lib/sprockets/compressors.rb +++ /dev/null @@ -1,85 +0,0 @@ -module Sprockets - module Compressors - extend self - - @@css_compressors = {} - @@js_compressors = {} - @@default_css_compressor = nil - @@default_js_compressor = nil - - def register_css_compressor(name, klass, options = {}) - @@default_css_compressor = name.to_sym if options[:default] || @@default_css_compressor.nil? - @@css_compressors[name.to_sym] = { :klass => klass.to_s, :require => options[:require] } - end - - def register_js_compressor(name, klass, options = {}) - @@default_js_compressor = name.to_sym if options[:default] || @@default_js_compressor.nil? - @@js_compressors[name.to_sym] = { :klass => klass.to_s, :require => options[:require] } - end - - def registered_css_compressor(name) - find_registered_compressor name, @@css_compressors, @@default_css_compressor - end - - def registered_js_compressor(name) - find_registered_compressor name, @@js_compressors, @@default_js_compressor - end - - # The default compressors must be registered in default plugins (ex. Sass-Rails) - register_css_compressor(:scss, 'Sass::Rails::Compressor', :require => 'sass/rails/compressor', :default => true) - register_js_compressor(:uglifier, 'Uglifier', :require => 'uglifier', :default => true) - - # Automaticaly register some compressors - register_css_compressor(:yui, 'YUI::CssCompressor', :require => 'yui/compressor') - register_js_compressor(:closure, 'Closure::Compiler', :require => 'closure-compiler') - register_js_compressor(:yui, 'YUI::JavaScriptCompressor', :require => 'yui/compressor') - - private - - def find_registered_compressor(name, compressors_hash, default_compressor_name) - if name.respond_to?(:to_sym) - compressor = compressors_hash[name.to_sym] || compressors_hash[default_compressor_name] - require compressor[:require] if compressor[:require] - compressor[:klass].constantize.new - else - name - end - end - end - - # An asset compressor which does nothing. - # - # This compressor simply returns the asset as-is, without any compression - # whatsoever. It is useful in development mode, when compression isn't - # needed but using the same asset pipeline as production is desired. - class NullCompressor #:nodoc: - def compress(content) - content - end - end - - # An asset compressor which only initializes the underlying compression - # engine when needed. - # - # This postpones the initialization of the compressor until - # <code>#compress</code> is called the first time. - class LazyCompressor #:nodoc: - # Initializes a new LazyCompressor. - # - # The block should return a compressor when called, i.e. an object - # which responds to <code>#compress</code>. - def initialize(&block) - @block = block - end - - def compress(content) - compressor.compress(content) - end - - private - - def compressor - @compressor ||= (@block.call || NullCompressor.new) - end - end -end diff --git a/actionpack/lib/sprockets/helpers.rb b/actionpack/lib/sprockets/helpers.rb deleted file mode 100644 index fee48386e0..0000000000 --- a/actionpack/lib/sprockets/helpers.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Sprockets - module Helpers - autoload :RailsHelper, "sprockets/helpers/rails_helper" - autoload :IsolatedHelper, "sprockets/helpers/isolated_helper" - end -end diff --git a/actionpack/lib/sprockets/helpers/isolated_helper.rb b/actionpack/lib/sprockets/helpers/isolated_helper.rb deleted file mode 100644 index 3adb928c45..0000000000 --- a/actionpack/lib/sprockets/helpers/isolated_helper.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Sprockets - module Helpers - module IsolatedHelper - def controller - nil - end - - def config - Rails.application.config.action_controller - end - end - end -end diff --git a/actionpack/lib/sprockets/helpers/rails_helper.rb b/actionpack/lib/sprockets/helpers/rails_helper.rb deleted file mode 100644 index 839ec7635f..0000000000 --- a/actionpack/lib/sprockets/helpers/rails_helper.rb +++ /dev/null @@ -1,167 +0,0 @@ -require "action_view" - -module Sprockets - module Helpers - module RailsHelper - extend ActiveSupport::Concern - include ActionView::Helpers::AssetTagHelper - - def asset_paths - @asset_paths ||= begin - paths = RailsHelper::AssetPaths.new(config, controller) - paths.asset_environment = asset_environment - paths.asset_digests = asset_digests - paths.compile_assets = compile_assets? - paths.digest_assets = digest_assets? - paths - end - end - - def javascript_include_tag(*sources) - options = sources.extract_options! - debug = options.delete(:debug) { debug_assets? } - body = options.delete(:body) { false } - digest = options.delete(:digest) { digest_assets? } - - sources.collect do |source| - if debug && asset = asset_paths.asset_for(source, 'js') - asset.to_a.map { |dep| - super(dep.pathname.to_s, { :src => path_to_asset(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options)) - } - else - super(source.to_s, { :src => path_to_asset(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options)) - end - end.join("\n").html_safe - end - - def stylesheet_link_tag(*sources) - options = sources.extract_options! - debug = options.delete(:debug) { debug_assets? } - body = options.delete(:body) { false } - digest = options.delete(:digest) { digest_assets? } - - sources.collect do |source| - if debug && asset = asset_paths.asset_for(source, 'css') - asset.to_a.map { |dep| - super(dep.pathname.to_s, { :href => path_to_asset(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options)) - } - else - super(source.to_s, { :href => path_to_asset(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options)) - end - end.join("\n").html_safe - end - - def asset_path(source, options = {}) - source = source.logical_path if source.respond_to?(:logical_path) - path = asset_paths.compute_public_path(source, asset_prefix, options.merge(:body => true)) - options[:body] ? "#{path}?body=1" : path - end - alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route - - def image_path(source) - path_to_asset(source) - end - alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route - - def font_path(source) - path_to_asset(source) - end - alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route - - def javascript_path(source) - path_to_asset(source, :ext => 'js') - end - alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with an javascript_path named route - - def stylesheet_path(source) - path_to_asset(source, :ext => 'css') - end - alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route - - private - def debug_assets? - compile_assets? && (Rails.application.config.assets.debug || params[:debug_assets]) - rescue NoMethodError - false - end - - # Override to specify an alternative prefix for asset path generation. - # When combined with a custom +asset_environment+, this can be used to - # implement themes that can take advantage of the asset pipeline. - # - # If you only want to change where the assets are mounted, refer to - # +config.assets.prefix+ instead. - def asset_prefix - Rails.application.config.assets.prefix - end - - def asset_digests - Rails.application.config.assets.digests - end - - def compile_assets? - Rails.application.config.assets.compile - end - - def digest_assets? - Rails.application.config.assets.digest - end - - # Override to specify an alternative asset environment for asset - # path generation. The environment should already have been mounted - # at the prefix returned by +asset_prefix+. - def asset_environment - Rails.application.assets - end - - class AssetPaths < ::ActionView::AssetPaths #:nodoc: - attr_accessor :asset_environment, :asset_prefix, :asset_digests, :compile_assets, :digest_assets - - class AssetNotPrecompiledError < StandardError; end - - def asset_for(source, ext) - source = source.to_s - return nil if is_uri?(source) - source = rewrite_extension(source, nil, ext) - asset_environment[source] - rescue Sprockets::FileOutsidePaths - nil - end - - def digest_for(logical_path) - if digest_assets && asset_digests && (digest = asset_digests[logical_path]) - return digest - end - - if compile_assets - if digest_assets && asset = asset_environment[logical_path] - return asset.digest_path - end - return logical_path - else - raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled") - end - end - - def rewrite_asset_path(source, dir, options = {}) - if source[0] == ?/ - source - else - source = digest_for(source) unless options[:digest] == false - source = File.join(dir, source) - source = "/#{source}" unless source =~ /^\// - source - end - end - - def rewrite_extension(source, dir, ext) - if ext && File.extname(source) != ".#{ext}" - "#{source}.#{ext}" - else - source - end - end - end - end - end -end diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb deleted file mode 100644 index 2bc482a39d..0000000000 --- a/actionpack/lib/sprockets/railtie.rb +++ /dev/null @@ -1,62 +0,0 @@ -require "action_controller/railtie" - -module Sprockets - autoload :Bootstrap, "sprockets/bootstrap" - autoload :Helpers, "sprockets/helpers" - autoload :Compressors, "sprockets/compressors" - autoload :LazyCompressor, "sprockets/compressors" - autoload :NullCompressor, "sprockets/compressors" - autoload :StaticCompiler, "sprockets/static_compiler" - - # TODO: Get rid of config.assets.enabled - class Railtie < ::Rails::Railtie - rake_tasks do - load "sprockets/assets.rake" - end - - initializer "sprockets.environment", :group => :all do |app| - config = app.config - next unless config.assets.enabled - - require 'sprockets' - - app.assets = Sprockets::Environment.new(app.root.to_s) do |env| - env.version = ::Rails.env + "-#{config.assets.version}" - - if config.assets.logger != false - env.logger = config.assets.logger || ::Rails.logger - end - - if config.assets.cache_store != false - env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache - end - end - - if config.assets.manifest - path = File.join(config.assets.manifest, "manifest.yml") - else - path = File.join(Rails.public_path, config.assets.prefix, "manifest.yml") - end - - if File.exist?(path) - config.assets.digests = YAML.load_file(path) - end - - ActiveSupport.on_load(:action_view) do - include ::Sprockets::Helpers::RailsHelper - app.assets.context_class.instance_eval do - include ::Sprockets::Helpers::IsolatedHelper - include ::Sprockets::Helpers::RailsHelper - end - end - end - - # We need to configure this after initialization to ensure we collect - # paths from all engines. This hook is invoked exactly before routes - # are compiled, and so that other Railties have an opportunity to - # register compressors. - config.after_initialize do |app| - Sprockets::Bootstrap.new(app).run - end - end -end diff --git a/actionpack/lib/sprockets/static_compiler.rb b/actionpack/lib/sprockets/static_compiler.rb deleted file mode 100644 index 9bbb464474..0000000000 --- a/actionpack/lib/sprockets/static_compiler.rb +++ /dev/null @@ -1,62 +0,0 @@ -require 'fileutils' - -module Sprockets - class StaticCompiler - attr_accessor :env, :target, :paths - - def initialize(env, target, paths, options = {}) - @env = env - @target = target - @paths = paths - @digest = options.fetch(:digest, true) - @manifest = options.fetch(:manifest, true) - @manifest_path = options.delete(:manifest_path) || target - @zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/ - end - - def compile - manifest = {} - env.each_logical_path do |logical_path| - next unless compile_path?(logical_path) - if asset = env.find_asset(logical_path) - manifest[logical_path] = write_asset(asset) - end - end - write_manifest(manifest) if @manifest - end - - def write_manifest(manifest) - FileUtils.mkdir_p(@manifest_path) - File.open("#{@manifest_path}/manifest.yml", 'wb') do |f| - YAML.dump(manifest, f) - end - end - - def write_asset(asset) - path_for(asset).tap do |path| - filename = File.join(target, path) - FileUtils.mkdir_p File.dirname(filename) - asset.write_to(filename) - asset.write_to("#{filename}.gz") if filename.to_s =~ @zip_files - end - end - - def compile_path?(logical_path) - paths.each do |path| - case path - when Regexp - return true if path.match(logical_path) - when Proc - return true if path.call(logical_path) - else - return true if File.fnmatch(path.to_s, logical_path) - end - end - false - end - - def path_for(asset) - @digest ? asset.digest_path : asset.logical_path - end - end -end diff --git a/actionpack/test/abstract/abstract_controller_test.rb b/actionpack/test/abstract/abstract_controller_test.rb index bf068aedcd..62f82a4c7a 100644 --- a/actionpack/test/abstract/abstract_controller_test.rb +++ b/actionpack/test/abstract/abstract_controller_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'set' module AbstractController module Testing @@ -28,7 +29,7 @@ module AbstractController # Test Render mixin # ==== class RenderingController < AbstractController::Base - include ::AbstractController::Rendering + include AbstractController::Rendering def _prefixes [] @@ -152,7 +153,7 @@ module AbstractController # ==== # self._layout is used when defined class WithLayouts < PrefixedViews - include Layouts + include AbstractController::Layouts private def self.layout(formats) @@ -254,7 +255,7 @@ module AbstractController class TestActionMethodsReloading < ActiveSupport::TestCase test "action_methods should be reloaded after defining a new method" do - assert_equal ["index"], Me6.action_methods + assert_equal Set.new(["index"]), Me6.action_methods end end diff --git a/actionpack/test/abstract/collector_test.rb b/actionpack/test/abstract/collector_test.rb index 2ebcebbbb7..c14d24905b 100644 --- a/actionpack/test/abstract/collector_test.rb +++ b/actionpack/test/abstract/collector_test.rb @@ -3,7 +3,7 @@ require 'abstract_unit' module AbstractController module Testing class MyCollector - include Collector + include AbstractController::Collector attr_accessor :responses def initialize @@ -54,4 +54,4 @@ module AbstractController end end end -end
\ No newline at end of file +end diff --git a/actionpack/test/abstract/helper_test.rb b/actionpack/test/abstract/helper_test.rb index b28a5b5afb..9a7445de7b 100644 --- a/actionpack/test/abstract/helper_test.rb +++ b/actionpack/test/abstract/helper_test.rb @@ -7,7 +7,7 @@ module AbstractController class ControllerWithHelpers < AbstractController::Base include AbstractController::Rendering - include Helpers + include AbstractController::Helpers def with_module render :inline => "Module <%= included_method %>" @@ -44,7 +44,7 @@ module AbstractController class AbstractHelpersBlock < ControllerWithHelpers helper do - include ::AbstractController::Testing::HelperyTest + include AbstractController::Testing::HelperyTest end end diff --git a/actionpack/test/abstract/layouts_test.rb b/actionpack/test/abstract/layouts_test.rb index fc25718d9e..558a45b87f 100644 --- a/actionpack/test/abstract/layouts_test.rb +++ b/actionpack/test/abstract/layouts_test.rb @@ -14,7 +14,10 @@ module AbstractControllerTests "layouts/overwrite.erb" => "Overwrite <%= yield %>", "layouts/with_false_layout.erb" => "False Layout <%= yield %>", "abstract_controller_tests/layouts/with_string_implied_child.erb" => - "With Implied <%= yield %>" + "With Implied <%= yield %>", + "abstract_controller_tests/layouts/with_grand_child_of_implied.erb" => + "With Grand Child <%= yield %>" + )] end @@ -64,6 +67,10 @@ module AbstractControllerTests class WithChildOfImplied < WithStringImpliedChild end + class WithGrandChildOfImplied < WithStringImpliedChild + layout nil + end + class WithProc < Base layout proc { |c| "overwrite" } @@ -72,6 +79,27 @@ module AbstractControllerTests end end + class WithZeroArityProc < Base + layout proc { "overwrite" } + + def index + render :template => ActionView::Template::Text.new("Hello zero arity proc!") + end + end + + class WithProcInContextOfInstance < Base + def an_instance_method; end + + layout proc { + break unless respond_to? :an_instance_method + "overwrite" + } + + def index + render :template => ActionView::Template::Text.new("Hello again zero arity proc!") + end + end + class WithSymbol < Base layout :hello @@ -221,6 +249,18 @@ module AbstractControllerTests assert_equal "Overwrite Hello proc!", controller.response_body end + test "when layout is specified as a proc without parameters it works just the same" do + controller = WithZeroArityProc.new + controller.process(:index) + assert_equal "Overwrite Hello zero arity proc!", controller.response_body + end + + test "when layout is specified as a proc without parameters the block is evaluated in the context of an instance" do + controller = WithProcInContextOfInstance.new + controller.process(:index) + assert_equal "Overwrite Hello again zero arity proc!", controller.response_body + end + test "when layout is specified as a symbol, call the requested method and use the layout returned" do controller = WithSymbol.new controller.process(:index) @@ -266,6 +306,13 @@ module AbstractControllerTests assert_equal "With Implied Hello string!", controller.response_body end + test "when a grandchild has nil layout specified, the child has an implied layout, and the " \ + "parent has specified a layout, use the child controller layout" do + controller = WithGrandChildOfImplied.new + controller.process(:index) + assert_equal "With Grand Child Hello string!", controller.response_body + end + test "raises an exception when specifying layout true" do assert_raises ArgumentError do Object.class_eval do @@ -299,6 +346,18 @@ module AbstractControllerTests controller.process(:index) assert_equal "Overwrite Hello index!", controller.response_body end + + test "layout for anonymous controller" do + klass = Class.new(WithString) do + def index + render :text => 'index', :layout => true + end + end + + controller = klass.new + controller.process(:index) + assert_equal "With String index", controller.response_body + end end end end diff --git a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb index 97be5a5bb0..8187eb72d5 100644 --- a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb +++ b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb @@ -130,14 +130,40 @@ class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveReco def test_render_with_record_in_nested_controller get :render_with_record_in_nested_controller - assert_template 'fun/games/_game' - assert_equal 'Pong', @response.body + assert_template %r{\Afun/games/_game\Z} + assert_equal "Fun Pong\n", @response.body end def test_render_with_record_collection_in_nested_controller get :render_with_record_collection_in_nested_controller - assert_template 'fun/games/_game' - assert_equal 'PongTank', @response.body + assert_template %r{\Afun/games/_game\Z} + assert_equal "Fun Pong\nFun Tank\n", @response.body + end +end + +class RenderPartialWithRecordIdentificationAndNestedControllersWithoutPrefixTest < ActiveRecordTestCase + tests Fun::NestedController + + def test_render_with_record_in_nested_controller + old_config = ActionView::Base.prefix_partial_path_with_controller_namespace + ActionView::Base.prefix_partial_path_with_controller_namespace = false + + get :render_with_record_in_nested_controller + assert_template %r{\Agames/_game\Z} + assert_equal "Just Pong\n", @response.body + ensure + ActionView::Base.prefix_partial_path_with_controller_namespace = old_config + end + + def test_render_with_record_collection_in_nested_controller + old_config = ActionView::Base.prefix_partial_path_with_controller_namespace + ActionView::Base.prefix_partial_path_with_controller_namespace = false + + get :render_with_record_collection_in_nested_controller + assert_template %r{\Agames/_game\Z} + assert_equal "Just Pong\nJust Tank\n", @response.body + ensure + ActionView::Base.prefix_partial_path_with_controller_namespace = old_config end end @@ -146,13 +172,39 @@ class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < Acti def test_render_with_record_in_deeper_nested_controller get :render_with_record_in_deeper_nested_controller - assert_template 'fun/serious/games/_game' - assert_equal 'Chess', @response.body + assert_template %r{\Afun/serious/games/_game\Z} + assert_equal "Serious Chess\n", @response.body end def test_render_with_record_collection_in_deeper_nested_controller get :render_with_record_collection_in_deeper_nested_controller - assert_template 'fun/serious/games/_game' - assert_equal 'ChessSudokuSolitaire', @response.body + assert_template %r{\Afun/serious/games/_game\Z} + assert_equal "Serious Chess\nSerious Sudoku\nSerious Solitaire\n", @response.body + end +end + +class RenderPartialWithRecordIdentificationAndNestedDeeperControllersWithoutPrefixTest < ActiveRecordTestCase + tests Fun::Serious::NestedDeeperController + + def test_render_with_record_in_deeper_nested_controller + old_config = ActionView::Base.prefix_partial_path_with_controller_namespace + ActionView::Base.prefix_partial_path_with_controller_namespace = false + + get :render_with_record_in_deeper_nested_controller + assert_template %r{\Agames/_game\Z} + assert_equal "Just Chess\n", @response.body + ensure + ActionView::Base.prefix_partial_path_with_controller_namespace = old_config + end + + def test_render_with_record_collection_in_deeper_nested_controller + old_config = ActionView::Base.prefix_partial_path_with_controller_namespace + ActionView::Base.prefix_partial_path_with_controller_namespace = false + + get :render_with_record_collection_in_deeper_nested_controller + assert_template %r{\Agames/_game\Z} + assert_equal "Just Chess\nJust Sudoku\nJust Solitaire\n", @response.body + ensure + ActionView::Base.prefix_partial_path_with_controller_namespace = old_config end end diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb index d3359e79a6..3d667f0a2f 100644 --- a/actionpack/test/controller/assert_select_test.rb +++ b/actionpack/test/controller/assert_select_test.rb @@ -131,6 +131,13 @@ class AssertSelectTest < ActionController::TestCase assert_raise(Assertion) { assert_select "pre", :html=>text } end + def test_strip_textarea + render_html %Q{<textarea>\n\nfoo\n</textarea>} + assert_select "textarea", "\nfoo\n" + render_html %Q{<textarea>\nfoo</textarea>} + assert_select "textarea", "foo" + end + def test_counts render_html %Q{<div id="1">foo</div><div id="2">foo</div>} assert_nothing_raised { assert_select "div", 2 } diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 791edb9069..2d4083252e 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -56,7 +56,7 @@ class UrlOptionsController < ActionController::Base end def url_options - super.merge(:host => 'www.override.com', :action => 'new', :locale => 'en') + super.merge(:host => 'www.override.com') end end @@ -130,8 +130,6 @@ class PerformActionTest < ActionController::TestCase @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @request.host = "www.nextangle.com" - - rescue_action_in_public! end def test_process_should_be_precise @@ -155,7 +153,6 @@ class UrlOptionsTest < ActionController::TestCase def setup super @request.host = 'www.example.com' - rescue_action_in_public! end def test_url_for_query_params_included @@ -183,9 +180,9 @@ class UrlOptionsTest < ActionController::TestCase get :from_view, :route => "from_view_url" - assert_equal 'http://www.override.com/from_view?locale=en', @response.body - assert_equal 'http://www.override.com/from_view?locale=en', @controller.send(:from_view_url) - assert_equal 'http://www.override.com/default_url_options/new?locale=en', @controller.url_for(:controller => 'default_url_options') + assert_equal 'http://www.override.com/from_view', @response.body + assert_equal 'http://www.override.com/from_view', @controller.send(:from_view_url) + assert_equal 'http://www.override.com/default_url_options/index', @controller.url_for(:controller => 'default_url_options') end end @@ -195,7 +192,6 @@ class UrlOptionsTest < ActionController::TestCase match "account/overview" end - @controller.class.send(:include, set.url_helpers) assert !@controller.class.action_methods.include?("account_overview_path") end end @@ -207,7 +203,6 @@ class DefaultUrlOptionsTest < ActionController::TestCase def setup super @request.host = 'www.example.com' - rescue_action_in_public! end def test_default_url_options_override @@ -258,7 +253,6 @@ class EmptyUrlOptionsTest < ActionController::TestCase def setup super @request.host = 'www.example.com' - rescue_action_in_public! end def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set diff --git a/actionpack/test/controller/force_ssl_test.rb b/actionpack/test/controller/force_ssl_test.rb index 7feeda25b3..5b423c8151 100644 --- a/actionpack/test/controller/force_ssl_test.rb +++ b/actionpack/test/controller/force_ssl_test.rb @@ -26,6 +26,14 @@ class ForceSSLExceptAction < ForceSSLController force_ssl :except => :banana end +class ForceSSLIfCondition < ForceSSLController + force_ssl :if => :use_force_ssl? + + def use_force_ssl? + action_name == 'cheeseburger' + end +end + class ForceSSLFlash < ForceSSLController force_ssl :except => [:banana, :set_flash, :use_flash] @@ -109,6 +117,21 @@ class ForceSSLExceptActionTest < ActionController::TestCase end end +class ForceSSLIfConditionTest < ActionController::TestCase + tests ForceSSLIfCondition + + def test_banana_not_redirects_to_https + get :banana + assert_response 200 + end + + def test_cheeseburger_redirects_to_https + get :cheeseburger + assert_response 301 + assert_equal "https://test.host/force_ssl_if_condition/cheeseburger", redirect_to_url + end +end + class ForceSSLFlashTest < ActionController::TestCase tests ForceSSLFlash diff --git a/actionpack/test/controller/http_digest_authentication_test.rb b/actionpack/test/controller/http_digest_authentication_test.rb index a91e3cafa5..828ea5b0fb 100644 --- a/actionpack/test/controller/http_digest_authentication_test.rb +++ b/actionpack/test/controller/http_digest_authentication_test.rb @@ -274,6 +274,6 @@ class HttpDigestAuthenticationTest < ActionController::TestCase end def decode_credentials(header) - ActionController::HttpAuthentication::Digest.decode_credentials(@response.headers['WWW-Authenticate']) + ActionController::HttpAuthentication::Digest.decode_credentials(header) end end diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index bc171e201b..c73b36f05e 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -78,6 +78,13 @@ end class DefaultLayoutController < LayoutTest end +class StreamingLayoutController < LayoutTest + def render(*args) + options = args.extract_options! || {} + super(*args, options.merge(:stream => true)) + end +end + class AbsolutePathLayoutController < LayoutTest layout File.expand_path(File.expand_path(__FILE__) + '/../../fixtures/layout_tests/layouts/layout_test') end @@ -122,6 +129,12 @@ class LayoutSetInResponseTest < ActionController::TestCase assert_template :layout => "layouts/layout_test" end + def test_layout_set_when_using_streaming_layout + @controller = StreamingLayoutController.new + get :hello + assert_template :hello + end + def test_layout_set_when_set_in_controller @controller = HasOwnLayoutController.new get :hello diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index ae368842b5..0e4099ddc6 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -132,7 +132,6 @@ class RespondToController < ActionController::Base end end - def iphone_with_html_response_type request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone" @@ -1135,7 +1134,7 @@ class PostController < AbstractPostController around_filter :with_iphone def index - respond_to(:html, :iphone) + respond_to(:html, :iphone, :js) end protected @@ -1182,4 +1181,45 @@ class MimeControllerLayoutsTest < ActionController::TestCase get :index assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body end + + def test_non_navigational_format_with_no_template_fallbacks_to_html_template_with_no_layout + get :index, :format => :js + assert_equal "Hello Firefox", @response.body + end +end + +class FlashResponder < ActionController::Responder + def initialize(controller, resources, options={}) + super + end + + def to_html + controller.flash[:notice] = 'Success' + super + end +end + +class FlashResponderController < ActionController::Base + self.responder = FlashResponder + respond_to :html + + def index + respond_with Object.new do |format| + format.html { render :text => 'HTML' } + end + end +end + +class FlashResponderControllerTest < ActionController::TestCase + tests FlashResponderController + + def test_respond_with_block_executed + get :index + assert_equal 'HTML', @response.body + end + + def test_flash_responder_executed + get :index + assert_equal 'Success', flash[:notice] + end end diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb index b1d76150f8..6dab42d75d 100644 --- a/actionpack/test/controller/redirect_test.rb +++ b/actionpack/test/controller/redirect_test.rb @@ -103,6 +103,14 @@ class RedirectController < ActionController::Base redirect_to proc { {:action => "hello_world"} } end + def redirect_with_header_break + redirect_to "/lol\r\nwat" + end + + def redirect_with_null_bytes + redirect_to "\000/lol\r\nwat" + end + def rescue_errors(e) raise e end protected @@ -120,6 +128,18 @@ class RedirectTest < ActionController::TestCase assert_equal "http://test.host/redirect/hello_world", redirect_to_url end + def test_redirect_with_header_break + get :redirect_with_header_break + assert_response :redirect + assert_equal "http://test.host/lolwat", redirect_to_url + end + + def test_redirect_with_null_bytes + get :redirect_with_null_bytes + assert_response :redirect + assert_equal "http://test.host/lolwat", redirect_to_url + end + def test_redirect_with_no_status get :simple_redirect assert_response 302 diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index e040878b26..fce13d096c 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -553,12 +553,33 @@ class TestController < ActionController::Base render :partial => 'partial' end + def partial_html_erb + render :partial => 'partial_html_erb' + end + def render_to_string_with_partial @partial_only = render_to_string :partial => "partial_only" @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") } render :template => "test/hello_world" end + def render_to_string_with_template_and_html_partial + @text = render_to_string :template => "test/with_partial", :formats => [:text] + @html = render_to_string :template => "test/with_partial", :formats => [:html] + render :template => "test/with_html_partial" + end + + def render_to_string_and_render_with_different_formats + @html = render_to_string :template => "test/with_partial", :formats => [:html] + render :template => "test/with_partial", :formats => [:text] + end + + def render_template_within_a_template_with_other_format + render :template => "test/with_xml_template", + :formats => [:html], + :layout => "with_html_partial" + end + def partial_with_counter render :partial => "counter", :locals => { :counter_counter => 5 } end @@ -1001,6 +1022,7 @@ class RenderTest < ActionController::TestCase def test_accessing_local_assigns_in_inline_template get :accessing_local_assigns_in_inline_template, :local_name => "Local David" assert_equal "Goodbye, Local David", @response.body + assert_equal "text/html", @response.content_type end def test_should_implicitly_render_html_template_from_xhr_request @@ -1246,22 +1268,57 @@ class RenderTest < ActionController::TestCase def test_partial_only get :partial_only assert_equal "only partial", @response.body + assert_equal "text/html", @response.content_type end def test_should_render_html_formatted_partial get :partial - assert_equal 'partial html', @response.body + assert_equal "partial html", @response.body + assert_equal "text/html", @response.content_type + end + + def test_render_html_formatted_partial_even_with_other_mime_time_in_accept + @request.accept = "text/javascript, text/html" + + get :partial_html_erb + + assert_equal "partial.html.erb", @response.body.strip + assert_equal "text/html", @response.content_type end def test_should_render_html_partial_with_formats get :partial_formats_html - assert_equal 'partial html', @response.body + assert_equal "partial html", @response.body + assert_equal "text/html", @response.content_type end def test_render_to_string_partial get :render_to_string_with_partial assert_equal "only partial", assigns(:partial_only) assert_equal "Hello: david", assigns(:partial_with_locals) + assert_equal "text/html", @response.content_type + end + + def test_render_to_string_with_template_and_html_partial + get :render_to_string_with_template_and_html_partial + assert_equal "**only partial**\n", assigns(:text) + assert_equal "<strong>only partial</strong>\n", assigns(:html) + assert_equal "<strong>only html partial</strong>\n", @response.body + assert_equal "text/html", @response.content_type + end + + def test_render_to_string_and_render_with_different_formats + get :render_to_string_and_render_with_different_formats + assert_equal "<strong>only partial</strong>\n", assigns(:html) + assert_equal "**only partial**\n", @response.body + assert_equal "text/plain", @response.content_type + end + + def test_render_template_within_a_template_with_other_format + get :render_template_within_a_template_with_other_format + expected = "only html partial<p>This is grand!</p>" + assert_equal expected, @response.body.strip + assert_equal "text/html", @response.content_type end def test_partial_with_counter diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index 64ed7f667f..066cd523be 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -35,6 +35,24 @@ module RequestForgeryProtectionActions def form_for_without_protection render :inline => "<%= form_for(:some_resource, :authenticity_token => false ) {} %>" end + + def form_for_remote + render :inline => "<%= form_for(:some_resource, :remote => true ) {} %>" + end + + def form_for_remote_with_token + render :inline => "<%= form_for(:some_resource, :remote => true, :authenticity_token => true ) {} %>" + end + + def form_for_with_token + render :inline => "<%= form_for(:some_resource, :authenticity_token => true ) {} %>" + end + + def form_for_remote_with_external_token + render :inline => "<%= form_for(:some_resource, :remote => true, :authenticity_token => 'external_token') {} %>" + end + + def rescue_action(e) raise e end end # sample controllers @@ -43,7 +61,7 @@ class RequestForgeryProtectionController < ActionController::Base protect_from_forgery :only => %w(index meta) end -class RequestForgeryProtectionControllerUsingOldBehaviour < ActionController::Base +class RequestForgeryProtectionControllerUsingException < ActionController::Base include RequestForgeryProtectionActions protect_from_forgery :only => %w(index meta) @@ -98,6 +116,60 @@ module RequestForgeryProtectionTests assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token end + def test_should_render_form_without_token_tag_if_remote + assert_not_blocked do + get :form_for_remote + end + assert_no_match(/authenticity_token/, response.body) + end + + def test_should_render_form_with_token_tag_if_remote_and_embedding_token_is_on + original = ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms + begin + ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = true + assert_not_blocked do + get :form_for_remote + end + assert_match(/authenticity_token/, response.body) + ensure + ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = original + end + end + + def test_should_render_form_with_token_tag_if_remote_and_external_authenticity_token_requested_and_embedding_is_on + original = ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms + begin + ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = true + assert_not_blocked do + get :form_for_remote_with_external_token + end + assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', 'external_token' + ensure + ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = original + end + end + + def test_should_render_form_with_token_tag_if_remote_and_external_authenticity_token_requested + assert_not_blocked do + get :form_for_remote_with_external_token + end + assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', 'external_token' + end + + def test_should_render_form_with_token_tag_if_remote_and_authenticity_token_requested + assert_not_blocked do + get :form_for_remote_with_token + end + assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token + end + + def test_should_render_form_with_token_tag_with_authenticity_token_requested + assert_not_blocked do + get :form_for_with_token + end + assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token + end + def test_should_allow_get assert_not_blocked { get :index } end @@ -215,7 +287,7 @@ class RequestForgeryProtectionControllerTest < ActionController::TestCase end end -class RequestForgeryProtectionControllerUsingOldBehaviourTest < ActionController::TestCase +class RequestForgeryProtectionControllerUsingExceptionTest < ActionController::TestCase include RequestForgeryProtectionTests def assert_blocked assert_raises(ActionController::InvalidAuthenticityToken) do diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 843ae1a813..272a7da8c5 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -59,11 +59,11 @@ end class MockController def self.build(helpers) Class.new do - def url_for(options) + def url_options + options = super options[:protocol] ||= "http" options[:host] ||= "test.host" - - super(options) + options end include helpers @@ -437,6 +437,15 @@ class LegacyRouteSetTests < ActiveSupport::TestCase assert_equal("/", routes.send(:root_path)) end + def test_named_route_root_without_hash + rs.draw do + root "hello#index" + end + routes = setup_for_named_route + assert_equal("http://test.host/", routes.send(:root_url)) + assert_equal("/", routes.send(:root_path)) + end + def test_named_route_with_regexps rs.draw do match 'page/:year/:month/:day/:title' => 'page#show', :as => 'article', diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb index 36884846be..3af17f495c 100644 --- a/actionpack/test/controller/send_file_test.rb +++ b/actionpack/test/controller/send_file_test.rb @@ -23,10 +23,6 @@ class SendFileController < ActionController::Base def data send_data(file_data, options) end - - def multibyte_text_data - send_data("Кирилица\n祝您好運.", options) - end end class SendFileTest < ActionController::TestCase diff --git a/actionpack/test/controller/sweeper_test.rb b/actionpack/test/controller/sweeper_test.rb new file mode 100644 index 0000000000..0561efc62f --- /dev/null +++ b/actionpack/test/controller/sweeper_test.rb @@ -0,0 +1,16 @@ +require 'abstract_unit' + + +class SweeperTest < ActionController::TestCase + + class ::AppSweeper < ActionController::Caching::Sweeper; end + + def test_sweeper_should_not_ignore_unknown_method_calls + sweeper = ActionController::Caching::Sweeper.send(:new) + assert_raise NameError do + sweeper.instance_eval do + some_method_that_doesnt_exist + end + end + end +end diff --git a/actionpack/test/controller/url_for_test.rb b/actionpack/test/controller/url_for_test.rb index 288efbf7c3..aa233d6135 100644 --- a/actionpack/test/controller/url_for_test.rb +++ b/actionpack/test/controller/url_for_test.rb @@ -16,6 +16,10 @@ module AbstractController W.default_url_options[:host] = 'www.basecamphq.com' end + def add_port! + W.default_url_options[:port] = 3000 + end + def add_numeric_host! W.default_url_options[:host] = '127.0.0.1' end @@ -121,6 +125,14 @@ module AbstractController ) end + def test_default_port + add_host! + add_port! + assert_equal('http://www.basecamphq.com:3000/c/a/i', + W.new.url_for(:controller => 'c', :action => 'a', :id => 'i') + ) + end + def test_protocol add_host! assert_equal('https://www.basecamphq.com/c/a/i', diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb index 0372c42920..e3f9faaa64 100644 --- a/actionpack/test/dispatch/mime_type_test.rb +++ b/actionpack/test/dispatch/mime_type_test.rb @@ -75,6 +75,12 @@ class MimeTypeTest < ActiveSupport::TestCase assert_equal expect, Mime::Type.parse(accept) end + test "parse arbitarry media type parameters" do + accept = 'multipart/form-data; boundary="simple boundary"' + expect = [Mime::MULTIPART_FORM] + assert_equal expect, Mime::Type.parse(accept) + end + # Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP) test "parse broken acceptlines" do accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5" diff --git a/actionpack/test/dispatch/routing_assertions_test.rb b/actionpack/test/dispatch/routing_assertions_test.rb index 9f95d82129..e953029456 100644 --- a/actionpack/test/dispatch/routing_assertions_test.rb +++ b/actionpack/test/dispatch/routing_assertions_test.rb @@ -3,6 +3,7 @@ require 'controller/fake_controllers' class SecureArticlesController < ArticlesController; end class BlockArticlesController < ArticlesController; end +class QueryArticlesController < ArticlesController; end class RoutingAssertionsTest < ActionController::TestCase @@ -18,6 +19,10 @@ class RoutingAssertionsTest < ActionController::TestCase scope 'block', :constraints => lambda { |r| r.ssl? } do resources :articles, :controller => 'block_articles' end + + scope 'query', :constraints => lambda { |r| r.params[:use_query] == 'true' } do + resources :articles, :controller => 'query_articles' + end end end @@ -62,6 +67,13 @@ class RoutingAssertionsTest < ActionController::TestCase assert_recognizes({ :controller => 'block_articles', :action => 'index' }, 'https://test.host/block/articles') end + def test_assert_recognizes_with_query_constraint + assert_raise(ActionController::RoutingError) do + assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'false' }, '/query/articles', { :use_query => 'false' }) + end + assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'true' }, '/query/articles', { :use_query => 'true' }) + end + def test_assert_routing assert_routing('/articles', :controller => 'articles', :action => 'index') end diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 700666600b..cc4279d9dd 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -475,6 +475,11 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest get :preview, :on => :member end + resources :profiles, :param => :username do + get :details, :on => :member + resources :messages + end + scope :as => "routes" do get "/c/:id", :as => :collision, :to => "collision#show" get "/collision", :to => "collision#show" @@ -2183,6 +2188,19 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest assert_equal "/posts/1/admin", post_admin_root_path(:post_id => '1') end + def test_custom_param + get '/profiles/bob' + assert_equal 'profiles#show', @response.body + assert_equal 'bob', @request.params[:username] + + get '/profiles/bob/details' + assert_equal 'bob', @request.params[:username] + + get '/profiles/bob/messages/34' + assert_equal 'bob', @request.params[:profile_username] + assert_equal '34', @request.params[:id] + end + private def with_https old_https = https? diff --git a/actionpack/test/dispatch/ssl_test.rb b/actionpack/test/dispatch/ssl_test.rb new file mode 100644 index 0000000000..6f075a9074 --- /dev/null +++ b/actionpack/test/dispatch/ssl_test.rb @@ -0,0 +1,157 @@ +require 'abstract_unit' + +class SSLTest < ActionDispatch::IntegrationTest + def default_app + lambda { |env| + headers = {'Content-Type' => "text/html"} + headers['Set-Cookie'] = "id=1; path=/\ntoken=abc; path=/; secure; HttpOnly" + [200, headers, ["OK"]] + } + end + + def app + @app ||= ActionDispatch::SSL.new(default_app) + end + attr_writer :app + + def test_allows_https_url + get "https://example.org/path?key=value" + assert_response :success + end + + def test_allows_https_proxy_header_url + get "http://example.org/", {}, 'HTTP_X_FORWARDED_PROTO' => "https" + assert_response :success + end + + def test_redirects_http_to_https + get "http://example.org/path?key=value" + assert_response :redirect + assert_equal "https://example.org/path?key=value", + response.headers['Location'] + end + + def test_hsts_header_by_default + get "https://example.org/" + assert_equal "max-age=31536000", + response.headers['Strict-Transport-Security'] + end + + def test_hsts_header + self.app = ActionDispatch::SSL.new(default_app, :hsts => true) + get "https://example.org/" + assert_equal "max-age=31536000", + response.headers['Strict-Transport-Security'] + end + + def test_disable_hsts_header + self.app = ActionDispatch::SSL.new(default_app, :hsts => false) + get "https://example.org/" + refute response.headers['Strict-Transport-Security'] + end + + def test_hsts_expires + self.app = ActionDispatch::SSL.new(default_app, :hsts => { :expires => 500 }) + get "https://example.org/" + assert_equal "max-age=500", + response.headers['Strict-Transport-Security'] + end + + def test_hsts_include_subdomains + self.app = ActionDispatch::SSL.new(default_app, :hsts => { :subdomains => true }) + get "https://example.org/" + assert_equal "max-age=31536000; includeSubDomains", + response.headers['Strict-Transport-Security'] + end + + def test_flag_cookies_as_secure + get "https://example.org/" + assert_equal ["id=1; path=/; secure", "token=abc; path=/; secure; HttpOnly" ], + response.headers['Set-Cookie'].split("\n") + end + + def test_flag_cookies_as_secure_at_end_of_line + self.app = ActionDispatch::SSL.new(lambda { |env| + headers = { + 'Content-Type' => "text/html", + 'Set-Cookie' => "problem=def; path=/; HttpOnly; secure" + } + [200, headers, ["OK"]] + }) + + get "https://example.org/" + assert_equal ["problem=def; path=/; HttpOnly; secure"], + response.headers['Set-Cookie'].split("\n") + end + + def test_flag_cookies_as_secure_with_more_spaces_before + self.app = ActionDispatch::SSL.new(lambda { |env| + headers = { + 'Content-Type' => "text/html", + 'Set-Cookie' => "problem=def; path=/; HttpOnly; secure" + } + [200, headers, ["OK"]] + }) + + get "https://example.org/" + assert_equal ["problem=def; path=/; HttpOnly; secure"], + response.headers['Set-Cookie'].split("\n") + end + + def test_flag_cookies_as_secure_with_more_spaces_after + self.app = ActionDispatch::SSL.new(lambda { |env| + headers = { + 'Content-Type' => "text/html", + 'Set-Cookie' => "problem=def; path=/; secure; HttpOnly" + } + [200, headers, ["OK"]] + }) + + get "https://example.org/" + assert_equal ["problem=def; path=/; secure; HttpOnly"], + response.headers['Set-Cookie'].split("\n") + end + + def test_no_cookies + self.app = ActionDispatch::SSL.new(lambda { |env| + [200, {'Content-Type' => "text/html"}, ["OK"]] + }) + get "https://example.org/" + assert !response.headers['Set-Cookie'] + end + + def test_redirect_to_host + self.app = ActionDispatch::SSL.new(default_app, :host => "ssl.example.org") + get "http://example.org/path?key=value" + assert_equal "https://ssl.example.org/path?key=value", + response.headers['Location'] + end + + def test_redirect_to_port + self.app = ActionDispatch::SSL.new(default_app, :port => 8443) + get "http://example.org/path?key=value" + assert_equal "https://example.org:8443/path?key=value", + response.headers['Location'] + end + + def test_redirect_to_host_and_port + self.app = ActionDispatch::SSL.new(default_app, :host => "ssl.example.org", :port => 8443) + get "http://example.org/path?key=value" + assert_equal "https://ssl.example.org:8443/path?key=value", + response.headers['Location'] + end + + def test_redirect_to_secure_host_when_on_subdomain + self.app = ActionDispatch::SSL.new(default_app, :host => "ssl.example.org") + get "http://ssl.example.org/path?key=value" + assert_equal "https://ssl.example.org/path?key=value", + response.headers['Location'] + end + + def test_redirect_to_secure_subdomain_when_on_deep_subdomain + self.app = ActionDispatch::SSL.new(default_app, :host => "example.co.uk") + get "http://double.rainbow.what.does.it.mean.example.co.uk/path?key=value" + assert_equal "https://example.co.uk/path?key=value", + response.headers['Location'] + end +end diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb index 092ca3e20a..112f470786 100644 --- a/actionpack/test/dispatch/static_test.rb +++ b/actionpack/test/dispatch/static_test.rb @@ -7,6 +7,10 @@ module StaticTests assert_equal "Hello, World!", get("/nofile").body end + def test_handles_urls_with_bad_encoding + assert_equal "Hello, World!", get("/doorkeeper%E3E4").body + end + def test_sets_cache_control response = get("/index.html") assert_html "/index.html", response diff --git a/actionpack/test/dispatch/uploaded_file_test.rb b/actionpack/test/dispatch/uploaded_file_test.rb index 0b95291e18..e69c1fbed4 100644 --- a/actionpack/test/dispatch/uploaded_file_test.rb +++ b/actionpack/test/dispatch/uploaded_file_test.rb @@ -12,7 +12,7 @@ module ActionDispatch uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new) assert_equal 'foo', uf.original_filename end - + def test_filename_should_be_in_utf_8 uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new) assert_equal "UTF-8", uf.original_filename.encoding.to_s @@ -65,6 +65,12 @@ module ActionDispatch end end + def test_delegate_eof_to_tempfile + tf = Class.new { def eof?; true end; } + uf = Http::UploadedFile.new(:tempfile => tf.new) + assert uf.eof? + end + def test_respond_to? tf = Class.new { def read; yield end } uf = Http::UploadedFile.new(:tempfile => tf.new) diff --git a/actionpack/test/fixtures/fun/games/_game.erb b/actionpack/test/fixtures/fun/games/_game.erb index d51b7b3ebc..f0f542ff92 100644 --- a/actionpack/test/fixtures/fun/games/_game.erb +++ b/actionpack/test/fixtures/fun/games/_game.erb @@ -1 +1 @@ -<%= game.name %>
\ No newline at end of file +Fun <%= game.name %> diff --git a/actionpack/test/fixtures/fun/serious/games/_game.erb b/actionpack/test/fixtures/fun/serious/games/_game.erb index d51b7b3ebc..523bc55bd7 100644 --- a/actionpack/test/fixtures/fun/serious/games/_game.erb +++ b/actionpack/test/fixtures/fun/serious/games/_game.erb @@ -1 +1 @@ -<%= game.name %>
\ No newline at end of file +Serious <%= game.name %> diff --git a/actionpack/test/fixtures/games/_game.erb b/actionpack/test/fixtures/games/_game.erb new file mode 100644 index 0000000000..1aeb81fcba --- /dev/null +++ b/actionpack/test/fixtures/games/_game.erb @@ -0,0 +1 @@ +Just <%= game.name %> diff --git a/actionpack/test/fixtures/layouts/with_html_partial.html.erb b/actionpack/test/fixtures/layouts/with_html_partial.html.erb new file mode 100644 index 0000000000..fd2896aeaa --- /dev/null +++ b/actionpack/test/fixtures/layouts/with_html_partial.html.erb @@ -0,0 +1 @@ +<%= render :partial => "partial_only_html" %><%= yield %> diff --git a/actionpack/test/fixtures/reply.rb b/actionpack/test/fixtures/reply.rb index 19cba93673..0d3b0a7c98 100644 --- a/actionpack/test/fixtures/reply.rb +++ b/actionpack/test/fixtures/reply.rb @@ -1,5 +1,5 @@ class Reply < ActiveRecord::Base - scope :base + scope :base, -> { scoped } belongs_to :topic, :include => [:replies] belongs_to :developer diff --git a/actionpack/test/fixtures/sprockets/alternate/stylesheets/style.css b/actionpack/test/fixtures/sprockets/alternate/stylesheets/style.css deleted file mode 100644 index bfb90bfa48..0000000000 --- a/actionpack/test/fixtures/sprockets/alternate/stylesheets/style.css +++ /dev/null @@ -1 +0,0 @@ -/* Different from other style.css */
\ No newline at end of file diff --git a/actionpack/test/fixtures/sprockets/app/fonts/dir/font.ttf b/actionpack/test/fixtures/sprockets/app/fonts/dir/font.ttf deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/fonts/dir/font.ttf +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/fonts/font.ttf b/actionpack/test/fixtures/sprockets/app/fonts/font.ttf deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/fonts/font.ttf +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/images/logo.png b/actionpack/test/fixtures/sprockets/app/images/logo.png Binary files differdeleted file mode 100644 index d5edc04e65..0000000000 --- a/actionpack/test/fixtures/sprockets/app/images/logo.png +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/javascripts/application.js b/actionpack/test/fixtures/sprockets/app/javascripts/application.js deleted file mode 100644 index e611d2b129..0000000000 --- a/actionpack/test/fixtures/sprockets/app/javascripts/application.js +++ /dev/null @@ -1 +0,0 @@ -//= require xmlhr diff --git a/actionpack/test/fixtures/sprockets/app/javascripts/dir/xmlhr.js b/actionpack/test/fixtures/sprockets/app/javascripts/dir/xmlhr.js deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/javascripts/dir/xmlhr.js +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/javascripts/extra.js b/actionpack/test/fixtures/sprockets/app/javascripts/extra.js deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/javascripts/extra.js +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/javascripts/xmlhr.js b/actionpack/test/fixtures/sprockets/app/javascripts/xmlhr.js deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/javascripts/xmlhr.js +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/stylesheets/application.css b/actionpack/test/fixtures/sprockets/app/stylesheets/application.css deleted file mode 100644 index 2365eaa4cd..0000000000 --- a/actionpack/test/fixtures/sprockets/app/stylesheets/application.css +++ /dev/null @@ -1 +0,0 @@ -/*= require style */ diff --git a/actionpack/test/fixtures/sprockets/app/stylesheets/dir/style.css b/actionpack/test/fixtures/sprockets/app/stylesheets/dir/style.css deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/stylesheets/dir/style.css +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/stylesheets/extra.css b/actionpack/test/fixtures/sprockets/app/stylesheets/extra.css deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/stylesheets/extra.css +++ /dev/null diff --git a/actionpack/test/fixtures/sprockets/app/stylesheets/style.css b/actionpack/test/fixtures/sprockets/app/stylesheets/style.css deleted file mode 100644 index e69de29bb2..0000000000 --- a/actionpack/test/fixtures/sprockets/app/stylesheets/style.css +++ /dev/null diff --git a/actionpack/test/fixtures/test/_b_layout_for_partial.html.erb b/actionpack/test/fixtures/test/_b_layout_for_partial.html.erb new file mode 100644 index 0000000000..e918ba8f83 --- /dev/null +++ b/actionpack/test/fixtures/test/_b_layout_for_partial.html.erb @@ -0,0 +1 @@ +<b><%= yield %></b>
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/_partial_html_erb.html.erb b/actionpack/test/fixtures/test/_partial_html_erb.html.erb new file mode 100644 index 0000000000..4b54875782 --- /dev/null +++ b/actionpack/test/fixtures/test/_partial_html_erb.html.erb @@ -0,0 +1 @@ +<%= "partial.html.erb" %> diff --git a/actionpack/test/fixtures/test/_partial_only_html.html b/actionpack/test/fixtures/test/_partial_only_html.html new file mode 100644 index 0000000000..d2d630bd40 --- /dev/null +++ b/actionpack/test/fixtures/test/_partial_only_html.html @@ -0,0 +1 @@ +only html partial
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/with_html_partial.html.erb b/actionpack/test/fixtures/test/with_html_partial.html.erb new file mode 100644 index 0000000000..d84d909d64 --- /dev/null +++ b/actionpack/test/fixtures/test/with_html_partial.html.erb @@ -0,0 +1 @@ +<strong><%= render :partial => "partial_only_html" %></strong> diff --git a/actionpack/test/fixtures/test/with_partial.html.erb b/actionpack/test/fixtures/test/with_partial.html.erb new file mode 100644 index 0000000000..7502364cf5 --- /dev/null +++ b/actionpack/test/fixtures/test/with_partial.html.erb @@ -0,0 +1 @@ +<strong><%= render :partial => "partial_only" %></strong> diff --git a/actionpack/test/fixtures/test/with_partial.text.erb b/actionpack/test/fixtures/test/with_partial.text.erb new file mode 100644 index 0000000000..5f068ebf27 --- /dev/null +++ b/actionpack/test/fixtures/test/with_partial.text.erb @@ -0,0 +1 @@ +**<%= render :partial => "partial_only" %>** diff --git a/actionpack/test/fixtures/test/with_xml_template.html.erb b/actionpack/test/fixtures/test/with_xml_template.html.erb new file mode 100644 index 0000000000..e54a7cd001 --- /dev/null +++ b/actionpack/test/fixtures/test/with_xml_template.html.erb @@ -0,0 +1 @@ +<%= render :template => "test/greeting", :formats => :xml %> diff --git a/actionpack/test/template/active_model_helper_test.rb b/actionpack/test/template/active_model_helper_test.rb index 52be0f1762..24511df444 100644 --- a/actionpack/test/template/active_model_helper_test.rb +++ b/actionpack/test/template/active_model_helper_test.rb @@ -29,28 +29,28 @@ class ActiveModelHelperTest < ActionView::TestCase def test_text_area_with_errors assert_dom_equal( - %(<div class="field_with_errors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div>), + %(<div class="field_with_errors"><textarea id="post_body" name="post[body]">\nBack to the hill and over it again!</textarea></div>), text_area("post", "body") ) end def test_text_field_with_errors assert_dom_equal( - %(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /></div>), + %(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" type="text" value="" /></div>), text_field("post", "author_name") ) end def test_date_select_with_errors assert_dom_equal( - %(<div class="field_with_errors"><select id="post_updated_at_1i" name="post[updated_at(1i)]">\n<option selected="selected" value="2004">2004</option>\n<option value="2005">2005</option>\n</select>\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="15" />\n</div>), + %(<div class="field_with_errors"><select id="post_updated_at_1i" name="post[updated_at(1i)]">\n<option selected="selected" value="2004">2004</option>\n<option value="2005">2005</option>\n</select>\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="1" />\n</div>), date_select("post", "updated_at", :discard_month => true, :discard_day => true, :start_year => 2004, :end_year => 2005) ) end def test_datetime_select_with_errors assert_dom_equal( - %(<div class="field_with_errors"><input id="post_updated_at_1i" name="post[updated_at(1i)]" type="hidden" value="2004" />\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="15" />\n<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n<option selected="selected" value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n</select>\n : <select id="post_updated_at_5i" name="post[updated_at(5i)]">\n<option selected="selected" value="00">00</option>\n</select>\n</div>), + %(<div class="field_with_errors"><input id="post_updated_at_1i" name="post[updated_at(1i)]" type="hidden" value="2004" />\n<input id="post_updated_at_2i" name="post[updated_at(2i)]" type="hidden" value="6" />\n<input id="post_updated_at_3i" name="post[updated_at(3i)]" type="hidden" value="1" />\n<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n<option selected="selected" value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n</select>\n : <select id="post_updated_at_5i" name="post[updated_at(5i)]">\n<option selected="selected" value="00">00</option>\n</select>\n</div>), datetime_select("post", "updated_at", :discard_year => true, :discard_month => true, :discard_day => true, :minute_step => 60) ) end @@ -76,7 +76,7 @@ class ActiveModelHelperTest < ActionView::TestCase end assert_dom_equal( - %(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /> <span class="error">can't be empty</span></div>), + %(<div class="field_with_errors"><input id="post_author_name" name="post[author_name]" type="text" value="" /> <span class="error">can't be empty</span></div>), text_field("post", "author_name") ) ensure diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 3d2eeea503..58ff055fc2 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -38,6 +38,7 @@ class AssetTagHelperTest < ActionView::TestCase @controller = BasicController.new @request = Class.new do + attr_accessor :script_name def protocol() 'http://' end def ssl?() false end def host_with_port() 'localhost' end @@ -647,6 +648,13 @@ class AssetTagHelperTest < ActionView::TestCase assert_equal %(<img alt="Rails" src="#{@controller.config.relative_url_root}/images/rails.png?#{expected_time}" />), image_tag("rails.png") end + # Same as above, but with script_name + def test_timebased_asset_id_with_script_name + @request.script_name = "/collaboration/hieraki" + expected_time = File.mtime(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).to_i.to_s + assert_equal %(<img alt="Rails" src="#{@request.script_name}/images/rails.png?#{expected_time}" />), image_tag("rails.png") + end + def test_should_skip_asset_id_on_complete_url assert_equal %(<img alt="Rails" src="http://www.example.com/rails.png" />), image_tag("http://www.example.com/rails.png") end @@ -897,6 +905,31 @@ class AssetTagHelperTest < ActionView::TestCase FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) end + # Same as above, but with script_name + def test_caching_javascript_include_tag_with_script_name + ENV["RAILS_ASSET_ID"] = "" + @request.script_name = "/collaboration/hieraki" + config.perform_caching = true + + assert_dom_equal( + %(<script src="/collaboration/hieraki/javascripts/all.js" type="text/javascript"></script>), + javascript_include_tag(:all, :cache => true) + ) + + assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) + + assert_dom_equal( + %(<script src="/collaboration/hieraki/javascripts/money.js" type="text/javascript"></script>), + javascript_include_tag(:all, :cache => "money") + ) + + assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) + + ensure + FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) + FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) + end + def test_caching_javascript_include_tag_with_named_paths_and_relative_url_root_when_caching_off ENV["RAILS_ASSET_ID"] = "" @controller.config.relative_url_root = "/collaboration/hieraki" @@ -917,6 +950,27 @@ class AssetTagHelperTest < ActionView::TestCase assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) end + # Same as above, but with script_name + def test_caching_javascript_include_tag_with_named_paths_and_script_name_when_caching_off + ENV["RAILS_ASSET_ID"] = "" + @request.script_name = "/collaboration/hieraki" + config.perform_caching = false + + assert_dom_equal( + %(<script src="/collaboration/hieraki/javascripts/robber.js" type="text/javascript"></script>), + javascript_include_tag('robber', :cache => true) + ) + + assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) + + assert_dom_equal( + %(<script src="/collaboration/hieraki/javascripts/robber.js" type="text/javascript"></script>), + javascript_include_tag('robber', :cache => "money", :recursive => true) + ) + + assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) + end + def test_caching_javascript_include_tag_when_caching_off ENV["RAILS_ASSET_ID"] = "" config.perform_caching = false @@ -1144,6 +1198,33 @@ class AssetTagHelperTest < ActionView::TestCase FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) end + # Same as above, but with script_name + def test_caching_stylesheet_link_tag_with_script_name + ENV["RAILS_ASSET_ID"] = "" + @request.script_name = "/collaboration/hieraki" + config.perform_caching = true + + assert_dom_equal( + %(<link href="/collaboration/hieraki/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />), + stylesheet_link_tag(:all, :cache => true) + ) + + files_to_be_joined = Dir["#{ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR}/[^all]*.css"] + + expected_mtime = files_to_be_joined.map { |p| File.mtime(p) }.max + assert_equal expected_mtime, File.mtime(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) + + assert_dom_equal( + %(<link href="/collaboration/hieraki/stylesheets/money.css" media="screen" rel="stylesheet" type="text/css" />), + stylesheet_link_tag(:all, :cache => "money") + ) + + assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) + ensure + FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) + FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) + end + def test_caching_stylesheet_link_tag_with_named_paths_and_relative_url_root_when_caching_off ENV["RAILS_ASSET_ID"] = "" @@ -1165,6 +1246,27 @@ class AssetTagHelperTest < ActionView::TestCase assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) end + # Same as above, but with script_name + def test_caching_stylesheet_link_tag_with_named_paths_and_script_name_when_caching_off + ENV["RAILS_ASSET_ID"] = "" + @request.script_name = "/collaboration/hieraki" + config.perform_caching = false + + assert_dom_equal( + %(<link href="/collaboration/hieraki/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />), + stylesheet_link_tag('robber', :cache => true) + ) + + assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) + + assert_dom_equal( + %(<link href="/collaboration/hieraki/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />), + stylesheet_link_tag('robber', :cache => "money") + ) + + assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) + end + diff --git a/actionpack/test/template/atom_feed_helper_test.rb b/actionpack/test/template/atom_feed_helper_test.rb index d26aa9aa85..89aae4ac56 100644 --- a/actionpack/test/template/atom_feed_helper_test.rb +++ b/actionpack/test/template/atom_feed_helper_test.rb @@ -45,6 +45,23 @@ class ScrollsController < ActionController::Base end end EOT + FEEDS["entry_type_options"] = <<-EOT + atom_feed(:schema_date => '2008') do |feed| + feed.title("My great blog!") + feed.updated((@scrolls.first.created_at)) + + @scrolls.each do |scroll| + feed.entry(scroll, :type => 'text/xml') do |entry| + entry.title(scroll.title) + entry.content(scroll.body, :type => 'html') + + entry.author do |author| + author.name("DHH") + end + end + end + end + EOT FEEDS["xml_block"] = <<-EOT atom_feed do |feed| feed.title("My great blog!") @@ -306,6 +323,20 @@ class AtomFeedTest < ActionController::TestCase end end + def test_feed_entry_type_option_default_to_text_html + with_restful_routing(:scrolls) do + get :index, :id => 'defaults' + assert_select "entry link[rel=alternate][type=text/html]" + end + end + + def test_feed_entry_type_option_specified + with_restful_routing(:scrolls) do + get :index, :id => 'entry_type_options' + assert_select "entry link[rel=alternate][type=text/xml]" + end + end + private def with_restful_routing(resources) with_routing do |set| diff --git a/actionpack/test/template/compressors_test.rb b/actionpack/test/template/compressors_test.rb deleted file mode 100644 index a273f15bd7..0000000000 --- a/actionpack/test/template/compressors_test.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'abstract_unit' -require 'sprockets/compressors' - -class CompressorsTest < ActiveSupport::TestCase - def test_register_css_compressor - Sprockets::Compressors.register_css_compressor(:null, Sprockets::NullCompressor) - compressor = Sprockets::Compressors.registered_css_compressor(:null) - assert_kind_of Sprockets::NullCompressor, compressor - end - - def test_register_js_compressor - Sprockets::Compressors.register_js_compressor(:uglifier, 'Uglifier', :require => 'uglifier') - compressor = Sprockets::Compressors.registered_js_compressor(:uglifier) - assert_kind_of Uglifier, compressor - end - - def test_register_default_css_compressor - Sprockets::Compressors.register_css_compressor(:null, Sprockets::NullCompressor, :default => true) - compressor = Sprockets::Compressors.registered_css_compressor(:default) - assert_kind_of Sprockets::NullCompressor, compressor - end - - def test_register_default_js_compressor - Sprockets::Compressors.register_js_compressor(:null, Sprockets::NullCompressor, :default => true) - compressor = Sprockets::Compressors.registered_js_compressor(:default) - assert_kind_of Sprockets::NullCompressor, compressor - end -end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 9e2f4ec347..c9b8a5bb70 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -711,7 +711,7 @@ class DateHelperTest < ActionView::TestCase # Since the order is incomplete nothing will be shown expected = %(<input id="date_first_year" name="date[first][year]" type="hidden" value="2003" />\n) expected << %(<input id="date_first_month" name="date[first][month]" type="hidden" value="8" />\n) - expected << %(<input id="date_first_day" name="date[first][day]" type="hidden" value="16" />\n) + expected << %(<input id="date_first_day" name="date[first][day]" type="hidden" value="1" />\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :order => [:day]) end @@ -943,7 +943,7 @@ class DateHelperTest < ActionView::TestCase expected << "</select>\n" expected << %(<input type="hidden" id="date_first_month" name="date[first][month]" value="8" />\n) - expected << %(<input type="hidden" id="date_first_day" name="date[first][day]" value="16" />\n) + expected << %(<input type="hidden" id="date_first_day" name="date[first][day]" value="1" />\n) assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { :date_separator => " / ", :discard_month => true, :discard_day => true, :start_year => 2003, :end_year => 2005, :prefix => "date[first]"}) end @@ -1396,6 +1396,20 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, date_select("post", "written_on", :order => [ :month, :year ]) end + def test_date_select_without_day_and_month + @post = Post.new + @post.written_on = Date.new(2004, 2, 29) + + expected = "<input type=\"hidden\" id=\"post_written_on_2i\" name=\"post[written_on(2i)]\" value=\"2\" />\n" + expected << "<input type=\"hidden\" id=\"post_written_on_3i\" name=\"post[written_on(3i)]\" value=\"1\" />\n" + + expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n} + expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n} + expected << "</select>\n" + + assert_dom_equal expected, date_select("post", "written_on", :order => [ :year ]) + end + def test_date_select_without_day_with_separator @post = Post.new @post.written_on = Date.new(2004, 6, 15) @@ -2118,6 +2132,18 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, datetime_select("post", "updated_at", { :date_separator => " / ", :datetime_separator => " , ", :time_separator => " - ", :include_seconds => true }) end + def test_datetime_select_with_integer + @post = Post.new + @post.updated_at = 3 + datetime_select("post", "updated_at") + end + + def test_datetime_select_with_infinity # Float + @post = Post.new + @post.updated_at = (-1.0/0) + datetime_select("post", "updated_at") + end + def test_datetime_select_with_default_prompt @post = Post.new @post.updated_at = nil @@ -2427,7 +2453,7 @@ class DateHelperTest < ActionView::TestCase 1999.upto(2009) { |i| expected << %(<option value="#{i}"#{' selected="selected"' if i == 2004}>#{i}</option>\n) } expected << "</select>\n" expected << %{<input type="hidden" id="post_updated_at_2i" name="post[updated_at(2i)]" value="6" />\n} - expected << %{<input type="hidden" id="post_updated_at_3i" name="post[updated_at(3i)]" value="15" />\n} + expected << %{<input type="hidden" id="post_updated_at_3i" name="post[updated_at(3i)]" value="1" />\n} expected << " — " @@ -2448,7 +2474,7 @@ class DateHelperTest < ActionView::TestCase expected = %{<input type="hidden" id="post_updated_at_1i" name="post[updated_at(1i)]" value="2004" />\n} expected << %{<input type="hidden" id="post_updated_at_2i" name="post[updated_at(2i)]" value="6" />\n} - expected << %{<input type="hidden" id="post_updated_at_3i" name="post[updated_at(3i)]" value="15" />\n} + expected << %{<input type="hidden" id="post_updated_at_3i" name="post[updated_at(3i)]" value="1" />\n} expected << %{<select id="post_updated_at_4i" name="post[updated_at(4i)]">\n} 0.upto(23) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 15}>#{sprintf("%02d", i)}</option>\n) } @@ -2467,7 +2493,7 @@ class DateHelperTest < ActionView::TestCase expected = %{<input type="hidden" id="post_updated_at_1i" disabled="disabled" name="post[updated_at(1i)]" value="2004" />\n} expected << %{<input type="hidden" id="post_updated_at_2i" disabled="disabled" name="post[updated_at(2i)]" value="6" />\n} - expected << %{<input type="hidden" id="post_updated_at_3i" disabled="disabled" name="post[updated_at(3i)]" value="15" />\n} + expected << %{<input type="hidden" id="post_updated_at_3i" disabled="disabled" name="post[updated_at(3i)]" value="1" />\n} expected << %{<select id="post_updated_at_4i" disabled="disabled" name="post[updated_at(4i)]">\n} 0.upto(23) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 15}>#{sprintf("%02d", i)}</option>\n) } @@ -2865,6 +2891,10 @@ class DateHelperTest < ActionView::TestCase assert_match(/<time.*>Right now<\/time>/, time_tag(Time.now, 'Right now')) end + def test_time_tag_with_given_block + assert_match(/<time.*><span>Right now<\/span><\/time>/, time_tag(Time.now){ '<span>Right now</span>'.html_safe }) + end + def test_time_tag_with_different_format time = Time.now expected = "<time datetime=\"#{time.xmlschema}\">#{I18n.l(time, :format => :short)}</time>" diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 3546bd3bee..f13296a175 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -249,35 +249,35 @@ class FormHelperTest < ActionView::TestCase end def test_label_with_block_in_erb - assert_equal "<label for=\"post_message\">\n Message\n <input id=\"post_message\" name=\"post[message]\" size=\"30\" type=\"text\" />\n</label>", view.render("test/label_with_block") + assert_equal "<label for=\"post_message\">\n Message\n <input id=\"post_message\" name=\"post[message]\" type=\"text\" />\n</label>", view.render("test/label_with_block") end def test_text_field assert_dom_equal( - '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title") + '<input id="post_title" name="post[title]" type="text" value="Hello World" />', text_field("post", "title") ) assert_dom_equal( - '<input id="post_title" name="post[title]" size="30" type="password" />', password_field("post", "title") + '<input id="post_title" name="post[title]" type="password" />', password_field("post", "title") ) assert_dom_equal( - '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title", :value => @post.title) + '<input id="post_title" name="post[title]" type="password" value="Hello World" />', password_field("post", "title", :value => @post.title) ) assert_dom_equal( - '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name") + '<input id="person_name" name="person[name]" type="password" />', password_field("person", "name") ) end def test_text_field_with_escapes @post.title = "<b>Hello World</b>" assert_dom_equal( - '<input id="post_title" name="post[title]" size="30" type="text" value="<b>Hello World</b>" />', text_field("post", "title") + '<input id="post_title" name="post[title]" type="text" value="<b>Hello World</b>" />', text_field("post", "title") ) end def test_text_field_with_html_entities @post.title = "The HTML Entity for & is &" assert_dom_equal( - '<input id="post_title" name="post[title]" size="30" type="text" value="The HTML Entity for & is &amp;" />', + '<input id="post_title" name="post[title]" type="text" value="The HTML Entity for & is &amp;" />', text_field("post", "title") ) end @@ -301,18 +301,18 @@ class FormHelperTest < ActionView::TestCase end def test_text_field_with_nil_value - expected = '<input id="post_title" name="post[title]" size="30" type="text" />' + expected = '<input id="post_title" name="post[title]" type="text" />' assert_dom_equal expected, text_field("post", "title", :value => nil) end def test_text_field_with_nil_name - expected = '<input id="post_title" size="30" type="text" value="Hello World" />' + expected = '<input id="post_title" type="text" value="Hello World" />' assert_dom_equal expected, text_field("post", "title", :name => nil) end def test_text_field_doesnt_change_param_values object_name = 'post[]' - expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />' + expected = '<input id="post_123_title" name="post[123][title]" type="text" value="Hello World" />' assert_equal expected, text_field(object_name, "title") assert_equal object_name, "post[]" end @@ -346,7 +346,7 @@ class FormHelperTest < ActionView::TestCase end def test_text_field_with_custom_type - assert_dom_equal '<input id="user_email" size="30" name="user[email]" type="email" />', + assert_dom_equal '<input id="user_email" name="user[email]" type="email" />', text_field("user", "email", :type => "email") end @@ -387,6 +387,11 @@ class FormHelperTest < ActionView::TestCase ) end + def test_check_box_with_include_hidden_false + @post.secret = false + assert_dom_equal('<input id="post_secret" name="post[secret]" type="checkbox" value="1" />', check_box("post", "secret", :include_hidden => false)) + end + def test_check_box_with_explicit_checked_and_unchecked_values @post.secret = "on" assert_dom_equal( @@ -474,7 +479,7 @@ class FormHelperTest < ActionView::TestCase def test_text_area assert_dom_equal( - '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>', + %{<textarea id="post_body" name="post[body]">\nBack to the hill and over it again!</textarea>}, text_area("post", "body") ) end @@ -482,14 +487,14 @@ class FormHelperTest < ActionView::TestCase def test_text_area_with_escapes @post.body = "Back to <i>the</i> hill and over it again!" assert_dom_equal( - '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to <i>the</i> hill and over it again!</textarea>', + %{<textarea id="post_body" name="post[body]">\nBack to <i>the</i> hill and over it again!</textarea>}, text_area("post", "body") ) end def test_text_area_with_alternate_value assert_dom_equal( - '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>', + %{<textarea id="post_body" name="post[body]">\nTesting alternate values.</textarea>}, text_area("post", "body", :value => 'Testing alternate values.') ) end @@ -497,25 +502,25 @@ class FormHelperTest < ActionView::TestCase def test_text_area_with_html_entities @post.body = "The HTML Entity for & is &" assert_dom_equal( - '<textarea cols="40" id="post_body" name="post[body]" rows="20">The HTML Entity for & is &amp;</textarea>', + %{<textarea id="post_body" name="post[body]">\nThe HTML Entity for & is &amp;</textarea>}, text_area("post", "body") ) end def test_text_area_with_size_option assert_dom_equal( - '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>', + %{<textarea cols="183" id="post_body" name="post[body]" rows="820">\nBack to the hill and over it again!</textarea>}, text_area("post", "body", :size => "183x820") ) end def test_search_field - expected = %{<input id="contact_notes_query" size="30" name="contact[notes_query]" type="search" />} + expected = %{<input id="contact_notes_query" name="contact[notes_query]" type="search" />} assert_dom_equal(expected, search_field("contact", "notes_query")) end def test_telephone_field - expected = %{<input id="user_cell" size="30" name="user[cell]" type="tel" />} + expected = %{<input id="user_cell" name="user[cell]" type="tel" />} assert_dom_equal(expected, telephone_field("user", "cell")) end @@ -546,12 +551,12 @@ class FormHelperTest < ActionView::TestCase end def test_url_field - expected = %{<input id="user_homepage" size="30" name="user[homepage]" type="url" />} + expected = %{<input id="user_homepage" name="user[homepage]" type="url" />} assert_dom_equal(expected, url_field("user", "homepage")) end def test_email_field - expected = %{<input id="user_address" size="30" name="user[address]" type="email" />} + expected = %{<input id="user_address" name="user[address]" type="email" />} assert_dom_equal(expected, email_field("user", "address")) end @@ -571,10 +576,10 @@ class FormHelperTest < ActionView::TestCase def test_explicit_name assert_dom_equal( - '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess") + '<input id="post_title" name="dont guess" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess") ) assert_dom_equal( - '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>', + %{<textarea id="post_body" name="really!">\nBack to the hill and over it again!</textarea>}, text_area("post", "body", "name" => "really!") ) assert_dom_equal( @@ -591,10 +596,10 @@ class FormHelperTest < ActionView::TestCase def test_explicit_id assert_dom_equal( - '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess") + '<input id="dont guess" name="post[title]" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess") ) assert_dom_equal( - '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>', + %{<textarea id="really!" name="post[body]">\nBack to the hill and over it again!</textarea>}, text_area("post", "body", "id" => "really!") ) assert_dom_equal( @@ -611,10 +616,10 @@ class FormHelperTest < ActionView::TestCase def test_nil_id assert_dom_equal( - '<input name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => nil) + '<input name="post[title]" type="text" value="Hello World" />', text_field("post", "title", "id" => nil) ) assert_dom_equal( - '<textarea cols="40" name="post[body]" rows="20">Back to the hill and over it again!</textarea>', + %{<textarea name="post[body]">\nBack to the hill and over it again!</textarea>}, text_area("post", "body", "id" => nil) ) assert_dom_equal( @@ -641,11 +646,11 @@ class FormHelperTest < ActionView::TestCase def test_index assert_dom_equal( - '<input name="post[5][title]" size="30" id="post_5_title" type="text" value="Hello World" />', + '<input name="post[5][title]" id="post_5_title" type="text" value="Hello World" />', text_field("post", "title", "index" => 5) ) assert_dom_equal( - '<textarea cols="40" name="post[5][body]" id="post_5_body" rows="20">Back to the hill and over it again!</textarea>', + %{<textarea name="post[5][body]" id="post_5_body">\nBack to the hill and over it again!</textarea>}, text_area("post", "body", "index" => 5) ) assert_dom_equal( @@ -668,11 +673,11 @@ class FormHelperTest < ActionView::TestCase def test_index_with_nil_id assert_dom_equal( - '<input name="post[5][title]" size="30" type="text" value="Hello World" />', + '<input name="post[5][title]" type="text" value="Hello World" />', text_field("post", "title", "index" => 5, 'id' => nil) ) assert_dom_equal( - '<textarea cols="40" name="post[5][body]" rows="20">Back to the hill and over it again!</textarea>', + %{<textarea name="post[5][body]">\nBack to the hill and over it again!</textarea>}, text_area("post", "body", "index" => 5, 'id' => nil) ) assert_dom_equal( @@ -700,10 +705,10 @@ class FormHelperTest < ActionView::TestCase label("post[]", "title") ) assert_dom_equal( - "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title") + "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title") ) assert_dom_equal( - "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>", + "<textarea id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\">\nBack to the hill and over it again!</textarea>", text_area("post[]", "body") ) assert_dom_equal( @@ -711,7 +716,7 @@ class FormHelperTest < ActionView::TestCase check_box("post[]", "secret") ) assert_dom_equal( -"<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />", + "<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />", radio_button("post[]", "title", "Hello World") ) assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />", @@ -722,11 +727,11 @@ class FormHelperTest < ActionView::TestCase def test_auto_index_with_nil_id pid = 123 assert_dom_equal( - "<input name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", + "<input name=\"post[#{pid}][title]\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title", :id => nil) ) assert_dom_equal( - "<textarea cols=\"40\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>", + "<textarea name=\"post[#{pid}][body]\">\nBack to the hill and over it again!</textarea>", text_area("post[]", "body", :id => nil) ) assert_dom_equal( @@ -760,8 +765,8 @@ class FormHelperTest < ActionView::TestCase expected = whole_form("/posts/123", "create-post" , "edit_post", :method => 'patch') do "<label for='post_title'>The Title</label>" + - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + "<input name='commit' type='submit' value='Create post' />" + @@ -771,6 +776,44 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_with_collection_radio_buttons + post = Post.new + def post.active; false; end + form_for(post) do |f| + concat f.collection_radio_buttons(:active, [true, false], :to_s, :to_s) + end + + expected = whole_form("/posts", "new_post" , "new_post") do + "<input id='post_active_true' name='post[active]' type='radio' value='true' />" + + "<label for='post_active_true'>true</label>" + + "<input checked='checked' id='post_active_false' name='post[active]' type='radio' value='false' />" + + "<label for='post_active_false'>false</label>" + end + + assert_dom_equal expected, output_buffer + end + + def test_form_for_with_collection_check_boxes + post = Post.new + def post.tag_ids; [1, 3]; end + collection = (1..3).map{|i| [i, "Tag #{i}"] } + form_for(post) do |f| + concat f.collection_check_boxes(:tag_ids, collection, :first, :last) + end + + expected = whole_form("/posts", "new_post" , "new_post") do + "<input checked='checked' id='post_tag_ids_1' name='post[tag_ids][]' type='checkbox' value='1' />" + + "<label for='post_tag_ids_1'>Tag 1</label>" + + "<input id='post_tag_ids_2' name='post[tag_ids][]' type='checkbox' value='2' />" + + "<label for='post_tag_ids_2'>Tag 2</label>" + + "<input checked='checked' id='post_tag_ids_3' name='post[tag_ids][]' type='checkbox' value='3' />" + + "<label for='post_tag_ids_3'>Tag 3</label>" + + "<input name='post[tag_ids][]' type='hidden' value='' />" + end + + assert_dom_equal expected, output_buffer + end + def test_form_for_with_file_field_generate_multipart Post.send :attr_accessor, :file @@ -821,7 +864,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/posts/44", "edit_post_44" , "edit_post", :method => 'patch') do - "<input name='post[title]' size='30' type='text' id='post_title' value='And his name will be forty and four.' />" + + "<input name='post[title]' type='text' id='post_title' value='And his name will be forty and four.' />" + "<input name='commit' type='submit' value='Edit post' />" end @@ -839,8 +882,8 @@ class FormHelperTest < ActionView::TestCase expected = whole_form("/posts/123", "create-post", "edit_other_name", :method => 'patch') do "<label for='other_name_title' class='post_title'>Title</label>" + - "<input name='other_name[title]' size='30' id='other_name_title' value='Hello World' type='text' />" + - "<textarea name='other_name[body]' id='other_name_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='other_name[title]' id='other_name_title' value='Hello World' type='text' />" + + "<textarea name='other_name[body]' id='other_name_body'>\nBack to the hill and over it again!</textarea>" + "<input name='other_name[secret]' value='0' type='hidden' />" + "<input name='other_name[secret]' checked='checked' id='other_name_secret' value='1' type='checkbox' />" + "<input name='commit' value='Create post' type='submit' />" @@ -857,8 +900,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/", "create-post", "edit_post", "delete") do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" end @@ -874,8 +917,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/", "create-post", "edit_post", "delete") do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" end @@ -891,7 +934,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/search", "search-post", "new_post", "get") do - "<input name='post[title]' size='30' type='search' id='post_title' />" + "<input name='post[title]' type='search' id='post_title' />" end assert_dom_equal expected, output_buffer @@ -905,8 +948,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/", "create-post", "edit_post", :method => 'patch', :remote => true) do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" end @@ -922,8 +965,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/", "create-post", "edit_post", :method => 'patch', :remote => true) do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" end @@ -941,8 +984,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/posts", 'new_post', 'new_post', :remote => true) do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" end @@ -958,8 +1001,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form("/", "create-post") do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" end @@ -977,8 +1020,8 @@ class FormHelperTest < ActionView::TestCase expected = whole_form('/posts/123', 'edit_post[]', 'edit_post[]', 'patch') do "<label for='post_123_title'>Title</label>" + - "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" + - "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[123][title]' type='text' id='post_123_title' value='Hello World' />" + + "<textarea name='post[123][body]' id='post_123_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[123][secret]' type='hidden' value='0' />" + "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" end @@ -994,8 +1037,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post[]', 'edit_post[]', 'patch') do - "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" + - "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[][title]' type='text' id='post__title' value='Hello World' />" + + "<textarea name='post[][body]' id='post__body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[][secret]' type='hidden' value='0' />" + "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" end @@ -1003,6 +1046,54 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_label_error_wrapping + form_for(@post) do |f| + concat f.label(:author_name, :class => 'label') + concat f.text_field(:author_name) + concat f.submit('Create post') + end + + expected = whole_form('/posts/123', 'edit_post_123' , 'edit_post', 'patch') do + "<div class='field_with_errors'><label for='post_author_name' class='label'>Author name</label></div>" + + "<div class='field_with_errors'><input name='post[author_name]' type='text' id='post_author_name' value='' /></div>" + + "<input name='commit' type='submit' value='Create post' />" + end + + assert_dom_equal expected, output_buffer + end + + def test_form_for_label_error_wrapping_without_conventional_instance_variable + post = remove_instance_variable :@post + + form_for(post) do |f| + concat f.label(:author_name, :class => 'label') + concat f.text_field(:author_name) + concat f.submit('Create post') + end + + expected = whole_form('/posts/123', 'edit_post_123' , 'edit_post', 'patch') do + "<div class='field_with_errors'><label for='post_author_name' class='label'>Author name</label></div>" + + "<div class='field_with_errors'><input name='post[author_name]' type='text' id='post_author_name' value='' /></div>" + + "<input name='commit' type='submit' value='Create post' />" + end + + assert_dom_equal expected, output_buffer + end + + def test_form_for_label_error_wrapping_block_and_non_block_versions + form_for(@post) do |f| + concat f.label(:author_name, 'Name', :class => 'label') + concat f.label(:author_name, :class => 'label') { 'Name' } + end + + expected = whole_form('/posts/123', 'edit_post_123' , 'edit_post', 'patch') do + "<div class='field_with_errors'><label for='post_author_name' class='label'>Name</label></div>" + + "<div class='field_with_errors'><label for='post_author_name' class='label'>Name</label></div>" + end + + assert_dom_equal expected, output_buffer + end + def test_form_for_with_namespace form_for(@post, :namespace => 'namespace') do |f| concat f.text_field(:title) @@ -1011,8 +1102,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'namespace_edit_post_123', 'edit_post', 'patch') do - "<input name='post[title]' size='30' type='text' id='namespace_post_title' value='Hello World' />" + - "<textarea name='post[body]' id='namespace_post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='namespace_post_title' value='Hello World' />" + + "<textarea name='post[body]' id='namespace_post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='namespace_post_secret' value='1' />" end @@ -1020,6 +1111,14 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_with_namespace_with_date_select + form_for(@post, :namespace => 'namespace') do |f| + concat f.date_select(:written_on) + end + + assert_select 'select#namespace_post_written_on_1i' + end + def test_form_for_with_namespace_with_label form_for(@post, :namespace => 'namespace') do |f| concat f.label(:title) @@ -1028,7 +1127,7 @@ class FormHelperTest < ActionView::TestCase expected = whole_form('/posts/123', 'namespace_edit_post_123', 'edit_post', 'patch') do "<label for='namespace_post_title'>Title</label>" + - "<input name='post[title]' size='30' type='text' id='namespace_post_title' value='Hello World' />" + "<input name='post[title]' type='text' id='namespace_post_title' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1042,7 +1141,7 @@ class FormHelperTest < ActionView::TestCase expected_1 = whole_form('/posts/123', 'namespace_1_edit_post_123', 'edit_post', 'patch') do "<label for='namespace_1_post_title'>Title</label>" + - "<input name='post[title]' size='30' type='text' id='namespace_1_post_title' value='Hello World' />" + "<input name='post[title]' type='text' id='namespace_1_post_title' value='Hello World' />" end assert_dom_equal expected_1, output_buffer @@ -1054,7 +1153,7 @@ class FormHelperTest < ActionView::TestCase expected_2 = whole_form('/posts/123', 'namespace_2_edit_post_123', 'edit_post', 'patch') do "<label for='namespace_2_post_title'>Title</label>" + - "<input name='post[title]' size='30' type='text' id='namespace_2_post_title' value='Hello World' />" + "<input name='post[title]' type='text' id='namespace_2_post_title' value='Hello World' />" end assert_dom_equal expected_2, output_buffer @@ -1071,9 +1170,9 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'namespace_edit_post_123', 'edit_post', 'patch') do - "<input name='post[title]' size='30' type='text' id='namespace_post_title' value='Hello World' />" + - "<textarea name='post[body]' id='namespace_post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + - "<input name='post[comment][body]' size='30' type='text' id='namespace_post_comment_body' value='Hello World' />" + "<input name='post[title]' type='text' id='namespace_post_title' value='Hello World' />" + + "<textarea name='post[body]' id='namespace_post_body'>\nBack to the hill and over it again!</textarea>" + + "<input name='post[comment][body]' type='text' id='namespace_post_comment_body' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1154,7 +1253,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - "<input name='post[comment][body]' size='30' type='text' id='post_comment_body' value='Hello World' />" + "<input name='post[comment][body]' type='text' id='post_comment_body' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1169,8 +1268,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post[]', 'edit_post[]', 'patch') do - "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" + - "<input name='post[123][comment][][name]' size='30' type='text' id='post_123_comment__name' value='new comment' />" + "<input name='post[123][title]' type='text' id='post_123_title' value='Hello World' />" + + "<input name='post[123][comment][][name]' type='text' id='post_123_comment__name' value='new comment' />" end assert_dom_equal expected, output_buffer @@ -1185,8 +1284,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', 'patch') do - "<input name='post[1][title]' size='30' type='text' id='post_1_title' value='Hello World' />" + - "<input name='post[1][comment][1][name]' size='30' type='text' id='post_1_comment_1_name' value='new comment' />" + "<input name='post[1][title]' type='text' id='post_1_title' value='Hello World' />" + + "<input name='post[1][comment][1][name]' type='text' id='post_1_comment_1_name' value='new comment' />" end assert_dom_equal expected, output_buffer @@ -1200,7 +1299,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', 'patch') do - "<input name='post[1][comment][title]' size='30' type='text' id='post_1_comment_title' value='Hello World' />" + "<input name='post[1][comment][title]' type='text' id='post_1_comment_title' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1214,7 +1313,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', 'patch') do - "<input name='post[1][comment][5][title]' size='30' type='text' id='post_1_comment_5_title' value='Hello World' />" + "<input name='post[1][comment][5][title]' type='text' id='post_1_comment_5_title' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1228,7 +1327,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post[]', 'edit_post[]', 'patch') do - "<input name='post[123][comment][title]' size='30' type='text' id='post_123_comment_title' value='Hello World' />" + "<input name='post[123][comment][title]' type='text' id='post_123_comment_title' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1256,7 +1355,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post[]', 'edit_post[]', 'patch') do - "<input name='post[123][comment][123][title]' size='30' type='text' id='post_123_comment_123_title' value='Hello World' />" + "<input name='post[123][comment][123][title]' type='text' id='post_123_comment_123_title' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1276,9 +1375,9 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post[]', 'edit_post[]', 'patch') do - "<input name='post[123][comment][5][title]' size='30' type='text' id='post_123_comment_5_title' value='Hello World' />" + "<input name='post[123][comment][5][title]' type='text' id='post_123_comment_5_title' value='Hello World' />" end + whole_form('/posts/123', 'edit_post', 'edit_post', 'patch') do - "<input name='post[1][comment][123][title]' size='30' type='text' id='post_1_comment_123_title' value='Hello World' />" + "<input name='post[1][comment][123][title]' type='text' id='post_1_comment_123_title' value='Hello World' />" end assert_dom_equal expected, output_buffer @@ -1295,8 +1394,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="new author" />' + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="new author" />' end assert_dom_equal expected, output_buffer @@ -1322,8 +1421,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' + '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' end @@ -1341,8 +1440,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' + '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' end @@ -1360,8 +1459,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' end assert_dom_equal expected, output_buffer @@ -1378,8 +1477,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' end assert_dom_equal expected, output_buffer @@ -1396,8 +1495,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' + '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' end @@ -1416,9 +1515,9 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' end assert_dom_equal expected, output_buffer @@ -1437,10 +1536,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' + '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' end @@ -1464,11 +1563,11 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' + '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' end assert_dom_equal expected, output_buffer @@ -1491,10 +1590,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' end assert_dom_equal expected, output_buffer @@ -1517,11 +1616,11 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="author #321" />' + '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' end assert_dom_equal expected, output_buffer @@ -1540,10 +1639,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' + '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' end @@ -1564,11 +1663,11 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' end assert_dom_equal expected, output_buffer @@ -1587,9 +1686,9 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="new comment" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="new comment" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="new comment" />' end assert_dom_equal expected, output_buffer @@ -1608,10 +1707,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #321" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="new comment" />' end assert_dom_equal expected, output_buffer @@ -1626,7 +1725,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' end assert_dom_equal expected, output_buffer @@ -1643,10 +1742,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' + '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' end @@ -1664,10 +1763,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' + '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' end @@ -1686,10 +1785,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #1" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' + + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="comment #2" />' + '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' end @@ -1709,10 +1808,10 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' + - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' + + '<input name="post[title]" type="text" id="post_title" value="Hello World" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #321" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' + - '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' + '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" type="text" value="new comment" />' end assert_dom_equal expected, output_buffer @@ -1729,7 +1828,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" size="30" type="text" value="comment #321" />' + + '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" type="text" value="comment #321" />' + '<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' end @@ -1765,16 +1864,16 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' + - '<input id="post_comments_attributes_0_relevances_attributes_0_value" name="post[comments_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="commentrelevance #314" />' + + '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" type="text" value="comment #321" />' + + '<input id="post_comments_attributes_0_relevances_attributes_0_value" name="post[comments_attributes][0][relevances_attributes][0][value]" type="text" value="commentrelevance #314" />' + '<input id="post_comments_attributes_0_relevances_attributes_0_id" name="post[comments_attributes][0][relevances_attributes][0][id]" type="hidden" value="314" />' + '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' + - '<input id="post_tags_attributes_0_value" name="post[tags_attributes][0][value]" size="30" type="text" value="tag #123" />' + - '<input id="post_tags_attributes_0_relevances_attributes_0_value" name="post[tags_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #3141" />' + + '<input id="post_tags_attributes_0_value" name="post[tags_attributes][0][value]" type="text" value="tag #123" />' + + '<input id="post_tags_attributes_0_relevances_attributes_0_value" name="post[tags_attributes][0][relevances_attributes][0][value]" type="text" value="tagrelevance #3141" />' + '<input id="post_tags_attributes_0_relevances_attributes_0_id" name="post[tags_attributes][0][relevances_attributes][0][id]" type="hidden" value="3141" />' + '<input id="post_tags_attributes_0_id" name="post[tags_attributes][0][id]" type="hidden" value="123" />' + - '<input id="post_tags_attributes_1_value" name="post[tags_attributes][1][value]" size="30" type="text" value="tag #456" />' + - '<input id="post_tags_attributes_1_relevances_attributes_0_value" name="post[tags_attributes][1][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #31415" />' + + '<input id="post_tags_attributes_1_value" name="post[tags_attributes][1][value]" type="text" value="tag #456" />' + + '<input id="post_tags_attributes_1_relevances_attributes_0_value" name="post[tags_attributes][1][relevances_attributes][0][value]" type="text" value="tagrelevance #31415" />' + '<input id="post_tags_attributes_1_relevances_attributes_0_id" name="post[tags_attributes][1][relevances_attributes][0][id]" type="hidden" value="31415" />' + '<input id="post_tags_attributes_1_id" name="post[tags_attributes][1][id]" type="hidden" value="456" />' end @@ -1792,7 +1891,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="hash backed author" />' + '<input id="post_author_attributes_name" name="post[author_attributes][name]" type="text" value="hash backed author" />' end assert_dom_equal expected, output_buffer @@ -1806,8 +1905,8 @@ class FormHelperTest < ActionView::TestCase end expected = - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" @@ -1822,8 +1921,8 @@ class FormHelperTest < ActionView::TestCase end expected = - "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" + - "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[123][title]' type='text' id='post_123_title' value='Hello World' />" + + "<textarea name='post[123][body]' id='post_123_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[123][secret]' type='hidden' value='0' />" + "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" @@ -1838,8 +1937,8 @@ class FormHelperTest < ActionView::TestCase end expected = - "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" + - "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[][title]' type='text' id='post__title' value='Hello World' />" + + "<textarea name='post[][body]' id='post__body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[][secret]' type='hidden' value='0' />" + "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" @@ -1854,8 +1953,8 @@ class FormHelperTest < ActionView::TestCase end expected = - "<input name='post[abc][title]' size='30' type='text' id='post_abc_title' value='Hello World' />" + - "<textarea name='post[abc][body]' id='post_abc_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[abc][title]' type='text' id='post_abc_title' value='Hello World' />" + + "<textarea name='post[abc][body]' id='post_abc_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[abc][secret]' type='hidden' value='0' />" + "<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />" @@ -1870,8 +1969,8 @@ class FormHelperTest < ActionView::TestCase end expected = - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" @@ -1886,8 +1985,8 @@ class FormHelperTest < ActionView::TestCase end expected = - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='post[secret]' type='hidden' value='0' />" + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" @@ -1901,7 +2000,7 @@ class FormHelperTest < ActionView::TestCase end assert_dom_equal "<label for=\"author_post_title\">Title</label>" + - "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />", + "<input name='author[post][title]' type='text' id='author_post_title' value='Hello World' />", output_buffer end @@ -1912,7 +2011,7 @@ class FormHelperTest < ActionView::TestCase end assert_dom_equal "<label for=\"author_post_1_title\">Title</label>" + - "<input name='author[post][1][title]' size='30' type='text' id='author_post_1_title' value='Hello World' />", + "<input name='author[post][1][title]' type='text' id='author_post_1_title' value='Hello World' />", output_buffer end @@ -1931,8 +2030,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'create-post', 'edit_post', :method => 'patch') do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + "<input name='parent_post[secret]' type='hidden' value='0' />" + "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" end @@ -1951,9 +2050,9 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'create-post', 'edit_post', :method => 'patch') do - "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + - "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + - "<input name='post[comment][name]' type='text' id='post_comment_name' value='new comment' size='30' />" + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" + + "<input name='post[comment][name]' type='text' id='post_comment_name' value='new comment' />" end assert_dom_equal expected, output_buffer @@ -1967,7 +2066,7 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', 'patch') do - "<input name='post[category][name]' type='text' size='30' id='post_category_name' />" + "<input name='post[category][name]' type='text' id='post_category_name' />" end assert_dom_equal expected, output_buffer @@ -1991,45 +2090,14 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" + - "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" + + "<label for='title'>Title:</label> <input name='post[title]' type='text' id='post_title' value='Hello World' /><br/>" + + "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea><br/>" + "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" end assert_dom_equal expected, output_buffer end - def hidden_fields(method = nil) - txt = %{<div style="margin:0;padding:0;display:inline">} - txt << %{<input name="utf8" type="hidden" value="✓" />} - if method && !method.to_s.in?(['get', 'post']) - txt << %{<input name="_method" type="hidden" value="#{method}" />} - end - txt << %{</div>} - end - - def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil) - txt = %{<form accept-charset="UTF-8" action="#{action}"} - txt << %{ enctype="multipart/form-data"} if multipart - txt << %{ data-remote="true"} if remote - txt << %{ class="#{html_class}"} if html_class - txt << %{ id="#{id}"} if id - method = method.to_s == "get" ? "get" : "post" - txt << %{ method="#{method}">} - end - - def whole_form(action = "/", id = nil, html_class = nil, options = nil) - contents = block_given? ? yield : "" - - if options.is_a?(Hash) - method, remote, multipart = options.values_at(:method, :remote, :multipart) - else - method = options - end - - form_text(action, id, html_class, remote, multipart, method) + hidden_fields(method) + contents + "</form>" - end - def test_default_form_builder old_default_form_builder, ActionView::Base.default_form_builder = ActionView::Base.default_form_builder, LabelledFormBuilder @@ -2041,8 +2109,8 @@ class FormHelperTest < ActionView::TestCase end expected = whole_form('/posts/123', 'edit_post_123', 'edit_post', :method => 'patch') do - "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" + - "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" + + "<label for='title'>Title:</label> <input name='post[title]' type='text' id='post_title' value='Hello World' /><br/>" + + "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea><br/>" + "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" end @@ -2059,8 +2127,8 @@ class FormHelperTest < ActionView::TestCase end expected = - "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" + - "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" + + "<label for='title'>Title:</label> <input name='post[title]' type='text' id='post_title' value='Hello World' /><br/>" + + "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea><br/>" + "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" assert_dom_equal expected, output_buffer @@ -2211,8 +2279,51 @@ class FormHelperTest < ActionView::TestCase assert_equal "fields", output end + def test_form_builder_block_argument_deprecation + builder_class = Class.new(ActionView::Helpers::FormBuilder) do + def initialize(object_name, object, template, options, block) + super + end + end + + assert_deprecated(/Giving a block to FormBuilder is deprecated and has no effect anymore/) do + builder_class.new(:foo, nil, nil, {}, proc {}) + end + end + protected + def hidden_fields(method = nil) + txt = %{<div style="margin:0;padding:0;display:inline">} + txt << %{<input name="utf8" type="hidden" value="✓" />} + if method && !method.to_s.in?(['get', 'post']) + txt << %{<input name="_method" type="hidden" value="#{method}" />} + end + txt << %{</div>} + end + + def form_text(action = "/", id = nil, html_class = nil, remote = nil, multipart = nil, method = nil) + txt = %{<form accept-charset="UTF-8" action="#{action}"} + txt << %{ enctype="multipart/form-data"} if multipart + txt << %{ data-remote="true"} if remote + txt << %{ class="#{html_class}"} if html_class + txt << %{ id="#{id}"} if id + method = method.to_s == "get" ? "get" : "post" + txt << %{ method="#{method}">} + end + + def whole_form(action = "/", id = nil, html_class = nil, options = nil) + contents = block_given? ? yield : "" + + if options.is_a?(Hash) + method, remote, multipart = options.values_at(:method, :remote, :multipart) + else + method = options + end + + form_text(action, id, html_class, remote, multipart, method) + hidden_fields(method) + contents + "</form>" + end + def protect_against_forgery? false end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 6b8d62df62..2c0da8473a 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -509,7 +509,7 @@ class FormOptionsHelperTest < ActionView::TestCase def test_select_under_fields_for_with_string_and_given_prompt @post = Post.new - options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>" + options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>".html_safe output_buffer = fields_for :post, @post do |f| concat f.select(:category, options, :prompt => 'The prompt') @@ -529,6 +529,14 @@ class FormOptionsHelperTest < ActionView::TestCase ) end + def test_select_with_multiple_and_without_hidden_input + output_buffer = select(:post, :category, "", {:include_hidden => false}, :multiple => true) + assert_dom_equal( + "<select multiple=\"multiple\" id=\"post_category\" name=\"post[category][]\"></select>", + output_buffer + ) + end + def test_select_with_multiple_and_disabled_to_add_disabled_hidden_input output_buffer = select(:post, :category, "", {}, :multiple => true, :disabled => true) assert_dom_equal( @@ -665,6 +673,13 @@ class FormOptionsHelperTest < ActionView::TestCase ) end + def test_select_escapes_options + assert_dom_equal( + '<select id="post_title" name="post[title]"><script>alert(1)</script></select>', + select('post', 'title', '<script>alert(1)</script>') + ) + end + def test_select_with_selected_nil @post = Post.new @post.category = "<mus>" diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 6ef4cf4dd2..1e92ff99ff 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -222,19 +222,19 @@ class FormTagHelperTest < ActionView::TestCase def test_text_area_tag_size_string actual = text_area_tag "body", "hello world", "size" => "20x40" - expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>) + expected = %(<textarea cols="20" id="body" name="body" rows="40">\nhello world</textarea>) assert_dom_equal expected, actual end def test_text_area_tag_size_symbol actual = text_area_tag "body", "hello world", :size => "20x40" - expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>) + expected = %(<textarea cols="20" id="body" name="body" rows="40">\nhello world</textarea>) assert_dom_equal expected, actual end def test_text_area_tag_should_disregard_size_if_its_given_as_an_integer actual = text_area_tag "body", "hello world", :size => 20 - expected = %(<textarea id="body" name="body">hello world</textarea>) + expected = %(<textarea id="body" name="body">\nhello world</textarea>) assert_dom_equal expected, actual end @@ -245,19 +245,19 @@ class FormTagHelperTest < ActionView::TestCase def test_text_area_tag_escape_content actual = text_area_tag "body", "<b>hello world</b>", :size => "20x40" - expected = %(<textarea cols="20" id="body" name="body" rows="40"><b>hello world</b></textarea>) + expected = %(<textarea cols="20" id="body" name="body" rows="40">\n<b>hello world</b></textarea>) assert_dom_equal expected, actual end def test_text_area_tag_unescaped_content actual = text_area_tag "body", "<b>hello world</b>", :size => "20x40", :escape => false - expected = %(<textarea cols="20" id="body" name="body" rows="40"><b>hello world</b></textarea>) + expected = %(<textarea cols="20" id="body" name="body" rows="40">\n<b>hello world</b></textarea>) assert_dom_equal expected, actual end def test_text_area_tag_unescaped_nil_content actual = text_area_tag "body", nil, :escape => false - expected = %(<textarea id="body" name="body"></textarea>) + expected = %(<textarea id="body" name="body">\n</textarea>) assert_dom_equal expected, actual end @@ -513,6 +513,16 @@ class FormTagHelperTest < ActionView::TestCase expected = %(<fieldset class="format">Hello world!</fieldset>) assert_dom_equal expected, output_buffer + + output_buffer = render_erb("<%= field_set_tag %>") + + expected = %(<fieldset></fieldset>) + assert_dom_equal expected, output_buffer + + output_buffer = render_erb("<%= field_set_tag('You legend!') %>") + + expected = %(<fieldset><legend>You legend!</legend></fieldset>) + assert_dom_equal expected, output_buffer end def test_text_area_tag_options_symbolize_keys_side_effects diff --git a/actionpack/test/template/html-scanner/sanitizer_test.rb b/actionpack/test/template/html-scanner/sanitizer_test.rb index 32c655c5fd..324caef224 100644 --- a/actionpack/test/template/html-scanner/sanitizer_test.rb +++ b/actionpack/test/template/html-scanner/sanitizer_test.rb @@ -125,6 +125,24 @@ class SanitizerTest < ActionController::TestCase assert_equal(text, sanitizer.sanitize(text, :attributes => ['foo'])) end + def test_should_raise_argument_error_if_tags_is_not_enumerable + sanitizer = HTML::WhiteListSanitizer.new + e = assert_raise(ArgumentError) do + sanitizer.sanitize('', :tags => 'foo') + end + + assert_equal "You should pass :tags as an Enumerable", e.message + end + + def test_should_raise_argument_error_if_attributes_is_not_enumerable + sanitizer = HTML::WhiteListSanitizer.new + e = assert_raise(ArgumentError) do + sanitizer.sanitize('', :attributes => 'foo') + end + + assert_equal "You should pass :attributes as an Enumerable", e.message + end + [%w(img src), %w(a href)].each do |(tag, attr)| define_method "test_should_strip_#{attr}_attribute_in_#{tag}_with_bad_protocols" do assert_sanitized %(<#{tag} #{attr}="javascript:bang" title="1">boo</#{tag}>), %(<#{tag} title="1">boo</#{tag}>) diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 9441bd6b38..64898f7ad1 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -28,6 +28,8 @@ class JavaScriptHelperTest < ActionView::TestCase assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) ) assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags)) assert_equal %(unicode 
 newline), escape_javascript(%(unicode \342\200\250 newline).force_encoding('UTF-8').encode!) + assert_equal %(unicode 
 newline), escape_javascript(%(unicode \342\200\251 newline).force_encoding('UTF-8').encode!) + assert_equal %(dont <\\/close> tags), j(%(dont </close> tags)) end diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 482d953907..5c6f23d70b 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -96,6 +96,7 @@ class NumberHelperTest < ActionView::TestCase assert_equal("0.001", number_with_precision(0.00111, :precision => 3)) assert_equal("10.00", number_with_precision(9.995, :precision => 2)) assert_equal("11.00", number_with_precision(10.995, :precision => 2)) + assert_equal("0.00", number_with_precision(-0.001, :precision => 2)) end def test_number_with_precision_with_custom_delimiter_and_separator diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 7347e15373..122b07d348 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -238,6 +238,10 @@ module RenderTestCases def test_render_partial_with_nil_values_in_collection assert_equal "Hello: davidHello: Anonymous", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), nil ]) end + + def test_render_partial_with_layout_using_collection_and_template + assert_equal "<b>Hello: Amazon</b><b>Hello: Yahoo</b>", @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ]) + end def test_render_partial_with_empty_array_should_return_nil assert_nil @view.render(:partial => []) diff --git a/actionpack/test/template/sprockets_helper_test.rb b/actionpack/test/template/sprockets_helper_test.rb deleted file mode 100644 index 1c591bdcc2..0000000000 --- a/actionpack/test/template/sprockets_helper_test.rb +++ /dev/null @@ -1,348 +0,0 @@ -require 'abstract_unit' -require 'sprockets' -require 'sprockets/helpers/rails_helper' -require 'mocha' - -class SprocketsHelperTest < ActionView::TestCase - include Sprockets::Helpers::RailsHelper - - attr_accessor :assets - - class MockRequest - def protocol() 'http://' end - def ssl?() false end - def host_with_port() 'localhost' end - end - - def setup - super - - @controller = BasicController.new - @controller.request = MockRequest.new - - @assets = Sprockets::Environment.new - @assets.append_path(FIXTURES.join("sprockets/app/javascripts")) - @assets.append_path(FIXTURES.join("sprockets/app/stylesheets")) - @assets.append_path(FIXTURES.join("sprockets/app/images")) - @assets.append_path(FIXTURES.join("sprockets/app/fonts")) - - application = Struct.new(:config, :assets).new(config, @assets) - Rails.stubs(:application).returns(application) - @config = config - @config.perform_caching = true - @config.assets.digest = true - @config.assets.compile = true - end - - def url_for(*args) - "http://www.example.com" - end - - def config - @controller ? @controller.config : @config - end - - def compute_host(source, request, options = {}) - raise "Should never get here" - end - - test "asset_path" do - assert_match %r{/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png") - assert_match %r{/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png", :digest => true) - assert_match %r{/assets/logo.png}, - asset_path("logo.png", :digest => false) - end - - test "custom_asset_path" do - @config.assets.prefix = '/s' - assert_match %r{/s/logo-[0-9a-f]+.png}, - asset_path("logo.png") - assert_match %r{/s/logo-[0-9a-f]+.png}, - asset_path("logo.png", :digest => true) - assert_match %r{/s/logo.png}, - asset_path("logo.png", :digest => false) - end - - test "asset_path with root relative assets" do - assert_equal "/images/logo", - asset_path("/images/logo") - assert_equal "/images/logo.gif", - asset_path("/images/logo.gif") - - assert_equal "/dir/audio", - asset_path("/dir/audio") - end - - test "asset_path with absolute urls" do - assert_equal "http://www.example.com/video/play", - asset_path("http://www.example.com/video/play") - assert_equal "http://www.example.com/video/play.mp4", - asset_path("http://www.example.com/video/play.mp4") - end - - test "with a simple asset host the url should default to protocol relative" do - @controller.config.default_asset_host_protocol = :relative - @controller.config.asset_host = "assets-%d.example.com" - assert_match %r{^//assets-\d.example.com/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png") - end - - test "with a simple asset host the url can be changed to use the request protocol" do - @controller.config.asset_host = "assets-%d.example.com" - @controller.config.default_asset_host_protocol = :request - assert_match %r{http://assets-\d.example.com/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png") - end - - test "With a proc asset host that returns no protocol the url should be protocol relative" do - @controller.config.default_asset_host_protocol = :relative - @controller.config.asset_host = Proc.new do |asset| - "assets-999.example.com" - end - assert_match %r{^//assets-999.example.com/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png") - end - - test "with a proc asset host that returns a protocol the url use it" do - @controller.config.asset_host = Proc.new do |asset| - "http://assets-999.example.com" - end - assert_match %r{http://assets-999.example.com/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png") - end - - test "stylesheets served with a controller in scope can access the request" do - config.asset_host = Proc.new do |asset, request| - assert_not_nil request - "http://assets-666.example.com" - end - assert_match %r{http://assets-666.example.com/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png") - end - - test "stylesheets served without a controller in scope cannot access the request" do - @controller = nil - @config.asset_host = Proc.new do |asset, request| - fail "This should not have been called." - end - assert_raises ActionController::RoutingError do - asset_path("logo.png") - end - @config.asset_host = method :compute_host - assert_raises ActionController::RoutingError do - asset_path("logo.png") - end - end - - test "image_tag" do - assert_dom_equal '<img alt="Xml" src="/assets/xml.png" />', image_tag("xml.png") - end - - test "image_path" do - assert_match %r{/assets/logo-[0-9a-f]+.png}, - image_path("logo.png") - - assert_match %r{/assets/logo-[0-9a-f]+.png}, - path_to_image("logo.png") - end - - test "font_path" do - assert_match %r{/assets/font-[0-9a-f]+.ttf}, - font_path("font.ttf") - - assert_match %r{/assets/font-[0-9a-f]+.ttf}, - path_to_font("font.ttf") - end - - test "javascript_path" do - assert_match %r{/assets/application-[0-9a-f]+.js}, - javascript_path("application") - - assert_match %r{/assets/application-[0-9a-f]+.js}, - javascript_path("application.js") - - assert_match %r{/assets/application-[0-9a-f]+.js}, - path_to_javascript("application.js") - end - - test "stylesheet_path" do - assert_match %r{/assets/application-[0-9a-f]+.css}, - stylesheet_path("application") - - assert_match %r{/assets/application-[0-9a-f]+.css}, - stylesheet_path("application.css") - - assert_match %r{/assets/application-[0-9a-f]+.css}, - path_to_stylesheet("application.css") - end - - test "stylesheets served without a controller in do not use asset hosts when the default protocol is :request" do - @controller = nil - @config.asset_host = "assets-%d.example.com" - @config.default_asset_host_protocol = :request - @config.perform_caching = true - - assert_match %r{/assets/logo-[0-9a-f]+.png}, - asset_path("logo.png") - end - - test "asset path with relative url root" do - @controller.config.relative_url_root = "/collaboration/hieraki" - assert_equal "/collaboration/hieraki/images/logo.gif", - asset_path("/images/logo.gif") - end - - test "asset path with relative url root when controller isn't present but relative_url_root is" do - @controller = nil - @config.relative_url_root = "/collaboration/hieraki" - assert_equal "/collaboration/hieraki/images/logo.gif", - asset_path("/images/logo.gif") - end - - test "font path through asset_path" do - assert_match %r{/assets/font-[0-9a-f]+.ttf}, - asset_path('font.ttf') - - assert_match %r{/assets/dir/font-[0-9a-f]+.ttf}, - asset_path("dir/font.ttf") - - assert_equal "http://www.example.com/fonts/font.ttf", - asset_path("http://www.example.com/fonts/font.ttf") - end - - test "javascript path through asset_path" do - assert_match %r{/assets/application-[0-9a-f]+.js}, - asset_path(:application, :ext => "js") - - assert_match %r{/assets/xmlhr-[0-9a-f]+.js}, - asset_path("xmlhr", :ext => "js") - assert_match %r{/assets/dir/xmlhr-[0-9a-f]+.js}, - asset_path("dir/xmlhr.js", :ext => "js") - - assert_equal "/dir/xmlhr.js", - asset_path("/dir/xmlhr", :ext => "js") - - assert_equal "http://www.example.com/js/xmlhr", - asset_path("http://www.example.com/js/xmlhr", :ext => "js") - assert_equal "http://www.example.com/js/xmlhr.js", - asset_path("http://www.example.com/js/xmlhr.js", :ext => "js") - end - - test "javascript include tag" do - assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>}, - javascript_include_tag(:application) - assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>}, - javascript_include_tag(:application, :digest => true) - assert_match %r{<script src="/assets/application.js" type="text/javascript"></script>}, - javascript_include_tag(:application, :digest => false) - - assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>}, - javascript_include_tag("xmlhr") - assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>}, - javascript_include_tag("xmlhr.js") - assert_equal '<script src="http://www.example.com/xmlhr" type="text/javascript"></script>', - javascript_include_tag("http://www.example.com/xmlhr") - - assert_match %r{<script src=\"/assets/xmlhr-[0-9a-f]+.js" type=\"text/javascript\"></script>\n<script src=\"/assets/extra-[0-9a-f]+.js" type=\"text/javascript\"></script>}, - javascript_include_tag("xmlhr", "extra") - - assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>}, - javascript_include_tag(:application, :debug => true) - - assert_match %r{<script src="/assets/jquery.plugin.js" type="text/javascript"></script>}, - javascript_include_tag('jquery.plugin', :digest => false) - - @config.assets.compile = true - @config.assets.debug = true - assert_match %r{<script src="/javascripts/application.js" type="text/javascript"></script>}, - javascript_include_tag('/javascripts/application') - assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>}, - javascript_include_tag(:application) - end - - test "stylesheet path through asset_path" do - assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, :ext => "css") - - assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css") - assert_match %r{/assets/dir/style-[0-9a-f]+.css}, asset_path("dir/style.css", :ext => "css") - assert_equal "/dir/style.css", asset_path("/dir/style.css", :ext => "css") - - assert_equal "http://www.example.com/css/style", - asset_path("http://www.example.com/css/style", :ext => "css") - assert_equal "http://www.example.com/css/style.css", - asset_path("http://www.example.com/css/style.css", :ext => "css") - end - - test "stylesheet link tag" do - assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag(:application) - assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag(:application, :digest => true) - assert_match %r{<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag(:application, :digest => false) - - assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag("style") - assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag("style.css") - - assert_equal '<link href="http://www.example.com/style.css" media="screen" rel="stylesheet" type="text/css" />', - stylesheet_link_tag("http://www.example.com/style.css") - assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="all" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag("style", :media => "all") - assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="print" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag("style", :media => "print") - - assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/extra-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag("style", "extra") - - assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag(:application, :debug => true) - - @config.assets.compile = true - @config.assets.debug = true - assert_match %r{<link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag('/stylesheets/application') - - assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag(:application) - - assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag(:application, :media => "print") - end - - test "alternate asset prefix" do - stubs(:asset_prefix).returns("/themes/test") - assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", :ext => "css") - end - - test "alternate asset environment" do - assets = Sprockets::Environment.new - assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets")) - stubs(:asset_environment).returns(assets) - assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css") - end - - test "alternate hash based on environment" do - assets = Sprockets::Environment.new - assets.version = 'development' - assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets")) - stubs(:asset_environment).returns(assets) - dev_path = asset_path("style", :ext => "css") - - assets.version = 'production' - prod_path = asset_path("style", :ext => "css") - - assert_not_equal prod_path, dev_path - end - - test "precedence of `config.digest = false` over manifest.yml asset digests" do - Rails.application.config.assets.digests = {'logo.png' => 'logo-d1g3st.png'} - @config.assets.digest = false - - assert_equal '/assets/logo.png', - asset_path("logo.png") - end -end diff --git a/actionpack/test/template/sprockets_helper_with_routes_test.rb b/actionpack/test/template/sprockets_helper_with_routes_test.rb deleted file mode 100644 index bcbd81a7dd..0000000000 --- a/actionpack/test/template/sprockets_helper_with_routes_test.rb +++ /dev/null @@ -1,57 +0,0 @@ -require 'abstract_unit' -require 'sprockets' -require 'sprockets/helpers/rails_helper' -require 'mocha' - -class SprocketsHelperWithRoutesTest < ActionView::TestCase - include Sprockets::Helpers::RailsHelper - - # Let's bring in some named routes to test namespace conflicts with potential *_paths. - # We have to do this after we bring in the Sprockets RailsHelper so if there are conflicts, - # they'll fail in the way we expect in a real live Rails app. - routes = ActionDispatch::Routing::RouteSet.new - routes.draw do - resources :assets - end - include routes.url_helpers - - def setup - super - @controller = BasicController.new - - @assets = Sprockets::Environment.new - @assets.append_path(FIXTURES.join("sprockets/app/javascripts")) - @assets.append_path(FIXTURES.join("sprockets/app/stylesheets")) - @assets.append_path(FIXTURES.join("sprockets/app/images")) - - application = Struct.new(:config, :assets).new(config, @assets) - Rails.stubs(:application).returns(application) - @config = config - @config.perform_caching = true - @config.assets.digest = true - @config.assets.compile = true - end - - test "namespace conflicts on a named route called asset_path" do - # Testing this for sanity - asset_path is now a named route! - assert_match asset_path('test_asset'), '/assets/test_asset' - - assert_match %r{/assets/logo-[0-9a-f]+.png}, - path_to_asset("logo.png") - assert_match %r{/assets/logo-[0-9a-f]+.png}, - path_to_asset("logo.png", :digest => true) - assert_match %r{/assets/logo.png}, - path_to_asset("logo.png", :digest => false) - end - - test "javascript_include_tag with a named_route named asset_path" do - assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>}, - javascript_include_tag(:application) - end - - test "stylesheet_link_tag with a named_route named asset_path" do - assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />}, - stylesheet_link_tag(:application) - end - -end
\ No newline at end of file diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb index 37858c1ba2..f2ed2ec609 100644 --- a/actionpack/test/template/test_case_test.rb +++ b/actionpack/test/template/test_case_test.rb @@ -277,6 +277,12 @@ module ActionView end class RenderTemplateTest < ActionView::TestCase + test "supports specifying templates with a Regexp" do + controller.controller_path = "fun" + render(:template => "fun/games/hello_world") + assert_template %r{\Afun/games/hello_world\Z} + end + test "supports specifying partials" do controller.controller_path = "test" render(:template => "test/calling_partial_with_layout") diff --git a/actionpack/test/ts_isolated.rb b/actionpack/test/ts_isolated.rb index 7430de2299..ae2a0c95f6 100644 --- a/actionpack/test/ts_isolated.rb +++ b/actionpack/test/ts_isolated.rb @@ -3,7 +3,6 @@ $:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib') require 'minitest/autorun' require 'rbconfig' -require 'active_support/core_ext/kernel/reporting' require 'abstract_unit' class TestIsolated < ActiveSupport::TestCase diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 0d70edd0ba..c6d8eae46c 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,11 +1,22 @@ +## Rails 4.0.0 (unreleased) ## + +* Added ActiveModel::Model, a mixin to make Ruby objects work with AP out of box *Guillermo Iguaran* + * `AM::Errors#to_json`: support `:full_messages` parameter *Bogdan Gusiev* * Trim down Active Model API by removing `valid?` and `errors.full_messages` *José Valim* + +## Rails 3.2.2 (March 1, 2012) ## + +* No changes. + + ## Rails 3.2.1 (January 26, 2012) ## * No changes. + ## Rails 3.2.0 (January 20, 2012) ## * Deprecated `define_attr_method` in `ActiveModel::AttributeMethods`, because this only existed to @@ -21,14 +32,17 @@ * Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior *Bogdan Gusiev* + ## Rails 3.1.3 (November 20, 2011) ## * No changes + ## Rails 3.1.2 (November 18, 2011) ## * No changes + ## Rails 3.1.1 (October 7, 2011) ## * Remove hard dependency on bcrypt-ruby to avoid make ActiveModel dependent on a binary library. @@ -38,6 +52,7 @@ See GH #2687. *Guillermo Iguaran* + ## Rails 3.1.0 (August 30, 2011) ## * Alternate I18n namespace lookup is no longer supported. @@ -61,7 +76,7 @@ * Add support for selectively enabling/disabling observers *Myron Marston* -## Rails 3.0.12 (unreleased) ## +## Rails 3.0.12 (March 1, 2012) ## * No changes. diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc index a7ba27ba73..4861b0a002 100644 --- a/activemodel/README.rdoc +++ b/activemodel/README.rdoc @@ -13,6 +13,25 @@ in code duplication and fragile applications that broke on upgrades. Active Model solves this by defining an explicit API. You can read more about the API in ActiveModel::Lint::Tests. +Active Model provides a default module that implements the basic API required +to integrate with Action Pack out of the box: +ActiveModel::Model+. + + class Person + include ActiveModel::Model + + attr_accessor :name, :age + validates_presence_of :name + end + + person = Person.new(:name => 'bob', :age => '18') + person.name # => 'bob' + person.age # => 18 + person.valid? # => false + +It includes model name introspections, conversions, translations and +validations, resulting in a class suitable to be used with Action Pack. +See +ActiveModel::Model+ for more examples. + Active Model also provides the following functionality to have ORM-like behavior out of the box: diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index 60c1d16934..f2d004fb0a 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -5,7 +5,7 @@ Gem::Specification.new do |s| s.name = 'activemodel' s.version = version s.summary = 'A toolkit for building modeling frameworks (part of Rails).' - s.description = 'A toolkit for building modeling frameworks like Active Record and Active Resource. Rich support for attributes, callbacks, validations, observers, serialization, internationalization, and testing.' + s.description = 'A toolkit for building modeling frameworks like Active Record. Rich support for attributes, callbacks, validations, observers, serialization, internationalization, and testing.' s.required_ruby_version = '>= 1.9.3' diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 85514e63fd..2586147a20 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -39,6 +39,7 @@ module ActiveModel autoload :Errors autoload :Lint autoload :MassAssignmentSecurity + autoload :Model autoload :Name, 'active_model/naming' autoload :Naming autoload :Observer, 'active_model/observing' diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb index c7c805f1a2..d7f30f0920 100644 --- a/activemodel/lib/active_model/conversion.rb +++ b/activemodel/lib/active_model/conversion.rb @@ -21,7 +21,7 @@ module ActiveModel # cm.to_model == self # => true # cm.to_key # => nil # cm.to_param # => nil - # cm.to_path # => "contact_messages/contact_message" + # cm.to_partial_path # => "contact_messages/contact_message" # module Conversion extend ActiveSupport::Concern @@ -57,7 +57,7 @@ module ActiveModel end module ClassMethods #:nodoc: - # Provide a class level cache for the to_path. This is an + # Provide a class level cache for #to_partial_path. This is an # internal method and should not be accessed directly. def _to_partial_path #:nodoc: @_to_partial_path ||= begin diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 026f077ee7..d327913824 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -1,6 +1,7 @@ require 'active_model/attribute_methods' require 'active_support/hash_with_indifferent_access' require 'active_support/core_ext/object/duplicable' +require 'active_support/core_ext/object/blank' module ActiveModel # == Active Model Dirty @@ -98,7 +99,7 @@ module ActiveModel # person.name = 'bob' # person.changed? # => true def changed? - changed_attributes.any? + changed_attributes.present? end # List of attributes with unsaved changes. diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb index a10fdefd1a..88b730626c 100644 --- a/activemodel/lib/active_model/lint.rb +++ b/activemodel/lib/active_model/lint.rb @@ -78,10 +78,10 @@ module ActiveModel def test_model_naming assert model.class.respond_to?(:model_name), "The model should respond to model_name" model_name = model.class.model_name - assert_kind_of String, model_name - assert_kind_of String, model_name.human - assert_kind_of String, model_name.singular - assert_kind_of String, model_name.plural + assert model_name.respond_to?(:to_str) + assert model_name.human.respond_to?(:to_str) + assert model_name.singular.respond_to?(:to_str) + assert model_name.plural.respond_to?(:to_str) end # == Errors Testing diff --git a/activemodel/lib/active_model/mass_assignment_security.rb b/activemodel/lib/active_model/mass_assignment_security.rb index 95de039676..5e5405fe27 100644 --- a/activemodel/lib/active_model/mass_assignment_security.rb +++ b/activemodel/lib/active_model/mass_assignment_security.rb @@ -85,7 +85,7 @@ module ActiveModel # end # end # - # When using the :default role : + # When using the :default role: # # customer = Customer.new # customer.assign_attributes({ "name" => "David", "email" => "a@b.com", :logins_count => 5 }, :as => :default) @@ -93,7 +93,7 @@ module ActiveModel # customer.email # => "a@b.com" # customer.logins_count # => nil # - # And using the :admin role : + # And using the :admin role: # # customer = Customer.new # customer.assign_attributes({ "name" => "David", "email" => "a@b.com", :logins_count => 5}, :as => :admin) @@ -107,8 +107,9 @@ module ActiveModel # To start from an all-closed default and enable attributes as needed, # have a look at +attr_accessible+. # - # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of +attr_protected+ - # to sanitize attributes won't provide sufficient protection. + # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of + # +attr_protected+ to sanitize attributes provides basically the same + # functionality, but it makes a bit tricky to deal with nested attributes. def attr_protected(*args) options = args.extract_options! role = options[:as] || :default @@ -152,7 +153,7 @@ module ActiveModel # end # end # - # When using the :default role : + # When using the :default role: # # customer = Customer.new # customer.assign_attributes({ "name" => "David", "credit_rating" => "Excellent", :last_login => 1.day.ago }, :as => :default) @@ -162,15 +163,16 @@ module ActiveModel # customer.credit_rating = "Average" # customer.credit_rating # => "Average" # - # And using the :admin role : + # And using the :admin role: # # customer = Customer.new # customer.assign_attributes({ "name" => "David", "credit_rating" => "Excellent", :last_login => 1.day.ago }, :as => :admin) # customer.name # => "David" # customer.credit_rating # => "Excellent" # - # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of +attr_accessible+ - # to sanitize attributes won't provide sufficient protection. + # Note that using <tt>Hash#except</tt> or <tt>Hash#slice</tt> in place of + # +attr_accessible+ to sanitize attributes provides basically the same + # functionality, but it makes a bit tricky to deal with nested attributes. def attr_accessible(*args) options = args.extract_options! role = options[:as] || :default diff --git a/activemodel/lib/active_model/mass_assignment_security/sanitizer.rb b/activemodel/lib/active_model/mass_assignment_security/sanitizer.rb index cfeb4aa7cd..4491e07a72 100644 --- a/activemodel/lib/active_model/mass_assignment_security/sanitizer.rb +++ b/activemodel/lib/active_model/mass_assignment_security/sanitizer.rb @@ -3,18 +3,16 @@ module ActiveModel class Sanitizer # Returns all attributes not denied by the authorizer. def sanitize(attributes, authorizer) - sanitized_attributes = attributes.reject { |key, value| authorizer.deny?(key) } - debug_protected_attribute_removal(attributes, sanitized_attributes) + rejected = [] + sanitized_attributes = attributes.reject do |key, value| + rejected << key if authorizer.deny?(key) + end + process_removed_attributes(rejected) unless rejected.empty? sanitized_attributes end protected - def debug_protected_attribute_removal(attributes, sanitized_attributes) - removed_keys = attributes.keys - sanitized_attributes.keys - process_removed_attributes(removed_keys) if removed_keys.any? - end - def process_removed_attributes(attrs) raise NotImplementedError, "#process_removed_attributes(attrs) suppose to be overwritten" end diff --git a/activemodel/lib/active_model/model.rb b/activemodel/lib/active_model/model.rb new file mode 100644 index 0000000000..6825fdc653 --- /dev/null +++ b/activemodel/lib/active_model/model.rb @@ -0,0 +1,76 @@ +module ActiveModel + + # == Active Model Basic Model + # + # Includes the required interface for an object to interact with +ActionPack+, + # using different +ActiveModel+ modules. It includes model name introspections, + # conversions, translations and validations. Besides that, it allows you to + # initialize the object with a hash of attributes, pretty much like + # +ActiveRecord+ does. + # + # A minimal implementation could be: + # + # class Person + # include ActiveModel::Model + # attr_accessor :name, :age + # end + # + # person = Person.new(:name => 'bob', :age => '18') + # person.name # => 'bob' + # person.age # => 18 + # + # Note that, by default, +ActiveModel::Model+ implements +persisted?+ to + # return +false+, which is the most common case. You may want to override it + # in your class to simulate a different scenario: + # + # class Person + # include ActiveModel::Model + # attr_accessor :id, :name + # + # def persisted? + # self.id == 1 + # end + # end + # + # person = Person.new(:id => 1, :name => 'bob') + # person.persisted? # => true + # + # Also, if for some reason you need to run code on +initialize+, make sure you + # call super if you want the attributes hash initialization to happen. + # + # class Person + # include ActiveModel::Model + # attr_accessor :id, :name, :omg + # + # def initialize(attributes) + # super + # @omg ||= true + # end + # end + # + # person = Person.new(:id => 1, :name => 'bob') + # person.omg # => true + # + # For more detailed information on other functionalities available, please refer + # to the specific modules included in +ActiveModel::Model+ (see below). + module Model + def self.included(base) + base.class_eval do + extend ActiveModel::Naming + extend ActiveModel::Translation + include ActiveModel::Validations + include ActiveModel::Conversion + end + end + + def initialize(params={}) + params.each do |attr, value| + self.public_send("#{attr}=", value) + end if params + end + + def persisted? + false + end + end +end diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 755e54efcd..5665e10002 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -1,39 +1,40 @@ require 'active_support/inflector' require 'active_support/core_ext/hash/except' require 'active_support/core_ext/module/introspection' -require 'active_support/core_ext/module/deprecation' +require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/object/blank' module ActiveModel - class Name < String - attr_reader :singular, :plural, :element, :collection, :partial_path, - :singular_route_key, :route_key, :param_key, :i18n_key + class Name + include Comparable + + attr_reader :singular, :plural, :element, :collection, + :singular_route_key, :route_key, :param_key, :i18n_key, + :name alias_method :cache_key, :collection - deprecate :partial_path => "ActiveModel::Name#partial_path is deprecated. Call #to_partial_path on model instances directly instead." + delegate :==, :===, :<=>, :=~, :"!~", :eql?, :to_s, + :to_str, :to => :name def initialize(klass, namespace = nil, name = nil) - name ||= klass.name - - raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if name.blank? + @name = name || klass.name - super(name) + raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if @name.blank? - @unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace + @unnamespaced = @name.sub(/^#{namespace.name}::/, '') if namespace @klass = klass - @singular = _singularize(self).freeze - @plural = ActiveSupport::Inflector.pluralize(@singular).freeze - @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze - @human = ActiveSupport::Inflector.humanize(@element).freeze - @collection = ActiveSupport::Inflector.tableize(self).freeze - @partial_path = "#{@collection}/#{@element}".freeze - @param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze - @i18n_key = self.underscore.to_sym + @singular = _singularize(@name) + @plural = ActiveSupport::Inflector.pluralize(@singular) + @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(@name)) + @human = ActiveSupport::Inflector.humanize(@element) + @collection = ActiveSupport::Inflector.tableize(@name) + @param_key = (namespace ? _singularize(@unnamespaced) : @singular) + @i18n_key = @name.underscore.to_sym @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural.dup) - @singular_route_key = ActiveSupport::Inflector.singularize(@route_key).freeze + @singular_route_key = ActiveSupport::Inflector.singularize(@route_key) @route_key << "_index" if @plural == @singular - @route_key.freeze end # Transform the model name into a more humane format, using I18n. By default, diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 51f078e662..4323ee1e09 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -1,7 +1,5 @@ require 'active_support/core_ext/hash/except' require 'active_support/core_ext/hash/slice' -require 'active_support/core_ext/array/wrap' - module ActiveModel # == Active Model Serialization @@ -11,7 +9,6 @@ module ActiveModel # A minimal implementation could be: # # class Person - # # include ActiveModel::Serialization # # attr_accessor :name @@ -19,7 +16,6 @@ module ActiveModel # def attributes # {'name' => nil} # end - # # end # # Which would provide you with: @@ -43,7 +39,6 @@ module ActiveModel # So a minimal implementation including XML and JSON would be: # # class Person - # # include ActiveModel::Serializers::JSON # include ActiveModel::Serializers::Xml # @@ -52,7 +47,6 @@ module ActiveModel # def attributes # {'name' => nil} # end - # # end # # Which would provide you with: @@ -88,7 +82,7 @@ module ActiveModel method_names.each { |n| hash[n.to_s] = send(n) } serializable_add_includes(options) do |association, records, opts| - hash[association] = if records.is_a?(Enumerable) + hash[association.to_s] = if records.is_a?(Enumerable) records.map { |a| a.serializable_hash(opts) } else records.serializable_hash(opts) @@ -126,13 +120,13 @@ module ActiveModel # +records+ - the association record(s) to be serialized # +opts+ - options for the association records def serializable_add_includes(options = {}) #:nodoc: - return unless include = options[:include] + return unless includes = options[:include] - unless include.is_a?(Hash) - include = Hash[Array.wrap(include).map { |n| n.is_a?(Hash) ? n.to_a.first : [n, {}] }] + unless includes.is_a?(Hash) + includes = Hash[Array(includes).map { |n| n.is_a?(Hash) ? n.to_a.first : [n, {}] }] end - include.each do |association, opts| + includes.each do |association, opts| if records = send(association) yield association, records, opts end diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb new file mode 100644 index 0000000000..b632a2bd6b --- /dev/null +++ b/activemodel/lib/active_model/validations/clusivity.rb @@ -0,0 +1,31 @@ +require 'active_support/core_ext/range.rb' + +module ActiveModel + module Validations + module Clusivity + ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " << + "and must be supplied as the :in option of the configuration hash" + + def check_validity! + unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) } + raise ArgumentError, ERROR_MESSAGE + end + end + + private + + def include?(record, value) + delimiter = options[:in] + exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter + exclusions.send(inclusion_method(exclusions), value) + end + + # In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the + # range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt> + # uses the previous logic of comparing a value with the range endpoints. + def inclusion_method(enumerable) + enumerable.is_a?(Range) ? :cover? : :include? + end + end + end +end diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index 644cc814a7..5fedb1978b 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -1,35 +1,17 @@ -require 'active_support/core_ext/range' +require "active_model/validations/clusivity" module ActiveModel # == Active Model Exclusion Validator module Validations class ExclusionValidator < EachValidator - ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " << - "and must be supplied as the :in option of the configuration hash" - - def check_validity! - unless [:include?, :call].any? { |method| options[:in].respond_to?(method) } - raise ArgumentError, ERROR_MESSAGE - end - end + include Clusivity def validate_each(record, attribute, value) - delimiter = options[:in] - exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter - if exclusions.send(inclusion_method(exclusions), value) + if include?(record, value) record.errors.add(attribute, :exclusion, options.except(:in).merge!(:value => value)) end end - - private - - # In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the - # range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt> - # uses the previous logic of comparing a value with the range endpoints. - def inclusion_method(enumerable) - enumerable.is_a?(Range) ? :cover? : :include? - end end module HelperMethods @@ -59,7 +41,7 @@ module ActiveModel # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. - # * <tt>:strict</tt> - Specifies whether validation should be strict. + # * <tt>:strict</tt> - Specifies whether validation should be strict. # See <tt>ActiveModel::Validation#validates!</tt> for more information def validates_exclusion_of(*attr_names) validates_with ExclusionValidator, _merge_attributes(attr_names) diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index 147e2ecb69..15ae7b1959 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -1,35 +1,17 @@ -require 'active_support/core_ext/range' +require "active_model/validations/clusivity" module ActiveModel # == Active Model Inclusion Validator module Validations class InclusionValidator < EachValidator - ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " << - "and must be supplied as the :in option of the configuration hash" - - def check_validity! - unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) } - raise ArgumentError, ERROR_MESSAGE - end - end + include Clusivity def validate_each(record, attribute, value) - delimiter = options[:in] - exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter - unless exclusions.send(inclusion_method(exclusions), value) + unless include?(record, value) record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) end end - - private - - # In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the - # range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt> - # uses the previous logic of comparing a value with the range endpoints. - def inclusion_method(enumerable) - enumerable.is_a?(Range) ? :cover? : :include? - end end module HelperMethods @@ -59,7 +41,7 @@ module ActiveModel # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # method, proc or string should return or evaluate to a true or false value. - # * <tt>:strict</tt> - Specifies whether validation should be strict. + # * <tt>:strict</tt> - Specifies whether validation should be strict. # See <tt>ActiveModel::Validation#validates!</tt> for more information def validates_inclusion_of(*attr_names) validates_with InclusionValidator, _merge_attributes(attr_names) diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index 9406328d3e..34298d31c2 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -188,6 +188,12 @@ class AttributeMethodsTest < ActiveModel::TestCase assert_raises(NoMethodError) { m.protected_method } end + class ClassWithProtected + protected + def protected_method + end + end + test 'should not interfere with respond_to? if the attribute has a private/protected method' do m = ModelWithAttributes2.new m.attributes = { 'private_method' => '<3', 'protected_method' => 'O_o' } @@ -195,9 +201,11 @@ class AttributeMethodsTest < ActiveModel::TestCase assert !m.respond_to?(:private_method) assert m.respond_to?(:private_method, true) + c = ClassWithProtected.new + # This is messed up, but it's how Ruby works at the moment. Apparently it will be changed # in the future. - assert m.respond_to?(:protected_method) + assert_equal c.respond_to?(:protected_method), m.respond_to?(:protected_method) assert m.respond_to?(:protected_method, true) end diff --git a/activemodel/test/cases/conversion_test.rb b/activemodel/test/cases/conversion_test.rb index 24552bcaf2..d679ad41aa 100644 --- a/activemodel/test/cases/conversion_test.rb +++ b/activemodel/test/cases/conversion_test.rb @@ -24,7 +24,7 @@ class ConversionTest < ActiveModel::TestCase assert_equal "1", Contact.new(:id => 1).to_param end - test "to_path default implementation returns a string giving a relative path" do + test "to_partial_path default implementation returns a string giving a relative path" do assert_equal "contacts/contact", Contact.new.to_partial_path assert_equal "helicopters/helicopter", Helicopter.new.to_partial_path, "ActiveModel::Conversion#to_partial_path caching should be class-specific" diff --git a/activemodel/test/cases/model_test.rb b/activemodel/test/cases/model_test.rb new file mode 100644 index 0000000000..d93fd96b88 --- /dev/null +++ b/activemodel/test/cases/model_test.rb @@ -0,0 +1,26 @@ +require 'cases/helper' + +class ModelTest < ActiveModel::TestCase + include ActiveModel::Lint::Tests + + class BasicModel + include ActiveModel::Model + attr_accessor :attr + end + + def setup + @model = BasicModel.new + end + + def test_initialize_with_params + object = BasicModel.new(:attr => "value") + assert_equal object.attr, "value" + end + + def test_initialize_with_nil_or_empty_hash_params_does_not_explode + assert_nothing_raised do + BasicModel.new() + BasicModel.new({}) + end + end +end diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 1e14d83bcb..49d8706ac2 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -25,12 +25,6 @@ class NamingTest < ActiveModel::TestCase assert_equal 'post/track_backs', @model_name.collection end - def test_partial_path - assert_deprecated(/#partial_path.*#to_partial_path/) do - assert_equal 'post/track_backs/track_back', @model_name.partial_path - end - end - def test_human assert_equal 'Track back', @model_name.human end @@ -61,12 +55,6 @@ class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase assert_equal 'blog/posts', @model_name.collection end - def test_partial_path - assert_deprecated(/#partial_path.*#to_partial_path/) do - assert_equal 'blog/posts/post', @model_name.partial_path - end - end - def test_human assert_equal 'Post', @model_name.human end @@ -105,12 +93,6 @@ class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase assert_equal 'blog/posts', @model_name.collection end - def test_partial_path - assert_deprecated(/#partial_path.*#to_partial_path/) do - assert_equal 'blog/posts/post', @model_name.partial_path - end - end - def test_human assert_equal 'Post', @model_name.human end @@ -149,12 +131,6 @@ class NamingWithSuppliedModelNameTest < ActiveModel::TestCase assert_equal 'articles', @model_name.collection end - def test_partial_path - assert_deprecated(/#partial_path.*#to_partial_path/) do - assert_equal 'articles/article', @model_name.partial_path - end - end - def test_human assert_equal 'Article', @model_name.human end diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb index 3b201a70f5..66b18d65e5 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serialization_test.rb @@ -88,62 +88,62 @@ class SerializationTest < ActiveModel::TestCase def test_include_option_with_singular_association expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com", - :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} + "address"=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} assert_equal expected, @user.serializable_hash(:include => :address) end def test_include_option_with_plural_association expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} assert_equal expected, @user.serializable_hash(:include => :friends) end def test_include_option_with_empty_association @user.friends = [] - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", :friends=>[]} + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "friends"=>[]} assert_equal expected, @user.serializable_hash(:include => :friends) end def test_multiple_includes expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + "address"=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, + "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} assert_equal expected, @user.serializable_hash(:include => [:address, :friends]) end def test_include_with_options expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane"}} + "address"=>{"street"=>"123 Lane"}} assert_equal expected, @user.serializable_hash(:include => {:address => {:only => "street"}}) end def test_nested_include @user.friends.first.friends = [@user] expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male', - :friends => [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', :friends => []}]} + "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male', + "friends"=> [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', "friends"=> []}]} assert_equal expected, @user.serializable_hash(:include => {:friends => {:include => :friends}}) end def test_only_include - expected = {"name"=>"David", :friends => [{"name" => "Joe"}, {"name" => "Sue"}]} + expected = {"name"=>"David", "friends" => [{"name" => "Joe"}, {"name" => "Sue"}]} assert_equal expected, @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}}) end def test_except_include expected = {"name"=>"David", "email"=>"david@example.com", - :friends => [{"name" => 'Joe', "email" => 'joe@example.com'}, + "friends"=> [{"name" => 'Joe', "email" => 'joe@example.com'}, {"name" => "Sue", "email" => 'sue@example.com'}]} assert_equal expected, @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}}) end def test_multiple_includes_with_options expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane"}, - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + "address"=>{"street"=>"123 Lane"}, + "friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} assert_equal expected, @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends]) end diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index cea50aaa9d..26f6093bc2 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,85 @@ ## Rails 4.0.0 (unreleased) ## +* Added `#find_by` and `#find_by!` to mirror the functionality + provided by dynamic finders in a way that allows dynamic input more + easily: + + Post.find_by name: 'Spartacus', rating: 4 + Post.find_by "published_at < ?", 2.weeks.ago + Post.find_by! name: 'Spartacus' + + *Jon Leighton* + +* Added ActiveRecord::Base#slice to return a hash of the given methods with + their names as keys and returned values as values. + + *Guillermo Iguaran* + +* Deprecate eager-evaluated scopes. + + Don't use this: + + scope :red, where(color: 'red') + default_scope where(color: 'red') + + Use this: + + scope :red, -> { where(color: 'red') } + default_scope { where(color: 'red') } + + The former has numerous issues. It is a common newbie gotcha to do + the following: + + scope :recent, where(published_at: Time.now - 2.weeks) + + Or a more subtle variant: + + scope :recent, -> { where(published_at: Time.now - 2.weeks) } + scope :recent_red, recent.where(color: 'red') + + Eager scopes are also very complex to implement within Active + Record, and there are still bugs. For example, the following does + not do what you expect: + + scope :remove_conditions, except(:where) + where(...).remove_conditions # => still has conditions + + *Jon Leighton* + +* Remove IdentityMap + + IdentityMap has never graduated to be an "enabled-by-default" feature, due + to some inconsistencies with associations, as described in this commit: + + https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6 + + Hence the removal from the codebase, until such issues are fixed. + + *Carlos Antonio da Silva* + +* Added the schema cache dump feature. + + `Schema cache dump` feature was implemetend. This feature can dump/load internal state of `SchemaCache` instance + because we want to boot rails more quickly when we have many models. + + Usage notes: + + 1) execute rake task. + RAILS_ENV=production bundle exec rake db:schema:cache:dump + => generate db/schema_cache.dump + + 2) add config.use_schema_cache_dump = true in config/production.rb. BTW, true is default. + + 3) boot rails. + RAILS_ENV=production bundle exec rails server + => use db/schema_cache.dump + + 4) If you remove clear dumped cache, execute rake task. + RAILS_ENV=production bundle exec rake db:schema:cache:clear + => remove db/schema_cache.dump + + *kennyj* + * Added support for partial indices to PostgreSQL adapter The `add_index` method now supports a `where` option that receives a @@ -130,6 +210,28 @@ * PostgreSQL hstore types are automatically deserialized from the database. +## Rails 3.2.3 (unreleased) ## + +* Added find_or_create_by_{attribute}! dynamic method. *Andrew White* + +* Whitelist all attribute assignment by default. Change the default for newly generated applications to whitelist all attribute assignment. Also update the generated model classes so users are reminded of the importance of attr_accessible. *NZKoz* + +* Update ActiveRecord::AttributeMethods#attribute_present? to return false for empty strings. *Jacobkg* + +* Fix associations when using per class databases. *larskanis* + +* Revert setting NOT NULL constraints in add_timestamps *fxn* + +* Fix mysql to use proper text types. Fixes #3931. *kennyj* + +* Fix #5069 - Protect foreign key from mass assignment through association builder. *byroot* + + +## Rails 3.2.2 (March 1, 2012) ## + +* No changes. + + ## Rails 3.2.1 (January 26, 2012) ## * The threshold for auto EXPLAIN is ignored if there's no logger. *fxn* @@ -320,7 +422,8 @@ *Aaron Christy* - ## Rails 3.1.4 (unreleased) ## + +## Rails 3.1.4 (March 1, 2012) ## * Fix a custom primary key regression *GH 3987* @@ -347,6 +450,7 @@ *Julius de Bruijn* + ### Rails 3.1.3 (November 20, 2011) ## * Perf fix: If we're deleting all records in an association, don't add a IN(..) clause @@ -360,6 +464,7 @@ *Christos Zisopoulos and Kenny J* + ### Rails 3.1.2 (November 18, 2011) ## * Fix bug with PostgreSQLAdapter#indexes. When the search path has multiple schemas, spaces @@ -407,6 +512,7 @@ *Kenny J* + ## Rails 3.1.1 (October 7, 2011) ## * Add deprecation for the preload_associations method. Fixes #3022. @@ -779,7 +885,7 @@ *Aaron Patterson* -## Rails 3.0.12 (unreleased) ## +## Rails 3.0.12 (March 1, 2012) ## * No changes. diff --git a/activerecord/RUNNING_UNIT_TESTS b/activerecord/RUNNING_UNIT_TESTS index a1ed6712c5..2c310e7ac3 100644 --- a/activerecord/RUNNING_UNIT_TESTS +++ b/activerecord/RUNNING_UNIT_TESTS @@ -26,13 +26,6 @@ You can run all the tests for a given database via rake: The 'rake test' task will run all the tests for mysql, mysql2, sqlite3 and postgresql. -== Identity Map - -By default the tests run with the Identity Map turned off. But all tests should pass whether or -not the identity map is on or off. You can turn it on using the IM env variable: - - $ IM=true ruby -Itest test/case/base_test.rb - == Config file By default, the config file is expected to be at the path test/config.yml. You can specify a diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 73c8a06ab7..0cb0644bcf 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -65,7 +65,6 @@ module ActiveRecord autoload :DynamicFinderMatch autoload :DynamicScopeMatch autoload :Explain - autoload :IdentityMap autoload :Inheritance autoload :Integration autoload :Migration diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 958821add6..b901f06ca4 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1382,7 +1382,9 @@ module ActiveRecord # and +decrement_counter+. The counter cache is incremented when an object of this # class is created and decremented when it's destroyed. This requires that a column # named <tt>#{table_name}_count</tt> (such as +comments_count+ for a belonging Comment class) - # is used on the associate class (such as a Post class). You can also specify a custom counter + # is used on the associate class (such as a Post class) - that is the migration for + # <tt>#{table_name}_count</tt> is created on the associate class (such that Post.comments_count will + # return the count cached, see note below). You can also specify a custom counter # cache column by providing a column name instead of a +true+/+false+ value to this # option (e.g., <tt>:counter_cache => :my_custom_counter</tt>.) # Note: Specifying a counter cache will add it to that model's list of readonly attributes @@ -1512,8 +1514,8 @@ module ActiveRecord # * <tt>Developer#projects.size</tt> # * <tt>Developer#projects.find(id)</tt> # * <tt>Developer#projects.exists?(...)</tt> - # * <tt>Developer#projects.build</tt> (similar to <tt>Project.new("project_id" => id)</tt>) - # * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new("project_id" => id); c.save; c</tt>) + # * <tt>Developer#projects.build</tt> (similar to <tt>Project.new("developer_id" => id)</tt>) + # * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new("developer_id" => id); c.save; c</tt>) # The declaration may include an options hash to specialize the behavior of the association. # # === Options diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 7887d59aad..fb0ca15c23 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -28,6 +28,7 @@ module ActiveRecord @target = nil @owner, @reflection = owner, reflection @updated = false + @stale_state = nil reset reset_scope @@ -44,7 +45,6 @@ module ActiveRecord # Resets the \loaded flag to +false+ and sets the \target to +nil+. def reset @loaded = false - IdentityMap.remove(target) if IdentityMap.enabled? && target @target = nil end @@ -134,17 +134,7 @@ module ActiveRecord # ActiveRecord::RecordNotFound is rescued within the method, and it is # not reraised. The proxy is \reset and +nil+ is the return value. def load_target - if find_target? - begin - if IdentityMap.enabled? && association_class && association_class.respond_to?(:base_class) - @target = IdentityMap.get(association_class, owner[reflection.foreign_key]) - end - rescue NameError - nil - ensure - @target ||= find_target - end - end + @target ||= find_target if find_target? loaded! unless loaded? target rescue ActiveRecord::RecordNotFound @@ -231,7 +221,8 @@ module ActiveRecord def build_record(attributes, options) reflection.build_association(attributes, options) do |record| - record.assign_attributes(create_scope.except(*record.changed), :without_protection => true) + attributes = create_scope.except(*(record.changed - [reflection.foreign_key])) + record.assign_attributes(attributes, :without_protection => true) end end end diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 982084c9b8..2972b7e13e 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -33,6 +33,23 @@ module ActiveRecord private + def column_for(table_name, column_name) + columns = alias_tracker.connection.schema_cache.columns_hash[table_name] + columns[column_name] + end + + def bind_value(scope, column, value) + substitute = alias_tracker.connection.substitute_at( + column, scope.bind_values.length) + scope.bind_values += [[column, value]] + substitute + end + + def bind(scope, table_name, column_name, value) + column = column_for table_name, column_name + bind_value scope, column, value + end + def add_constraints(scope) tables = construct_tables @@ -67,10 +84,13 @@ module ActiveRecord conditions = self.conditions[i] if reflection == chain.last - scope = scope.where(table[key].eq(owner[foreign_key])) + bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key] + scope = scope.where(table[key].eq(bind_val)) if reflection.type - scope = scope.where(table[reflection.type].eq(owner.class.base_class.name)) + value = owner.class.base_class.name + bind_val = bind scope, table.table_name, reflection.type.to_s, value + scope = scope.where(table[reflection.type].eq(bind_val)) end conditions.each do |condition| diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index b2136605e1..da4c311bce 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -544,7 +544,7 @@ module ActiveRecord # If using a custom finder_sql, #find scans the entire collection. def find_by_scan(*args) expects_array = args.first.kind_of?(Array) - ids = args.flatten.compact.uniq.map { |arg| arg.to_i } + ids = args.flatten.compact.map{ |arg| arg.to_i }.uniq if ids.size == 1 id = ids.first 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 9657cb081d..53d49fef2e 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -73,7 +73,9 @@ module ActiveRecord # association def build_through_record(record) @through_records[record.object_id] ||= begin - through_record = through_association.build(construct_join_attributes(record)) + ensure_mutable + + through_record = through_association.build through_record.send("#{source_reflection.name}=", record) through_record end diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 253998fb23..b4c3908b10 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -77,7 +77,7 @@ module ActiveRecord # Some databases impose a limit on the number of ids in a list (in Oracle it's 1000) # Make several smaller queries if necessary or make one query if the adapter supports it sliced = owner_keys.each_slice(model.connection.in_clause_length || owner_keys.size) - records = sliced.map { |slice| records_for(slice) }.flatten + records = sliced.map { |slice| records_for(slice).to_a }.flatten end # Each record may have multiple owners, and vice-versa @@ -93,7 +93,8 @@ module ActiveRecord end def build_scope - scope = klass.scoped + scope = klass.unscoped + scope.default_scoped = true scope = scope.where(interpolate(options[:conditions])) scope = scope.where(interpolate(preload_options[:conditions])) diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index f95e5337c2..fd0e90aaf0 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -37,9 +37,7 @@ module ActiveRecord # situation it is more natural for the user to just create or modify their join records # directly as required. def construct_join_attributes(*records) - if source_reflection.macro != :belongs_to - raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection) - end + ensure_mutable join_attributes = { source_reflection.foreign_key => @@ -73,6 +71,12 @@ module ActiveRecord !owner[through_reflection.foreign_key].nil? end + def ensure_mutable + if source_reflection.macro != :belongs_to + raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection) + end + end + def ensure_not_nested if reflection.nested? raise HasManyThroughNestedAssociationsAreReadonly.new(owner, reflection) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 3e27e85f02..39ea885246 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -49,14 +49,6 @@ module ActiveRecord @attribute_methods_generated ||= false end - # We will define the methods as instance methods, but will call them as singleton - # methods. This allows us to use method_defined? to check if the method exists, - # which is fast and won't give any false positives from the ancestors (because - # there are no ancestors). - def generated_external_attribute_methods - @generated_external_attribute_methods ||= Module.new { extend self } - end - def undefine_attribute_methods super if attribute_methods_generated? @attribute_methods_generated = false @@ -189,7 +181,7 @@ module ActiveRecord # nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings). def attribute_present?(attribute) value = read_attribute(attribute) - !value.nil? || (value.respond_to?(:empty?) && !value.empty?) + !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end # Returns the column object for the named attribute. @@ -214,37 +206,64 @@ module ActiveRecord value end - # Returns a copy of the attributes hash where all the values have been safely quoted for use in - # an Arel insert/update method. - def arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys) - attrs = {} - klass = self.class - arel_table = klass.arel_table + def arel_attributes_with_values_for_create(pk_attribute_allowed) + arel_attributes_with_values(attributes_for_create(pk_attribute_allowed)) + end - attribute_names.each do |name| - if (column = column_for_attribute(name)) && (include_primary_key || !column.primary) + def arel_attributes_with_values_for_update(attribute_names) + arel_attributes_with_values(attributes_for_update(attribute_names)) + end + + def attribute_method?(attr_name) + defined?(@attributes) && @attributes.include?(attr_name) + end - if include_readonly_attributes || !self.class.readonly_attributes.include?(name) + private - value = if klass.serialized_attributes.include?(name) - @attributes[name].serialized_value - else - # FIXME: we need @attributes to be used consistently. - # If the values stored in @attributes were already type - # casted, this code could be simplified - read_attribute(name) - end + # Returns a Hash of the Arel::Attributes and attribute values that have been + # type casted for use in an Arel insert/update method. + def arel_attributes_with_values(attribute_names) + attrs = {} + arel_table = self.class.arel_table - attrs[arel_table[name]] = value - end - end + attribute_names.each do |name| + attrs[arel_table[name]] = typecasted_attribute_value(name) end - attrs end - def attribute_method?(attr_name) - defined?(@attributes) && @attributes.include?(attr_name) + # Filters the primary keys and readonly attributes from the attribute names. + def attributes_for_update(attribute_names) + attribute_names.select do |name| + column_for_attribute(name) && !pk_attribute?(name) && !readonly_attribute?(name) + end + end + + # Filters out the primary keys, from the attribute names, when the primary + # key is to be generated (e.g. the id attribute has no value). + def attributes_for_create(pk_attribute_allowed) + @attributes.keys.select do |name| + column_for_attribute(name) && (pk_attribute_allowed || !pk_attribute?(name)) + end + end + + def readonly_attribute?(name) + self.class.readonly_attributes.include?(name) + end + + def pk_attribute?(name) + column_for_attribute(name).primary + end + + def typecasted_attribute_value(name) + if self.class.serialized_attributes.include?(name) + @attributes[name].serialized_value + else + # FIXME: we need @attributes to be used consistently. + # If the values stored in @attributes were already typecasted, this code + # could be simplified + read_attribute(name) + end end end end diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 433d508357..11c63591e3 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -22,8 +22,6 @@ module ActiveRecord if status = super @previously_changed = changes @changed_attributes.clear - elsif IdentityMap.enabled? - IdentityMap.remove(self) end status end @@ -34,9 +32,6 @@ module ActiveRecord @previously_changed = changes @changed_attributes.clear end - rescue - IdentityMap.remove(self) if IdentityMap.enabled? - raise end # <tt>reload</tt> the record and clears changed attributes. @@ -58,8 +53,6 @@ module ActiveRecord @changed_attributes.delete(attr) unless _field_changed?(attr, old, value) else old = clone_attribute_value(:read_attribute, attr) - # Save Time objects as TimeWithZone if time_zone_aware_attributes == true - old = old.in_time_zone if clone_with_time_zone_conversion_attribute?(attr, old) @changed_attributes[attr] = old if _field_changed?(attr, old, value) end @@ -92,10 +85,6 @@ module ActiveRecord old != value end - - def clone_with_time_zone_conversion_attribute?(attr, old) - old.class.name == "Time" && time_zone_aware_attributes && !self.skip_time_zone_conversion_for_attributes.include?(attr.to_sym) - end end end end diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 8e0b3e1402..7b7811a706 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -43,12 +43,6 @@ module ActiveRecord if attr_name == primary_key && attr_name != 'id' generated_attribute_methods.send(:alias_method, :id, primary_key) - generated_external_attribute_methods.module_eval <<-CODE, __FILE__, __LINE__ - def id(v, attributes, attributes_cache, attr_name) - attr_name = '#{primary_key}' - send(attr_name, attributes[attr_name], attributes, attributes_cache, attr_name) - end - CODE end end @@ -111,9 +105,8 @@ module ActiveRecord # end # Project.primary_key # => "foo_id" def primary_key=(value) - @original_primary_key = @primary_key if defined?(@primary_key) - @primary_key = value && value.to_s - @quoted_primary_key = nil + @primary_key = value && value.to_s + @quoted_primary_key = nil end end end diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 846ac03d82..dcc3d79de9 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -29,35 +29,8 @@ module ActiveRecord cached_attributes.include?(attr_name) end - def undefine_attribute_methods - generated_external_attribute_methods.module_eval do - instance_methods.each { |m| undef_method(m) } - end - - super - end - - def type_cast_attribute(attr_name, attributes, cache = {}) #:nodoc: - return unless attr_name - attr_name = attr_name.to_s - - if generated_external_attribute_methods.method_defined?(attr_name) - if attributes.has_key?(attr_name) || attr_name == 'id' - generated_external_attribute_methods.send(attr_name, attributes[attr_name], attributes, cache, attr_name) - end - elsif !attribute_methods_generated? - # If we haven't generated the caster methods yet, do that and - # then try again - define_attribute_methods - type_cast_attribute(attr_name, attributes, cache) - else - # If we get here, the attribute has no associated DB column, so - # just return it verbatim. - attributes[attr_name] - end - end - protected + # We want to generate the methods via module_eval rather than define_method, # because define_method is slower on dispatch and uses more memory (because it # creates a closure). @@ -67,19 +40,9 @@ module ActiveRecord # we first define with the __temp__ identifier, and then use alias method to # rename it to what we want. def define_method_attribute(attr_name) - cast_code = attribute_cast_code(attr_name) - generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 def __temp__ - #{internal_attribute_access_code(attr_name, cast_code)} - end - alias_method '#{attr_name}', :__temp__ - undef_method :__temp__ - STR - - generated_external_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 - def __temp__(v, attributes, attributes_cache, attr_name) - #{external_attribute_access_code(attr_name, cast_code)} + read_attribute('#{attr_name}') { |n| missing_attribute(n, caller) } end alias_method '#{attr_name}', :__temp__ undef_method :__temp__ @@ -87,6 +50,7 @@ module ActiveRecord end private + def cacheable_column?(column) if attribute_types_cached_by_default == ATTRIBUTE_TYPES_CACHED_BY_DEFAULT ! serialized_attributes.include? column.name @@ -94,33 +58,19 @@ module ActiveRecord attribute_types_cached_by_default.include?(column.type) end end - - def internal_attribute_access_code(attr_name, cast_code) - "read_attribute('#{attr_name}') { |n| missing_attribute(n, caller) }" - end - - def external_attribute_access_code(attr_name, cast_code) - access_code = "v && #{cast_code}" - - if cache_attribute?(attr_name) - access_code = "attributes_cache[attr_name] ||= (#{access_code})" - end - - access_code - end - - def attribute_cast_code(attr_name) - columns_hash[attr_name].type_cast_code('v') - end end # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example, # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). def read_attribute(attr_name) # If it's cached, just return it - @attributes_cache.fetch(attr_name) { |name| + @attributes_cache.fetch(attr_name.to_s) { |name| column = @columns_hash.fetch(name) { - return self.class.type_cast_attribute(name, @attributes, @attributes_cache) + return @attributes.fetch(name) { + if name == 'id' && self.class.primary_key != name + read_attribute(self.class.primary_key) + end + } } value = @attributes.fetch(name) { diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb index 20372c5c18..ac31b636db 100644 --- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb +++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb @@ -58,6 +58,7 @@ module ActiveRecord end time = time.in_time_zone rescue nil if time write_attribute(:#{attr_name}, original_time) + #{attr_name}_will_change! @attributes_cache["#{attr_name}"] = time end EOV diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 4bafadc666..3005bef092 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -28,7 +28,7 @@ module ActiveRecord # Association with autosave option defines several callbacks on your # model (before_save, after_create, after_update). Please note that # callbacks are executed in the order they were defined in - # model. You should avoid modyfing the association content, before + # model. You should avoid modifying the association content, before # autosave callbacks are executed. Placing your callbacks after # associations is usually a good practice. # @@ -191,23 +191,21 @@ module ActiveRecord # Doesn't use after_save as that would save associations added in after_create/after_update twice after_create save_method after_update save_method + elsif reflection.macro == :has_one + define_method(save_method) { save_has_one_association(reflection) } + # Configures two callbacks instead of a single after_save so that + # the model may rely on their execution order relative to its + # own callbacks. + # + # For example, given that after_creates run before after_saves, if + # we configured instead an after_save there would be no way to fire + # a custom after_create callback after the child association gets + # created. + after_create save_method + after_update save_method else - if reflection.macro == :has_one - define_method(save_method) { save_has_one_association(reflection) } - # Configures two callbacks instead of a single after_save so that - # the model may rely on their execution order relative to its - # own callbacks. - # - # For example, given that after_creates run before after_saves, if - # we configured instead an after_save there would be no way to fire - # a custom after_create callback after the child association gets - # created. - after_create save_method - after_update save_method - else - define_non_cyclic_method(save_method, reflection) { save_belongs_to_association(reflection) } - before_save save_method - end + define_non_cyclic_method(save_method, reflection) { save_belongs_to_association(reflection) } + before_save save_method end end @@ -330,14 +328,14 @@ module ActiveRecord autosave = reflection.options[:autosave] if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave) - begin + records_to_destroy = [] records.each do |record| next if record.destroyed? saved = true if autosave && record.marked_for_destruction? - association.proxy.destroy(record) + records_to_destroy << record elsif autosave != false && (@new_record_before_save || record.new_record?) if autosave saved = association.insert_record(record, false) @@ -350,11 +348,10 @@ module ActiveRecord raise ActiveRecord::Rollback unless saved end - rescue - records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled? - raise - end + records_to_destroy.each do |record| + association.proxy.destroy(record) + end end # reconstruct the scope now that we know the owner's id diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index d4d0220fb7..d25a821688 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -201,6 +201,9 @@ module ActiveRecord #:nodoc: # # Now 'Bob' exist and is an 'admin' # User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true } # + # Adding an exclamation point (!) on to the end of <tt>find_or_create_by_</tt> will + # raise an <tt>ActiveRecord::RecordInvalid</tt> error if the new record is invalid. + # # Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without # saving it first. Protected attributes won't be set unless they are given in a block. # diff --git a/activerecord/lib/active_record/coders/yaml_column.rb b/activerecord/lib/active_record/coders/yaml_column.rb index 77af540c3e..66a0c83c41 100644 --- a/activerecord/lib/active_record/coders/yaml_column.rb +++ b/activerecord/lib/active_record/coders/yaml_column.rb @@ -15,7 +15,13 @@ module ActiveRecord end def dump(obj) - YAML.dump(obj) unless obj.nil? + return if obj.nil? + + unless obj.is_a?(object_class) + raise SerializationTypeMismatch, + "Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}" + end + YAML.dump obj end def load(yaml) 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 37a9d216df..561e48d52e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb @@ -128,10 +128,11 @@ module ActiveRecord @reserved_connections[current_connection_id] ||= checkout end - # Check to see if there is an active connection in this connection - # pool. + # Is there an open connection that is being used for the current thread? def active_connection? - active_connections.any? + @reserved_connections.fetch(current_connection_id) { + return false + }.in_use? end # Signal that the thread is finished with the current connection. @@ -185,8 +186,9 @@ module ActiveRecord end def clear_stale_cached_connections! # :nodoc: + reap end - deprecate :clear_stale_cached_connections! + deprecate :clear_stale_cached_connections! => "Please use #reap instead" # Check-out a database connection from the pool, indicating that you want # to use it. You should call #checkin when you no longer need this. @@ -233,6 +235,8 @@ module ActiveRecord conn.run_callbacks :checkin do conn.expire end + + release conn end end @@ -244,10 +248,7 @@ module ActiveRecord # FIXME: we might want to store the key on the connection so that removing # from the reserved hash will be a little easier. - thread_id = @reserved_connections.keys.find { |k| - @reserved_connections[k] == conn - } - @reserved_connections.delete thread_id if thread_id + release conn end end @@ -265,6 +266,20 @@ module ActiveRecord private + def release(conn) + thread_id = nil + + if @reserved_connections[current_connection_id] == conn + thread_id = current_connection_id + else + thread_id = @reserved_connections.keys.find { |k| + @reserved_connections[k] == conn + } + end + + @reserved_connections.delete thread_id if thread_id + end + def new_connection ActiveRecord::Base.send(spec.adapter_method, spec.config) end @@ -288,10 +303,6 @@ module ActiveRecord end c end - - def active_connections - @connections.find_all { |c| c.in_use? } - end end # ConnectionHandler is a collection of ConnectionPool objects. It is used diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index db99c3fbef..e919068909 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -20,14 +20,14 @@ module ActiveRecord # Returns a record hash with the column names as keys and column values # as values. - def select_one(arel, name = nil) - result = select_all(arel, name) + def select_one(arel, name = nil, binds = []) + result = select_all(arel, name, binds) result.first if result end # Returns a single value from a record - def select_value(arel, name = nil) - if result = select_one(arel, name) + def select_value(arel, name = nil, binds = []) + if result = select_one(arel, name, binds) result.values.first end end @@ -57,21 +57,21 @@ module ActiveRecord end # Executes insert +sql+ statement in the context of this connection using - # +binds+ as the bind substitutes. +name+ is the logged along with + # +binds+ as the bind substitutes. +name+ is logged along with # the executed +sql+ statement. def exec_insert(sql, name, binds) exec_query(sql, name, binds) end # Executes delete +sql+ statement in the context of this connection using - # +binds+ as the bind substitutes. +name+ is the logged along with + # +binds+ as the bind substitutes. +name+ is logged along with # the executed +sql+ statement. def exec_delete(sql, name, binds) exec_query(sql, name, binds) end # Executes update +sql+ statement in the context of this connection using - # +binds+ as the bind substitutes. +name+ is the logged along with + # +binds+ as the bind substitutes. +name+ is logged along with # the executed +sql+ statement. def exec_update(sql, name, binds) exec_query(sql, name, binds) 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 ad2e8634eb..7ee8f40631 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -6,6 +6,9 @@ require 'bigdecimal/util' module ActiveRecord module ConnectionAdapters #:nodoc: + # Abstract representation of an index definition on a table. Instances of + # this type are typically created and returned by methods in database + # adapters. e.g. ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter#indexes class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :orders, :where) #:nodoc: end 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 6bac05191d..8b9e830040 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -174,7 +174,7 @@ module ActiveRecord end # Creates a new join table with the name created using the lexical order of the first two - # arguments. These arguments can be be a String or a Symbol. + # arguments. These arguments can be a String or a Symbol. # # # Creates a table called 'assemblies_parts' with no id. # create_join_table(:assemblies, :parts) @@ -539,8 +539,8 @@ module ActiveRecord # ===== Examples # add_timestamps(:suppliers) def add_timestamps(table_name) - add_column table_name, :created_at, :datetime, :null => false - add_column table_name, :updated_at, :datetime, :null => false + add_column table_name, :created_at, :datetime + add_column table_name, :updated_at, :datetime end # Removes the timestamp columns (created_at and updated_at) from the table definition. diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index c6af682d28..1d713e472b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -57,7 +57,7 @@ module ActiveRecord define_callbacks :checkout, :checkin attr_accessor :visitor, :pool - attr_reader :schema_cache, :last_use, :in_use + attr_reader :schema_cache, :last_use, :in_use, :logger alias :in_use? :in_use def initialize(connection, logger = nil, pool = nil) #:nodoc: @@ -86,6 +86,11 @@ module ActiveRecord end end + def schema_cache=(cache) + cache.connection = self + @schema_cache = cache + end + def expire @in_use = false end @@ -279,26 +284,25 @@ module ActiveRecord protected - def log(sql, name = "SQL", binds = []) - @instrumenter.instrument( - "sql.active_record", - :sql => sql, - :name => name, - :connection_id => object_id, - :binds => binds) { yield } - rescue Exception => e - message = "#{e.class.name}: #{e.message}: #{sql}" - @logger.debug message if @logger - exception = translate_exception(e, message) - exception.set_backtrace e.backtrace - raise exception - end - - def translate_exception(e, message) - # override in derived class - ActiveRecord::StatementInvalid.new(message) - end - + def log(sql, name = "SQL", binds = []) + @instrumenter.instrument( + "sql.active_record", + :sql => sql, + :name => name, + :connection_id => object_id, + :binds => binds) { yield } + rescue Exception => e + message = "#{e.class.name}: #{e.message}: #{sql}" + @logger.error message if @logger + exception = translate_exception(e, message) + exception.set_backtrace e.backtrace + raise exception + end + + def translate_exception(e, message) + # override in derived class + ActiveRecord::StatementInvalid.new(message) + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index eec7efadc2..64f922b7ad 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -375,7 +375,7 @@ module ActiveRecord def tables(name = nil, database = nil, like = nil) #:nodoc: sql = "SHOW TABLES " - sql << "IN #{database} " if database + sql << "IN #{quote_table_name(database)} " if database sql << "LIKE #{quote(like)}" if like execute_and_free(sql, 'SCHEMA') do |result| @@ -484,15 +484,26 @@ module ActiveRecord # Maps logical Rails types to MySQL-specific data types. def type_to_sql(type, limit = nil, precision = nil, scale = nil) - return super unless type.to_s == 'integer' - - case limit - when 1; 'tinyint' - when 2; 'smallint' - when 3; 'mediumint' - when nil, 4, 11; 'int(11)' # compatibility with MySQL default - when 5..8; 'bigint' - else raise(ActiveRecordError, "No integer type has byte size #{limit}") + case type.to_s + when 'integer' + case limit + when 1; 'tinyint' + when 2; 'smallint' + when 3; 'mediumint' + when nil, 4, 11; 'int(11)' # compatibility with MySQL default + when 5..8; 'bigint' + else raise(ActiveRecordError, "No integer type has byte size #{limit}") + end + when 'text' + case limit + when 0..0xff; 'tinytext' + when nil, 0x100..0xffff; 'text' + when 0x10000..0xffffff; 'mediumtext' + when 0x1000000..0xffffffff; 'longtext' + else raise(ActiveRecordError, "No text type has character length #{limit}") + end + else + super end end @@ -515,7 +526,7 @@ module ActiveRecord execute_and_free("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA') do |result| create_table = each_hash(result).first[:"Create Table"] if create_table.to_s =~ /PRIMARY KEY\s+\((.+)\)/ - keys = $1.split(",").map { |key| key.gsub(/[`"]/, "") } + keys = $1.split(",").map { |key| key.delete('`"') } keys.length == 1 ? [keys.first, nil] : nil else nil diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 78e54c4c9b..b7e1513422 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -1,4 +1,5 @@ require 'set' +require 'active_support/deprecation' module ActiveRecord # :stopdoc: @@ -107,6 +108,9 @@ module ActiveRecord end def type_cast_code(var_name) + ActiveSupport::Deprecation.warn("Column#type_cast_code is deprecated in favor of" \ + "using Column#type_cast only, and it is going to be removed in future Rails versions.") + klass = self.class.name case type diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index c675b64a26..10a178e369 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -978,6 +978,27 @@ module ActiveRecord end_sql end + # Returns an array of schema names. + def schema_names + query(<<-SQL).flatten + SELECT nspname + FROM pg_namespace + WHERE nspname !~ '^pg_.*' + AND nspname NOT IN ('information_schema') + ORDER by nspname; + SQL + end + + # Creates a schema for the given schema name. + def create_schema schema_name + execute "CREATE SCHEMA #{schema_name}" + end + + # Drops the schema for the given schema name. + def drop_schema schema_name + execute "DROP SCHEMA #{schema_name} CASCADE" + end + # Sets the schema search path to a string of comma-separated schema names. # Names beginning with $ have to be quoted (e.g. $user => '$user'). # See: http://www.postgresql.org/docs/current/static/ddl-schemas.html @@ -985,7 +1006,7 @@ module ActiveRecord # This should be not be called manually but set in database.yml. def schema_search_path=(schema_csv) if schema_csv - execute "SET search_path TO #{schema_csv}" + execute("SET search_path TO #{schema_csv}", 'SCHEMA') @schema_search_path = schema_csv end end @@ -1189,7 +1210,7 @@ module ActiveRecord # Construct a clean list of column names from the ORDER BY clause, removing # any ASC/DESC modifiers - order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*/i, '') } + order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') } order_columns.delete_if { |c| c.blank? } order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" } diff --git a/activerecord/lib/active_record/connection_adapters/schema_cache.rb b/activerecord/lib/active_record/connection_adapters/schema_cache.rb index 962718da56..aad1f9a7ef 100644 --- a/activerecord/lib/active_record/connection_adapters/schema_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/schema_cache.rb @@ -1,26 +1,17 @@ module ActiveRecord module ConnectionAdapters class SchemaCache - attr_reader :columns, :columns_hash, :primary_keys, :tables - attr_reader :connection + attr_reader :columns, :columns_hash, :primary_keys, :tables, :version + attr_accessor :connection def initialize(conn) @connection = conn - @tables = {} - @columns = Hash.new do |h, table_name| - h[table_name] = conn.columns(table_name) - end - - @columns_hash = Hash.new do |h, table_name| - h[table_name] = Hash[columns[table_name].map { |col| - [col.name, col] - }] - end - - @primary_keys = Hash.new do |h, table_name| - h[table_name] = table_exists?(table_name) ? conn.primary_key(table_name) : nil - end + @columns = {} + @columns_hash = {} + @primary_keys = {} + @tables = {} + prepare_default_proc end # A cached lookup for table existence. @@ -30,12 +21,22 @@ module ActiveRecord @tables[name] = connection.table_exists?(name) end + # Add internal cache for table with +table_name+. + def add(table_name) + if table_exists?(table_name) + @primary_keys[table_name] + @columns[table_name] + @columns_hash[table_name] + end + end + # Clears out internal caches def clear! @columns.clear @columns_hash.clear @primary_keys.clear @tables.clear + @version = nil end # Clear out internal caches for table with +table_name+. @@ -45,6 +46,37 @@ module ActiveRecord @primary_keys.delete table_name @tables.delete table_name end + + def marshal_dump + # if we get current version during initialization, it happens stack over flow. + @version = ActiveRecord::Migrator.current_version + [@version] + [:@columns, :@columns_hash, :@primary_keys, :@tables].map do |val| + self.instance_variable_get(val).inject({}) { |h, v| h[v[0]] = v[1]; h } + end + end + + def marshal_load(array) + @version, @columns, @columns_hash, @primary_keys, @tables = array + prepare_default_proc + end + + private + + def prepare_default_proc + @columns.default_proc = Proc.new do |h, table_name| + h[table_name] = connection.columns(table_name) + end + + @columns_hash.default_proc = Proc.new do |h, table_name| + h[table_name] = Hash[columns[table_name].map { |col| + [col.name, col] + }] + end + + @primary_keys.default_proc = Proc.new do |h, table_name| + h[table_name] = table_exists?(table_name) ? connection.primary_key(table_name) : nil + end + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index c85c4c6e51..3d8dfab05c 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -202,7 +202,7 @@ module ActiveRecord value = super if column.type == :string && value.encoding == Encoding::ASCII_8BIT - @logger.error "Binary data inserted for `string` type on column `#{column.name}`" + logger.error "Binary data inserted for `string` type on column `#{column.name}`" if logger value.encode! 'utf-8' end value diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 3f90d25962..76c424e8b4 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -1,4 +1,5 @@ require 'active_support/concern' +require 'active_support/core_ext/hash/indifferent_access' require 'thread' module ActiveRecord @@ -130,7 +131,7 @@ module ActiveRecord end def arel_engine - @arel_engine ||= connection_handler.connection_pools[name] ? self : active_record_super.arel_engine + @arel_engine ||= connection_handler.retrieve_connection_pool(self) ? self : active_record_super.arel_engine end private @@ -176,7 +177,7 @@ module ActiveRecord assign_attributes(attributes, options) if attributes yield self if block_given? - run_callbacks :initialize + run_callbacks :initialize if _initialize_callbacks.any? end # Initialize an empty model object from +coder+. +coder+ must contain @@ -193,7 +194,6 @@ module ActiveRecord @attributes = self.class.initialize_attributes(coder['attributes']) @columns_hash = self.class.column_types.merge(coder['column_types'] || {}) - init_internals @new_record = false @@ -327,6 +327,11 @@ module ActiveRecord "#<#{self.class} #{inspection}>" end + # Returns a hash of the given methods with their names as keys and returned values as values. + def slice(*methods) + Hash[methods.map { |method| [method, public_send(method)] }].with_indifferent_access + end + private # Under Ruby 1.9, Array#flatten will call #to_ary (recursively) on each of the elements diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index 224f5276eb..f52979ebd9 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -69,9 +69,7 @@ module ActiveRecord "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}" end - IdentityMap.remove_by_id(symbolized_base_class, id) if IdentityMap.enabled? - - update_all(updates.join(', '), primary_key => id ) + update_all(updates.join(', '), primary_key => id) end # Increment a number field by one, usually representing a count. diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb index 38dbbef5fc..0473d6aafc 100644 --- a/activerecord/lib/active_record/dynamic_finder_match.rb +++ b/activerecord/lib/active_record/dynamic_finder_match.rb @@ -7,7 +7,7 @@ module ActiveRecord class DynamicFinderMatch def self.match(method) method = method.to_s - klass = [FindBy, FindByBang, FindOrInitializeCreateBy].find do |_klass| + klass = klasses.find do |_klass| _klass.matches?(method) end klass.new(method) if klass @@ -17,6 +17,10 @@ module ActiveRecord method =~ self::METHOD_PATTERN end + def self.klasses + [FindBy, FindByBang, FindOrInitializeCreateBy, FindOrCreateByBang] + end + def initialize(method) @finder = :first @instantiator = nil @@ -47,6 +51,14 @@ module ActiveRecord arguments.size >= @attribute_names.size end + def save_record? + @instantiator == :create + end + + def save_method + bang? ? :save! : :save + end + private def initialize_from_match_data(match_data) @@ -81,4 +93,16 @@ module ActiveRecord arguments.size == 1 && arguments.first.is_a?(Hash) || super end end + + class FindOrCreateByBang < DynamicFinderMatch + METHOD_PATTERN = /^find_or_create_by_([_a-zA-Z]\w*)\!$/ + + def initialize_from_match_data(match_data) + @instantiator = :create + end + + def bang? + true + end + end end diff --git a/activerecord/lib/active_record/dynamic_matchers.rb b/activerecord/lib/active_record/dynamic_matchers.rb index 60ce3dd4f1..e35b1c91a0 100644 --- a/activerecord/lib/active_record/dynamic_matchers.rb +++ b/activerecord/lib/active_record/dynamic_matchers.rb @@ -1,13 +1,10 @@ module ActiveRecord module DynamicMatchers def respond_to?(method_id, include_private = false) - if match = DynamicFinderMatch.match(method_id) - return true if all_attributes_exists?(match.attribute_names) - elsif match = DynamicScopeMatch.match(method_id) - return true if all_attributes_exists?(match.attribute_names) - end + match = find_dynamic_match(method_id) + valid_match = match && all_attributes_exists?(match.attribute_names) - super + valid_match || super end private @@ -22,22 +19,18 @@ module ActiveRecord # Each dynamic finder using <tt>scoped_by_*</tt> is also defined in the class after it # is first invoked, so that future attempts to use it do not run through method_missing. def method_missing(method_id, *arguments, &block) - if match = (DynamicFinderMatch.match(method_id) || DynamicScopeMatch.match(method_id)) + if match = find_dynamic_match(method_id) attribute_names = match.attribute_names super unless all_attributes_exists?(attribute_names) + unless match.valid_arguments?(arguments) method_trace = "#{__FILE__}:#{__LINE__}:in `#{method_id}'" backtrace = [method_trace] + caller raise ArgumentError, "wrong number of arguments (#{arguments.size} for #{attribute_names.size})", backtrace end + if match.respond_to?(:scope?) && match.scope? - self.class_eval <<-METHOD, __FILE__, __LINE__ + 1 - def self.#{method_id}(*args) # def self.scoped_by_user_name_and_password(*args) - attributes = Hash[[:#{attribute_names.join(',:')}].zip(args)] # attributes = Hash[[:user_name, :password].zip(args)] - # - scoped(:conditions => attributes) # scoped(:conditions => attributes) - end # end - METHOD + define_scope_method(method_id, attribute_names) send(method_id, *arguments) elsif match.finder? options = arguments.extract_options! @@ -51,17 +44,30 @@ module ActiveRecord end end + def define_scope_method(method_id, attribute_names) #:nodoc + self.class_eval <<-METHOD, __FILE__, __LINE__ + 1 + def self.#{method_id}(*args) # def self.scoped_by_user_name_and_password(*args) + conditions = Hash[[:#{attribute_names.join(',:')}].zip(args)] # conditions = Hash[[:user_name, :password].zip(args)] + where(conditions) # where(conditions) + end # end + METHOD + end + + def find_dynamic_match(method_id) #:nodoc: + DynamicFinderMatch.match(method_id) || DynamicScopeMatch.match(method_id) + end + # Similar in purpose to +expand_hash_conditions_for_aggregates+. def expand_attribute_names_for_aggregates(attribute_names) - attribute_names.map { |attribute_name| - unless (aggregation = reflect_on_aggregation(attribute_name.to_sym)).nil? + attribute_names.map do |attribute_name| + if aggregation = reflect_on_aggregation(attribute_name.to_sym) aggregate_mapping(aggregation).map do |field_attr, _| field_attr.to_sym end else attribute_name.to_sym end - }.flatten + end.flatten end def all_attributes_exists?(attribute_names) @@ -73,7 +79,5 @@ module ActiveRecord mapping = reflection.options[:mapping] || [reflection.name, reflection.name] mapping.first.is_a?(Array) ? mapping : [mapping] end - - end end diff --git a/activerecord/lib/active_record/dynamic_scope_match.rb b/activerecord/lib/active_record/dynamic_scope_match.rb index a502155aac..6c043d29c4 100644 --- a/activerecord/lib/active_record/dynamic_scope_match.rb +++ b/activerecord/lib/active_record/dynamic_scope_match.rb @@ -7,9 +7,12 @@ module ActiveRecord # chain more <tt>scoped_by_* </tt> methods after the other. It acts like a named # scope except that it's dynamic. class DynamicScopeMatch + METHOD_PATTERN = /^scoped_by_([_a-zA-Z]\w*)$/ + def self.match(method) - return unless method.to_s =~ /^scoped_by_([_a-zA-Z]\w*)$/ - new(true, $1 && $1.split('_and_')) + if method.to_s =~ METHOD_PATTERN + new(true, $1 && $1.split('_and_')) + end end def initialize(scope, attribute_names) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index b82d5b5621..9796b0a321 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -4,16 +4,12 @@ require 'zlib' require 'active_support/dependencies' require 'active_support/core_ext/object/blank' require 'active_record/fixtures/file' +require 'active_record/errors' -if defined? ActiveRecord +module ActiveRecord class FixtureClassNotFound < ActiveRecord::ActiveRecordError #:nodoc: end -else - class FixtureClassNotFound < StandardError #:nodoc: - end -end -module ActiveRecord # \Fixtures are a way of organizing data that you want to test against; in short, sample data. # # They are stored in YAML files, one file per model, which are placed in the directory @@ -796,9 +792,7 @@ module ActiveRecord @fixture_cache[fixture_name].delete(fixture) if force_reload if @loaded_fixtures[fixture_name][fixture.to_s] - ActiveRecord::IdentityMap.without do - @fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find - end + @fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find else raise StandardError, "No entry named '#{fixture}' found for fixture collection '#{fixture_name}'" end diff --git a/activerecord/lib/active_record/identity_map.rb b/activerecord/lib/active_record/identity_map.rb deleted file mode 100644 index d9777bb2f6..0000000000 --- a/activerecord/lib/active_record/identity_map.rb +++ /dev/null @@ -1,144 +0,0 @@ -module ActiveRecord - # = Active Record Identity Map - # - # Ensures that each object gets loaded only once by keeping every loaded - # object in a map. Looks up objects using the map when referring to them. - # - # More information on Identity Map pattern: - # http://www.martinfowler.com/eaaCatalog/identityMap.html - # - # == Configuration - # - # In order to enable IdentityMap, set <tt>config.active_record.identity_map = true</tt> - # in your <tt>config/application.rb</tt> file. - # - # IdentityMap is disabled by default and still in development (i.e. use it with care). - # - # == Associations - # - # Active Record Identity Map does not track associations yet. For example: - # - # comment = @post.comments.first - # comment.post = nil - # @post.comments.include?(comment) #=> true - # - # Ideally, the example above would return false, removing the comment object from the - # post association when the association is nullified. This may cause side effects, as - # in the situation below, if Identity Map is enabled: - # - # Post.has_many :comments, :dependent => :destroy - # - # comment = @post.comments.first - # comment.post = nil - # comment.save - # Post.destroy(@post.id) - # - # Without using Identity Map, the code above will destroy the @post object leaving - # the comment object intact. However, once we enable Identity Map, the post loaded - # by Post.destroy is exactly the same object as the object @post. As the object @post - # still has the comment object in @post.comments, once Identity Map is enabled, the - # comment object will be accidently removed. - # - # This inconsistency is meant to be fixed in future Rails releases. - # - module IdentityMap - - class << self - def enabled=(flag) - Thread.current[:identity_map_enabled] = flag - end - - def enabled - Thread.current[:identity_map_enabled] - end - alias enabled? enabled - - def repository - Thread.current[:identity_map] ||= Hash.new { |h,k| h[k] = {} } - end - - def use - old, self.enabled = enabled, true - - yield if block_given? - ensure - self.enabled = old - clear - end - - def without - old, self.enabled = enabled, false - - yield if block_given? - ensure - self.enabled = old - end - - def get(klass, primary_key) - record = repository[klass.symbolized_sti_name][primary_key] - - if record.is_a?(klass) - ActiveSupport::Notifications.instrument("identity.active_record", - :line => "From Identity Map (id: #{primary_key})", - :name => "#{klass} Loaded", - :connection_id => object_id) - - record - else - nil - end - end - - def add(record) - repository[record.class.symbolized_sti_name][record.id] = record - end - - def remove(record) - repository[record.class.symbolized_sti_name].delete(record.id) - end - - def remove_by_id(symbolized_sti_name, id) - repository[symbolized_sti_name].delete(id) - end - - def clear - repository.clear - end - end - - # Reinitialize an Identity Map model object from +coder+. - # +coder+ must contain the attributes necessary for initializing an empty - # model object. - def reinit_with(coder) - @attributes_cache = {} - dirty = @changed_attributes.keys - attributes = self.class.initialize_attributes(coder['attributes'].except(*dirty)) - @attributes.update(attributes) - @changed_attributes.update(coder['attributes'].slice(*dirty)) - @changed_attributes.delete_if{|k,v| v.eql? @attributes[k]} - - run_callbacks :find - - self - end - - class Middleware - def initialize(app) - @app = app - end - - def call(env) - enabled = IdentityMap.enabled - IdentityMap.enabled = true - - response = @app.call(env) - response[2] = Rack::BodyProxy.new(response[2]) do - IdentityMap.enabled = enabled - IdentityMap.clear - end - - response - end - end - end -end diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 2c766411a0..46d253b0a7 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -48,6 +48,20 @@ module ActiveRecord end # Set this to true if this is an abstract class (see <tt>abstract_class?</tt>). + # If you are using inheritance with ActiveRecord and don't want child classes + # to utilize the implied STI table name of the parent class, this will need to be true. + # For example, given the following: + # + # class SuperClass < ActiveRecord::Base + # self.abstract_class = true + # end + # class Child < SuperClass + # self.table_name = 'the_table_i_really_want' + # end + # + # + # <tt>self.abstract_class = true</tt> is required to make <tt>Child<.find,.create, or any Arel method></tt> use <tt>the_table_i_really_want</tt> instead of a table called <tt>super_classes</tt> + # attr_accessor :abstract_class # Returns whether this class is an abstract class or not. @@ -63,26 +77,9 @@ module ActiveRecord # single-table inheritance model that makes it possible to create # objects of different types from the same table. def instantiate(record, column_types = {}) - sti_class = find_sti_class(record[inheritance_column]) - record_id = sti_class.primary_key && record[sti_class.primary_key] - - if ActiveRecord::IdentityMap.enabled? && record_id - if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number? - record_id = record_id.to_i - end - if instance = IdentityMap.get(sti_class, record_id) - instance.reinit_with('attributes' => record) - else - instance = sti_class.allocate.init_with('attributes' => record) - IdentityMap.add(instance) - end - else - column_types = sti_class.decorate_columns(column_types) - instance = sti_class.allocate.init_with('attributes' => record, - 'column_types' => column_types) - end - - instance + sti_class = find_sti_class(record[inheritance_column]) + column_types = sti_class.decorate_columns(column_types) + sti_class.allocate.init_with('attributes' => record, 'column_types' => column_types) end # For internal use. diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index 4d73cdd37a..a3412582fa 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -84,7 +84,7 @@ module ActiveRecord relation.table[self.class.primary_key].eq(id).and( relation.table[lock_col].eq(quote_value(previous_lock_value)) ) - ).arel.compile_update(arel_attributes_values(false, false, attribute_names)) + ).arel.compile_update(arel_attributes_with_values_for_update(attribute_names)) affected_rows = connection.update stmt @@ -101,24 +101,29 @@ module ActiveRecord end end - def destroy #:nodoc: - return super unless locking_enabled? + def destroy_row + affected_rows = super - if persisted? - table = self.class.arel_table - lock_col = self.class.locking_column - predicate = table[self.class.primary_key].eq(id). - and(table[lock_col].eq(send(lock_col).to_i)) + if locking_enabled? && affected_rows != 1 + raise ActiveRecord::StaleObjectError.new(self, "destroy") + end - affected_rows = self.class.unscoped.where(predicate).delete_all + affected_rows + end - unless affected_rows == 1 - raise ActiveRecord::StaleObjectError.new(self, "destroy") - end + def relation_for_destroy + relation = super + + if locking_enabled? + column_name = self.class.locking_column + column = self.class.columns_hash[column_name] + substitute = connection.substitute_at(column, relation.bind_values.length) + + relation = relation.where(self.class.arel_table[column_name].eq(substitute)) + relation.bind_values << [column, self[column_name].to_i] end - @destroyed = true - freeze + relation end module ClassMethods @@ -133,8 +138,7 @@ module ActiveRecord # Set the column to use for optimistic locking. Defaults to +lock_version+. def locking_column=(value) - @original_locking_column = @locking_column if defined?(@locking_column) - @locking_column = value.to_s + @locking_column = value.to_s end # The version column used for optimistic locking. Defaults to +lock_version+. diff --git a/activerecord/lib/active_record/model.rb b/activerecord/lib/active_record/model.rb index 86de5ab2fa..105d1e0e2b 100644 --- a/activerecord/lib/active_record/model.rb +++ b/activerecord/lib/active_record/model.rb @@ -60,7 +60,6 @@ module ActiveRecord include AttributeMethods include Callbacks, ActiveModel::Observing, Timestamp include Associations - include IdentityMap include ActiveModel::SecurePassword include AutosaveAssociation, NestedAttributes include Aggregations, Transactions, Reflection, Serialization, Store diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index b8764217d3..c85d590ce1 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -114,11 +114,18 @@ module ActiveRecord # You can also just define your own <tt>self.table_name</tt> method; see # the documentation for ActiveRecord::Base#table_name. def table_name=(value) - @original_table_name = @table_name if defined?(@table_name) - @table_name = value && value.to_s - @quoted_table_name = nil - @arel_table = nil - @relation = Relation.new(self, arel_table) + value = value && value.to_s + + if defined?(@table_name) + return if value == @table_name + reset_column_information if connected? + end + + @table_name = value + @quoted_table_name = nil + @arel_table = nil + @sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name + @relation = Relation.new(self, arel_table) end # Returns a quoted version of the table name, used to construct SQL statements. @@ -152,8 +159,8 @@ module ActiveRecord # Sets the value of inheritance_column def inheritance_column=(value) - @original_inheritance_column = inheritance_column - @inheritance_column = value.to_s + @inheritance_column = value.to_s + @explicit_inheritance_column = true end def sequence_name @@ -165,7 +172,8 @@ module ActiveRecord end def reset_sequence_name #:nodoc: - self.sequence_name = connection.default_sequence_name(table_name, primary_key) + @sequence_name = connection.default_sequence_name(table_name, primary_key) + @explicit_sequence_name = false end # Sets the name of the sequence to use when generating ids to the given @@ -183,8 +191,8 @@ module ActiveRecord # self.sequence_name = "projectseq" # default would have been "project_seq" # end def sequence_name=(value) - @original_sequence_name = @sequence_name if defined?(@sequence_name) @sequence_name = value.to_s + @explicit_sequence_name = true end # Indicates whether the table associated with this class exists @@ -296,7 +304,7 @@ module ActiveRecord @column_types = nil @content_columns = nil @dynamic_methods_hash = nil - @inheritance_column = nil + @inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column @relation = nil end diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 9e21039c4f..32a1dae6bc 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -248,10 +248,18 @@ module ActiveRecord # exception is raised. If omitted, any number associations can be processed. # Note that the :limit option is only applicable to one-to-many associations. # [:update_only] - # Allows you to specify that an existing record may only be updated. - # A new record may only be created when there is no existing record. - # This option only works for one-to-one associations and is ignored for - # collection associations. This option is off by default. + # For a one-to-one association, this option allows you to specify how + # nested attributes are to be used when an associated record already + # exists. In general, an existing record may either be updated with the + # new set of attribute values or be replaced by a wholly new record + # containing those values. By default the :update_only option is +false+ + # and the nested attributes are used to update the existing record only + # if they include the record's <tt>:id</tt> value. Otherwise a new + # record will be instantiated and used to replace the existing one. + # However if the :update_only option is +true+, the nested attributes + # are used to update the record's attributes always, regardless of + # whether the <tt>:id</tt> is present. The option is ignored for collection + # associations. # # Examples: # # creates avatar_attributes= @@ -280,7 +288,7 @@ module ActiveRecord # def pirate_attributes=(attributes) # assign_nested_attributes_for_one_to_one_association(:pirate, attributes, mass_assignment_options) # end - class_eval <<-eoruby, __FILE__, __LINE__ + 1 + generated_feature_methods.module_eval <<-eoruby, __FILE__, __LINE__ + 1 if method_defined?(:#{association_name}_attributes=) remove_method(:#{association_name}_attributes=) end @@ -312,10 +320,13 @@ module ActiveRecord # Assigns the given attributes to the association. # - # If update_only is false and the given attributes include an <tt>:id</tt> - # that matches the existing record's id, then the existing record will be - # modified. If update_only is true, a new record is only created when no - # object exists. Otherwise a new record will be built. + # If an associated record does not yet exist, one will be instantiated. If + # an associated record already exists, the method's behavior depends on + # the value of the update_only option. If update_only is +false+ and the + # given attributes include an <tt>:id</tt> that matches the existing record's + # id, then the existing record will be modified. If no <tt>:id</tt> is provided + # it will be replaced with a new record. If update_only is +true+ the existing + # record will be modified regardless of whether an <tt>:id</tt> is provided. # # If the given attributes include a matching <tt>:id</tt> attribute, or # update_only is true, and a <tt>:_destroy</tt> key set to a truthy value, diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 9bc046c775..bb504ae90f 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -115,10 +115,7 @@ module ActiveRecord # callbacks, Observer methods, or any <tt>:dependent</tt> association # options, use <tt>#destroy</tt>. def delete - if persisted? - self.class.delete(id) - IdentityMap.remove(self) if IdentityMap.enabled? - end + self.class.delete(id) if persisted? @destroyed = true freeze end @@ -127,20 +124,7 @@ module ActiveRecord # that no changes should be made (since they can't be persisted). def destroy destroy_associations - - if persisted? - IdentityMap.remove(self) if IdentityMap.enabled? - pk = self.class.primary_key - column = self.class.columns_hash[pk] - substitute = connection.substitute_at(column, 0) - - relation = self.class.unscoped.where( - self.class.arel_table[pk].eq(substitute)) - - relation.bind_values = [[column, id]] - relation.delete_all - end - + destroy_row if persisted? @destroyed = true freeze end @@ -176,7 +160,7 @@ module ActiveRecord # def update_attribute(name, value) name = name.to_s - raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name) + verify_readonly_attribute(name) send("#{name}=", value) save(:validate => false) end @@ -191,7 +175,7 @@ module ActiveRecord # attribute is marked as readonly. def update_column(name, value) name = name.to_s - raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name) + verify_readonly_attribute(name) raise ActiveRecordError, "can not update on a new record object" unless persisted? raw_write_attribute(name, value) self.class.update_all({ name => value }, self.class.primary_key => id) == 1 @@ -284,11 +268,9 @@ module ActiveRecord clear_aggregation_cache clear_association_cache - IdentityMap.without do - fresh_object = self.class.unscoped { self.class.find(id, options) } - @attributes.update(fresh_object.instance_variable_get('@attributes')) - @columns_hash = fresh_object.instance_variable_get('@columns_hash') - end + fresh_object = self.class.unscoped { self.class.find(id, options) } + @attributes.update(fresh_object.instance_variable_get('@attributes')) + @columns_hash = fresh_object.instance_variable_get('@columns_hash') @attributes_cache = {} self @@ -323,7 +305,8 @@ module ActiveRecord changes = {} attributes.each do |column| - changes[column.to_s] = write_attribute(column.to_s, current_time) + column = column.to_s + changes[column] = write_attribute(column, current_time) end changes[self.class.locking_column] = increment_lock if locking_enabled? @@ -340,6 +323,22 @@ module ActiveRecord def destroy_associations end + def destroy_row + relation_for_destroy.delete_all + end + + def relation_for_destroy + pk = self.class.primary_key + column = self.class.columns_hash[pk] + substitute = connection.substitute_at(column, 0) + + relation = self.class.unscoped.where( + self.class.arel_table[pk].eq(substitute)) + + relation.bind_values = [[column, id]] + relation + end + def create_or_update raise ReadOnlyRecord if readonly? result = new_record? ? create : update @@ -349,7 +348,7 @@ module ActiveRecord # Updates the associated record with values matching those of the instance attributes. # Returns the number of affected rows. def update(attribute_names = @attributes.keys) - attributes_with_values = arel_attributes_values(false, false, attribute_names) + attributes_with_values = arel_attributes_with_values_for_update(attribute_names) return 0 if attributes_with_values.empty? klass = self.class stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id)).arel.compile_update(attributes_with_values) @@ -359,15 +358,17 @@ module ActiveRecord # Creates a record with values matching those of the instance attributes # and returns its id. def create - attributes_values = arel_attributes_values(!id.nil?) + attributes_values = arel_attributes_with_values_for_create(!id.nil?) new_id = self.class.unscoped.insert attributes_values - self.id ||= new_id if self.class.primary_key - IdentityMap.add(self) if IdentityMap.enabled? @new_record = false id end + + def verify_readonly_attribute(name) + raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name) + end end end diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index 0e6fecbc4b..95565b503a 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -5,6 +5,7 @@ module ActiveRecord module Querying delegate :find, :first, :first!, :last, :last!, :all, :exists?, :any?, :many?, :to => :scoped delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :scoped + delegate :find_by, :find_by!, :to => :scoped delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :scoped delegate :find_each, :find_in_batches, :to => :scoped delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins, diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 058dd58efb..eb2769f1ef 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -53,11 +53,6 @@ module ActiveRecord ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger } end - initializer "active_record.identity_map" do |app| - config.app_middleware.insert_after "::ActionDispatch::Callbacks", - "ActiveRecord::IdentityMap::Middleware" if config.active_record.delete(:identity_map) - end - initializer "active_record.set_configs" do |app| ActiveSupport.on_load(:active_record) do if app.config.active_record.delete(:whitelist_attributes) @@ -73,7 +68,9 @@ module ActiveRecord # and then establishes the connection. initializer "active_record.initialize_database" do |app| ActiveSupport.on_load(:active_record) do - self.configurations = app.config.database_configuration + unless ENV['DATABASE_URL'] + self.configurations = app.config.database_configuration + end establish_connection end end @@ -107,7 +104,7 @@ module ActiveRecord config.watchable_files.concat ["#{app.root}/db/schema.rb", "#{app.root}/db/structure.sql"] end - config.after_initialize do + config.after_initialize do |app| ActiveSupport.on_load(:active_record) do ActiveRecord::Base.instantiate_observers @@ -115,6 +112,21 @@ module ActiveRecord ActiveRecord::Base.instantiate_observers end end + + ActiveSupport.on_load(:active_record) do + if app.config.use_schema_cache_dump + filename = File.join(app.config.paths["db"].first, "schema_cache.dump") + if File.file?(filename) + cache = Marshal.load File.binread filename + if cache.version == ActiveRecord::Migrator.current_version + ActiveRecord::Base.connection.schema_cache = cache + else + warn "schema_cache.dump is expired. Current version is #{ActiveRecord::Migrator.current_version}, but cache version is #{cache.version}." + end + end + end + end + end end end diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 14bb0a724e..f26e18b1e0 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -372,6 +372,25 @@ db_namespace = namespace :db do task :load_if_ruby => 'db:create' do db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby end + + namespace :cache do + desc 'Create a db/schema_cache.dump file.' + task :dump => :environment do + con = ActiveRecord::Base.connection + filename = File.join(Rails.application.config.paths["db"].first, "schema_cache.dump") + + con.schema_cache.clear! + con.tables.each { |table| con.schema_cache.add(table) } + open(filename, 'wb') { |f| f.write(Marshal.dump(con.schema_cache)) } + end + + desc 'Clear a db/schema_cache.dump file.' + task :clear => :environment do + filename = File.join(Rails.application.config.paths["db"].first, "schema_cache.dump") + FileUtils.rm(filename) if File.exists?(filename) + end + end + end namespace :structure do @@ -407,6 +426,7 @@ db_namespace = namespace :db do if ActiveRecord::Base.connection.supports_migrations? File.open(filename, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information } end + db_namespace['structure:dump'].reenable end # desc "Recreate the databases from the structure.sql file" @@ -458,7 +478,7 @@ db_namespace = namespace :db do db_namespace["test:load_schema"].invoke when :sql db_namespace["test:load_structure"].invoke - end + end end # desc "Recreate the test database from an existent structure.sql file" @@ -546,7 +566,7 @@ namespace :railties do # desc "Copies missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2" task :migrations => :'db:load_config' do to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map {|n| n.strip } - railties = ActiveSupport::OrderedHash.new + railties = {} Rails.application.railties.all do |railtie| next unless to_load == :all || to_load.include?(railtie.railtie_name) diff --git a/activerecord/lib/active_record/railties/jdbcmysql_error.rb b/activerecord/lib/active_record/railties/jdbcmysql_error.rb index 0b75983484..6a38211bff 100644 --- a/activerecord/lib/active_record/railties/jdbcmysql_error.rb +++ b/activerecord/lib/active_record/railties/jdbcmysql_error.rb @@ -1,6 +1,6 @@ #FIXME Remove if ArJdbcMysql will give. module ArJdbcMySQL #:nodoc: - class Error < StandardError + class Error < StandardError #:nodoc: attr_accessor :error_number, :sql_state def initialize msg diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 8f8a3ec3bb..d4f4d593c6 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -23,11 +23,11 @@ module ActiveRecord module ClassMethods def create_reflection(macro, name, options, active_record) case macro - when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many - klass = options[:through] ? ThroughReflection : AssociationReflection - reflection = klass.new(macro, name, options, active_record) - when :composed_of - reflection = AggregateReflection.new(macro, name, options, active_record) + when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many + klass = options[:through] ? ThroughReflection : AssociationReflection + reflection = klass.new(macro, name, options, active_record) + when :composed_of + reflection = AggregateReflection.new(macro, name, options, active_record) end self.reflections = self.reflections.merge(name => reflection) @@ -44,7 +44,8 @@ module ActiveRecord # Account.reflect_on_aggregation(:balance) # => the balance AggregateReflection # def reflect_on_aggregation(aggregation) - reflections[aggregation].is_a?(AggregateReflection) ? reflections[aggregation] : nil + reflection = reflections[aggregation] + reflection if reflection.is_a?(AggregateReflection) end # Returns an array of AssociationReflection objects for all the @@ -68,7 +69,8 @@ module ActiveRecord # Invoice.reflect_on_association(:line_items).macro # returns :has_many # def reflect_on_association(association) - reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil + reflection = reflections[association] + reflection if reflection.is_a?(AssociationReflection) end # Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled. @@ -77,7 +79,6 @@ module ActiveRecord end end - # Abstract base class for AggregateReflection and AssociationReflection. Objects of # AggregateReflection and AssociationReflection are returned by the Reflection::ClassMethods. class MacroReflection @@ -452,7 +453,6 @@ module ActiveRecord # Recursively fill out the rest of the array from the through reflection conditions += through_conditions - # And return conditions end end diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 3d1ca4f99b..b125449127 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -168,13 +168,7 @@ module ActiveRecord default_scoped = with_default_scope if default_scoped.equal?(self) - @records = if @readonly_value.nil? && !@klass.locking_enabled? - eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values) - else - IdentityMap.without do - eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values) - end - end + @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values) preload = @preload_values preload += @includes_values unless eager_loading? @@ -262,7 +256,7 @@ module ActiveRecord # # Update all books with 'Rails' in their title # Book.update_all "author = 'David'", "title LIKE '%Rails%'" # - # # Update all avatars migrated more than a week ago + # # Update all avatars migrated more recently than a week ago # Avatar.update_all ['migrated_at = ?', Time.now.utc], ['migrated_at > ?', 1.week.ago] # # # Update all books that match conditions, but limit it to 5 ordered by date @@ -274,7 +268,6 @@ module ActiveRecord # # The same idea applies to limit and order # Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David') def update_all(updates, conditions = nil, options = {}) - IdentityMap.repository[symbolized_base_class].clear if IdentityMap.enabled? if conditions || options.present? where(conditions).apply_finder_options(options.slice(:limit, :order)).update_all(updates) else @@ -404,7 +397,6 @@ module ActiveRecord # If you need to destroy dependent associations or call your <tt>before_*</tt> or # +after_destroy+ callbacks, use the +destroy_all+ method instead. def delete_all(conditions = nil) - IdentityMap.repository[symbolized_base_class] = {} if IdentityMap.enabled? if conditions where(conditions).delete_all else @@ -437,7 +429,6 @@ module ActiveRecord # # Delete multiple rows # Todo.delete([2,3,4]) def delete(id_or_array) - IdentityMap.remove_by_id(self.symbolized_base_class, id_or_array) if IdentityMap.enabled? where(primary_key => id_or_array).delete_all end @@ -463,7 +454,12 @@ module ActiveRecord node.left.relation.name == table_name } - Hash[equalities.map { |where| [where.left.name, where.right] }] + binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }] + + Hash[equalities.map { |where| + name = where.left.name + [name, binds.fetch(name.to_s) { where.right }] + }] end def scope_for_create @@ -511,6 +507,10 @@ module ActiveRecord end end + def blank? + to_a.blank? + end + private def references_eager_loaded_tables? diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 63365e501b..f613014f23 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -174,7 +174,7 @@ module ActiveRecord # # Person.pluck(:id) # SELECT people.id FROM people # Person.uniq.pluck(:role) # SELECT DISTINCT role FROM people - # Person.where(:confirmed => true).limit(5).pluck(:id) + # Person.where(:age => 21).limit(5).pluck(:id) # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5 # def pluck(column_name) key = column_name.to_s.split('.', 2).last @@ -183,7 +183,7 @@ module ActiveRecord column_name = "#{table_name}.#{column_name}" end - result = klass.connection.select_all(select(column_name).arel) + result = klass.connection.select_all(select(column_name).arel, nil, bind_values) types = result.column_types.merge klass.column_types column = types[key] @@ -254,7 +254,8 @@ module ActiveRecord query_builder = relation.arel end - type_cast_calculated_value(@klass.connection.select_value(query_builder), column_for(column_name), operation) + result = @klass.connection.select_value(query_builder, nil, relation.bind_values) + type_cast_calculated_value(result, column_for(column_name), operation) end def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: @@ -290,7 +291,7 @@ module ActiveRecord relation = except(:group).group(group.join(',')) relation.select_values = select_values - calculated_data = @klass.connection.select_all(relation) + calculated_data = @klass.connection.select_all(relation, nil, bind_values) if association key_ids = calculated_data.collect { |row| row[group_aliases.first] } @@ -298,7 +299,7 @@ module ActiveRecord key_records = Hash[key_records.map { |r| [r.id, r] }] end - ActiveSupport::OrderedHash[calculated_data.map do |row| + Hash[calculated_data.map do |row| key = group_columns.map { |aliaz, column| type_cast_calculated_value(row[aliaz], column) } diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 26a5165abf..74f8e30404 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -109,6 +109,25 @@ module ActiveRecord end end + # Finds the first record matching the specified conditions. There + # is no implied ording so if order matters, you should specify it + # yourself. + # + # If no record is found, returns <tt>nil</tt>. + # + # Post.find_by name: 'Spartacus', rating: 4 + # Post.find_by "published_at < ?", 2.weeks.ago + # + def find_by(*args) + where(*args).first + end + + # Like <tt>find_by</tt>, except that if no record is found, raises + # an <tt>ActiveRecord::RecordNotFound</tt> error. + def find_by!(*args) + where(*args).first! + end + # A convenience wrapper for <tt>find(:first, *args)</tt>. You can pass in all the # same arguments to this method as you can to <tt>find(:first)</tt>. def first(*args) @@ -200,7 +219,7 @@ module ActiveRecord relation = relation.where(table[primary_key].eq(id)) if id end - connection.select_value(relation, "#{name} Exists") ? true : false + connection.select_value(relation, "#{name} Exists", relation.bind_values) end protected @@ -290,7 +309,7 @@ module ActiveRecord r.assign_attributes(unprotected_attributes_for_create, :without_protection => true) end yield(record) if block_given? - record.save if match.instantiator == :create + record.send(match.save_method) if match.save_record? end record @@ -318,20 +337,10 @@ module ActiveRecord def find_one(id) id = id.id if ActiveRecord::Base === id - if IdentityMap.enabled? && where_values.blank? && - limit_value.blank? && order_values.blank? && - includes_values.blank? && preload_values.blank? && - readonly_value.nil? && joins_values.blank? && - !@klass.locking_enabled? && - record = IdentityMap.get(@klass, id) - return record - end - column = columns_hash[primary_key] - substitute = connection.substitute_at(column, @bind_values.length) relation = where(table[primary_key].eq(substitute)) - relation.bind_values = [[column, id]] + relation.bind_values += [[column, id]] record = relation.first unless record diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 1d04e763f6..b40bf2b3cf 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -1,7 +1,7 @@ module ActiveRecord class PredicateBuilder # :nodoc: def self.build_from_hash(engine, attributes, default_table) - predicates = attributes.map do |column, value| + attributes.map do |column, value| table = default_table if value.is_a?(Hash) @@ -17,20 +17,18 @@ module ActiveRecord build(table[column.to_sym], value) end - end - predicates.flatten + end.flatten end def self.references(attributes) - references = attributes.map do |key, value| + attributes.map do |key, value| if value.is_a?(Hash) key else key = key.to_s key.split('.').first.to_sym if key.include?('.') end - end - references.compact + end.compact end private @@ -41,26 +39,27 @@ module ActiveRecord attribute.in(value.arel.ast) when Array, ActiveRecord::Associations::CollectionProxy values = value.to_a.map {|x| x.is_a?(ActiveRecord::Model) ? x.id : x} - ranges, values = values.partition {|v| v.is_a?(Range) || v.is_a?(Arel::Relation)} + ranges, values = values.partition {|v| v.is_a?(Range)} - array_predicates = ranges.map {|range| attribute.in(range)} - - if values.include?(nil) + values_predicate = if values.include?(nil) values = values.compact + case values.length when 0 - array_predicates << attribute.eq(nil) + attribute.eq(nil) when 1 - array_predicates << attribute.eq(values.first).or(attribute.eq(nil)) + attribute.eq(values.first).or(attribute.eq(nil)) else - array_predicates << attribute.in(values).or(attribute.eq(nil)) + attribute.in(values).or(attribute.eq(nil)) end else - array_predicates << attribute.in(values) + attribute.in(values) end - array_predicates.inject {|composite, predicate| composite.or(predicate)} - when Range, Arel::Relation + array_predicates = ranges.map { |range| attribute.in(range) } + array_predicates << values_predicate + array_predicates.inject { |composite, predicate| composite.or(predicate) } + when Range attribute.in(value) when ActiveRecord::Model attribute.eq(value.id) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 87dd513880..d737b34115 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -329,7 +329,7 @@ module ActiveRecord arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty? arel.take(connection.sanitize_limit(@limit_value)) if @limit_value - arel.skip(@offset_value) if @offset_value + arel.skip(@offset_value.to_i) if @offset_value arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty? diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index 706b0cd1cc..03ba8c8628 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -22,7 +22,7 @@ module ActiveRecord end end - (Relation::MULTI_VALUE_METHODS - [:joins, :where, :order]).each do |method| + (Relation::MULTI_VALUE_METHODS - [:joins, :where, :order, :binds]).each do |method| value = r.send(:"#{method}_values") next if value.empty? @@ -34,6 +34,8 @@ module ActiveRecord merged_wheres = @where_values + r.where_values + merged_binds = (@bind_values + r.bind_values).uniq(&:first) + unless @where_values.empty? # Remove duplicates, last one wins. seen = Hash.new { |h,table| h[table] = {} } @@ -50,6 +52,7 @@ module ActiveRecord end merged_relation.where_values = merged_wheres + merged_relation.bind_values = merged_binds (Relation::SINGLE_VALUE_METHODS - [:lock, :create_with, :reordering]).each do |method| value = r.send(:"#{method}_value") diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index 2d7d83d160..81b13fe529 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -37,9 +37,9 @@ module ActiveRecord # { :name => nil, :group_id => 4 } returns "name = NULL , group_id='4'" def sanitize_sql_for_assignment(assignments) case assignments - when Array; sanitize_sql_array(assignments) - when Hash; sanitize_sql_hash_for_assignment(assignments) - else assignments + when Array; sanitize_sql_array(assignments) + when Hash; sanitize_sql_hash_for_assignment(assignments) + else assignments end end @@ -57,7 +57,7 @@ module ActiveRecord def expand_hash_conditions_for_aggregates(attrs) expanded_attrs = {} attrs.each do |attr, value| - unless (aggregation = reflect_on_aggregation(attr.to_sym)).nil? + if aggregation = reflect_on_aggregation(attr.to_sym) mapping = aggregate_mapping(aggregation) mapping.each do |field_attr, aggregate_attr| if mapping.size == 1 && !value.respond_to?(aggregate_attr) diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index dcbd165e58..95fd33c1d1 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -40,7 +40,7 @@ module ActiveRecord def header(stream) define_params = @version ? ":version => #{@version}" : "" - if stream.respond_to?(:external_encoding) + if stream.respond_to?(:external_encoding) && stream.external_encoding stream.puts "# encoding: #{stream.external_encoding.name}" end diff --git a/activerecord/lib/active_record/scoping/default.rb b/activerecord/lib/active_record/scoping/default.rb index 5f05d146f2..b0609a8c08 100644 --- a/activerecord/lib/active_record/scoping/default.rb +++ b/activerecord/lib/active_record/scoping/default.rb @@ -1,4 +1,5 @@ require 'active_support/concern' +require 'active_support/deprecation' module ActiveRecord module Scoping @@ -51,7 +52,7 @@ module ActiveRecord # the model. # # class Article < ActiveRecord::Base - # default_scope where(:published => true) + # default_scope { where(:published => true) } # end # # Article.all # => SELECT * FROM articles WHERE published = true @@ -62,12 +63,6 @@ module ActiveRecord # Article.new.published # => true # Article.create.published # => true # - # You can also use <tt>default_scope</tt> with a block, in order to have it lazily evaluated: - # - # class Article < ActiveRecord::Base - # default_scope { where(:published_at => Time.now - 1.week) } - # end - # # (You can also pass any object which responds to <tt>call</tt> to the <tt>default_scope</tt> # macro, and it will be called when building the default scope.) # @@ -75,8 +70,8 @@ module ActiveRecord # be merged together: # # class Article < ActiveRecord::Base - # default_scope where(:published => true) - # default_scope where(:rating => 'G') + # default_scope { where(:published => true) } + # default_scope { where(:rating => 'G') } # end # # Article.all # => SELECT * FROM articles WHERE published = true AND rating = 'G' @@ -94,6 +89,16 @@ module ActiveRecord # end def default_scope(scope = {}) scope = Proc.new if block_given? + + if scope.is_a?(Relation) || !scope.respond_to?(:call) + ActiveSupport::Deprecation.warn( + "Calling #default_scope without a block is deprecated. For example instead " \ + "of `default_scope where(color: 'red')`, please use " \ + "`default_scope { where(color: 'red') }`. (Alternatively you can just redefine " \ + "self.default_scope.)" + ) + end + self.default_scopes = default_scopes + [scope] end @@ -106,7 +111,7 @@ module ActiveRecord if scope.is_a?(Hash) default_scope.apply_finder_options(scope) elsif !scope.is_a?(Relation) && scope.respond_to?(:call) - default_scope.merge(scope.call) + default_scope.merge(unscoped { scope.call }) else default_scope.merge(scope) end diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 0edc3f1dcc..077e2d067e 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -3,6 +3,7 @@ require 'active_support/core_ext/hash/except' require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/class/attribute' +require 'active_support/deprecation' module ActiveRecord # = Active Record Named \Scopes @@ -171,30 +172,30 @@ module ActiveRecord # Article.published.featured.latest_article # Article.featured.titles - def scope(name, scope_options = {}) - name = name.to_sym - valid_scope_name?(name) - extension = Module.new(&Proc.new) if block_given? + def scope(name, body = {}, &block) + extension = Module.new(&block) if block - scope_proc = lambda do |*args| - options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options + # Check body.is_a?(Relation) to prevent the relation actually being + # loaded by respond_to? + if body.is_a?(Relation) || !body.respond_to?(:call) + ActiveSupport::Deprecation.warn( + "Using #scope without passing a callable object is deprecated. For " \ + "example `scope :red, where(color: 'red')` should be changed to " \ + "`scope :red, -> { where(color: 'red') }`. There are numerous gotchas " \ + "in the former usage and it makes the implementation more complicated " \ + "and buggy. (If you prefer, you can just define a class method named " \ + "`self.red`.)" + ) + end + + singleton_class.send(:define_method, name) do |*args| + options = body.respond_to?(:call) ? unscoped { body.call(*args) } : body options = scoped.apply_finder_options(options) if options.is_a?(Hash) relation = scoped.merge(options) extension ? relation.extending(extension) : relation end - - singleton_class.send(:redefine_method, name, &scope_proc) - end - - protected - - def valid_scope_name?(name) - if respond_to?(name, true) - logger.warn "Creating scope :#{name}. " \ - "Overwriting existing method #{self.name}.#{name}." - end end end end diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index 4d881f0f7d..fcaa4b74a6 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -7,20 +7,10 @@ module ActiveRecord # # Defines some test assertions to test against SQL queries. class TestCase < ActiveSupport::TestCase #:nodoc: - setup :cleanup_identity_map - - def setup - cleanup_identity_map - end - def teardown SQLCounter.log.clear end - def cleanup_identity_map - ActiveRecord::IdentityMap.clear - end - def assert_date_from_db(expected, actual, message = nil) # SybaseAdapter doesn't have a separate column type just for dates, # so the time is in the string and incorrectly formatted diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index b492377d18..743dfc5a38 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -251,7 +251,6 @@ module ActiveRecord remember_transaction_record_state yield rescue Exception - IdentityMap.remove(self) if IdentityMap.enabled? restore_transaction_record_state raise ensure @@ -270,7 +269,6 @@ module ActiveRecord def rolledback!(force_restore_state = false) #:nodoc: run_callbacks :rollback ensure - IdentityMap.remove(self) if IdentityMap.enabled? restore_transaction_record_state(force_restore_state) end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 4b075183c3..d06020b3ce 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -14,7 +14,7 @@ module ActiveRecord def initialize(record) @record = record errors = @record.errors.full_messages.join(", ") - super(I18n.t("activerecord.errors.messages.record_invalid", :errors => errors)) + super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid")) end end diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 9556878f63..db618f617f 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -35,8 +35,14 @@ module ActiveRecord relation = relation.and(table[scope_item].eq(scope_value)) end - if finder_class.unscoped.where(relation).exists? - record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value)) + relation = finder_class.unscoped.where(relation) + + if options[:conditions] + relation = relation.merge(options[:conditions]) + end + + if relation.exists? + record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope, :conditions).merge(:value => value)) end end @@ -102,6 +108,14 @@ module ActiveRecord # validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id] # end # + # It is also possible to limit the uniqueness constraint to a set of records matching certain conditions. + # In this example archived articles are not being taken into consideration when validating uniqueness + # of the title attribute: + # + # class Article < ActiveRecord::Base + # validates_uniqueness_of :title, :conditions => where('status != ?', 'archived') + # end + # # When the record is created, a check is performed to make sure that no record exists in the database # with the given value for the specified attribute (that maps to a column). When the record is updated, # the same check is made but disregarding the record itself. @@ -109,6 +123,8 @@ module ActiveRecord # Configuration options: # * <tt>:message</tt> - Specifies a custom error message (default is: "has already been taken"). # * <tt>:scope</tt> - One or more columns by which to limit the scope of the uniqueness constraint. + # * <tt>:conditions</tt> - Specify the conditions to be included as a <tt>WHERE</tt> SQL fragment to limit + # the uniqueness constraint lookup. (e.g. <tt>:conditions => where('status = ?', 'active')</tt>) # * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (+true+ by default). # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+). # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+). 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 d084a00ed7..b9b5ec7956 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -5,7 +5,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> <%- if attribute.has_index? -%> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> - <%- end %> + <%- end -%> <%- end -%> end <%- else -%> @@ -13,9 +13,9 @@ class <%= migration_class_name %> < ActiveRecord::Migration <% attributes.each do |attribute| -%> <%- if migration_action -%> <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %> - <% if attribute.has_index? && migration_action == 'add' %> + <%- if attribute.has_index? && migration_action == 'add' -%> add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> - <% end -%> + <%- end -%> <%- end -%> <%- end -%> end @@ -24,6 +24,9 @@ class <%= migration_class_name %> < ActiveRecord::Migration <% attributes.reverse.each do |attribute| -%> <%- if migration_action -%> <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %> + <%- if attribute.has_index? && migration_action == 'remove' -%> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> + <%- end -%> <%- end -%> <%- end -%> end diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 99a022461e..f3bb70fb41 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -30,6 +30,10 @@ module ActiveRecord attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) } end + def accessible_attributes + attributes.reject(&:reference?) + end + hook_for :test_framework protected diff --git a/activerecord/lib/rails/generators/active_record/model/templates/model.rb b/activerecord/lib/rails/generators/active_record/model/templates/model.rb index 5c47f8b241..d56f9f57a4 100644 --- a/activerecord/lib/rails/generators/active_record/model/templates/model.rb +++ b/activerecord/lib/rails/generators/active_record/model/templates/model.rb @@ -3,5 +3,10 @@ class <%= class_name %> < <%= parent_class_name.classify %> <% attributes.select {|attr| attr.reference? }.each do |attribute| -%> belongs_to :<%= attribute.name %> <% end -%> +<% if !accessible_attributes.empty? -%> + attr_accessible <%= accessible_attributes.map {|a| ":#{a.name}" }.sort.join(', ') %> +<% else -%> + # attr_accessible :title, :body +<% end -%> end <% end -%> diff --git a/activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb b/activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb index 7fe2c02c04..5aa4743e7b 100644 --- a/activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb +++ b/activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb @@ -46,6 +46,16 @@ module ActiveRecord assert_equal str, value end + def test_tables_quoting + begin + @conn.tables(nil, "foo-bar", nil) + flunk + rescue => e + # assertion for *quoted* database properly + assert_match(/database 'foo-bar'/, e.inspect) + end + end + private def insert(ctx, data) binds = data.map { |name, value| diff --git a/activerecord/test/cases/adapters/mysql2/schema_test.rb b/activerecord/test/cases/adapters/mysql2/schema_test.rb index d5676bc522..de4d9c2b33 100644 --- a/activerecord/test/cases/adapters/mysql2/schema_test.rb +++ b/activerecord/test/cases/adapters/mysql2/schema_test.rb @@ -35,6 +35,17 @@ module ActiveRecord def test_table_exists_wrong_schema assert(!@connection.table_exists?("#{@db_name}.zomg"), "table should not exist") end + + def test_tables_quoting + begin + @connection.tables(nil, "foo-bar", nil) + flunk + rescue => e + # assertion for *quoted* database properly + assert_match(/database 'foo-bar'/, e.inspect) + end + end + end end end diff --git a/activerecord/test/cases/adapters/postgresql/explain_test.rb b/activerecord/test/cases/adapters/postgresql/explain_test.rb index 0b61f61572..619d581d5f 100644 --- a/activerecord/test/cases/adapters/postgresql/explain_test.rb +++ b/activerecord/test/cases/adapters/postgresql/explain_test.rb @@ -22,6 +22,13 @@ module ActiveRecord assert_match %(EXPLAIN for: SELECT "audit_logs".* FROM "audit_logs" WHERE "audit_logs"."developer_id" IN (1)), explain assert_match %(Seq Scan on audit_logs), explain end + + def test_dont_explain_for_set_search_path + queries = Thread.current[:available_queries_for_explain] = [] + ActiveRecord::Base.connection.schema_search_path = "public" + assert queries.empty? + end + end end end diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 898d28456b..a71d0bb848 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -185,6 +185,11 @@ module ActiveRecord assert_equal "(number > 100)", index.where end + def test_distinct_with_nulls + assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls first"]) + assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls last"]) + end + private def insert(ctx, data) binds = data.map { |name, value| diff --git a/activerecord/test/cases/adapters/postgresql/schema_test.rb b/activerecord/test/cases/adapters/postgresql/schema_test.rb index 18c81d2b09..9208f53997 100644 --- a/activerecord/test/cases/adapters/postgresql/schema_test.rb +++ b/activerecord/test/cases/adapters/postgresql/schema_test.rb @@ -71,6 +71,45 @@ class SchemaTest < ActiveRecord::TestCase @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE" end + def test_schema_names + assert_equal ["public", "test_schema", "test_schema2"], @connection.schema_names + end + + def test_create_schema + begin + @connection.create_schema "test_schema3" + assert @connection.schema_names.include? "test_schema3" + ensure + @connection.drop_schema "test_schema3" + end + end + + def test_raise_create_schema_with_existing_schema + begin + @connection.create_schema "test_schema3" + assert_raises(ActiveRecord::StatementInvalid) do + @connection.create_schema "test_schema3" + end + ensure + @connection.drop_schema "test_schema3" + end + end + + def test_drop_schema + begin + @connection.create_schema "test_schema3" + ensure + @connection.drop_schema "test_schema3" + end + assert !@connection.schema_names.include?("test_schema3") + end + + def test_raise_drop_schema_with_nonexisting_schema + assert_raises(ActiveRecord::StatementInvalid) do + @connection.drop_schema "test_schema3" + end + end + def test_schema_change_with_prepared_stmt altered = false @connection.exec_query "select * from developers where id = $1", 'sql', [[nil, 1]] @@ -183,13 +222,13 @@ class SchemaTest < ActiveRecord::TestCase end def test_raise_on_unquoted_schema_name - assert_raise(ActiveRecord::StatementInvalid) do + assert_raises(ActiveRecord::StatementInvalid) do with_schema_search_path '$user,public' end end def test_without_schema_search_path - assert_raise(ActiveRecord::StatementInvalid) { columns(TABLE_NAME) } + assert_raises(ActiveRecord::StatementInvalid) { columns(TABLE_NAME) } end def test_ignore_nil_schema_search_path diff --git a/activerecord/test/cases/adapters/sqlite3/quoting_test.rb b/activerecord/test/cases/adapters/sqlite3/quoting_test.rb index 46da1b0a2b..1dbeb66af6 100644 --- a/activerecord/test/cases/adapters/sqlite3/quoting_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/quoting_test.rb @@ -1,6 +1,7 @@ require "cases/helper" require 'bigdecimal' require 'yaml' +require 'securerandom' module ActiveRecord module ConnectionAdapters @@ -12,6 +13,14 @@ module ActiveRecord :timeout => 100 end + def test_type_cast_binary_encoding_without_logger + @conn.extend(Module.new { def logger; end }) + column = Struct.new(:type, :name).new(:string, "foo") + binary = SecureRandom.hex + expected = binary.dup.encode!('utf-8') + assert_equal expected, @conn.type_cast(binary, column) + end + def test_type_cast_symbol assert_equal 'foo', @conn.type_cast(:foo, nil) end diff --git a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb index 7965bb404c..3044a8c312 100644 --- a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb +++ b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb @@ -27,7 +27,6 @@ class EagerLoadIncludeFullStiClassNamesTest < ActiveRecord::TestCase post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging ) assert_nil post.tagging - ActiveRecord::IdentityMap.clear ActiveRecord::Base.store_full_sti_class = true post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging ) assert_instance_of Tagging, post.tagging diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index b79c69bbb5..efdb7cbb36 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -197,7 +197,7 @@ class EagerAssociationTest < ActiveRecord::TestCase author = authors(:david) post = author.post_about_thinking_with_last_comment last_comment = post.last_comment - author = assert_queries(ActiveRecord::IdentityMap.enabled? ? 2 : 3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments + author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments assert_no_queries do assert_equal post, author.post_about_thinking_with_last_comment assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment @@ -208,7 +208,7 @@ class EagerAssociationTest < ActiveRecord::TestCase post = posts(:welcome) author = post.author author_address = author.author_address - post = assert_queries(ActiveRecord::IdentityMap.enabled? ? 2 : 3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address + post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address assert_no_queries do assert_equal author, post.author_with_address assert_equal author_address, post.author_with_address.author_address @@ -611,9 +611,9 @@ class EagerAssociationTest < ActiveRecord::TestCase assert posts[1].categories.include?(categories(:general)) end - # This is only really relevant when the identity map is off. Since the preloader for habtm - # gets raw row hashes from the database and then instantiates them, this test ensures that - # it only instantiates one actual object per record from the database. + # Since the preloader for habtm gets raw row hashes from the database and then + # instantiates them, this test ensures that it only instantiates one actual + # object per record from the database. def test_has_and_belongs_to_many_should_not_instantiate_same_records_multiple_times welcome = posts(:welcome) categories = Category.includes(:posts) @@ -1002,18 +1002,18 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal [posts(:welcome)], posts assert_equal authors(:david), assert_no_queries { posts[0].author} - posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do + posts = assert_queries(2) do Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') end assert_equal [posts(:welcome)], posts assert_equal authors(:david), assert_no_queries { posts[0].author} - posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do + posts = assert_queries(2) do Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id') end assert_equal posts(:welcome, :thinking), posts - posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do + posts = assert_queries(2) do Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id') end assert_equal posts(:welcome, :thinking), posts @@ -1027,7 +1027,7 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal [posts(:welcome)], posts assert_equal authors(:david), assert_no_queries { posts[0].author} - posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do + posts = assert_queries(2) do Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') end assert_equal [posts(:welcome)], posts @@ -1116,7 +1116,7 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_preloading_empty_belongs_to_polymorphic t = Tagging.create!(:taggable_type => 'Post', :taggable_id => Post.maximum(:id) + 1, :tag => tags(:general)) - tagging = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) { Tagging.preload(:taggable).find(t.id) } + tagging = assert_queries(2) { Tagging.preload(:taggable).find(t.id) } assert_no_queries { assert_nil tagging.taggable } end @@ -1169,4 +1169,15 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_no_queries { assert_equal 1, posts[0].categories[1].categorizations.length } assert_no_queries { assert_equal 2, posts[1].categories[0].categorizations.length } end + + test "scoping with a circular preload" do + assert_equal Comment.find(1), Comment.preload(:post => :comments).scoping { Comment.find(1) } + end + + test "preload ignores the scoping" do + assert_equal( + Comment.find(1).post, + Post.where('1 = 0').scoping { Comment.preload(:post).find(1).post } + ) + end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 3967009c82..6a4f972356 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -130,6 +130,28 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal car.id, bulb.car_id end + def test_association_protect_foreign_key + invoice = Invoice.create + + line_item = invoice.line_items.new + assert_equal invoice.id, line_item.invoice_id + + line_item = invoice.line_items.new :invoice_id => invoice.id + 1 + assert_equal invoice.id, line_item.invoice_id + + line_item = invoice.line_items.build + assert_equal invoice.id, line_item.invoice_id + + line_item = invoice.line_items.build :invoice_id => invoice.id + 1 + assert_equal invoice.id, line_item.invoice_id + + line_item = invoice.line_items.create + assert_equal invoice.id, line_item.invoice_id + + line_item = invoice.line_items.create :invoice_id => invoice.id + 1 + assert_equal invoice.id, line_item.invoice_id + end + def test_association_conditions_bypass_attribute_protection car = Car.create(:name => 'honda') @@ -356,6 +378,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, Firm.find(:first, :order => "id").clients_using_sql.size end + def test_finding_using_sql_take_into_account_only_uniq_ids + firm = Firm.find(:first, :order => "id") + client = firm.clients_using_sql.first + assert_equal client, firm.clients_using_sql.find(client.id, client.id) + assert_equal client, firm.clients_using_sql.find(client.id, client.id.to_s) + end + def test_counting_using_sql assert_equal 1, Firm.find(:first, :order => "id").clients_using_counter_sql.size assert Firm.find(:first, :order => "id").clients_using_counter_sql.any? 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 9cc09194dc..e9b930204f 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -44,17 +44,33 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end def test_associate_existing - posts(:thinking); people(:david) # Warm cache + post = posts(:thinking) + person = people(:david) assert_queries(1) do - posts(:thinking).people << people(:david) + post.people << person end assert_queries(1) do - assert posts(:thinking).people.include?(people(:david)) + assert post.people.include?(person) end - assert posts(:thinking).reload.people(true).include?(people(:david)) + assert post.reload.people(true).include?(person) + end + + def test_associate_existing_with_strict_mass_assignment_sanitizer + SecureReader.mass_assignment_sanitizer = :strict + + SecureReader.new + + post = posts(:thinking) + person = people(:david) + + assert_queries(1) do + post.secure_people << person + end + ensure + SecureReader.mass_assignment_sanitizer = :logger end def test_associate_existing_record_twice_should_add_to_target_twice @@ -688,6 +704,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert author.comments.include?(comment) end + def test_through_association_readonly_should_be_false + assert !people(:michael).posts.first.readonly? + assert !people(:michael).posts.all.first.readonly? + end + + def test_can_update_through_association + assert_nothing_raised do + people(:michael).posts.first.update_attributes!(:title => "Can write") + end + end + def test_has_many_through_polymorphic_with_primary_key_option assert_equal [categories(:general)], authors(:david).essay_categories diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 246877bbed..9c05b36426 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -448,6 +448,22 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_equal car.id, bulb.car_id end + def test_association_protect_foreign_key + pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") + + ship = pirate.build_ship + assert_equal pirate.id, ship.pirate_id + + ship = pirate.build_ship :pirate_id => pirate.id + 1 + assert_equal pirate.id, ship.pirate_id + + ship = pirate.create_ship + assert_equal pirate.id, ship.pirate_id + + ship = pirate.create_ship :pirate_id => pirate.id + 1 + assert_equal pirate.id, ship.pirate_id + end + def test_association_conditions_bypass_attribute_protection car = Car.create(:name => 'honda') diff --git a/activerecord/test/cases/associations/identity_map_test.rb b/activerecord/test/cases/associations/identity_map_test.rb deleted file mode 100644 index 9b8635774c..0000000000 --- a/activerecord/test/cases/associations/identity_map_test.rb +++ /dev/null @@ -1,137 +0,0 @@ -require "cases/helper" -require 'models/author' -require 'models/post' - -if ActiveRecord::IdentityMap.enabled? -class InverseHasManyIdentityMapTest < ActiveRecord::TestCase - fixtures :authors, :posts - - def test_parent_instance_should_be_shared_with_every_child_on_find - m = Author.first - is = m.posts - is.each do |i| - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance" - end - end - - def test_parent_instance_should_be_shared_with_eager_loaded_children - m = Author.find(:first, :include => :posts) - is = m.posts - is.each do |i| - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance" - end - - m = Author.find(:first, :include => :posts, :order => 'posts.id') - is = m.posts - is.each do |i| - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance" - end - end - - def test_parent_instance_should_be_shared_with_newly_built_child - m = Author.first - i = m.posts.build(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum') - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to just-built-child-owned instance" - end - - def test_parent_instance_should_be_shared_with_newly_block_style_built_child - m = Author.first - i = m.posts.build {|ii| ii.title = 'Industrial Revolution Re-enactment'; ii.body = 'Lorem ipsum'} - assert_not_nil i.title, "Child attributes supplied to build via blocks should be populated" - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to just-built-child-owned instance" - end - - def test_parent_instance_should_be_shared_with_newly_created_child - m = Author.first - i = m.posts.create(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum') - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance" - end - - def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child - m = Author.first - i = m.posts.create!(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum') - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance" - end - - def test_parent_instance_should_be_shared_with_newly_block_style_created_child - m = Author.first - i = m.posts.create {|ii| ii.title = 'Industrial Revolution Re-enactment'; ii.body = 'Lorem ipsum'} - assert_not_nil i.title, "Child attributes supplied to create via blocks should be populated" - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance" - end - - def test_parent_instance_should_be_shared_with_poked_in_child - m = Author.first - i = Post.create(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum') - m.posts << i - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance" - end - - def test_parent_instance_should_be_shared_with_replaced_via_accessor_children - m = Author.first - i = Post.new(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum') - m.posts = [i] - assert_same m, i.author - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to replaced-child-owned instance" - end - - def test_parent_instance_should_be_shared_with_replaced_via_method_children - m = Author.first - i = Post.new(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum') - m.posts = [i] - assert_not_nil i.author - assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance" - m.name = 'Bongo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance" - i.author.name = 'Mungo' - assert_equal m.name, i.author.name, "Name of man should be the same after changes to replaced-child-owned instance" - end -end -end diff --git a/activerecord/test/cases/attribute_methods/read_test.rb b/activerecord/test/cases/attribute_methods/read_test.rb index 764305459d..98c38535a6 100644 --- a/activerecord/test/cases/attribute_methods/read_test.rb +++ b/activerecord/test/cases/attribute_methods/read_test.rb @@ -6,10 +6,6 @@ module ActiveRecord module AttributeMethods class ReadTest < ActiveRecord::TestCase class FakeColumn < Struct.new(:name) - def type_cast_code(var) - var - end - def type; :integer; end end diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 4078b7eb0b..ef01476ae4 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -30,9 +30,12 @@ class AttributeMethodsTest < ActiveRecord::TestCase t = Topic.new t.title = "hello there!" t.written_on = Time.now + t.author_name = "" assert t.attribute_present?("title") assert t.attribute_present?("written_on") assert !t.attribute_present?("content") + assert !t.attribute_present?("author_name") + end def test_attribute_present_with_booleans @@ -625,6 +628,16 @@ class AttributeMethodsTest < ActiveRecord::TestCase end end + def test_time_zone_aware_attribute_saved + in_time_zone 1 do + record = @target.create(:written_on => '2012-02-20 10:00') + + record.written_on = '2012-02-20 09:00' + record.save + assert_equal Time.zone.local(2012, 02, 20, 9), record.reload.written_on + end + end + def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil in_time_zone "Pacific Time (US & Canada)" do record = @target.new diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 1376810adf..d15487ab11 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -767,6 +767,16 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase assert_equal before, @pirate.reload.birds end + def test_when_new_record_a_child_marked_for_destruction_should_not_affect_other_records_from_saving + @pirate = @ship.build_pirate(:catchphrase => "Arr' now I shall keep me eye on you matey!") # new record + + 3.times { |i| @pirate.birds.build(:name => "birds_#{i}") } + @pirate.birds[1].mark_for_destruction + @pirate.save! + + assert_equal 2, @pirate.birds.reload.length + end + # Add and remove callbacks tests for association collections. %w{ method proc }.each do |callback_type| define_method("test_should_run_add_callback_#{callback_type}s_for_has_many") do @@ -978,10 +988,7 @@ class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase values = [@pirate.reload.catchphrase, @pirate.ship.name, *@pirate.ship.parts.map(&:name)] # Oracle saves empty string as NULL if current_adapter?(:OracleAdapter) - expected = ActiveRecord::IdentityMap.enabled? ? - [nil, nil, '', ''] : - [nil, nil, nil, nil] - assert_equal expected, values + assert_equal [nil, nil, nil, nil], values else assert_equal ['', '', '', ''], values end @@ -1077,8 +1084,7 @@ class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase @ship.save(:validate => false) # Oracle saves empty string as NULL if current_adapter?(:OracleAdapter) - expected = ActiveRecord::IdentityMap.enabled? ? [nil, ''] : [nil, nil] - assert_equal expected, [@ship.reload.name, @ship.pirate.catchphrase] + assert_equal [nil, nil], [@ship.reload.name, @ship.pirate.catchphrase] else assert_equal ['', ''], [@ship.reload.name, @ship.pirate.catchphrase] end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 698c3d0cb1..5fb49d540f 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -62,7 +62,11 @@ end class Weird < ActiveRecord::Base; end -class Boolean < ActiveRecord::Base; end +class Boolean < ActiveRecord::Base + def has_fun + super + end +end class LintTest < ActiveRecord::TestCase include ActiveModel::Lint::Tests @@ -163,7 +167,7 @@ class BasicsTest < ActiveRecord::TestCase def test_select_symbol topic_ids = Topic.select(:id).map(&:id).sort - assert_equal Topic.all.map(&:id).sort, topic_ids + assert_equal Topic.pluck(:id).sort, topic_ids end def test_table_exists @@ -957,6 +961,16 @@ class BasicsTest < ActiveRecord::TestCase assert b_true.value? end + def test_boolean_without_questionmark + b_true = Boolean.create({ "value" => true }) + true_id = b_true.id + + subclass = Class.new(Boolean).find true_id + superclass = Boolean.find true_id + + assert_equal superclass.read_attribute(:has_fun), subclass.read_attribute(:has_fun) + end + def test_boolean_cast_from_string b_blank = Boolean.create({ "value" => "" }) blank_id = b_blank.id @@ -1139,7 +1153,7 @@ class BasicsTest < ActiveRecord::TestCase assert g.save # Reload and check that we have all the geometric attributes. - h = ActiveRecord::IdentityMap.without { Geometric.find(g.id) } + h = Geometric.find(g.id) assert_equal '(5,6.1)', h.a_point assert_equal '[(2,3),(5.5,7)]', h.a_line_segment @@ -1168,7 +1182,7 @@ class BasicsTest < ActiveRecord::TestCase assert g.save # Reload and check that we have all the geometric attributes. - h = ActiveRecord::IdentityMap.without { Geometric.find(g.id) } + h = Geometric.find(g.id) assert_equal '(5,6.1)', h.a_point assert_equal '[(2,3),(5.5,7)]', h.a_line_segment @@ -1479,6 +1493,40 @@ class BasicsTest < ActiveRecord::TestCase end end + def test_clear_cash_when_setting_table_name + Joke.table_name = "cold_jokes" + before_columns = Joke.columns + before_seq = Joke.sequence_name + + Joke.table_name = "funny_jokes" + after_columns = Joke.columns + after_seq = Joke.sequence_name + + assert_not_equal before_columns, after_columns + assert_not_equal before_seq, after_seq unless before_seq.blank? && after_seq.blank? + end + + def test_dont_clear_sequence_name_when_setting_explicitly + Joke.sequence_name = "black_jokes_seq" + Joke.table_name = "cold_jokes" + before_seq = Joke.sequence_name + + Joke.table_name = "funny_jokes" + after_seq = Joke.sequence_name + + assert_equal before_seq, after_seq unless before_seq.blank? && after_seq.blank? + end + + def test_dont_clear_inheritnce_column_when_setting_explicitly + Joke.inheritance_column = "my_type" + before_inherit = Joke.inheritance_column + + Joke.reset_column_information + after_inherit = Joke.inheritance_column + + assert_equal before_inherit, after_inherit unless before_inherit.blank? && after_inherit.blank? + end + def test_set_table_name_symbol_converted_to_string Joke.table_name = :cold_jokes assert_equal 'cold_jokes', Joke.table_name @@ -1920,6 +1968,15 @@ class BasicsTest < ActiveRecord::TestCase assert_equal 1, post.comments.length end + def test_marshalling_new_record_round_trip_with_associations + post = Post.new + post.comments.build + + post = Marshal.load(Marshal.dump(post)) + + assert post.new_record?, "should be a new record" + end + def test_attribute_names assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id"], Company.attribute_names @@ -2000,4 +2057,30 @@ class BasicsTest < ActiveRecord::TestCase def test_typecasting_aliases assert_equal 10, Topic.select('10 as tenderlove').first.tenderlove end + + def test_slice + company = Company.new(:rating => 1, :name => "37signals", :firm_name => "37signals") + hash = company.slice(:name, :rating, "arbitrary_method") + assert_equal hash[:name], company.name + assert_equal hash['name'], company.name + assert_equal hash[:rating], company.rating + assert_equal hash['arbitrary_method'], company.arbitrary_method + assert_equal hash[:arbitrary_method], company.arbitrary_method + assert_nil hash[:firm_name] + assert_nil hash['firm_name'] + end + + ["find_by", "find_by!"].each do |meth| + test "#{meth} delegates to scoped" do + record = stub + + scope = mock + scope.expects(meth).with(:foo, :bar).returns(record) + + klass = Class.new(ActiveRecord::Base) + klass.stubs(:scoped => scope) + + assert_equal record, klass.public_send(meth, :foo, :bar) + end + end end diff --git a/activerecord/test/cases/coders/yaml_column_test.rb b/activerecord/test/cases/coders/yaml_column_test.rb index c7dcc21809..b874adc081 100644 --- a/activerecord/test/cases/coders/yaml_column_test.rb +++ b/activerecord/test/cases/coders/yaml_column_test.rb @@ -9,6 +9,13 @@ module ActiveRecord assert_equal Object, coder.object_class end + def test_type_mismatch_on_different_classes_on_dump + coder = YAMLColumn.new(Array) + assert_raises(SerializationTypeMismatch) do + coder.dump("a") + end + end + def test_type_mismatch_on_different_classes coder = YAMLColumn.new(Array) assert_raises(SerializationTypeMismatch) do diff --git a/activerecord/test/cases/connection_adapters/schema_cache_test.rb b/activerecord/test/cases/connection_adapters/schema_cache_test.rb index 42e39d534c..541e983758 100644 --- a/activerecord/test/cases/connection_adapters/schema_cache_test.rb +++ b/activerecord/test/cases/connection_adapters/schema_cache_test.rb @@ -39,6 +39,21 @@ module ActiveRecord assert_equal 0, @cache.tables.size assert_equal 0, @cache.primary_keys.size end + + def test_dump_and_load + @cache.columns['posts'] + @cache.columns_hash['posts'] + @cache.tables['posts'] + @cache.primary_keys['posts'] + + @cache = Marshal.load(Marshal.dump(@cache)) + + assert_equal 12, @cache.columns['posts'].size + assert_equal 12, @cache.columns_hash['posts'].size + assert @cache.tables['posts'] + assert_equal 'id', @cache.primary_keys['posts'] + end + end end end diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb index 2c69bfde5b..da93500ce3 100644 --- a/activerecord/test/cases/connection_pool_test.rb +++ b/activerecord/test/cases/connection_pool_test.rb @@ -3,6 +3,8 @@ require "cases/helper" module ActiveRecord module ConnectionAdapters class ConnectionPoolTest < ActiveRecord::TestCase + attr_reader :pool + def setup super @@ -25,6 +27,67 @@ module ActiveRecord @pool.disconnect! end + def active_connections(pool) + pool.connections.find_all(&:in_use?) + end + + def test_checkout_after_close + connection = pool.connection + assert connection.in_use? + + connection.close + assert !connection.in_use? + + assert pool.connection.in_use? + end + + def test_released_connection_moves_between_threads + thread_conn = nil + + Thread.new { + pool.with_connection do |conn| + thread_conn = conn + end + }.join + + assert thread_conn + + Thread.new { + pool.with_connection do |conn| + assert_equal thread_conn, conn + end + }.join + end + + def test_with_connection + assert_equal 0, active_connections(pool).size + + main_thread = pool.connection + assert_equal 1, active_connections(pool).size + + Thread.new { + pool.with_connection do |conn| + assert conn + assert_equal 2, active_connections(pool).size + end + assert_equal 1, active_connections(pool).size + }.join + + main_thread.close + assert_equal 0, active_connections(pool).size + end + + def test_active_connection_in_use + assert !pool.active_connection? + main_thread = pool.connection + + assert pool.active_connection? + + main_thread.close + + assert !pool.active_connection? + end + def test_full_pool_exception assert_raises(PoolFullError) do (@pool.size + 1).times do diff --git a/activerecord/test/cases/dynamic_finder_match_test.rb b/activerecord/test/cases/dynamic_finder_match_test.rb index e576870317..db619faa83 100644 --- a/activerecord/test/cases/dynamic_finder_match_test.rb +++ b/activerecord/test/cases/dynamic_finder_match_test.rb @@ -83,6 +83,14 @@ module ActiveRecord assert_equal :create, m.instantiator end + def test_find_or_create! + m = DynamicFinderMatch.match(:find_or_create_by_foo!) + assert_equal :first, m.finder + assert m.bang?, 'should be banging' + assert_equal %w{ foo }, m.attribute_names + assert_equal :create, m.instantiator + end + def test_find_or_initialize m = DynamicFinderMatch.match(:find_or_initialize_by_foo) assert_equal :first, m.finder diff --git a/activerecord/test/cases/finder_respond_to_test.rb b/activerecord/test/cases/finder_respond_to_test.rb index 235805a67c..810c1500cc 100644 --- a/activerecord/test/cases/finder_respond_to_test.rb +++ b/activerecord/test/cases/finder_respond_to_test.rb @@ -56,6 +56,16 @@ class FinderRespondToTest < ActiveRecord::TestCase assert_respond_to Topic, :find_or_create_by_title_and_author_name end + def test_should_respond_to_find_or_create_from_one_attribute_bang + ensure_topic_method_is_not_cached(:find_or_create_by_title!) + assert_respond_to Topic, :find_or_create_by_title! + end + + def test_should_respond_to_find_or_create_from_two_attributes_bang + ensure_topic_method_is_not_cached(:find_or_create_by_title_and_author_name!) + assert_respond_to Topic, :find_or_create_by_title_and_author_name! + end + def test_should_not_respond_to_find_by_one_missing_attribute assert !Topic.respond_to?(:find_by_undertitle) end diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 76c041397a..96c8eb6417 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -862,6 +862,28 @@ class FinderTest < ActiveRecord::TestCase assert another.persisted? end + def test_find_or_create_from_one_attribute_bang + number_of_companies = Company.count + assert_raises(ActiveRecord::RecordInvalid) { Company.find_or_create_by_name!("") } + assert_equal number_of_companies, Company.count + sig38 = Company.find_or_create_by_name!("38signals") + assert_equal number_of_companies + 1, Company.count + assert_equal sig38, Company.find_or_create_by_name!("38signals") + assert sig38.persisted? + end + + def test_find_or_create_from_two_attributes_bang + number_of_companies = Company.count + assert_raises(ActiveRecord::RecordInvalid) { Company.find_or_create_by_name_and_firm_id!("", 17) } + assert_equal number_of_companies, Company.count + sig38 = Company.find_or_create_by_name_and_firm_id!("38signals", 17) + assert_equal number_of_companies + 1, Company.count + assert_equal sig38, Company.find_or_create_by_name_and_firm_id!("38signals", 17) + assert sig38.persisted? + assert_equal "38signals", sig38.name + assert_equal 17, sig38.firm_id + end + def test_find_or_create_from_two_attributes_with_one_being_an_aggregate number_of_customers = Customer.count created_customer = Customer.find_or_create_by_balance_and_name(Money.new(123), "Elizabeth") @@ -1185,6 +1207,10 @@ class FinderTest < ActiveRecord::TestCase end end + def test_finder_with_offset_string + assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.find(:all, :offset => "3") } + end + protected def bind(statement, *vars) if vars.first.is_a?(Hash) diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 562b370c97..e660b2ca35 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -514,7 +514,7 @@ class InvalidTableNameFixturesTest < ActiveRecord::TestCase self.use_transactional_fixtures = false def test_raises_error - assert_raise FixtureClassNotFound do + assert_raise ActiveRecord::FixtureClassNotFound do funny_jokes(:a_joke) end end diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 9f5f012073..5c3560a33b 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -19,9 +19,6 @@ require 'support/connection' # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true -# Enable Identity Map only when ENV['IM'] is set to "true" -ActiveRecord::IdentityMap.enabled = (ENV['IM'] == "true") - # Avoid deprecation warning setting dependent_restrict_raises to false. The default is true ActiveRecord::Base.dependent_restrict_raises = false diff --git a/activerecord/test/cases/identity_map/middleware_test.rb b/activerecord/test/cases/identity_map/middleware_test.rb deleted file mode 100644 index 5b32a1c6d2..0000000000 --- a/activerecord/test/cases/identity_map/middleware_test.rb +++ /dev/null @@ -1,74 +0,0 @@ -require "cases/helper" -require "rack" - -module ActiveRecord - module IdentityMap - class MiddlewareTest < ActiveRecord::TestCase - def setup - super - @enabled = IdentityMap.enabled - IdentityMap.enabled = false - end - - def teardown - super - IdentityMap.enabled = @enabled - IdentityMap.clear - end - - def test_delegates - called = false - mw = Middleware.new lambda { |env| - called = true - [200, {}, nil] - } - mw.call({}) - assert called, 'middleware delegated' - end - - def test_im_enabled_during_delegation - mw = Middleware.new lambda { |env| - assert IdentityMap.enabled?, 'identity map should be enabled' - [200, {}, nil] - } - mw.call({}) - end - - class Enum < Struct.new(:iter) - def each(&b) - iter.call(&b) - end - end - - def test_im_enabled_during_body_each - mw = Middleware.new lambda { |env| - [200, {}, Enum.new(lambda { |&b| - assert IdentityMap.enabled?, 'identity map should be enabled' - b.call "hello" - })] - } - body = mw.call({}).last - body.each { |x| assert_equal 'hello', x } - end - - def test_im_disabled_after_body_close - mw = Middleware.new lambda { |env| [200, {}, []] } - body = mw.call({}).last - assert IdentityMap.enabled?, 'identity map should be enabled' - body.close - assert !IdentityMap.enabled?, 'identity map should be disabled' - end - - def test_im_cleared_after_body_close - mw = Middleware.new lambda { |env| [200, {}, []] } - body = mw.call({}).last - - IdentityMap.repository['hello'] = 'world' - assert !IdentityMap.repository.empty?, 'repo should not be empty' - - body.close - assert IdentityMap.repository.empty?, 'repo should be empty' - end - end - end -end diff --git a/activerecord/test/cases/identity_map_test.rb b/activerecord/test/cases/identity_map_test.rb deleted file mode 100644 index 3efc8bf559..0000000000 --- a/activerecord/test/cases/identity_map_test.rb +++ /dev/null @@ -1,439 +0,0 @@ -require "cases/helper" - -require 'models/developer' -require 'models/project' -require 'models/company' -require 'models/topic' -require 'models/reply' -require 'models/computer' -require 'models/customer' -require 'models/order' -require 'models/post' -require 'models/author' -require 'models/tag' -require 'models/tagging' -require 'models/comment' -require 'models/sponsor' -require 'models/member' -require 'models/essay' -require 'models/subscriber' -require "models/pirate" -require "models/bird" -require "models/parrot" - -if ActiveRecord::IdentityMap.enabled? -class IdentityMapTest < ActiveRecord::TestCase - fixtures :accounts, :companies, :developers, :projects, :topics, - :developers_projects, :computers, :authors, :author_addresses, - :posts, :tags, :taggings, :comments, :subscribers - - ############################################################################## - # Basic tests checking if IM is functioning properly on basic find operations# - ############################################################################## - - def test_find_id - assert_same(Client.find(3), Client.find(3)) - end - - def test_find_id_without_identity_map - ActiveRecord::IdentityMap.without do - assert_not_same(Client.find(3), Client.find(3)) - end - end - - def test_find_id_use_identity_map - ActiveRecord::IdentityMap.enabled = false - ActiveRecord::IdentityMap.use do - assert_same(Client.find(3), Client.find(3)) - end - ActiveRecord::IdentityMap.enabled = true - end - - def test_find_pkey - assert_same( - Subscriber.find('swistak'), - Subscriber.find('swistak') - ) - end - - def test_find_by_id - assert_same( - Client.find_by_id(3), - Client.find_by_id(3) - ) - end - - def test_find_by_string_and_numeric_id - assert_same( - Client.find_by_id("3"), - Client.find_by_id(3) - ) - end - - def test_find_by_pkey - assert_same( - Subscriber.find_by_nick('swistak'), - Subscriber.find_by_nick('swistak') - ) - end - - def test_find_first_id - assert_same( - Client.find(:first, :conditions => {:id => 1}), - Client.find(:first, :conditions => {:id => 1}) - ) - end - - def test_find_first_pkey - assert_same( - Subscriber.find(:first, :conditions => {:nick => 'swistak'}), - Subscriber.find(:first, :conditions => {:nick => 'swistak'}) - ) - end - - def test_queries_are_not_executed_when_finding_by_id - Post.find 1 - assert_no_queries do - Post.find 1 - end - end - - ############################################################################## - # Tests checking if IM is functioning properly on more advanced finds # - # and associations # - ############################################################################## - - def test_owner_object_is_associated_from_identity_map - post = Post.find(1) - comment = post.comments.first - - assert_no_queries do - comment.post - end - assert_same post, comment.post - end - - def test_associated_object_are_assigned_from_identity_map - post = Post.find(1) - - post.comments.each do |comment| - assert_same post, comment.post - assert_equal post.object_id, comment.post.object_id - end - end - - def test_creation - t1 = Topic.create("title" => "t1") - t2 = Topic.find(t1.id) - assert_same(t1, t2) - end - - ############################################################################## - # Tests checking if IM is functioning properly on classes with multiple # - # types of inheritance # - ############################################################################## - - def test_inherited_without_type_attribute_without_identity_map - ActiveRecord::IdentityMap.without do - p1 = DestructivePirate.create!(:catchphrase => "I'm not a regular Pirate") - p2 = Pirate.find(p1.id) - assert_not_same(p1, p2) - end - end - - def test_inherited_with_type_attribute_without_identity_map - ActiveRecord::IdentityMap.without do - c = comments(:sub_special_comment) - c1 = SubSpecialComment.find(c.id) - c2 = Comment.find(c.id) - assert_same(c1.class, c2.class) - end - end - - def test_inherited_without_type_attribute - p1 = DestructivePirate.create!(:catchphrase => "I'm not a regular Pirate") - p2 = Pirate.find(p1.id) - assert_not_same(p1, p2) - end - - def test_inherited_with_type_attribute - c = comments(:sub_special_comment) - c1 = SubSpecialComment.find(c.id) - c2 = Comment.find(c.id) - assert_same(c1, c2) - end - - ############################################################################## - # Tests checking dirty attribute behavior with IM # - ############################################################################## - - def test_loading_new_instance_should_not_update_dirty_attributes - swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'}) - swistak.name = "Swistak Sreberkowiec" - assert_equal(["name"], swistak.changed) - assert_equal({"name" => ["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes) - - assert swistak.name_changed? - assert_equal("Swistak Sreberkowiec", swistak.name) - end - - def test_loading_new_instance_should_change_dirty_attribute_original_value - swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'}) - swistak.name = "Swistak Sreberkowiec" - - Subscriber.update_all({:name => "Raczkowski Marcin"}, {:name => "Marcin Raczkowski"}) - - assert_equal({"name"=>["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes) - assert_equal("Swistak Sreberkowiec", swistak.name) - end - - def test_loading_new_instance_should_remove_dirt - swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'}) - swistak.name = "Swistak Sreberkowiec" - - assert_equal({"name" => ["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes) - - Subscriber.update_all({:name => "Swistak Sreberkowiec"}, {:name => "Marcin Raczkowski"}) - - assert_equal("Swistak Sreberkowiec", swistak.name) - assert_equal({"name"=>["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes) - assert swistak.name_changed? - end - - def test_has_many_associations - pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?") - pirate.birds.create!(:name => 'Posideons Killer') - pirate.birds.create!(:name => 'Killer bandita Dionne') - - posideons, _ = pirate.birds - - pirate.reload - - pirate.birds_attributes = [{ :id => posideons.id, :name => 'Grace OMalley' }] - assert_equal 'Grace OMalley', pirate.birds.to_a.find { |r| r.id == posideons.id }.name - end - - def test_changing_associations - post1 = Post.create("title" => "One post", "body" => "Posting...") - post2 = Post.create("title" => "Another post", "body" => "Posting... Again...") - comment = Comment.new("body" => "comment") - - comment.post = post1 - assert comment.save - - assert_same(post1.comments.first, comment) - - comment.post = post2 - assert comment.save - - assert_same(post2.comments.first, comment) - assert_equal(0, post1.comments.size) - end - - def test_im_with_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins - tag = posts(:welcome).tags.first - tag_with_joins_and_select = posts(:welcome).tags.add_joins_and_select.first - assert_same(tag, tag_with_joins_and_select) - assert_nothing_raised(NoMethodError, "Joins/select was not loaded") { tag.author_id } - end - - ############################################################################## - # Tests checking Identity Map behavior with preloaded associations, joins, # - # includes etc. # - ############################################################################## - - def test_find_with_preloaded_associations - assert_queries(2) do - posts = Post.preload(:comments).order('posts.id') - assert posts.first.comments.first - end - - # With IM we'll retrieve post object from previous query, it'll have comments - # already preloaded from first call - assert_queries(1) do - posts = Post.preload(:comments).order('posts.id') - assert posts.first.comments.first - end - - assert_queries(2) do - posts = Post.preload(:author).order('posts.id') - assert posts.first.author - end - - # With IM we'll retrieve post object from previous query, it'll have comments - # already preloaded from first call - assert_queries(1) do - posts = Post.preload(:author).order('posts.id') - assert posts.first.author - end - - assert_queries(1) do - posts = Post.preload(:author, :comments).order('posts.id') - assert posts.first.author - assert posts.first.comments.first - end - end - - def test_find_with_included_associations - assert_queries(2) do - posts = Post.includes(:comments).order('posts.id') - assert posts.first.comments.first - end - - assert_queries(1) do - posts = Post.scoped.includes(:comments).order('posts.id') - assert posts.first.comments.first - end - - assert_queries(2) do - posts = Post.includes(:author).order('posts.id') - assert posts.first.author - end - - assert_queries(1) do - posts = Post.includes(:author, :comments).order('posts.id') - assert posts.first.author - assert posts.first.comments.first - end - end - - def test_eager_loading_with_conditions_on_joined_table_preloads - posts = Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') - assert_equal [posts(:welcome)], posts - assert_equal authors(:david), assert_no_queries { posts[0].author} - assert_same posts.first.author, Author.first - - posts = Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') - assert_equal [posts(:welcome)], posts - assert_equal authors(:david), assert_no_queries { posts[0].author} - assert_same posts.first.author, Author.first - - posts = Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id') - assert_equal posts(:welcome, :thinking), posts - assert_same posts.first.author, Author.first - - posts = Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id') - assert_equal posts(:welcome, :thinking), posts - assert_same posts.first.author, Author.first - end - - def test_eager_loading_with_conditions_on_string_joined_table_preloads - posts = assert_queries(2) do - Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') - end - assert_equal [posts(:welcome)], posts - assert_equal authors(:david), assert_no_queries { posts[0].author} - - posts = assert_queries(1) do - Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id') - end - assert_equal [posts(:welcome)], posts - assert_equal authors(:david), assert_no_queries { posts[0].author} - end - - ############################################################################## - # Behaviour related to saving failures - ############################################################################## - - def test_reload_object_if_save_failed - developer = Developer.first - developer.salary = 0 - - assert !developer.save - - same_developer = Developer.first - - assert_not_same developer, same_developer - assert_not_equal 0, same_developer.salary - assert_not_equal developer.salary, same_developer.salary - end - - def test_reload_object_if_forced_save_failed - developer = Developer.first - developer.salary = 0 - - assert_raise(ActiveRecord::RecordInvalid) { developer.save! } - - same_developer = Developer.first - - assert_not_same developer, same_developer - assert_not_equal 0, same_developer.salary - assert_not_equal developer.salary, same_developer.salary - end - - def test_reload_object_if_update_attributes_fails - developer = Developer.first - developer.salary = 0 - - assert !developer.update_attributes(:salary => 0) - - same_developer = Developer.first - - assert_not_same developer, same_developer - assert_not_equal 0, same_developer.salary - assert_not_equal developer.salary, same_developer.salary - end - - ############################################################################## - # Behaviour of readonly, frozen, destroyed - ############################################################################## - - def test_find_using_identity_map_respects_readonly_when_loading_associated_object_first - author = Author.first - readonly_comment = author.readonly_comments.first - - comment = Comment.first - assert !comment.readonly? - - assert readonly_comment.readonly? - - assert_raise(ActiveRecord::ReadOnlyRecord) {readonly_comment.save} - assert comment.save - end - - def test_find_using_identity_map_respects_readonly - comment = Comment.first - assert !comment.readonly? - - author = Author.first - readonly_comment = author.readonly_comments.first - - assert readonly_comment.readonly? - - assert_raise(ActiveRecord::ReadOnlyRecord) {readonly_comment.save} - assert comment.save - end - - def test_find_using_select_and_identity_map - author_id, author = Author.select('id').first, Author.first - - assert_equal author_id, author - assert_same author_id, author - assert_not_nil author.name - - post, post_id = Post.first, Post.select('id').first - - assert_equal post_id, post - assert_same post_id, post - assert_not_nil post.title - end - -# Currently AR is not allowing changing primary key (see Persistence#update) -# So we ignore it. If this changes, this test needs to be uncommented. -# def test_updating_of_pkey -# assert client = Client.find(3), -# client.update_attribute(:id, 666) -# -# assert Client.find(666) -# assert_same(client, Client.find(666)) -# -# s = Subscriber.find_by_nick('swistak') -# assert s.update_attribute(:nick, 'swistakTheJester') -# assert_equal('swistakTheJester', s.nick) -# -# assert stj = Subscriber.find_by_nick('swistakTheJester') -# assert_same(s, stj) -# end - -end -end diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 807274ca67..cc6baa6153 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -9,6 +9,7 @@ require 'models/string_key_object' require 'models/car' require 'models/engine' require 'models/wheel' +require 'models/treasure' class LockWithoutDefault < ActiveRecord::Base; end @@ -22,7 +23,7 @@ class ReadonlyFirstNamePerson < Person end class OptimisticLockingTest < ActiveRecord::TestCase - fixtures :people, :legacy_things, :references, :string_key_objects + fixtures :people, :legacy_things, :references, :string_key_objects, :peoples_treasures def test_non_integer_lock_existing s1 = StringKeyObject.find("record1") @@ -230,15 +231,24 @@ class OptimisticLockingTest < ActiveRecord::TestCase def test_polymorphic_destroy_with_dependencies_and_lock_version car = Car.create! - + assert_difference 'car.wheels.count' do car.wheels << Wheel.create! - end + end assert_difference 'car.wheels.count', -1 do car.destroy end assert car.destroyed? end + + def test_removing_has_and_belongs_to_many_associations_upon_destroy + p = RichPerson.create! first_name: 'Jon' + p.treasures.create! + assert !p.treasures.empty? + p.destroy + assert p.treasures.empty? + assert RichPerson.connection.select_all("SELECT * FROM peoples_treasures WHERE rich_person_id = 1").empty? + end end class OptimisticLockingWithSchemaChangeTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index d1f0ace184..acd2fcdad4 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -11,8 +11,6 @@ class LogSubscriberTest < ActiveRecord::TestCase def setup @old_logger = ActiveRecord::Base.logger - @using_identity_map = ActiveRecord::IdentityMap.enabled? - ActiveRecord::IdentityMap.enabled = false Developer.primary_key super ActiveRecord::LogSubscriber.attach_to(:active_record) @@ -22,7 +20,6 @@ class LogSubscriberTest < ActiveRecord::TestCase super ActiveRecord::LogSubscriber.log_subscribers.pop ActiveRecord::Base.logger = @old_logger - ActiveRecord::IdentityMap.enabled = @using_identity_map end def set_logger(logger) @@ -103,13 +100,4 @@ class LogSubscriberTest < ActiveRecord::TestCase def test_initializes_runtime Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join end - - def test_log - ActiveRecord::IdentityMap.use do - Post.find 1 - Post.find 1 - end - wait - assert_match(/From Identity Map/, @logger.logged(:debug).last) - end end diff --git a/activerecord/test/cases/multiple_db_test.rb b/activerecord/test/cases/multiple_db_test.rb index a802cfbf31..06d6596725 100644 --- a/activerecord/test/cases/multiple_db_test.rb +++ b/activerecord/test/cases/multiple_db_test.rb @@ -86,17 +86,25 @@ class MultipleDbTest < ActiveRecord::TestCase end def test_arel_table_engines - assert_equal Entrant.arel_engine, Bird.arel_engine + assert_not_equal Entrant.arel_engine, Bird.arel_engine + assert_not_equal Entrant.arel_engine, Course.arel_engine end - def test_associations_should_work_when_model_has_no_connection - begin - ActiveRecord::Model.remove_connection - assert_nothing_raised ActiveRecord::ConnectionNotEstablished do - College.first.courses.first + def test_connection + assert_equal Entrant.arel_engine.connection, Bird.arel_engine.connection + assert_not_equal Entrant.arel_engine.connection, Course.arel_engine.connection + end + + unless in_memory_db? + def test_associations_should_work_when_model_has_no_connection + begin + ActiveRecord::Model.remove_connection + assert_nothing_raised ActiveRecord::ConnectionNotEstablished do + College.first.courses.first + end + ensure + ActiveRecord::Model.establish_connection 'arunit' end - ensure - ActiveRecord::Model.establish_connection 'arunit' end end end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index e17ba76437..0d3c0b20a4 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -123,16 +123,6 @@ class NamedScopeTest < ActiveRecord::TestCase assert objects.all?(&:approved?), 'all objects should be approved' end - def test_extensions - assert_equal 1, Topic.anonymous_extension.one - assert_equal 2, Topic.named_extension.two - end - - def test_multiple_extensions - assert_equal 2, Topic.multiple_extensions.extension_two - assert_equal 1, Topic.multiple_extensions.extension_one - end - def test_has_many_associations_have_access_to_scopes assert_not_equal Post.containing_the_letter_a, authors(:david).posts assert !Post.containing_the_letter_a.empty? @@ -393,25 +383,6 @@ class NamedScopeTest < ActiveRecord::TestCase end end - def test_scopes_with_reserved_names - class << Topic - def public_method; end - public :public_method - - def protected_method; end - protected :protected_method - - def private_method; end - private :private_method - end - - [:public_method, :protected_method, :private_method].each do |reserved_method| - assert Topic.respond_to?(reserved_method, true) - ActiveRecord::Base.logger.expects(:warn) - Topic.scope(reserved_method) - end - end - def test_scopes_on_relations # Topic.replied approved_topics = Topic.scoped.approved.order('id DESC') @@ -483,6 +454,41 @@ class NamedScopeTest < ActiveRecord::TestCase require "models/without_table" end end + + def test_eager_scopes_are_deprecated + klass = Class.new(ActiveRecord::Base) + klass.table_name = 'posts' + + assert_deprecated do + klass.scope :welcome, { :conditions => { :id => posts(:welcome).id } } + end + assert_equal [posts(:welcome).title], klass.welcome.map(&:title) + + assert_deprecated do + klass.scope :welcome_2, klass.where(:id => posts(:welcome).id) + end + assert_equal [posts(:welcome).title], klass.welcome_2.map(&:title) + end + + def test_eager_default_scope_hashes_are_deprecated + klass = Class.new(ActiveRecord::Base) + klass.table_name = 'posts' + + assert_deprecated do + klass.send(:default_scope, :conditions => { :id => posts(:welcome).id }) + end + assert_equal [posts(:welcome).title], klass.all.map(&:title) + end + + def test_eager_default_scope_relations_are_deprecated + klass = Class.new(ActiveRecord::Base) + klass.table_name = 'posts' + + assert_deprecated do + klass.send(:default_scope, klass.where(:id => posts(:welcome).id)) + end + assert_equal [posts(:welcome).title], klass.all.map(&:title) + end end class DynamicScopeMatchTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 2ae9cb4888..0559bbbe9a 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -172,6 +172,19 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase man.interests_attributes = [{:id => interest.id, :topic => 'gardening'}] assert_equal man.interests.first.topic, man.interests[0].topic end + + def test_allows_class_to_override_setter_and_call_super + mean_pirate_class = Class.new(Pirate) do + accepts_nested_attributes_for :parrot + def parrot_attributes=(attrs) + super(attrs.merge(:color => "blue")) + end + end + mean_pirate = mean_pirate_class.new + mean_pirate.parrot_attributes = { :name => "James" } + assert_equal "James", mean_pirate.parrot.name + assert_equal "blue", mean_pirate.parrot.color + end end class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase @@ -663,7 +676,7 @@ module NestedAttributesOnACollectionAssociationTests end def test_should_sort_the_hash_by_the_keys_before_building_new_associated_models - attributes = ActiveSupport::OrderedHash.new + attributes = {} attributes['123726353'] = { :name => 'Grace OMalley' } attributes['2'] = { :name => 'Privateers Greed' } # 2 is lower then 123726353 @pirate.send(association_setter, attributes) @@ -673,7 +686,7 @@ module NestedAttributesOnACollectionAssociationTests def test_should_raise_an_argument_error_if_something_else_than_a_hash_is_passed assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, {}) } - assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, ActiveSupport::OrderedHash.new) } + assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, Hash.new) } assert_raise_with_message ArgumentError, 'Hash or Array expected, got String ("foo")' do @pirate.send(association_setter, "foo") diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index f36f4237b1..d93478513b 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -107,7 +107,7 @@ class QueryCacheTest < ActiveRecord::TestCase end def test_find_queries - assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) { Task.find(1); Task.find(1) } + assert_queries(2) { Task.find(1); Task.find(1) } end def test_find_queries_with_cache diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 7b1d65c6db..25eb7c1672 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -323,7 +323,7 @@ class RelationTest < ActiveRecord::TestCase assert posts.first.comments.first end - assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do + assert_queries(2) do posts = Post.preload(:comments).order('posts.id') assert posts.first.comments.first end @@ -333,12 +333,12 @@ class RelationTest < ActiveRecord::TestCase assert posts.first.author end - assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do + assert_queries(2) do posts = Post.preload(:author).order('posts.id') assert posts.first.author end - assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 3) do + assert_queries(3) do posts = Post.preload(:author, :comments).order('posts.id') assert posts.first.author assert posts.first.comments.first @@ -351,7 +351,7 @@ class RelationTest < ActiveRecord::TestCase assert posts.first.comments.first end - assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do + assert_queries(2) do posts = Post.scoped.includes(:comments).order('posts.id') assert posts.first.comments.first end @@ -361,7 +361,7 @@ class RelationTest < ActiveRecord::TestCase assert posts.first.author end - assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 3) do + assert_queries(3) do posts = Post.includes(:author, :comments).order('posts.id') assert posts.first.author assert posts.first.comments.first @@ -462,6 +462,18 @@ class RelationTest < ActiveRecord::TestCase assert_equal authors(:david), authors.find_or_create_by_name(:name => 'David') end + def test_dynamic_find_or_create_by_attributes_bang + authors = Author.scoped + + assert_raises(ActiveRecord::RecordInvalid) { authors.find_or_create_by_name!('') } + + lifo = authors.find_or_create_by_name!('Lifo') + assert_equal "Lifo", lifo.name + assert lifo.persisted? + + assert_equal authors(:david), authors.find_or_create_by_name!(:name => 'David') + end + def test_find_id authors = Author.scoped @@ -673,10 +685,8 @@ class RelationTest < ActiveRecord::TestCase end def test_relation_merging_with_preload - ActiveRecord::IdentityMap.without do - [Post.scoped.merge(Post.preload(:author)), Post.preload(:author).merge(Post.scoped)].each do |posts| - assert_queries(2) { assert posts.first.author } - end + [Post.scoped.merge(Post.preload(:author)), Post.preload(:author).merge(Post.scoped)].each do |posts| + assert_queries(2) { assert posts.first.author } end end @@ -1224,4 +1234,60 @@ class RelationTest < ActiveRecord::TestCase scope = Post.order('foo(comments.body)') assert_equal [], scope.references_values end + + def test_presence + topics = Topic.scoped + + # the first query is triggered because there are no topics yet. + assert_queries(1) { assert topics.present? } + + # checking if there are topics is used before you actually display them, + # thus it shouldn't invoke an extra count query. + assert_no_queries { assert topics.present? } + assert_no_queries { assert !topics.blank? } + + # shows count of topics and loops after loading the query should not trigger extra queries either. + assert_no_queries { topics.size } + assert_no_queries { topics.length } + assert_no_queries { topics.each } + + # count always trigger the COUNT query. + assert_queries(1) { topics.count } + + assert topics.loaded? + end + + test "find_by with hash conditions returns the first matching record" do + assert_equal posts(:eager_other), Post.order(:id).find_by(author_id: 2) + end + + test "find_by with non-hash conditions returns the first matching record" do + assert_equal posts(:eager_other), Post.order(:id).find_by("author_id = 2") + end + + test "find_by with multi-arg conditions returns the first matching record" do + assert_equal posts(:eager_other), Post.order(:id).find_by('author_id = ?', 2) + end + + test "find_by returns nil if the record is missing" do + assert_equal nil, Post.scoped.find_by("1 = 0") + end + + test "find_by! with hash conditions returns the first matching record" do + assert_equal posts(:eager_other), Post.order(:id).find_by!(author_id: 2) + end + + test "find_by! with non-hash conditions returns the first matching record" do + assert_equal posts(:eager_other), Post.order(:id).find_by!("author_id = 2") + end + + test "find_by! with multi-arg conditions returns the first matching record" do + assert_equal posts(:eager_other), Post.order(:id).find_by!('author_id = ?', 2) + end + + test "find_by! raises RecordNotFound if the record is missing" do + assert_raises(ActiveRecord::RecordNotFound) do + Post.scoped.find_by!("1 = 0") + end + end end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 3314013cd4..15ceaa1fcc 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -239,7 +239,7 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_schema_dump_includes_hstores_shorthand_definition output = standard_dump if %r{create_table "postgresql_hstores"} =~ output - assert_match %r{t.hstore "hash_store", default => ""}, output + assert_match %r[t.hstore "hash_store", :default => {}], output end end diff --git a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb index 628029f8df..a8e513d81f 100644 --- a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb @@ -8,6 +8,16 @@ class I18nGenerateMessageValidationTest < ActiveRecord::TestCase I18n.backend = I18n::Backend::Simple.new end + def reset_i18n_load_path + @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend + I18n.load_path.clear + I18n.backend = I18n::Backend::Simple.new + yield + ensure + I18n.load_path.replace @old_load_path + I18n.backend = @old_backend + end + # validates_associated: generate_message(attr_name, :invalid, :message => custom_message, :value => value) def test_generate_message_invalid_with_default_message assert_equal 'is invalid', @topic.errors.generate_message(:title, :invalid, :value => 'title') @@ -35,4 +45,13 @@ class I18nGenerateMessageValidationTest < ActiveRecord::TestCase assert_equal "Validation failed: Title is invalid, Title can't be blank", ActiveRecord::RecordInvalid.new(topic).message end + test "RecordInvalid exception translation falls back to the :errors namespace" do + reset_i18n_load_path do + I18n.backend.store_translations 'en', :errors => {:messages => {:record_invalid => 'fallback message'}} + topic = Topic.new + topic.errors.add(:title, :blank) + assert_equal "fallback message", ActiveRecord::RecordInvalid.new(topic).message + end + end + end diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 79442d68b0..ec09479c95 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -325,4 +325,16 @@ 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 end diff --git a/activerecord/test/fixtures/peoples_treasures.yml b/activerecord/test/fixtures/peoples_treasures.yml new file mode 100644 index 0000000000..a72b190d0c --- /dev/null +++ b/activerecord/test/fixtures/peoples_treasures.yml @@ -0,0 +1,3 @@ +michael_diamond: + rich_person_id: <%= ActiveRecord::Fixtures.identify(:michael) %> + treasure_id: <%= ActiveRecord::Fixtures.identify(:diamond) %> diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index d50e11d6c9..14444a0092 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -140,8 +140,8 @@ class Author < ActiveRecord::Base has_many :posts_with_default_include, :class_name => 'PostWithDefaultInclude' has_many :comments_on_posts_with_default_include, :through => :posts_with_default_include, :source => :comments - scope :relation_include_posts, includes(:posts) - scope :relation_include_tags, includes(:tags) + scope :relation_include_posts, -> { includes(:posts) } + scope :relation_include_tags, -> { includes(:tags) } attr_accessor :post_log after_initialize :set_post_log diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb index 888afc7604..640e57555d 100644 --- a/activerecord/test/models/bulb.rb +++ b/activerecord/test/models/bulb.rb @@ -1,5 +1,5 @@ class Bulb < ActiveRecord::Base - default_scope where(:name => 'defaulty') + default_scope { where(:name => 'defaulty') } belongs_to :car attr_protected :car_id, :frickinawesome diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb index 6ff1329d8e..42ac81690f 100644 --- a/activerecord/test/models/car.rb +++ b/activerecord/test/models/car.rb @@ -11,18 +11,18 @@ class Car < ActiveRecord::Base has_many :engines, :dependent => :destroy has_many :wheels, :as => :wheelable, :dependent => :destroy - scope :incl_tyres, includes(:tyres) - scope :incl_engines, includes(:engines) + scope :incl_tyres, -> { includes(:tyres) } + scope :incl_engines, -> { includes(:engines) } - scope :order_using_new_style, order('name asc') - scope :order_using_old_style, :order => 'name asc' + scope :order_using_new_style, -> { order('name asc') } + scope :order_using_old_style, -> { { :order => 'name asc' } } end class CoolCar < Car - default_scope :order => 'name desc' + default_scope { order('name desc') } end class FastCar < Car - default_scope :order => 'name desc' + default_scope { order('name desc') } end diff --git a/activerecord/test/models/categorization.rb b/activerecord/test/models/categorization.rb index 4bd980e606..6588531de6 100644 --- a/activerecord/test/models/categorization.rb +++ b/activerecord/test/models/categorization.rb @@ -12,7 +12,7 @@ end class SpecialCategorization < ActiveRecord::Base self.table_name = 'categorizations' - default_scope where(:special => true) + default_scope { where(:special => true) } belongs_to :author belongs_to :category diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index 02b85fd38a..ab3139680c 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -27,7 +27,7 @@ class Category < ActiveRecord::Base has_many :authors, :through => :categorizations has_many :authors_with_select, :through => :categorizations, :source => :author, :select => 'authors.*, categorizations.post_id' - scope :general, :conditions => { :name => 'General' } + scope :general, -> { where(:name => 'General') } end class SpecialCategory < Category diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 88b139d931..00f5a74070 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,12 +1,10 @@ class Comment < ActiveRecord::Base scope :limit_by, lambda {|l| limit(l) } - scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" - scope :not_again, where("comments.body NOT LIKE '%again%'") - scope :for_first_post, :conditions => { :post_id => 1 } - scope :for_first_author, - :joins => :post, - :conditions => { "posts.author_id" => 1 } - scope :created + scope :containing_the_letter_e, -> { where("comments.body LIKE '%e%'") } + scope :not_again, -> { where("comments.body NOT LIKE '%again%'") } + scope :for_first_post, -> { where(:post_id => 1) } + scope :for_first_author, -> { joins(:post).where("posts.author_id" => 1) } + scope :created, -> { scoped } belongs_to :post, :counter_cache => true has_many :ratings @@ -25,7 +23,7 @@ class Comment < ActiveRecord::Base def self.all_as_method all end - scope :all_as_scope, {} + scope :all_as_scope, -> { scoped } end class SpecialComment < Comment diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 4dc9fff9fd..4cc4947e3b 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -45,7 +45,7 @@ class Developer < ActiveRecord::Base has_many :audit_logs - scope :jamises, :conditions => {:name => 'Jamis'} + scope :jamises, -> { where(:name => 'Jamis') } validates_inclusion_of :salary, :in => 50000..200000 validates_length_of :name, :within => 3..20 @@ -88,20 +88,20 @@ end class DeveloperWithSelect < ActiveRecord::Base self.table_name = 'developers' - default_scope select('name') + default_scope { select('name') } end class DeveloperWithIncludes < ActiveRecord::Base self.table_name = 'developers' has_many :audit_logs, :foreign_key => :developer_id - default_scope includes(:audit_logs) + default_scope { includes(:audit_logs) } end class DeveloperOrderedBySalary < ActiveRecord::Base self.table_name = 'developers' - default_scope :order => 'salary DESC' + default_scope { order('salary DESC') } - scope :by_name, order('name DESC') + scope :by_name, -> { order('name DESC') } def self.all_ordered_by_name with_scope(:find => { :order => 'name DESC' }) do @@ -112,7 +112,7 @@ end class DeveloperCalledDavid < ActiveRecord::Base self.table_name = 'developers' - default_scope where("name = 'David'") + default_scope { where("name = 'David'") } end class LazyLambdaDeveloperCalledDavid < ActiveRecord::Base @@ -140,7 +140,7 @@ end class ClassMethodReferencingScopeDeveloperCalledDavid < ActiveRecord::Base self.table_name = 'developers' - scope :david, where(:name => 'David') + scope :david, -> { where(:name => 'David') } def self.default_scope david @@ -149,40 +149,40 @@ end class LazyBlockReferencingScopeDeveloperCalledDavid < ActiveRecord::Base self.table_name = 'developers' - scope :david, where(:name => 'David') + scope :david, -> { where(:name => 'David') } default_scope { david } end class DeveloperCalledJamis < ActiveRecord::Base self.table_name = 'developers' - default_scope where(:name => 'Jamis') - scope :poor, where('salary < 150000') + default_scope { where(:name => 'Jamis') } + scope :poor, -> { where('salary < 150000') } end class PoorDeveloperCalledJamis < ActiveRecord::Base self.table_name = 'developers' - default_scope where(:name => 'Jamis', :salary => 50000) + default_scope -> { where(:name => 'Jamis', :salary => 50000) } end class InheritedPoorDeveloperCalledJamis < DeveloperCalledJamis self.table_name = 'developers' - default_scope where(:salary => 50000) + default_scope -> { where(:salary => 50000) } end class MultiplePoorDeveloperCalledJamis < ActiveRecord::Base self.table_name = 'developers' - default_scope where(:name => 'Jamis') - default_scope where(:salary => 50000) + default_scope -> { where(:name => 'Jamis') } + default_scope -> { where(:salary => 50000) } end module SalaryDefaultScope extend ActiveSupport::Concern - included { default_scope where(:salary => 50000) } + included { default_scope { where(:salary => 50000) } } end class ModuleIncludedPoorDeveloperCalledJamis < DeveloperCalledJamis @@ -195,7 +195,7 @@ class EagerDeveloperWithDefaultScope < ActiveRecord::Base self.table_name = 'developers' has_and_belongs_to_many :projects, :foreign_key => 'developer_id', :join_table => 'developers_projects', :order => 'projects.id' - default_scope includes(:projects) + default_scope { includes(:projects) } end class EagerDeveloperWithClassMethodDefaultScope < ActiveRecord::Base diff --git a/activerecord/test/models/organization.rb b/activerecord/test/models/organization.rb index 4a4111833f..72e7bade68 100644 --- a/activerecord/test/models/organization.rb +++ b/activerecord/test/models/organization.rb @@ -8,5 +8,5 @@ class Organization < ActiveRecord::Base has_one :author, :primary_key => :name has_one :author_owned_essay_category, :through => :author, :source => :owned_essay_category - scope :clubs, { :from => 'clubs' } + scope :clubs, -> { from('clubs') } end diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index d2a0c6b40c..d5c0b351aa 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -1,8 +1,10 @@ class Person < ActiveRecord::Base has_many :readers + has_many :secure_readers has_one :reader has_many :posts, :through => :readers + has_many :secure_posts, :through => :secure_readers has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments, :conditions => 'comments.id is null', :references => :comments @@ -25,8 +27,8 @@ class Person < ActiveRecord::Base has_many :agents_posts, :through => :agents, :source => :posts has_many :agents_posts_authors, :through => :agents_posts, :source => :author - scope :males, :conditions => { :gender => 'M' } - scope :females, :conditions => { :gender => 'F' } + scope :males, -> { where(:gender => 'M') } + scope :females, -> { where(:gender => 'F') } end class PersonWithDependentDestroyJobs < ActiveRecord::Base @@ -83,3 +85,9 @@ class TightPerson < ActiveRecord::Base end class TightDescendant < TightPerson; end + +class RichPerson < ActiveRecord::Base + self.table_name = 'people' + + has_and_belongs_to_many :treasures, :join_table => 'peoples_treasures' +end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 1cab78d8c7..5002ab9ff8 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -5,8 +5,8 @@ class Post < ActiveRecord::Base end end - scope :containing_the_letter_a, where("body LIKE '%a%'") - scope :ranked_by_comments, order("comments_count DESC") + scope :containing_the_letter_a, -> { where("body LIKE '%a%'") } + scope :ranked_by_comments, -> { order("comments_count DESC") } scope :limit_by, lambda {|l| limit(l) } scope :with_authors_at_address, lambda { |address| { @@ -30,8 +30,8 @@ class Post < ActiveRecord::Base has_one :first_comment, :class_name => 'Comment', :order => 'id ASC' has_one :last_comment, :class_name => 'Comment', :order => 'id desc' - scope :with_special_comments, :joins => :comments, :conditions => {:comments => {:type => 'SpecialComment'} } - scope :with_very_special_comments, joins(:comments).where(:comments => {:type => 'VerySpecialComment'}) + scope :with_special_comments, -> { joins(:comments).where(:comments => {:type => 'SpecialComment'}) } + scope :with_very_special_comments, -> { joins(:comments).where(:comments => {:type => 'VerySpecialComment'}) } scope :with_post, lambda {|post_id| { :joins => :comments, :conditions => {:comments => {:post_id => post_id} } } } @@ -115,8 +115,10 @@ class Post < ActiveRecord::Base has_many :named_categories, :through => :standard_categorizations has_many :readers + has_many :secure_readers has_many :readers_with_person, :include => :person, :class_name => "Reader" has_many :people, :through => :readers + has_many :secure_people, :through => :secure_readers has_many :single_people, :through => :readers has_many :people_with_callbacks, :source=>:person, :through => :readers, :before_add => lambda {|owner, reader| log(:added, :before, reader.first_name) }, @@ -169,7 +171,7 @@ end class FirstPost < ActiveRecord::Base self.table_name = 'posts' - default_scope where(:id => 1) + default_scope { where(:id => 1) } has_many :comments, :foreign_key => :post_id has_one :comment, :foreign_key => :post_id @@ -177,16 +179,16 @@ end class PostWithDefaultInclude < ActiveRecord::Base self.table_name = 'posts' - default_scope includes(:comments) + default_scope { includes(:comments) } has_many :comments, :foreign_key => :post_id end class PostWithDefaultScope < ActiveRecord::Base self.table_name = 'posts' - default_scope :order => :title + default_scope { order(:title) } end class SpecialPostWithDefaultScope < ActiveRecord::Base self.table_name = 'posts' - default_scope where(:id => [1, 5,6]) + default_scope { where(:id => [1, 5,6]) } end diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index efe1ce67da..32ce164995 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -32,7 +32,7 @@ class Project < ActiveRecord::Base def self.all_as_method all end - scope :all_as_scope, {} + scope :all_as_scope, -> { scoped } end class SpecialProject < Project diff --git a/activerecord/test/models/reader.rb b/activerecord/test/models/reader.rb index 0207a2bd92..59005ac604 100644 --- a/activerecord/test/models/reader.rb +++ b/activerecord/test/models/reader.rb @@ -3,3 +3,12 @@ class Reader < ActiveRecord::Base belongs_to :person, :inverse_of => :readers belongs_to :single_person, :class_name => 'Person', :foreign_key => :person_id, :inverse_of => :reader end + +class SecureReader < ActiveRecord::Base + self.table_name = "readers" + + belongs_to :secure_post, :class_name => "Post", :foreign_key => "post_id" + belongs_to :secure_person, :inverse_of => :secure_readers, :class_name => "Person", :foreign_key => "person_id" + + attr_accessible nil +end diff --git a/activerecord/test/models/reference.rb b/activerecord/test/models/reference.rb index c5af0b5d5f..561b431766 100644 --- a/activerecord/test/models/reference.rb +++ b/activerecord/test/models/reference.rb @@ -19,5 +19,5 @@ end class BadReference < ActiveRecord::Base self.table_name = 'references' - default_scope where(:favourite => false) + default_scope { where(:favourite => false) } end diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index 6adfe0ae3c..53bc95e5f2 100644 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -1,7 +1,7 @@ require 'models/topic' class Reply < Topic - scope :base + scope :base, -> { scoped } belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true belongs_to :topic_with_primary_key, :class_name => "Topic", :primary_key => "title", :foreign_key => "parent_title", :counter_cache => "replies_count" diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 8bcb9df8a8..785839be75 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -1,21 +1,24 @@ class Topic < ActiveRecord::Base - scope :base + scope :base, -> { scoped } scope :written_before, lambda { |time| if time { :conditions => ['written_on < ?', time] } end } - scope :approved, :conditions => {:approved => true} - scope :rejected, :conditions => {:approved => false} + scope :approved, -> { where(:approved => true) } + scope :rejected, -> { where(:approved => false) } scope :scope_with_lambda, lambda { scoped } - scope :by_lifo, :conditions => {:author_name => 'lifo'} + scope :by_lifo, -> { where(:author_name => 'lifo') } - scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}} - scope 'approved_as_string', :conditions => {:approved => true} - scope :replied, :conditions => ['replies_count > 0'] - scope :anonymous_extension do + ActiveSupport::Deprecation.silence do + scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}} + scope :replied, :conditions => ['replies_count > 0'] + end + + scope 'approved_as_string', -> { where(:approved => true) } + scope :anonymous_extension, -> { scoped } do def one 1 end @@ -42,8 +45,8 @@ class Topic < ActiveRecord::Base 2 end end - scope :named_extension, :extend => NamedExtension - scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] + scope :named_extension, -> { { :extend => NamedExtension } } + scope :multiple_extensions, -> { { :extend => [MultipleExtensionTwo, MultipleExtensionOne] } } has_many :replies, :dependent => :destroy, :foreign_key => "parent_id" has_many :replies_with_primary_key, :class_name => "Reply", :dependent => :destroy, :primary_key => "title", :foreign_key => "parent_title" diff --git a/activerecord/test/models/toy.rb b/activerecord/test/models/toy.rb index 0377e50011..ddc7048a56 100644 --- a/activerecord/test/models/toy.rb +++ b/activerecord/test/models/toy.rb @@ -2,5 +2,5 @@ class Toy < ActiveRecord::Base self.primary_key = :toy_id belongs_to :pet - scope :with_pet, joins(:pet) + scope :with_pet, -> { joins(:pet) } end diff --git a/activerecord/test/models/without_table.rb b/activerecord/test/models/without_table.rb index 184ab1649e..50c824e4ac 100644 --- a/activerecord/test/models/without_table.rb +++ b/activerecord/test/models/without_table.rb @@ -1,3 +1,3 @@ class WithoutTable < ActiveRecord::Base - default_scope where(:published => true) + default_scope -> { where(:published => true) } end diff --git a/activerecord/test/schema/mysql2_specific_schema.rb b/activerecord/test/schema/mysql2_specific_schema.rb index ab2c7ccc10..65b6f9f227 100644 --- a/activerecord/test/schema/mysql2_specific_schema.rb +++ b/activerecord/test/schema/mysql2_specific_schema.rb @@ -1,5 +1,5 @@ ActiveRecord::Schema.define do - create_table :binary_fields, :force => true, :options => 'CHARACTER SET latin1' do |t| + create_table :binary_fields, :force => true do |t| t.binary :tiny_blob, :limit => 255 t.binary :normal_blob, :limit => 65535 t.binary :medium_blob, :limit => 16777215 @@ -32,4 +32,4 @@ CREATE TABLE collation_tests ( ) CHARACTER SET utf8 COLLATE utf8_general_ci SQL -end
\ No newline at end of file +end diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb index a0adfe3752..7d324f98c4 100644 --- a/activerecord/test/schema/mysql_specific_schema.rb +++ b/activerecord/test/schema/mysql_specific_schema.rb @@ -1,5 +1,5 @@ ActiveRecord::Schema.define do - create_table :binary_fields, :force => true, :options => 'CHARACTER SET latin1' do |t| + create_table :binary_fields, :force => true do |t| t.binary :tiny_blob, :limit => 255 t.binary :normal_blob, :limit => 65535 t.binary :medium_blob, :limit => 16777215 diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 428a85ab4e..377fde5c96 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -91,6 +91,7 @@ ActiveRecord::Schema.define do create_table :booleans, :force => true do |t| t.boolean :value + t.boolean :has_fun, :null => false, :default => false end create_table :bulbs, :force => true do |t| @@ -438,6 +439,7 @@ ActiveRecord::Schema.define do create_table :parrots, :force => true do |t| t.column :name, :string + t.column :color, :string t.column :parrot_sti_class, :string t.column :killer_id, :integer t.column :created_at, :datetime @@ -468,6 +470,11 @@ ActiveRecord::Schema.define do t.timestamps end + create_table :peoples_treasures, :id => false, :force => true do |t| + t.column :rich_person_id, :integer + t.column :treasure_id, :integer + end + create_table :pets, :primary_key => :pet_id ,:force => true do |t| t.string :name t.integer :owner_id, :integer diff --git a/activerecord/test/support/connection.rb b/activerecord/test/support/connection.rb index 11154c3797..c176316a05 100644 --- a/activerecord/test/support/connection.rb +++ b/activerecord/test/support/connection.rb @@ -12,7 +12,7 @@ module ARTest end def self.connect - puts "Using #{connection_name} with Identity Map #{ActiveRecord::IdentityMap.enabled? ? 'on' : 'off'}" + puts "Using #{connection_name}" ActiveRecord::Model.logger = ActiveSupport::Logger.new("debug.log") ActiveRecord::Model.configurations = connection_config ActiveRecord::Model.establish_connection 'arunit' diff --git a/activeresource/CHANGELOG.md b/activeresource/CHANGELOG.md deleted file mode 100644 index e092621a33..0000000000 --- a/activeresource/CHANGELOG.md +++ /dev/null @@ -1,385 +0,0 @@ -## Rails 4.0.0 (unreleased) ## - -* Adds support for PATCH requests. *dlee* - - -## Rails 3.2.1 (January 26, 2012) ## - -* Documentation fixes. - - -## Rails 3.2.0 (January 20, 2012) ## - -* Redirect responses: 303 See Other and 307 Temporary Redirect now behave like - 301 Moved Permanently and 302 Found. GH #3302. - - *Jim Herz* - - -## Rails 3.1.4 (unreleased) ## - -* No changes - - -## Rails 3.1.3 (November 20, 2011) ## - -* No changes - - -## Rails 3.1.2 (November 18, 2011) ## - -* No changes - - -## Rails 3.1.1 (October 7, 2011) ## - -* No changes. - - -## Rails 3.1.0 (August 30, 2011) ## - -* The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set `self.format = :xml` in the class. eg. - - class User < ActiveResource::Base self.format = :xml - end - - -## Rails 3.0.12 (unreleased) ## - -* No changes. - - -## Rails 3.0.11 (November 18, 2011) ## - -* No changes. - - -## Rails 3.0.10 (August 16, 2011) ## - -* No changes. - - -## Rails 3.0.9 (June 16, 2011) ## - -* No changes. - - -## Rails 3.0.8 (June 7, 2011) ## - -* No Changes - - -## Rails 3.0.7 (April 18, 2011) ## - -* No changes. - - -## Rails 3.0.6 (April 5, 2011) ## - -* No changes. - - -## Rails 3.0.5 (February 26, 2011) ## - -* No changes. - - -## Rails 3.0.4 (February 8, 2011) ## - -* No changes. - - -## Rails 3.0.3 (November 16, 2010) ## - -* No changes. - - -## Rails 3.0.2 (November 15, 2010) ## - -* No changes - - -## Rails 3.0.1 (October 15, 2010) ## - -* No Changes, just a version bump. - - -## Rails 3.0.0 (August 29, 2010) ## - -* JSON: set Base.include_root_in_json = true to include a root value in the JSON: {"post": {"title": ...}}. Mirrors the Active Record option. *Santiago Pastorino* - -* Add support for errors in JSON format. #1956 *Fabien Jakimowicz* - -* Recognizes 410 as Resource Gone. #2316 *Jordan Brough, Jatinder Singh* - -* More thorough SSL support. #2370 *Roy Nicholson* - -* HTTP proxy support. #2133 *Marshall Huss, Sébastien Dabet* - - -## 2.3.2 Final (March 15, 2009) ## - -* Nothing new, just included in 2.3.2 - - -## 2.2.1 RC2 (November 14th, 2008) ## - -* Fixed that ActiveResource#post would post an empty string when it shouldn't be posting anything #525 *Paolo Angelini* - - -## 2.2.0 RC1 (October 24th, 2008) ## - -* Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json. #1011 *Rasik Pandey, Cody Fauser* - -* Add ActiveResource::Base.find(:last). [#754 state:resolved] (Adrian Mugnolo) - -* Fixed problems with the logger used if the logging string included %'s [#840 state:resolved] (Jamis Buck) - -* Fixed Base#exists? to check status code as integer [#299 state:resolved] (Wes Oldenbeuving) - - -## 2.1.0 (May 31st, 2008) ## - -* Fixed response logging to use length instead of the entire thing (seangeo) *#27* - -* Fixed that to_param should be used and honored instead of hardcoding the id #11406 *gspiers* - -* Improve documentation. *Ryan Bigg, Jan De Poorter, Cheah Chu Yeow, Xavier Shay, Jack Danger Canty, Emilio Tagua, Xavier Noria, Sunny Ripert* - -* Use HEAD instead of GET in exists? *bscofield* - -* Fix small documentation typo. Closes #10670 *Luca Guidi* - -* find_or_create_resource_for handles module nesting. #10646 *xavier* - -* Allow setting ActiveResource::Base#format before #site. *Rick Olson* - -* Support agnostic formats when calling custom methods. Closes #10635 *joerichsen* - -* Document custom methods. #10589 *Cheah Chu Yeow* - -* Ruby 1.9 compatibility. *Jeremy Kemper* - - -## 2.0.2 (December 16th, 2007) ## - -* Added more specific exceptions for 400, 401, and 403 (all descending from ClientError so existing rescues will work) #10326 *trek* - -* Correct empty response handling. #10445 *seangeo* - - -## 2.0.1 (December 7th, 2007) ## - -* Don't cache net/http object so that ActiveResource is more thread-safe. Closes #10142 *kou* - -* Update XML documentation examples to include explicit type attributes. Closes #9754 *Josh Susser* - -* Added one-off declarations of mock behavior [David Heinemeier Hansson]. Example: - - Before: - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.xml", {}, "<person><name>David</name></person>" - end - - Now: - ActiveResource::HttpMock.respond_to.get "/people/1.xml", {}, "<person><name>David</name></person>" - -* Added ActiveResource.format= which defaults to :xml but can also be set to :json [David Heinemeier Hansson]. Example: - - class Person < ActiveResource::Base - self.site = "http://app/" - self.format = :json - end - - person = Person.find(1) # => GET http://app/people/1.json - person.name = "David" - person.save # => PUT http://app/people/1.json {name: "David"} - - Person.format = :xml - person.name = "Mary" - person.save # => PUT http://app/people/1.json <person><name>Mary</name></person> - -* Fix reload error when path prefix is used. #8727 *Ian Warshak* - -* Remove ActiveResource::Struct because it hasn't proven very useful. Creating a new ActiveResource::Base subclass is often less code and always clearer. #8612 *Josh Peek* - -* Fix query methods on resources. *Cody Fauser* - -* pass the prefix_options to the instantiated record when using find without a specific id. Closes #8544 *Eloy Duran* - -* Recognize and raise an exception on 405 Method Not Allowed responses. #7692 *Josh Peek* - -* Handle string and symbol param keys when splitting params into prefix params and query params. - - Comment.find(:all, :params => { :article_id => 5, :page => 2 }) or Comment.find(:all, :params => { 'article_id' => 5, :page => 2 }) - -* Added find-one with symbol [David Heinemeier Hansson]. Example: Person.find(:one, :from => :leader) # => GET /people/leader.xml - -* BACKWARDS INCOMPATIBLE: Changed the finder API to be more extensible with :params and more strict usage of scopes [David Heinemeier Hansson]. Changes: - - Person.find(:all, :title => "CEO") ...becomes: Person.find(:all, :params => { :title => "CEO" }) - Person.find(:managers) ...becomes: Person.find(:all, :from => :managers) - Person.find("/companies/1/manager.xml") ...becomes: Person.find(:one, :from => "/companies/1/manager.xml") - -* Add support for setting custom headers per Active Resource model *Rick Olson* - - class Project - headers['X-Token'] = 'foo' - end - - \# makes the GET request with the custom X-Token header - Project.find(:all) - -* Added find-by-path options to ActiveResource::Base.find [David Heinemeier Hansson]. Examples: - - employees = Person.find(:all, :from => "/companies/1/people.xml") # => GET /companies/1/people.xml - manager = Person.find("/companies/1/manager.xml") # => GET /companies/1/manager.xml - - -* Added support for using classes from within a single nested module [David Heinemeier Hansson]. Example: - - module Highrise - class Note < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - class Comment < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - end - - assert_kind_of Highrise::Comment, Note.find(1).comments.first - - -* Added load_attributes_from_response as a way of loading attributes from other responses than just create *David Heinemeier Hansson* - - class Highrise::Task < ActiveResource::Base - def complete - load_attributes_from_response(post(:complete)) - end - end - - ...will set "done_at" when complete is called. - - -* Added support for calling custom methods #6979 *rwdaigle* - - Person.find(:managers) # => GET /people/managers.xml - Kase.find(1).post(:close) # => POST /kases/1/close.xml - -* Remove explicit prefix_options parameter for ActiveResource::Base#initialize. *Rick Olson* - ActiveResource splits the prefix_options from it automatically. - -* Allow ActiveResource::Base.delete with custom prefix. *Rick Olson* - -* Add ActiveResource::Base#dup *Rick Olson* - -* Fixed constant warning when fetching the same object multiple times *David Heinemeier Hansson* - -* Added that saves which get a body response (and not just a 201) will use that response to update themselves *David Heinemeier Hansson* - -* Disregard namespaces from the default element name, so Highrise::Person will just try to fetch from "/people", not "/highrise/people" *David Heinemeier Hansson* - -* Allow array and hash query parameters. #7756 *Greg Spurrier* - -* Loading a resource preserves its prefix_options. #7353 *Ryan Daigle* - -* Carry over the convenience of #create from ActiveRecord. Closes #7340. *Ryan Daigle* - -* Increase ActiveResource::Base test coverage. Closes #7173, #7174 *Rich Collins* - -* Interpret 422 Unprocessable Entity as ResourceInvalid. #7097 *dkubb* - -* Mega documentation patches. #7025, #7069 *rwdaigle* - -* Base.exists?(id, options) and Base#exists? check whether the resource is found. #6970 *rwdaigle* - -* Query string support. *untext, Jeremy Kemper* - # GET /forums/1/topics.xml?sort=created_at - Topic.find(:all, :forum_id => 1, :sort => 'created_at') - -* Base#==, eql?, and hash methods. == returns true if its argument is identical to self or if it's an instance of the same class, is not new?, and has the same id. eql? is an alias for ==. hash delegates to id. *Jeremy Kemper* - -* Allow subclassed resources to share the site info *Rick Olson, Jeremy Kemper* - d class BeastResource < ActiveResource::Base - self.site = 'http://beast.caboo.se' - end - - class Forum < BeastResource - # taken from BeastResource - # self.site = 'http://beast.caboo.se' - end - - class Topic < BeastResource - self.site += '/forums/:forum_id' - end - -* Fix issues with ActiveResource collection handling. Closes #6291. *bmilekic* - -* Use attr_accessor_with_default to dry up attribute initialization. References #6538. *Stuart Halloway* - -* Add basic logging support for logging outgoing requests. *Jamis Buck* - -* Add Base.delete for deleting resources without having to instantiate them first. *Jamis Buck* - -* Make #save behavior mimic AR::Base#save (true on success, false on failure). *Jamis Buck* - -* Add Basic HTTP Authentication to ActiveResource (closes #6305). *jonathan* - -* Extracted #id_from_response as an entry point for customizing how a created resource gets its own ID. - By default, it extracts from the Location response header. - -* Optimistic locking: raise ActiveResource::ResourceConflict on 409 Conflict response. *Jeremy Kemper* - - # Example controller action - def update - @person.save! - rescue ActiveRecord::StaleObjectError - render :xml => @person.reload.to_xml, :status => '409 Conflict' - end - -* Basic validation support *Rick Olson* - - Parses the xml response of ActiveRecord::Errors#to_xml with a similar interface to ActiveRecord::Errors. - - render :xml => @person.errors.to_xml, :status => '400 Validation Error' - -* Deep hashes are converted into collections of resources. *Jeremy Kemper* - Person.new :name => 'Bob', - :address => { :id => 1, :city => 'Portland' }, - :contacts => [{ :id => 1 }, { :id => 2 }] - Looks for Address and Contact resources and creates them if unavailable. - So clients can fetch a complex resource in a single request if you e.g. - render :xml => @person.to_xml(:include => [:address, :contacts]) - in your controller action. - -* Major updates *Rick Olson* - - * Add full support for find/create/update/destroy - * Add support for specifying prefixes. - * Allow overriding of element_name, collection_name, and primary key - * Provide simpler HTTP mock interface for testing - - # rails routing code - map.resources :posts do |post| - post.resources :comments - end - - # ActiveResources - class Post < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000/" - end - - class Comment < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000/posts/:post_id/" - end - - @post = Post.find 5 - @comments = Comment.find :all, :post_id => @post.id - - @comment = Comment.new({:body => 'hello world'}, {:post_id => @post.id}) - @comment.save - -* Base.site= accepts URIs. 200...400 are valid response codes. PUT and POST request bodies default to ''. *Jeremy Kemper* - -* Initial checkin: object-oriented client for restful HTTP resources which follow the Rails convention. *David Heinemeier Hansson* diff --git a/activeresource/MIT-LICENSE b/activeresource/MIT-LICENSE deleted file mode 100644 index 187e748f83..0000000000 --- a/activeresource/MIT-LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2006-2012 David Heinemeier Hansson - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file diff --git a/activeresource/README.rdoc b/activeresource/README.rdoc deleted file mode 100644 index 8170f29973..0000000000 --- a/activeresource/README.rdoc +++ /dev/null @@ -1,189 +0,0 @@ -= Active Resource - -Active Resource (ARes) connects business objects and Representational State Transfer (REST) -web services. It implements object-relational mapping for REST web services to provide transparent -proxying capabilities between a client (ActiveResource) and a RESTful service (which is provided by Simply RESTful routing -in ActionController::Resources). - -== Philosophy - -Active Resource attempts to provide a coherent wrapper object-relational mapping for REST -web services. It follows the same philosophy as Active Record, in that one of its prime aims -is to reduce the amount of code needed to map to these resources. This is made possible -by relying on a number of code- and protocol-based conventions that make it easy for Active Resource -to infer complex relations and structures. These conventions are outlined in detail in the documentation -for ActiveResource::Base. - -== Overview - -Model classes are mapped to remote REST resources by Active Resource much the same way Active Record maps model classes to database -tables. When a request is made to a remote resource, a REST XML request is generated, transmitted, and the result -received and serialized into a usable Ruby object. - -== Download and installation - -The latest version of Active Support can be installed with RubyGems: - - % [sudo] gem install activeresource - -Source code can be downloaded as part of the Rails project on GitHub - -* https://github.com/rails/rails/tree/master/activeresource - -=== Configuration and Usage - -Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class -that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it: - - class Person < ActiveResource::Base - self.site = "http://api.people.com:3000" - end - -Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes -life cycle methods that operate against a persistent store. - - # Find a person with id = 1 - ryan = Person.find(1) - Person.exists?(1) # => true - -As you can see, the methods are quite similar to Active Record's methods for dealing with database -records. But rather than dealing directly with a database record, you're dealing with HTTP resources (which may or may not be database records). - -==== Protocol - -Active Resource is built on a standard XML format for requesting and submitting resources over HTTP. It mirrors the RESTful routing -built into Action Controller but will also work with any other REST service that properly implements the protocol. -REST uses HTTP, but unlike "typical" web applications, it makes use of all the verbs available in the HTTP specification: - -* GET requests are used for finding and retrieving resources. -* POST requests are used to create new resources. -* PUT requests are used to update existing resources. -* DELETE requests are used to delete resources. - -For more information on how this protocol works with Active Resource, see the ActiveResource::Base documentation; -for more general information on REST web services, see the article here[http://en.wikipedia.org/wiki/Representational_State_Transfer]. - -==== Find - -Find requests use the GET method and expect the XML form of whatever resource/resources is/are being requested. So, -for a request for a single element, the XML of that item is expected in response: - - # Expects a response of - # - # <person><id type="integer">1</id><attribute1>value1</attribute1><attribute2>..</attribute2></person> - # - # for GET http://api.people.com:3000/people/1.xml - # - ryan = Person.find(1) - -The XML document that is received is used to build a new object of type Person, with each -XML element becoming an attribute on the object. - - ryan.is_a? Person # => true - ryan.attribute1 # => 'value1' - -Any complex element (one that contains other elements) becomes its own object: - - # With this response: - # - # <person><id>1</id><attribute1>value1</attribute1><complex><attribute2>value2</attribute2></complex></person> - # - # for GET http://api.people.com:3000/people/1.xml - # - ryan = Person.find(1) - ryan.complex # => <Person::Complex::xxxxx> - ryan.complex.attribute2 # => 'value2' - -Collections can also be requested in a similar fashion - - # Expects a response of - # - # <people type="array"> - # <person><id type="integer">1</id><first>Ryan</first></person> - # <person><id type="integer">2</id><first>Jim</first></person> - # </people> - # - # for GET http://api.people.com:3000/people.xml - # - people = Person.all - people.first # => <Person::xxx 'first' => 'Ryan' ...> - people.last # => <Person::xxx 'first' => 'Jim' ...> - -==== Create - -Creating a new resource submits the XML form of the resource as the body of the request and expects -a 'Location' header in the response with the RESTful URL location of the newly created resource. The -id of the newly created resource is parsed out of the Location response header and automatically set -as the id of the ARes object. - - # <person><first>Ryan</first></person> - # - # is submitted as the body on - # - # POST http://api.people.com:3000/people.xml - # - # when save is called on a new Person object. An empty response is - # is expected with a 'Location' header value: - # - # Response (201): Location: http://api.people.com:3000/people/2 - # - ryan = Person.new(:first => 'Ryan') - ryan.new? # => true - ryan.save # => true - ryan.new? # => false - ryan.id # => 2 - -==== Update - -'save' is also used to update an existing resource and follows the same protocol as creating a resource -with the exception that no response headers are needed -- just an empty response when the update on the -server side was successful. - - # <person><first>Ryan</first></person> - # - # is submitted as the body on - # - # PUT http://api.people.com:3000/people/1.xml - # - # when save is called on an existing Person object. An empty response is - # is expected with code (204) - # - ryan = Person.find(1) - ryan.first # => 'Ryan' - ryan.first = 'Rizzle' - ryan.save # => true - -==== Delete - -Destruction of a resource can be invoked as a class and instance method of the resource. - - # A request is made to - # - # DELETE http://api.people.com:3000/people/1.xml - # - # for both of these forms. An empty response with - # is expected with response code (200) - # - ryan = Person.find(1) - ryan.destroy # => true - ryan.exists? # => false - Person.delete(2) # => true - Person.exists?(2) # => false - -== License - -Active Support is released under the MIT license: - -* http://www.opensource.org/licenses/MIT - -== Support - -API documentation is at - -* http://api.rubyonrails.org - -Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here: - -* https://github.com/rails/rails/issues - -You can find more usage information in the ActiveResource::Base documentation. diff --git a/activeresource/Rakefile b/activeresource/Rakefile deleted file mode 100755 index b85d8c7eb5..0000000000 --- a/activeresource/Rakefile +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env rake -require 'rake/testtask' -require 'rake/packagetask' -require 'rubygems/package_task' - -desc "Default Task" -task :default => [ :test ] - -# Run the unit tests - -Rake::TestTask.new { |t| - t.libs << "test" - t.pattern = 'test/**/*_test.rb' - t.warning = true - t.verbose = true -} - -namespace :test do - task :isolated do - ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME')) - activesupport_path = "#{File.dirname(__FILE__)}/../activesupport/lib" - Dir.glob("test/**/*_test.rb").all? do |file| - sh(ruby, '-w', "-Ilib:test:#{activesupport_path}", file) - end or raise "Failures" - end -end - -spec = eval(File.read('activeresource.gemspec')) - -Gem::PackageTask.new(spec) do |p| - p.gem_spec = spec -end - -task :lines do - lines, codelines, total_lines, total_codelines = 0, 0, 0, 0 - - FileList["lib/active_resource/**/*.rb"].each do |file_name| - next if file_name =~ /vendor/ - f = File.open(file_name) - - while line = f.gets - lines += 1 - next if line =~ /^\s*$/ - next if line =~ /^\s*#/ - codelines += 1 - end - puts "L: #{sprintf("%4d", lines)}, LOC #{sprintf("%4d", codelines)} | #{file_name}" - - total_lines += lines - total_codelines += codelines - - lines, codelines = 0, 0 - end - - puts "Total: Lines #{total_lines}, LOC #{total_codelines}" -end - - -# Publishing ------------------------------------------------------ - -desc "Release to gemcutter" -task :release => :package do - require 'rake/gemcutter' - Rake::Gemcutter::Tasks.new(spec).define - Rake::Task['gem:push'].invoke -end diff --git a/activeresource/activeresource.gemspec b/activeresource/activeresource.gemspec deleted file mode 100644 index ae1972a7d7..0000000000 --- a/activeresource/activeresource.gemspec +++ /dev/null @@ -1,24 +0,0 @@ -version = File.read(File.expand_path("../../RAILS_VERSION", __FILE__)).strip - -Gem::Specification.new do |s| - s.platform = Gem::Platform::RUBY - s.name = 'activeresource' - s.version = version - s.summary = 'REST modeling framework (part of Rails).' - s.description = 'REST on Rails. Wrap your RESTful web app with Ruby classes and work with them like Active Record models.' - - s.required_ruby_version = '>= 1.9.3' - - s.author = 'David Heinemeier Hansson' - s.email = 'david@loudthinking.com' - s.homepage = 'http://www.rubyonrails.org' - - s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'examples/**/*', 'lib/**/*'] - s.require_path = 'lib' - - s.extra_rdoc_files = %w( README.rdoc ) - s.rdoc_options.concat ['--main', 'README.rdoc'] - - s.add_dependency('activesupport', version) - s.add_dependency('activemodel', version) -end diff --git a/activeresource/examples/performance.rb b/activeresource/examples/performance.rb deleted file mode 100644 index e4df7a38a4..0000000000 --- a/activeresource/examples/performance.rb +++ /dev/null @@ -1,70 +0,0 @@ -require 'rubygems' -require 'active_resource' -require 'benchmark' - -TIMES = (ENV['N'] || 10_000).to_i - -# deep nested resource -attrs = { - :id => 1, - :name => 'Luis', - :age => 21, - :friends => [ - { - :name => 'JK', - :age => 24, - :colors => ['red', 'green', 'blue'], - :brothers => [ - { - :name => 'Mateo', - :age => 35, - :children => [{ :name => 'Edith', :age => 5 }, { :name => 'Martha', :age => 4 }] - }, - { - :name => 'Felipe', - :age => 33, - :children => [{ :name => 'Bryan', :age => 1 }, { :name => 'Luke', :age => 0 }] - } - ] - }, - { - :name => 'Eduardo', - :age => 20, - :colors => [], - :brothers => [ - { - :name => 'Sebas', - :age => 23, - :children => [{ :name => 'Andres', :age => 0 }, { :name => 'Jorge', :age => 2 }] - }, - { - :name => 'Elsa', - :age => 19, - :children => [{ :name => 'Natacha', :age => 1 }] - }, - { - :name => 'Milena', - :age => 16, - :children => [] - } - ] - } - ] -} - -class Customer < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" -end - -module Nested - class Customer < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end -end - -Benchmark.bm(40) do |x| - x.report('Model.new (instantiation)') { TIMES.times { Customer.new } } - x.report('Nested::Model.new (instantiation)') { TIMES.times { Nested::Customer.new } } - x.report('Model.new (setting attributes)') { TIMES.times { Customer.new attrs } } - x.report('Nested::Model.new (setting attributes)') { TIMES.times { Nested::Customer.new attrs } } -end diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb deleted file mode 100644 index ab06086631..0000000000 --- a/activeresource/lib/active_resource.rb +++ /dev/null @@ -1,45 +0,0 @@ -#-- -# Copyright (c) 2006-2012 David Heinemeier Hansson -# -# Permission is hereby granted, free of charge, to any person obtaining -# a copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so, subject to -# the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -#++ - -activesupport_path = File.expand_path('../../../activesupport/lib', __FILE__) -$:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.include?(activesupport_path) - -activemodel_path = File.expand_path('../../../activemodel/lib', __FILE__) -$:.unshift(activemodel_path) if File.directory?(activemodel_path) && !$:.include?(activemodel_path) - -require 'active_support' -require 'active_model' -require 'active_resource/version' - -module ActiveResource - extend ActiveSupport::Autoload - - autoload :Base - autoload :Connection - autoload :CustomMethods - autoload :Formats - autoload :HttpMock - autoload :Observing - autoload :Schema - autoload :Validations -end diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb deleted file mode 100644 index 0c2d070aef..0000000000 --- a/activeresource/lib/active_resource/base.rb +++ /dev/null @@ -1,1515 +0,0 @@ -require 'active_support' -require 'active_support/core_ext/class/attribute_accessors' -require 'active_support/core_ext/class/attribute' -require 'active_support/core_ext/hash/indifferent_access' -require 'active_support/core_ext/kernel/reporting' -require 'active_support/core_ext/module/delegation' -require 'active_support/core_ext/module/aliasing' -require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/object/to_query' -require 'active_support/core_ext/object/duplicable' -require 'set' -require 'uri' - -require 'active_support/core_ext/uri' -require 'active_resource/exceptions' -require 'active_resource/connection' -require 'active_resource/formats' -require 'active_resource/schema' -require 'active_resource/log_subscriber' - -module ActiveResource - # ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application. - # - # For an outline of what Active Resource is capable of, see its {README}[link:files/activeresource/README_rdoc.html]. - # - # == Automated mapping - # - # Active Resource objects represent your RESTful resources as manipulatable Ruby objects. To map resources - # to Ruby objects, Active Resource only needs a class name that corresponds to the resource name (e.g., the class - # Person maps to the resources people, very similarly to Active Record) and a +site+ value, which holds the - # URI of the resources. - # - # class Person < ActiveResource::Base - # self.site = "https://api.people.com" - # end - # - # Now the Person class is mapped to RESTful resources located at <tt>https://api.people.com/people/</tt>, and - # you can now use Active Resource's life cycle methods to manipulate resources. In the case where you already have - # an existing model with the same name as the desired RESTful resource you can set the +element_name+ value. - # - # class PersonResource < ActiveResource::Base - # self.site = "https://api.people.com" - # self.element_name = "person" - # end - # - # If your Active Resource object is required to use an HTTP proxy you can set the +proxy+ value which holds a URI. - # - # class PersonResource < ActiveResource::Base - # self.site = "https://api.people.com" - # self.proxy = "https://user:password@proxy.people.com:8080" - # end - # - # - # == Life cycle methods - # - # Active Resource exposes methods for creating, finding, updating, and deleting resources - # from REST web services. - # - # ryan = Person.new(:first => 'Ryan', :last => 'Daigle') - # ryan.save # => true - # ryan.id # => 2 - # Person.exists?(ryan.id) # => true - # ryan.exists? # => true - # - # ryan = Person.find(1) - # # Resource holding our newly created Person object - # - # ryan.first = 'Rizzle' - # ryan.save # => true - # - # ryan.destroy # => true - # - # As you can see, these are very similar to Active Record's life cycle methods for database records. - # You can read more about each of these methods in their respective documentation. - # - # === Custom REST methods - # - # Since simple CRUD/life cycle methods can't accomplish every task, Active Resource also supports - # defining your own custom REST methods. To invoke them, Active Resource provides the <tt>get</tt>, - # <tt>post</tt>, <tt>put</tt> and <tt>\delete</tt> methods where you can specify a custom REST method - # name to invoke. - # - # # POST to the custom 'register' REST method, i.e. POST /people/new/register.json. - # Person.new(:name => 'Ryan').post(:register) - # # => { :id => 1, :name => 'Ryan', :position => 'Clerk' } - # - # # PUT an update by invoking the 'promote' REST method, i.e. PUT /people/1/promote.json?position=Manager. - # Person.find(1).put(:promote, :position => 'Manager') - # # => { :id => 1, :name => 'Ryan', :position => 'Manager' } - # - # # GET all the positions available, i.e. GET /people/positions.json. - # Person.get(:positions) - # # => [{:name => 'Manager'}, {:name => 'Clerk'}] - # - # # DELETE to 'fire' a person, i.e. DELETE /people/1/fire.json. - # Person.find(1).delete(:fire) - # - # For more information on using custom REST methods, see the - # ActiveResource::CustomMethods documentation. - # - # == Validations - # - # You can validate resources client side by overriding validation methods in the base class. - # - # class Person < ActiveResource::Base - # self.site = "https://api.people.com" - # protected - # def validate - # errors.add("last", "has invalid characters") unless last =~ /[a-zA-Z]*/ - # end - # end - # - # See the ActiveResource::Validations documentation for more information. - # - # == Authentication - # - # Many REST APIs require authentication. The HTTP spec describes two ways to - # make requests with a username and password (see RFC 2617). - # - # Basic authentication simply sends a username and password along with HTTP - # requests. These sensitive credentials are sent unencrypted, visible to - # any onlooker, so this scheme should only be used with SSL. - # - # Digest authentication sends a crytographic hash of the username, password, - # HTTP method, URI, and a single-use secret key provided by the server. - # Sensitive credentials aren't visible to onlookers, so digest authentication - # doesn't require SSL. However, this doesn't mean the connection is secure! - # Just the username and password. - # - # (You really, really want to use SSL. There's little reason not to.) - # - # === Picking an authentication scheme - # - # Basic authentication is the default. To switch to digest authentication, - # set +auth_type+ to +:digest+: - # - # class Person < ActiveResource::Base - # self.auth_type = :digest - # end - # - # === Setting the username and password - # - # Set +user+ and +password+ on the class, or include them in the +site+ URL. - # - # class Person < ActiveResource::Base - # # Set user and password directly: - # self.user = "ryan" - # self.password = "password" - # - # # Or include them in the site: - # self.site = "https://ryan:password@api.people.com" - # end - # - # === Certificate Authentication - # - # You can also authenticate using an X509 certificate. <tt>See ssl_options=</tt> for all options. - # - # class Person < ActiveResource::Base - # self.site = "https://secure.api.people.com/" - # - # File.open(pem_file_path, 'rb') do |pem_file| - # self.ssl_options = { - # cert: OpenSSL::X509::Certificate.new(pem_file), - # key: OpenSSL::PKey::RSA.new(pem_file), - # ca_path: "/path/to/OpenSSL/formatted/CA_Certs", - # verify_mode: OpenSSL::SSL::VERIFY_PEER } - # end - # end - # - # - # == Errors & Validation - # - # Error handling and validation is handled in much the same manner as you're used to seeing in - # Active Record. Both the response code in the HTTP response and the body of the response are used to - # indicate that an error occurred. - # - # === Resource errors - # - # When a GET is requested for a resource that does not exist, the HTTP <tt>404</tt> (Resource Not Found) - # response code will be returned from the server which will raise an ActiveResource::ResourceNotFound - # exception. - # - # # GET https://api.people.com/people/999.json - # ryan = Person.find(999) # 404, raises ActiveResource::ResourceNotFound - # - # - # <tt>404</tt> is just one of the HTTP error response codes that Active Resource will handle with its own exception. The - # following HTTP response codes will also result in these exceptions: - # - # * 200..399 - Valid response. No exceptions, other than these redirects: - # * 301, 302, 303, 307 - ActiveResource::Redirection - # * 400 - ActiveResource::BadRequest - # * 401 - ActiveResource::UnauthorizedAccess - # * 403 - ActiveResource::ForbiddenAccess - # * 404 - ActiveResource::ResourceNotFound - # * 405 - ActiveResource::MethodNotAllowed - # * 409 - ActiveResource::ResourceConflict - # * 410 - ActiveResource::ResourceGone - # * 422 - ActiveResource::ResourceInvalid (rescued by save as validation errors) - # * 401..499 - ActiveResource::ClientError - # * 500..599 - ActiveResource::ServerError - # * Other - ActiveResource::ConnectionError - # - # These custom exceptions allow you to deal with resource errors more naturally and with more precision - # rather than returning a general HTTP error. For example: - # - # begin - # ryan = Person.find(my_id) - # rescue ActiveResource::ResourceNotFound - # redirect_to :action => 'not_found' - # rescue ActiveResource::ResourceConflict, ActiveResource::ResourceInvalid - # redirect_to :action => 'new' - # end - # - # When a GET is requested for a nested resource and you don't provide the prefix_param - # an ActiveResource::MissingPrefixParam will be raised. - # - # class Comment < ActiveResource::Base - # self.site = "https://someip.com/posts/:post_id" - # end - # - # Comment.find(1) - # # => ActiveResource::MissingPrefixParam: post_id prefix_option is missing - # - # === Validation errors - # - # Active Resource supports validations on resources and will return errors if any of these validations fail - # (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by - # a response code of <tt>422</tt> and an XML or JSON representation of the validation errors. The save operation will - # then fail (with a <tt>false</tt> return value) and the validation errors can be accessed on the resource in question. - # - # ryan = Person.find(1) - # ryan.first # => '' - # ryan.save # => false - # - # # When - # # PUT https://api.people.com/people/1.xml - # # or - # # PUT https://api.people.com/people/1.json - # # is requested with invalid values, the response is: - # # - # # Response (422): - # # <errors><error>First cannot be empty</error></errors> - # # or - # # {"errors":{"first":["cannot be empty"]}} - # # - # - # ryan.errors.invalid?(:first) # => true - # ryan.errors.full_messages # => ['First cannot be empty'] - # - # For backwards-compatibility with older endpoints, the following formats are also supported in JSON responses: - # - # # {"errors":['First cannot be empty']} - # # This was the required format for previous versions of ActiveResource - # # {"first":["cannot be empty"]} - # # This was the default format produced by respond_with in ActionController <3.2.1 - # - # Parsing either of these formats will result in a deprecation warning. - # - # Learn more about Active Resource's validation features in the ActiveResource::Validations documentation. - # - # === Timeouts - # - # Active Resource relies on HTTP to access RESTful APIs and as such is inherently susceptible to slow or - # unresponsive servers. In such cases, your Active Resource method calls could \timeout. You can control the - # amount of time before Active Resource times out with the +timeout+ variable. - # - # class Person < ActiveResource::Base - # self.site = "https://api.people.com" - # self.timeout = 5 - # end - # - # This sets the +timeout+ to 5 seconds. You can adjust the +timeout+ to a value suitable for the RESTful API - # you are accessing. It is recommended to set this to a reasonably low value to allow your Active Resource - # clients (especially if you are using Active Resource in a Rails application) to fail-fast (see - # http://en.wikipedia.org/wiki/Fail-fast) rather than cause cascading failures that could incapacitate your - # server. - # - # When a \timeout occurs, an ActiveResource::TimeoutError is raised. You should rescue from - # ActiveResource::TimeoutError in your Active Resource method calls. - # - # Internally, Active Resource relies on Ruby's Net::HTTP library to make HTTP requests. Setting +timeout+ - # sets the <tt>read_timeout</tt> of the internal Net::HTTP instance to the same value. The default - # <tt>read_timeout</tt> is 60 seconds on most Ruby implementations. - class Base - ## - # :singleton-method: - # The logger for diagnosing and tracing Active Resource calls. - cattr_accessor :logger - - class_attribute :_format - - class << self - # Creates a schema for this resource - setting the attributes that are - # known prior to fetching an instance from the remote system. - # - # The schema helps define the set of <tt>known_attributes</tt> of the - # current resource. - # - # There is no need to specify a schema for your Active Resource. If - # you do not, the <tt>known_attributes</tt> will be guessed from the - # instance attributes returned when an instance is fetched from the - # remote system. - # - # example: - # class Person < ActiveResource::Base - # schema do - # # define each attribute separately - # attribute 'name', :string - # - # # or use the convenience methods and pass >=1 attribute names - # string 'eye_color', 'hair_color' - # integer 'age' - # float 'height', 'weight' - # - # # unsupported types should be left as strings - # # overload the accessor methods if you need to convert them - # attribute 'created_at', 'string' - # end - # end - # - # p = Person.new - # p.respond_to? :name # => true - # p.respond_to? :age # => true - # p.name # => nil - # p.age # => nil - # - # j = Person.find_by_name('John') # <person><name>John</name><age>34</age><num_children>3</num_children></person> - # j.respond_to? :name # => true - # j.respond_to? :age # => true - # j.name # => 'John' - # j.age # => '34' # note this is a string! - # j.num_children # => '3' # note this is a string! - # - # p.num_children # => NoMethodError - # - # Attribute-types must be one of: - # string, integer, float - # - # Note: at present the attribute-type doesn't do anything, but stay - # tuned... - # Shortly it will also *cast* the value of the returned attribute. - # ie: - # j.age # => 34 # cast to an integer - # j.weight # => '65' # still a string! - # - def schema(&block) - if block_given? - schema_definition = Schema.new - schema_definition.instance_eval(&block) - - # skip out if we didn't define anything - return unless schema_definition.attrs.present? - - @schema ||= {}.with_indifferent_access - @known_attributes ||= [] - - schema_definition.attrs.each do |k,v| - @schema[k] = v - @known_attributes << k - end - - schema - else - @schema ||= nil - end - end - - # Alternative, direct way to specify a <tt>schema</tt> for this - # Resource. <tt>schema</tt> is more flexible, but this is quick - # for a very simple schema. - # - # Pass the schema as a hash with the keys being the attribute-names - # and the value being one of the accepted attribute types (as defined - # in <tt>schema</tt>) - # - # example: - # - # class Person < ActiveResource::Base - # schema = {'name' => :string, 'age' => :integer } - # end - # - # The keys/values can be strings or symbols. They will be converted to - # strings. - # - def schema=(the_schema) - unless the_schema.present? - # purposefully nulling out the schema - @schema = nil - @known_attributes = [] - return - end - - raise ArgumentError, "Expected a hash" unless the_schema.kind_of? Hash - - schema do - the_schema.each {|k,v| attribute(k,v) } - end - end - - # Returns the list of known attributes for this resource, gathered - # from the provided <tt>schema</tt> - # Attributes that are known will cause your resource to return 'true' - # when <tt>respond_to?</tt> is called on them. A known attribute will - # return nil if not set (rather than <t>MethodNotFound</tt>); thus - # known attributes can be used with <tt>validates_presence_of</tt> - # without a getter-method. - def known_attributes - @known_attributes ||= [] - end - - # Gets the URI of the REST resources to map for this class. The site variable is required for - # Active Resource's mapping to work. - def site - # Not using superclass_delegating_reader because don't want subclasses to modify superclass instance - # - # With superclass_delegating_reader - # - # Parent.site = 'https://anonymous@test.com' - # Subclass.site # => 'https://anonymous@test.com' - # Subclass.site.user = 'david' - # Parent.site # => 'https://david@test.com' - # - # Without superclass_delegating_reader (expected behavior) - # - # Parent.site = 'https://anonymous@test.com' - # Subclass.site # => 'https://anonymous@test.com' - # Subclass.site.user = 'david' # => TypeError: can't modify frozen object - # - if defined?(@site) - @site - elsif superclass != Object && superclass.site - superclass.site.dup.freeze - end - end - - # Sets the URI of the REST resources to map for this class to the value in the +site+ argument. - # The site variable is required for Active Resource's mapping to work. - def site=(site) - @connection = nil - if site.nil? - @site = nil - else - @site = create_site_uri_from(site) - @user = URI.parser.unescape(@site.user) if @site.user - @password = URI.parser.unescape(@site.password) if @site.password - end - end - - # Gets the \proxy variable if a proxy is required - def proxy - # Not using superclass_delegating_reader. See +site+ for explanation - if defined?(@proxy) - @proxy - elsif superclass != Object && superclass.proxy - superclass.proxy.dup.freeze - end - end - - # Sets the URI of the http proxy to the value in the +proxy+ argument. - def proxy=(proxy) - @connection = nil - @proxy = proxy.nil? ? nil : create_proxy_uri_from(proxy) - end - - # Gets the \user for REST HTTP authentication. - def user - # Not using superclass_delegating_reader. See +site+ for explanation - if defined?(@user) - @user - elsif superclass != Object && superclass.user - superclass.user.dup.freeze - end - end - - # Sets the \user for REST HTTP authentication. - def user=(user) - @connection = nil - @user = user - end - - # Gets the \password for REST HTTP authentication. - def password - # Not using superclass_delegating_reader. See +site+ for explanation - if defined?(@password) - @password - elsif superclass != Object && superclass.password - superclass.password.dup.freeze - end - end - - # Sets the \password for REST HTTP authentication. - def password=(password) - @connection = nil - @password = password - end - - def auth_type - if defined?(@auth_type) - @auth_type - end - end - - def auth_type=(auth_type) - @connection = nil - @auth_type = auth_type - end - - # Sets the format that attributes are sent and received in from a mime type reference: - # - # Person.format = :json - # Person.find(1) # => GET /people/1.json - # - # Person.format = ActiveResource::Formats::XmlFormat - # Person.find(1) # => GET /people/1.xml - # - # Default format is <tt>:json</tt>. - def format=(mime_type_reference_or_format) - format = mime_type_reference_or_format.is_a?(Symbol) ? - ActiveResource::Formats[mime_type_reference_or_format] : mime_type_reference_or_format - - self._format = format - connection.format = format if site - end - - # Returns the current format, default is ActiveResource::Formats::JsonFormat. - def format - self._format || ActiveResource::Formats::JsonFormat - end - - # Sets the number of seconds after which requests to the REST API should time out. - def timeout=(timeout) - @connection = nil - @timeout = timeout - end - - # Gets the number of seconds after which requests to the REST API should time out. - def timeout - if defined?(@timeout) - @timeout - elsif superclass != Object && superclass.timeout - superclass.timeout - end - end - - # Options that will get applied to an SSL connection. - # - # * <tt>:key</tt> - An OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object. - # * <tt>:cert</tt> - An OpenSSL::X509::Certificate object as client certificate - # * <tt>:ca_file</tt> - Path to a CA certification file in PEM format. The file can contain several CA certificates. - # * <tt>:ca_path</tt> - Path of a CA certification directory containing certifications in PEM format. - # * <tt>:verify_mode</tt> - Flags for server the certification verification at beginning of SSL/TLS session. (OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER is acceptable) - # * <tt>:verify_callback</tt> - The verify callback for the server certification verification. - # * <tt>:verify_depth</tt> - The maximum depth for the certificate chain verification. - # * <tt>:cert_store</tt> - OpenSSL::X509::Store to verify peer certificate. - # * <tt>:ssl_timeout</tt> -The SSL timeout in seconds. - def ssl_options=(opts={}) - @connection = nil - @ssl_options = opts - end - - # Returns the SSL options hash. - def ssl_options - if defined?(@ssl_options) - @ssl_options - elsif superclass != Object && superclass.ssl_options - superclass.ssl_options - end - end - - # An instance of ActiveResource::Connection that is the base \connection to the remote service. - # The +refresh+ parameter toggles whether or not the \connection is refreshed at every request - # or not (defaults to <tt>false</tt>). - def connection(refresh = false) - if defined?(@connection) || superclass == Object - @connection = Connection.new(site, format) if refresh || @connection.nil? - @connection.proxy = proxy if proxy - @connection.user = user if user - @connection.password = password if password - @connection.auth_type = auth_type if auth_type - @connection.timeout = timeout if timeout - @connection.ssl_options = ssl_options if ssl_options - @connection - else - superclass.connection - end - end - - def headers - @headers ||= {} - - if superclass != Object && superclass.headers - @headers = superclass.headers.merge(@headers) - else - @headers - end - end - - attr_writer :element_name - - def element_name - @element_name ||= model_name.element - end - - attr_writer :collection_name - - def collection_name - @collection_name ||= ActiveSupport::Inflector.pluralize(element_name) - end - - attr_writer :primary_key - - def primary_key - @primary_key ||= 'id' - end - - # Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>) - # This method is regenerated at runtime based on what the \prefix is set to. - def prefix(options={}) - default = site.path - default << '/' unless default[-1..-1] == '/' - # generate the actual method based on the current site path - self.prefix = default - prefix(options) - end - - # An attribute reader for the source string for the resource path \prefix. This - # method is regenerated at runtime based on what the \prefix is set to. - def prefix_source - prefix # generate #prefix and #prefix_source methods first - prefix_source - end - - # Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>). - # Default value is <tt>site.path</tt>. - def prefix=(value = '/') - # Replace :placeholders with '#{embedded options[:lookups]}' - prefix_call = value.gsub(/:\w+/) { |key| "\#{URI.parser.escape options[#{key}].to_s}" } - - # Clear prefix parameters in case they have been cached - @prefix_parameters = nil - - silence_warnings do - # Redefine the new methods. - instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def prefix_source() "#{value}" end - def prefix(options={}) "#{prefix_call}" end - RUBY_EVAL - end - rescue Exception => e - logger.error "Couldn't set prefix: #{e}\n #{code}" if logger - raise - end - - alias_method :set_prefix, :prefix= #:nodoc: - - alias_method :set_element_name, :element_name= #:nodoc: - alias_method :set_collection_name, :collection_name= #:nodoc: - - # Gets the element path for the given ID in +id+. If the +query_options+ parameter is omitted, Rails - # will split from the \prefix options. - # - # ==== Options - # +prefix_options+ - A \hash to add a \prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt> - # would yield a URL like <tt>/accounts/19/purchases.json</tt>). - # +query_options+ - A \hash to add items to the query string for the request. - # - # ==== Examples - # Post.element_path(1) - # # => /posts/1.json - # - # class Comment < ActiveResource::Base - # self.site = "https://37s.sunrise.com/posts/:post_id" - # end - # - # Comment.element_path(1, :post_id => 5) - # # => /posts/5/comments/1.json - # - # Comment.element_path(1, :post_id => 5, :active => 1) - # # => /posts/5/comments/1.json?active=1 - # - # Comment.element_path(1, {:post_id => 5}, {:active => 1}) - # # => /posts/5/comments/1.json?active=1 - # - def element_path(id, prefix_options = {}, query_options = nil) - check_prefix_options(prefix_options) - - prefix_options, query_options = split_options(prefix_options) if query_options.nil? - "#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}.#{format.extension}#{query_string(query_options)}" - end - - # Gets the new element path for REST resources. - # - # ==== Options - # * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt> - # would yield a URL like <tt>/accounts/19/purchases/new.json</tt>). - # - # ==== Examples - # Post.new_element_path - # # => /posts/new.json - # - # class Comment < ActiveResource::Base - # self.site = "https://37s.sunrise.com/posts/:post_id" - # end - # - # Comment.collection_path(:post_id => 5) - # # => /posts/5/comments/new.json - def new_element_path(prefix_options = {}) - "#{prefix(prefix_options)}#{collection_name}/new.#{format.extension}" - end - - # Gets the collection path for the REST resources. If the +query_options+ parameter is omitted, Rails - # will split from the +prefix_options+. - # - # ==== Options - # * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt> - # would yield a URL like <tt>/accounts/19/purchases.json</tt>). - # * +query_options+ - A hash to add items to the query string for the request. - # - # ==== Examples - # Post.collection_path - # # => /posts.json - # - # Comment.collection_path(:post_id => 5) - # # => /posts/5/comments.json - # - # Comment.collection_path(:post_id => 5, :active => 1) - # # => /posts/5/comments.json?active=1 - # - # Comment.collection_path({:post_id => 5}, {:active => 1}) - # # => /posts/5/comments.json?active=1 - # - def collection_path(prefix_options = {}, query_options = nil) - check_prefix_options(prefix_options) - prefix_options, query_options = split_options(prefix_options) if query_options.nil? - "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}" - end - - alias_method :set_primary_key, :primary_key= #:nodoc: - - # Builds a new, unsaved record using the default values from the remote server so - # that it can be used with RESTful forms. - # - # ==== Options - # * +attributes+ - A hash that overrides the default values from the server. - # - # Returns the new resource instance. - # - def build(attributes = {}) - attrs = self.format.decode(connection.get("#{new_element_path}").body).merge(attributes) - self.new(attrs) - end - - # Creates a new resource instance and makes a request to the remote service - # that it be saved, making it equivalent to the following simultaneous calls: - # - # ryan = Person.new(:first => 'ryan') - # ryan.save - # - # Returns the newly created resource. If a failure has occurred an - # exception will be raised (see <tt>save</tt>). If the resource is invalid and - # has not been saved then <tt>valid?</tt> will return <tt>false</tt>, - # while <tt>new?</tt> will still return <tt>true</tt>. - # - # ==== Examples - # Person.create(:name => 'Jeremy', :email => 'myname@nospam.com', :enabled => true) - # my_person = Person.find(:first) - # my_person.email # => myname@nospam.com - # - # dhh = Person.create(:name => 'David', :email => 'dhh@nospam.com', :enabled => true) - # dhh.valid? # => true - # dhh.new? # => false - # - # # We'll assume that there's a validation that requires the name attribute - # that_guy = Person.create(:name => '', :email => 'thatguy@nospam.com', :enabled => true) - # that_guy.valid? # => false - # that_guy.new? # => true - def create(attributes = {}) - self.new(attributes).tap { |resource| resource.save } - end - - # Core method for finding resources. Used similarly to Active Record's +find+ method. - # - # ==== Arguments - # The first argument is considered to be the scope of the query. That is, how many - # resources are returned from the request. It can be one of the following. - # - # * <tt>:one</tt> - Returns a single resource. - # * <tt>:first</tt> - Returns the first resource found. - # * <tt>:last</tt> - Returns the last resource found. - # * <tt>:all</tt> - Returns every resource that matches the request. - # - # ==== Options - # - # * <tt>:from</tt> - Sets the path or custom method that resources will be fetched from. - # * <tt>:params</tt> - Sets query and \prefix (nested URL) parameters. - # - # ==== Examples - # Person.find(1) - # # => GET /people/1.json - # - # Person.find(:all) - # # => GET /people.json - # - # Person.find(:all, :params => { :title => "CEO" }) - # # => GET /people.json?title=CEO - # - # Person.find(:first, :from => :managers) - # # => GET /people/managers.json - # - # Person.find(:last, :from => :managers) - # # => GET /people/managers.json - # - # Person.find(:all, :from => "/companies/1/people.json") - # # => GET /companies/1/people.json - # - # Person.find(:one, :from => :leader) - # # => GET /people/leader.json - # - # Person.find(:all, :from => :developers, :params => { :language => 'ruby' }) - # # => GET /people/developers.json?language=ruby - # - # Person.find(:one, :from => "/companies/1/manager.json") - # # => GET /companies/1/manager.json - # - # StreetAddress.find(1, :params => { :person_id => 1 }) - # # => GET /people/1/street_addresses/1.json - # - # == Failure or missing data - # A failure to find the requested object raises a ResourceNotFound - # exception if the find was called with an id. - # With any other scope, find returns nil when no data is returned. - # - # Person.find(1) - # # => raises ResourceNotFound - # - # Person.find(:all) - # Person.find(:first) - # Person.find(:last) - # # => nil - def find(*arguments) - scope = arguments.slice!(0) - options = arguments.slice!(0) || {} - - case scope - when :all then find_every(options) - when :first then find_every(options).first - when :last then find_every(options).last - when :one then find_one(options) - else find_single(scope, options) - end - end - - - # A convenience wrapper for <tt>find(:first, *args)</tt>. You can pass - # in all the same arguments to this method as you can to - # <tt>find(:first)</tt>. - def first(*args) - find(:first, *args) - end - - # A convenience wrapper for <tt>find(:last, *args)</tt>. You can pass - # in all the same arguments to this method as you can to - # <tt>find(:last)</tt>. - def last(*args) - find(:last, *args) - end - - # This is an alias for find(:all). You can pass in all the same - # arguments to this method as you can to <tt>find(:all)</tt> - def all(*args) - find(:all, *args) - end - - - # Deletes the resources with the ID in the +id+ parameter. - # - # ==== Options - # All options specify \prefix and query parameters. - # - # ==== Examples - # Event.delete(2) # sends DELETE /events/2 - # - # Event.create(:name => 'Free Concert', :location => 'Community Center') - # my_event = Event.find(:first) # let's assume this is event with ID 7 - # Event.delete(my_event.id) # sends DELETE /events/7 - # - # # Let's assume a request to events/5/cancel.json - # Event.delete(params[:id]) # sends DELETE /events/5 - def delete(id, options = {}) - connection.delete(element_path(id, options)) - end - - # Asserts the existence of a resource, returning <tt>true</tt> if the resource is found. - # - # ==== Examples - # Note.create(:title => 'Hello, world.', :body => 'Nothing more for now...') - # Note.exists?(1) # => true - # - # Note.exists(1349) # => false - def exists?(id, options = {}) - if id - prefix_options, query_options = split_options(options[:params]) - path = element_path(id, prefix_options, query_options) - response = connection.head(path, headers) - response.code.to_i == 200 - end - # id && !find_single(id, options).nil? - rescue ActiveResource::ResourceNotFound, ActiveResource::ResourceGone - false - end - - private - - def check_prefix_options(prefix_options) - p_options = HashWithIndifferentAccess.new(prefix_options) - prefix_parameters.each do |p| - raise(MissingPrefixParam, "#{p} prefix_option is missing") if p_options[p].blank? - end - end - - # Find every resource - def find_every(options) - begin - case from = options[:from] - when Symbol - instantiate_collection(get(from, options[:params])) - when String - path = "#{from}#{query_string(options[:params])}" - instantiate_collection(format.decode(connection.get(path, headers).body) || []) - else - prefix_options, query_options = split_options(options[:params]) - path = collection_path(prefix_options, query_options) - instantiate_collection( (format.decode(connection.get(path, headers).body) || []), prefix_options ) - end - rescue ActiveResource::ResourceNotFound - # Swallowing ResourceNotFound exceptions and return nil - as per - # ActiveRecord. - nil - end - end - - # Find a single resource from a one-off URL - def find_one(options) - case from = options[:from] - when Symbol - instantiate_record(get(from, options[:params])) - when String - path = "#{from}#{query_string(options[:params])}" - instantiate_record(format.decode(connection.get(path, headers).body)) - end - end - - # Find a single resource from the default URL - def find_single(scope, options) - prefix_options, query_options = split_options(options[:params]) - path = element_path(scope, prefix_options, query_options) - instantiate_record(format.decode(connection.get(path, headers).body), prefix_options) - end - - def instantiate_collection(collection, prefix_options = {}) - collection.collect! { |record| instantiate_record(record, prefix_options) } - end - - def instantiate_record(record, prefix_options = {}) - new(record, true).tap do |resource| - resource.prefix_options = prefix_options - end - end - - - # Accepts a URI and creates the site URI from that. - def create_site_uri_from(site) - site.is_a?(URI) ? site.dup : URI.parse(site) - end - - # Accepts a URI and creates the proxy URI from that. - def create_proxy_uri_from(proxy) - proxy.is_a?(URI) ? proxy.dup : URI.parse(proxy) - end - - # contains a set of the current prefix parameters. - def prefix_parameters - @prefix_parameters ||= prefix_source.scan(/:\w+/).map { |key| key[1..-1].to_sym }.to_set - end - - # Builds the query string for the request. - def query_string(options) - "?#{options.to_query}" unless options.nil? || options.empty? - end - - # split an option hash into two hashes, one containing the prefix options, - # and the other containing the leftovers. - def split_options(options = {}) - prefix_options, query_options = {}, {} - - (options || {}).each do |key, value| - next if key.blank? || !key.respond_to?(:to_sym) - (prefix_parameters.include?(key.to_sym) ? prefix_options : query_options)[key.to_sym] = value - end - - [ prefix_options, query_options ] - end - end - - attr_accessor :attributes #:nodoc: - attr_accessor :prefix_options #:nodoc: - - # If no schema has been defined for the class (see - # <tt>ActiveResource::schema=</tt>), the default automatic schema is - # generated from the current instance's attributes - def schema - self.class.schema || self.attributes - end - - # This is a list of known attributes for this resource. Either - # gathered from the provided <tt>schema</tt>, or from the attributes - # set on this instance after it has been fetched from the remote system. - def known_attributes - self.class.known_attributes + self.attributes.keys.map(&:to_s) - end - - - # Constructor method for \new resources; the optional +attributes+ parameter takes a \hash - # of attributes for the \new resource. - # - # ==== Examples - # my_course = Course.new - # my_course.name = "Western Civilization" - # my_course.lecturer = "Don Trotter" - # my_course.save - # - # my_other_course = Course.new(:name => "Philosophy: Reason and Being", :lecturer => "Ralph Cling") - # my_other_course.save - def initialize(attributes = {}, persisted = false) - @attributes = {}.with_indifferent_access - @prefix_options = {} - @persisted = persisted - load(attributes) - end - - # Returns a \clone of the resource that hasn't been assigned an +id+ yet and - # is treated as a \new resource. - # - # ryan = Person.find(1) - # not_ryan = ryan.clone - # not_ryan.new? # => true - # - # Any active resource member attributes will NOT be cloned, though all other - # attributes are. This is to prevent the conflict between any +prefix_options+ - # that refer to the original parent resource and the newly cloned parent - # resource that does not exist. - # - # ryan = Person.find(1) - # ryan.address = StreetAddress.find(1, :person_id => ryan.id) - # ryan.hash = {:not => "an ARes instance"} - # - # not_ryan = ryan.clone - # not_ryan.new? # => true - # not_ryan.address # => NoMethodError - # not_ryan.hash # => {:not => "an ARes instance"} - def clone - # Clone all attributes except the pk and any nested ARes - cloned = Hash[attributes.reject {|k,v| k == self.class.primary_key || v.is_a?(ActiveResource::Base)}.map { |k, v| [k, v.clone] }] - # Form the new resource - bypass initialize of resource with 'new' as that will call 'load' which - # attempts to convert hashes into member objects and arrays into collections of objects. We want - # the raw objects to be cloned so we bypass load by directly setting the attributes hash. - resource = self.class.new({}) - resource.prefix_options = self.prefix_options - resource.send :instance_variable_set, '@attributes', cloned - resource - end - - - # Returns +true+ if this object hasn't yet been saved, otherwise, returns +false+. - # - # ==== Examples - # not_new = Computer.create(:brand => 'Apple', :make => 'MacBook', :vendor => 'MacMall') - # not_new.new? # => false - # - # is_new = Computer.new(:brand => 'IBM', :make => 'Thinkpad', :vendor => 'IBM') - # is_new.new? # => true - # - # is_new.save - # is_new.new? # => false - # - def new? - !persisted? - end - alias :new_record? :new? - - # Returns +true+ if this object has been saved, otherwise returns +false+. - # - # ==== Examples - # persisted = Computer.create(:brand => 'Apple', :make => 'MacBook', :vendor => 'MacMall') - # persisted.persisted? # => true - # - # not_persisted = Computer.new(:brand => 'IBM', :make => 'Thinkpad', :vendor => 'IBM') - # not_persisted.persisted? # => false - # - # not_persisted.save - # not_persisted.persisted? # => true - # - def persisted? - @persisted - end - - # Gets the <tt>\id</tt> attribute of the resource. - def id - attributes[self.class.primary_key] - end - - # Sets the <tt>\id</tt> attribute of the resource. - def id=(id) - attributes[self.class.primary_key] = id - end - - # Test for equality. Resource are equal if and only if +other+ is the same object or - # is an instance of the same class, is not <tt>new?</tt>, and has the same +id+. - # - # ==== Examples - # ryan = Person.create(:name => 'Ryan') - # jamie = Person.create(:name => 'Jamie') - # - # ryan == jamie - # # => false (Different name attribute and id) - # - # ryan_again = Person.new(:name => 'Ryan') - # ryan == ryan_again - # # => false (ryan_again is new?) - # - # ryans_clone = Person.create(:name => 'Ryan') - # ryan == ryans_clone - # # => false (Different id attributes) - # - # ryans_twin = Person.find(ryan.id) - # ryan == ryans_twin - # # => true - # - def ==(other) - other.equal?(self) || (other.instance_of?(self.class) && other.id == id && other.prefix_options == prefix_options) - end - - # Tests for equality (delegates to ==). - def eql?(other) - self == other - end - - # Delegates to id in order to allow two resources of the same type and \id to work with something like: - # [(a = Person.find 1), (b = Person.find 2)] & [(c = Person.find 1), (d = Person.find 4)] # => [a] - def hash - id.hash - end - - # Duplicates the current resource without saving it. - # - # ==== Examples - # my_invoice = Invoice.create(:customer => 'That Company') - # next_invoice = my_invoice.dup - # next_invoice.new? # => true - # - # next_invoice.save - # next_invoice == my_invoice # => false (different id attributes) - # - # my_invoice.customer # => That Company - # next_invoice.customer # => That Company - def dup - self.class.new.tap do |resource| - resource.attributes = @attributes - resource.prefix_options = @prefix_options - end - end - - # Saves (+POST+) or \updates (+PUT+) a resource. Delegates to +create+ if the object is \new, - # +update+ if it exists. If the response to the \save includes a body, it will be assumed that this body - # is Json for the final object as it looked after the \save (which would include attributes like +created_at+ - # that weren't part of the original submit). - # - # ==== Examples - # my_company = Company.new(:name => 'RoleModel Software', :owner => 'Ken Auer', :size => 2) - # my_company.new? # => true - # my_company.save # sends POST /companies/ (create) - # - # my_company.new? # => false - # my_company.size = 10 - # my_company.save # sends PUT /companies/1 (update) - def save - new? ? create : update - end - - # Saves the resource. - # - # If the resource is new, it is created via +POST+, otherwise the - # existing resource is updated via +PUT+. - # - # With <tt>save!</tt> validations always run. If any of them fail - # ActiveResource::ResourceInvalid gets raised, and nothing is POSTed to - # the remote system. - # See ActiveResource::Validations for more information. - # - # There's a series of callbacks associated with <tt>save!</tt>. If any - # of the <tt>before_*</tt> callbacks return +false+ the action is - # cancelled and <tt>save!</tt> raises ActiveResource::ResourceInvalid. - def save! - save || raise(ResourceInvalid.new(self)) - end - - # Deletes the resource from the remote service. - # - # ==== Examples - # my_id = 3 - # my_person = Person.find(my_id) - # my_person.destroy - # Person.find(my_id) # 404 (Resource Not Found) - # - # new_person = Person.create(:name => 'James') - # new_id = new_person.id # => 7 - # new_person.destroy - # Person.find(new_id) # 404 (Resource Not Found) - def destroy - connection.delete(element_path, self.class.headers) - end - - # Evaluates to <tt>true</tt> if this resource is not <tt>new?</tt> and is - # found on the remote service. Using this method, you can check for - # resources that may have been deleted between the object's instantiation - # and actions on it. - # - # ==== Examples - # Person.create(:name => 'Theodore Roosevelt') - # that_guy = Person.find(:first) - # that_guy.exists? # => true - # - # that_lady = Person.new(:name => 'Paul Bean') - # that_lady.exists? # => false - # - # guys_id = that_guy.id - # Person.delete(guys_id) - # that_guy.exists? # => false - def exists? - !new? && self.class.exists?(to_param, :params => prefix_options) - end - - # Returns the serialized string representation of the resource in the configured - # serialization format specified in ActiveResource::Base.format. The options - # applicable depend on the configured encoding format. - def encode(options={}) - send("to_#{self.class.format.extension}", options) - end - - # A method to \reload the attributes of this object from the remote web service. - # - # ==== Examples - # my_branch = Branch.find(:first) - # my_branch.name # => "Wislon Raod" - # - # # Another client fixes the typo... - # - # my_branch.name # => "Wislon Raod" - # my_branch.reload - # my_branch.name # => "Wilson Road" - def reload - self.load(self.class.find(to_param, :params => @prefix_options).attributes) - end - - # A method to manually load attributes from a \hash. Recursively loads collections of - # resources. This method is called in +initialize+ and +create+ when a \hash of attributes - # is provided. - # - # ==== Examples - # my_attrs = {:name => 'J&J Textiles', :industry => 'Cloth and textiles'} - # my_attrs = {:name => 'Marty', :colors => ["red", "green", "blue"]} - # - # the_supplier = Supplier.find(:first) - # the_supplier.name # => 'J&M Textiles' - # the_supplier.load(my_attrs) - # the_supplier.name('J&J Textiles') - # - # # These two calls are the same as Supplier.new(my_attrs) - # my_supplier = Supplier.new - # my_supplier.load(my_attrs) - # - # # These three calls are the same as Supplier.create(my_attrs) - # your_supplier = Supplier.new - # your_supplier.load(my_attrs) - # your_supplier.save - def load(attributes, remove_root = false) - raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) - @prefix_options, attributes = split_options(attributes) - - if attributes.keys.size == 1 - remove_root = self.class.element_name == attributes.keys.first.to_s - end - - attributes = Formats.remove_root(attributes) if remove_root - - attributes.each do |key, value| - @attributes[key.to_s] = - case value - when Array - resource = nil - value.map do |attrs| - if attrs.is_a?(Hash) - resource ||= find_or_create_resource_for_collection(key) - resource.new(attrs) - else - attrs.duplicable? ? attrs.dup : attrs - end - end - when Hash - resource = find_or_create_resource_for(key) - resource.new(value) - else - value.duplicable? ? value.dup : value - end - end - self - end - - # Updates a single attribute and then saves the object. - # - # Note: Unlike ActiveRecord::Base.update_attribute, this method <b>is</b> - # subject to normal validation routines as an update sends the whole body - # of the resource in the request. (See Validations). - # - # As such, this method is equivalent to calling update_attributes with a single attribute/value pair. - # - # If the saving fails because of a connection or remote service error, an - # exception will be raised. If saving fails because the resource is - # invalid then <tt>false</tt> will be returned. - def update_attribute(name, value) - self.send("#{name}=".to_sym, value) - self.save - end - - # Updates this resource with all the attributes from the passed-in Hash - # and requests that the record be saved. - # - # If the saving fails because of a connection or remote service error, an - # exception will be raised. If saving fails because the resource is - # invalid then <tt>false</tt> will be returned. - # - # Note: Though this request can be made with a partial set of the - # resource's attributes, the full body of the request will still be sent - # in the save request to the remote service. - def update_attributes(attributes) - load(attributes, false) && save - end - - # For checking <tt>respond_to?</tt> without searching the attributes (which is faster). - alias_method :respond_to_without_attributes?, :respond_to? - - # A method to determine if an object responds to a message (e.g., a method call). In Active Resource, a Person object with a - # +name+ attribute can answer <tt>true</tt> to <tt>my_person.respond_to?(:name)</tt>, <tt>my_person.respond_to?(:name=)</tt>, and - # <tt>my_person.respond_to?(:name?)</tt>. - def respond_to?(method, include_priv = false) - method_name = method.to_s - if attributes.nil? - super - elsif known_attributes.include?(method_name) - true - elsif method_name =~ /(?:=|\?)$/ && attributes.include?($`) - true - else - # super must be called at the end of the method, because the inherited respond_to? - # would return true for generated readers, even if the attribute wasn't present - super - end - end - - def to_json(options={}) - super({ :root => self.class.element_name }.merge(options)) - end - - def to_xml(options={}) - super({ :root => self.class.element_name }.merge(options)) - end - - protected - def connection(refresh = false) - self.class.connection(refresh) - end - - # Update the resource on the remote service. - def update - connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response| - load_attributes_from_response(response) - end - end - - # Create (i.e., \save to the remote service) the \new resource. - def create - connection.post(collection_path, encode, self.class.headers).tap do |response| - self.id = id_from_response(response) - load_attributes_from_response(response) - end - end - - def load_attributes_from_response(response) - if (response_code_allows_body?(response.code) && - (response['Content-Length'].nil? || response['Content-Length'] != "0") && - !response.body.nil? && response.body.strip.size > 0) - load(self.class.format.decode(response.body), true) - @persisted = true - end - end - - # Takes a response from a typical create post and pulls the ID out - def id_from_response(response) - response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1] if response['Location'] - end - - def element_path(options = nil) - self.class.element_path(to_param, options || prefix_options) - end - - def new_element_path - self.class.new_element_path(prefix_options) - end - - def collection_path(options = nil) - self.class.collection_path(options || prefix_options) - end - - private - - def read_attribute_for_serialization(n) - attributes[n] - end - - # Determine whether the response is allowed to have a body per HTTP 1.1 spec section 4.4.1 - def response_code_allows_body?(c) - !((100..199).include?(c) || [204,304].include?(c)) - end - - # Tries to find a resource for a given collection name; if it fails, then the resource is created - def find_or_create_resource_for_collection(name) - find_or_create_resource_for(ActiveSupport::Inflector.singularize(name.to_s)) - end - - # Tries to find a resource in a non empty list of nested modules - # if it fails, then the resource is created - def find_or_create_resource_in_modules(resource_name, module_names) - receiver = Object - namespaces = module_names[0, module_names.size-1].map do |module_name| - receiver = receiver.const_get(module_name) - end - const_args = [resource_name, false] - if namespace = namespaces.reverse.detect { |ns| ns.const_defined?(*const_args) } - namespace.const_get(*const_args) - else - create_resource_for(resource_name) - end - end - - # Tries to find a resource for a given name; if it fails, then the resource is created - def find_or_create_resource_for(name) - resource_name = name.to_s.camelize - - const_args = [resource_name, false] - if self.class.const_defined?(*const_args) - self.class.const_get(*const_args) - else - ancestors = self.class.name.split("::") - if ancestors.size > 1 - find_or_create_resource_in_modules(resource_name, ancestors) - else - if Object.const_defined?(*const_args) - Object.const_get(*const_args) - else - create_resource_for(resource_name) - end - end - end - end - - # Create and return a class definition for a resource inside the current resource - def create_resource_for(resource_name) - resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base)) - resource.prefix = self.class.prefix - resource.site = self.class.site - resource - end - - def split_options(options = {}) - self.class.__send__(:split_options, options) - end - - def method_missing(method_symbol, *arguments) #:nodoc: - method_name = method_symbol.to_s - - if method_name =~ /(=|\?)$/ - case $1 - when "=" - attributes[$`] = arguments.first - when "?" - attributes[$`] - end - else - return attributes[method_name] if attributes.include?(method_name) - # not set right now but we know about it - return nil if known_attributes.include?(method_name) - super - end - end - end - - class Base - extend ActiveModel::Naming - include CustomMethods, Observing, Validations - include ActiveModel::Conversion - include ActiveModel::Serializers::JSON - include ActiveModel::Serializers::Xml - end -end diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb deleted file mode 100644 index 8a8dc3146d..0000000000 --- a/activeresource/lib/active_resource/connection.rb +++ /dev/null @@ -1,284 +0,0 @@ -require 'active_support/core_ext/benchmark' -require 'active_support/core_ext/uri' -require 'active_support/core_ext/object/inclusion' -require 'net/https' -require 'date' -require 'time' -require 'uri' - -module ActiveResource - # Class to handle connections to remote web services. - # This class is used by ActiveResource::Base to interface with REST - # services. - class Connection - - HTTP_FORMAT_HEADER_NAMES = { :get => 'Accept', - :put => 'Content-Type', - :post => 'Content-Type', - :patch => 'Content-Type', - :delete => 'Accept', - :head => 'Accept' - } - - attr_reader :site, :user, :password, :auth_type, :timeout, :proxy, :ssl_options - attr_accessor :format - - class << self - def requests - @@requests ||= [] - end - end - - # The +site+ parameter is required and will set the +site+ - # attribute to the URI for the remote resource service. - def initialize(site, format = ActiveResource::Formats::JsonFormat) - raise ArgumentError, 'Missing site URI' unless site - @user = @password = nil - self.site = site - self.format = format - end - - # Set URI for remote service. - def site=(site) - @site = site.is_a?(URI) ? site : URI.parse(site) - @user = URI.parser.unescape(@site.user) if @site.user - @password = URI.parser.unescape(@site.password) if @site.password - end - - # Set the proxy for remote service. - def proxy=(proxy) - @proxy = proxy.is_a?(URI) ? proxy : URI.parse(proxy) - end - - # Sets the user for remote service. - def user=(user) - @user = user - end - - # Sets the password for remote service. - def password=(password) - @password = password - end - - # Sets the auth type for remote service. - def auth_type=(auth_type) - @auth_type = legitimize_auth_type(auth_type) - end - - # Sets the number of seconds after which HTTP requests to the remote service should time out. - def timeout=(timeout) - @timeout = timeout - end - - # Hash of options applied to Net::HTTP instance when +site+ protocol is 'https'. - def ssl_options=(opts={}) - @ssl_options = opts - end - - # Executes a GET request. - # Used to get (find) resources. - def get(path, headers = {}) - with_auth { request(:get, path, build_request_headers(headers, :get, self.site.merge(path))) } - end - - # Executes a DELETE request (see HTTP protocol documentation if unfamiliar). - # Used to delete resources. - def delete(path, headers = {}) - with_auth { request(:delete, path, build_request_headers(headers, :delete, self.site.merge(path))) } - end - - # Executes a PATCH request (see HTTP protocol documentation if unfamiliar). - # Used to update resources. - def patch(path, body = '', headers = {}) - with_auth { request(:patch, path, body.to_s, build_request_headers(headers, :patch, self.site.merge(path))) } - end - - # Executes a PUT request (see HTTP protocol documentation if unfamiliar). - # Used to update resources. - def put(path, body = '', headers = {}) - with_auth { request(:put, path, body.to_s, build_request_headers(headers, :put, self.site.merge(path))) } - end - - # Executes a POST request. - # Used to create new resources. - def post(path, body = '', headers = {}) - with_auth { request(:post, path, body.to_s, build_request_headers(headers, :post, self.site.merge(path))) } - end - - # Executes a HEAD request. - # Used to obtain meta-information about resources, such as whether they exist and their size (via response headers). - def head(path, headers = {}) - with_auth { request(:head, path, build_request_headers(headers, :head, self.site.merge(path))) } - end - - private - # Makes a request to the remote service. - def request(method, path, *arguments) - result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload| - payload[:method] = method - payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}" - payload[:result] = http.send(method, path, *arguments) - end - handle_response(result) - rescue Timeout::Error => e - raise TimeoutError.new(e.message) - rescue OpenSSL::SSL::SSLError => e - raise SSLError.new(e.message) - end - - # Handles response and error codes from the remote service. - def handle_response(response) - case response.code.to_i - when 301, 302, 303, 307 - raise(Redirection.new(response)) - when 200...400 - response - when 400 - raise(BadRequest.new(response)) - when 401 - raise(UnauthorizedAccess.new(response)) - when 403 - raise(ForbiddenAccess.new(response)) - when 404 - raise(ResourceNotFound.new(response)) - when 405 - raise(MethodNotAllowed.new(response)) - when 409 - raise(ResourceConflict.new(response)) - when 410 - raise(ResourceGone.new(response)) - when 422 - raise(ResourceInvalid.new(response)) - when 401...500 - raise(ClientError.new(response)) - when 500...600 - raise(ServerError.new(response)) - else - raise(ConnectionError.new(response, "Unknown response code: #{response.code}")) - end - end - - # Creates new Net::HTTP instance for communication with the - # remote service and resources. - def http - configure_http(new_http) - end - - def new_http - if @proxy - Net::HTTP.new(@site.host, @site.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password) - else - Net::HTTP.new(@site.host, @site.port) - end - end - - def configure_http(http) - apply_ssl_options(http).tap do |https| - # Net::HTTP timeouts default to 60 seconds. - if defined? @timeout - https.open_timeout = @timeout - https.read_timeout = @timeout - end - end - end - - def apply_ssl_options(http) - http.tap do |https| - # Skip config if site is already a https:// URI. - if defined? @ssl_options - http.use_ssl = true - - # Default to no cert verification (WTF? FIXME) - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - - # All the SSL options have corresponding http settings. - @ssl_options.each { |key, value| http.send "#{key}=", value } - end - end - end - - def default_header - @default_header ||= {} - end - - # Builds headers for request to remote service. - def build_request_headers(headers, http_method, uri) - authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers) - end - - def response_auth_header - @response_auth_header ||= "" - end - - def with_auth - retried ||= false - yield - rescue UnauthorizedAccess => e - raise if retried || auth_type != :digest - @response_auth_header = e.response['WWW-Authenticate'] - retried = true - retry - end - - def authorization_header(http_method, uri) - if @user || @password - if auth_type == :digest - { 'Authorization' => digest_auth_header(http_method, uri) } - else - { 'Authorization' => 'Basic ' + ["#{@user}:#{@password}"].pack('m').delete("\r\n") } - end - else - {} - end - end - - def digest_auth_header(http_method, uri) - params = extract_params_from_response - - request_uri = uri.path - request_uri << "?#{uri.query}" if uri.query - - ha1 = Digest::MD5.hexdigest("#{@user}:#{params['realm']}:#{@password}") - ha2 = Digest::MD5.hexdigest("#{http_method.to_s.upcase}:#{request_uri}") - - params.merge!('cnonce' => client_nonce) - request_digest = Digest::MD5.hexdigest([ha1, params['nonce'], "0", params['cnonce'], params['qop'], ha2].join(":")) - "Digest #{auth_attributes_for(uri, request_digest, params)}" - end - - def client_nonce - Digest::MD5.hexdigest("%x" % (Time.now.to_i + rand(65535))) - end - - def extract_params_from_response - params = {} - if response_auth_header =~ /^(\w+) (.*)/ - $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 } - end - params - end - - def auth_attributes_for(uri, request_digest, params) - [ - %Q(username="#{@user}"), - %Q(realm="#{params['realm']}"), - %Q(qop="#{params['qop']}"), - %Q(uri="#{uri.path}"), - %Q(nonce="#{params['nonce']}"), - %Q(nc="0"), - %Q(cnonce="#{params['cnonce']}"), - %Q(opaque="#{params['opaque']}"), - %Q(response="#{request_digest}")].join(", ") - end - - def http_format_header(http_method) - {HTTP_FORMAT_HEADER_NAMES[http_method] => format.mime_type} - end - - def legitimize_auth_type(auth_type) - return :basic if auth_type.nil? - auth_type = auth_type.to_sym - auth_type.in?([:basic, :digest]) ? auth_type : :basic - end - end -end diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb deleted file mode 100644 index 839dc79d50..0000000000 --- a/activeresource/lib/active_resource/custom_methods.rb +++ /dev/null @@ -1,127 +0,0 @@ -require 'active_support/core_ext/object/blank' - -module ActiveResource - # A module to support custom REST methods and sub-resources, allowing you to break out - # of the "default" REST methods with your own custom resource requests. For example, - # say you use Rails to expose a REST service and configure your routes with: - # - # map.resources :people, :new => { :register => :post }, - # :member => { :promote => :put, :deactivate => :delete } - # :collection => { :active => :get } - # - # This route set creates routes for the following HTTP requests: - # - # POST /people/new/register.json # PeopleController.register - # PATCH/PUT /people/1/promote.json # PeopleController.promote with :id => 1 - # DELETE /people/1/deactivate.json # PeopleController.deactivate with :id => 1 - # GET /people/active.json # PeopleController.active - # - # Using this module, Active Resource can use these custom REST methods just like the - # standard methods. - # - # class Person < ActiveResource::Base - # self.site = "https://37s.sunrise.com" - # end - # - # Person.new(:name => 'Ryan').post(:register) # POST /people/new/register.json - # # => { :id => 1, :name => 'Ryan' } - # - # Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.json - # Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.json - # - # Person.get(:active) # GET /people/active.json - # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}] - # - module CustomMethods - extend ActiveSupport::Concern - - included do - class << self - alias :orig_delete :delete - - # Invokes a GET to a given custom REST method. For example: - # - # Person.get(:active) # GET /people/active.json - # # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}] - # - # Person.get(:active, :awesome => true) # GET /people/active.json?awesome=true - # # => [{:id => 1, :name => 'Ryan'}] - # - # Note: the objects returned from this method are not automatically converted - # into ActiveResource::Base instances - they are ordinary Hashes. If you are expecting - # ActiveResource::Base instances, use the <tt>find</tt> class method with the - # <tt>:from</tt> option. For example: - # - # Person.find(:all, :from => :active) - def get(custom_method_name, options = {}) - hashified = format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body) - derooted = Formats.remove_root(hashified) - derooted.is_a?(Array) ? derooted.map { |e| Formats.remove_root(e) } : derooted - end - - def post(custom_method_name, options = {}, body = '') - connection.post(custom_method_collection_url(custom_method_name, options), body, headers) - end - - def patch(custom_method_name, options = {}, body = '') - connection.patch(custom_method_collection_url(custom_method_name, options), body, headers) - end - - def put(custom_method_name, options = {}, body = '') - connection.put(custom_method_collection_url(custom_method_name, options), body, headers) - end - - def delete(custom_method_name, options = {}) - # Need to jump through some hoops to retain the original class 'delete' method - if custom_method_name.is_a?(Symbol) - connection.delete(custom_method_collection_url(custom_method_name, options), headers) - else - orig_delete(custom_method_name, options) - end - end - end - end - - module ClassMethods - def custom_method_collection_url(method_name, options = {}) - prefix_options, query_options = split_options(options) - "#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}" - end - end - - def get(method_name, options = {}) - self.class.format.decode(connection.get(custom_method_element_url(method_name, options), self.class.headers).body) - end - - def post(method_name, options = {}, body = nil) - request_body = body.blank? ? encode : body - if new? - connection.post(custom_method_new_element_url(method_name, options), request_body, self.class.headers) - else - connection.post(custom_method_element_url(method_name, options), request_body, self.class.headers) - end - end - - def patch(method_name, options = {}, body = '') - connection.patch(custom_method_element_url(method_name, options), body, self.class.headers) - end - - def put(method_name, options = {}, body = '') - connection.put(custom_method_element_url(method_name, options), body, self.class.headers) - end - - def delete(method_name, options = {}) - connection.delete(custom_method_element_url(method_name, options), self.class.headers) - end - - - private - def custom_method_element_url(method_name, options = {}) - "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}" - end - - def custom_method_new_element_url(method_name, options = {}) - "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}" - end - end -end diff --git a/activeresource/lib/active_resource/exceptions.rb b/activeresource/lib/active_resource/exceptions.rb deleted file mode 100644 index 51bede3bd0..0000000000 --- a/activeresource/lib/active_resource/exceptions.rb +++ /dev/null @@ -1,82 +0,0 @@ -module ActiveResource - class ConnectionError < StandardError # :nodoc: - attr_reader :response - - def initialize(response, message = nil) - @response = response - @message = message - end - - def to_s - message = "Failed." - message << " Response code = #{response.code}." if response.respond_to?(:code) - message << " Response message = #{response.message}." if response.respond_to?(:message) - message - end - end - - # Raised when a Timeout::Error occurs. - class TimeoutError < ConnectionError - def initialize(message) - @message = message - end - def to_s; @message ;end - end - - # Raised when a OpenSSL::SSL::SSLError occurs. - class SSLError < ConnectionError - def initialize(message) - @message = message - end - def to_s; @message ;end - end - - # 3xx Redirection - class Redirection < ConnectionError # :nodoc: - def to_s - response['Location'] ? "#{super} => #{response['Location']}" : super - end - end - - class MissingPrefixParam < ArgumentError # :nodoc: - end - - # 4xx Client Error - class ClientError < ConnectionError # :nodoc: - end - - # 400 Bad Request - class BadRequest < ClientError # :nodoc: - end - - # 401 Unauthorized - class UnauthorizedAccess < ClientError # :nodoc: - end - - # 403 Forbidden - class ForbiddenAccess < ClientError # :nodoc: - end - - # 404 Not Found - class ResourceNotFound < ClientError # :nodoc: - end - - # 409 Conflict - class ResourceConflict < ClientError # :nodoc: - end - - # 410 Gone - class ResourceGone < ClientError # :nodoc: - end - - # 5xx Server Error - class ServerError < ConnectionError # :nodoc: - end - - # 405 Method Not Allowed - class MethodNotAllowed < ClientError # :nodoc: - def allowed_methods - @response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym } - end - end -end diff --git a/activeresource/lib/active_resource/formats.rb b/activeresource/lib/active_resource/formats.rb deleted file mode 100644 index f7ad689cc5..0000000000 --- a/activeresource/lib/active_resource/formats.rb +++ /dev/null @@ -1,22 +0,0 @@ -module ActiveResource - module Formats - autoload :XmlFormat, 'active_resource/formats/xml_format' - autoload :JsonFormat, 'active_resource/formats/json_format' - - # Lookup the format class from a mime type reference symbol. Example: - # - # ActiveResource::Formats[:xml] # => ActiveResource::Formats::XmlFormat - # ActiveResource::Formats[:json] # => ActiveResource::Formats::JsonFormat - def self.[](mime_type_reference) - ActiveResource::Formats.const_get(ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format") - end - - def self.remove_root(data) - if data.is_a?(Hash) && data.keys.size == 1 - data.values.first - else - data - end - end - end -end diff --git a/activeresource/lib/active_resource/formats/json_format.rb b/activeresource/lib/active_resource/formats/json_format.rb deleted file mode 100644 index 827d1cc23a..0000000000 --- a/activeresource/lib/active_resource/formats/json_format.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'active_support/json' - -module ActiveResource - module Formats - module JsonFormat - extend self - - def extension - "json" - end - - def mime_type - "application/json" - end - - def encode(hash, options = nil) - ActiveSupport::JSON.encode(hash, options) - end - - def decode(json) - Formats.remove_root(ActiveSupport::JSON.decode(json)) - end - end - end -end diff --git a/activeresource/lib/active_resource/formats/xml_format.rb b/activeresource/lib/active_resource/formats/xml_format.rb deleted file mode 100644 index 49cb9aa1ac..0000000000 --- a/activeresource/lib/active_resource/formats/xml_format.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'active_support/core_ext/hash/conversions' - -module ActiveResource - module Formats - module XmlFormat - extend self - - def extension - "xml" - end - - def mime_type - "application/xml" - end - - def encode(hash, options={}) - hash.to_xml(options) - end - - def decode(xml) - Formats.remove_root(Hash.from_xml(xml)) - end - end - end -end diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb deleted file mode 100644 index 9856f5872f..0000000000 --- a/activeresource/lib/active_resource/http_mock.rb +++ /dev/null @@ -1,332 +0,0 @@ -require 'active_support/core_ext/kernel/reporting' -require 'active_support/core_ext/object/inclusion' - -module ActiveResource - class InvalidRequestError < StandardError; end #:nodoc: - - # One thing that has always been a pain with remote web services is testing. The HttpMock - # class makes it easy to test your Active Resource models by creating a set of mock responses to specific - # requests. - # - # To test your Active Resource model, you simply call the ActiveResource::HttpMock.respond_to - # method with an attached block. The block declares a set of URIs with expected input, and the output - # each request should return. The passed in block has any number of entries in the following generalized - # format: - # - # mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - # - # * <tt>http_method</tt> - The HTTP method to listen for. This can be +get+, +post+, +patch+, +put+, +delete+ or - # +head+. - # * <tt>path</tt> - A string, starting with a "/", defining the URI that is expected to be - # called. - # * <tt>request_headers</tt> - Headers that are expected along with the request. This argument uses a - # hash format, such as <tt>{ "Content-Type" => "application/json" }</tt>. This mock will only trigger - # if your tests sends a request with identical headers. - # * <tt>body</tt> - The data to be returned. This should be a string of Active Resource parseable content, - # such as Json. - # * <tt>status</tt> - The HTTP response code, as an integer, to return with the response. - # * <tt>response_headers</tt> - Headers to be returned with the response. Uses the same hash format as - # <tt>request_headers</tt> listed above. - # - # In order for a mock to deliver its content, the incoming request must match by the <tt>http_method</tt>, - # +path+ and <tt>request_headers</tt>. If no match is found an +InvalidRequestError+ exception - # will be raised showing you what request it could not find a response for and also what requests and response - # pairs have been recorded so you can create a new mock for that request. - # - # ==== Example - # def setup - # @matz = { :person => { :id => 1, :name => "Matz" } }.to_json - # ActiveResource::HttpMock.respond_to do |mock| - # mock.post "/people.json", {}, @matz, 201, "Location" => "/people/1.json" - # mock.get "/people/1.json", {}, @matz - # mock.put "/people/1.json", {}, nil, 204 - # mock.delete "/people/1.json", {}, nil, 200 - # end - # end - # - # def test_get_matz - # person = Person.find(1) - # assert_equal "Matz", person.name - # end - # - class HttpMock - class Responder #:nodoc: - def initialize(responses) - @responses = responses - end - - [ :post, :patch, :put, :get, :delete, :head ].each do |method| - # def post(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - # @responses[Request.new(:post, path, nil, request_headers)] = Response.new(body || "", status, response_headers) - # end - module_eval <<-EOE, __FILE__, __LINE__ + 1 - def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - request = Request.new(:#{method}, path, nil, request_headers) - response = Response.new(body || "", status, response_headers) - - delete_duplicate_responses(request) - - @responses << [request, response] - end - EOE - end - - private - - def delete_duplicate_responses(request) - @responses.delete_if {|r| r[0] == request } - end - end - - class << self - - # Returns an array of all request objects that have been sent to the mock. You can use this to check - # if your model actually sent an HTTP request. - # - # ==== Example - # def setup - # @matz = { :person => { :id => 1, :name => "Matz" } }.to_json - # ActiveResource::HttpMock.respond_to do |mock| - # mock.get "/people/1.json", {}, @matz - # end - # end - # - # def test_should_request_remote_service - # person = Person.find(1) # Call the remote service - # - # # This request object has the same HTTP method and path as declared by the mock - # expected_request = ActiveResource::Request.new(:get, "/people/1.json") - # - # # Assert that the mock received, and responded to, the expected request from the model - # assert ActiveResource::HttpMock.requests.include?(expected_request) - # end - def requests - @@requests ||= [] - end - - # Returns the list of requests and their mocked responses. Look up a - # response for a request using <tt>responses.assoc(request)</tt>. - def responses - @@responses ||= [] - end - - # Accepts a block which declares a set of requests and responses for the HttpMock to respond to in - # the following format: - # - # mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - # - # === Example - # - # @matz = { :person => { :id => 1, :name => "Matz" } }.to_json - # ActiveResource::HttpMock.respond_to do |mock| - # mock.post "/people.json", {}, @matz, 201, "Location" => "/people/1.json" - # mock.get "/people/1.json", {}, @matz - # mock.put "/people/1.json", {}, nil, 204 - # mock.delete "/people/1.json", {}, nil, 200 - # end - # - # Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate - # these the following format: - # - # ActiveResource::Request.new(method, path, body, request_headers) - # ActiveResource::Response.new(body, status, response_headers) - # - # === Example - # - # Request.new(:#{method}, path, nil, request_headers) - # - # @matz = { :person => { :id => 1, :name => "Matz" } }.to_json - # - # create_matz = ActiveResource::Request.new(:post, '/people.json', @matz, {}) - # created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.json"}) - # get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil) - # ok_response = ActiveResource::Response.new("", 200, {}) - # - # pairs = {create_matz => created_response, get_matz => ok_response} - # - # ActiveResource::HttpMock.respond_to(pairs) - # - # Note, by default, every time you call +respond_to+, any previous request and response pairs stored - # in HttpMock will be deleted giving you a clean slate to work on. - # - # If you want to override this behavior, pass in +false+ as the last argument to +respond_to+ - # - # === Example - # - # ActiveResource::HttpMock.respond_to do |mock| - # mock.send(:get, "/people/1", {}, "JSON1") - # end - # ActiveResource::HttpMock.responses.length #=> 1 - # - # ActiveResource::HttpMock.respond_to(false) do |mock| - # mock.send(:get, "/people/2", {}, "JSON2") - # end - # ActiveResource::HttpMock.responses.length #=> 2 - # - # This also works with passing in generated pairs of requests and responses, again, just pass in false - # as the last argument: - # - # === Example - # - # ActiveResource::HttpMock.respond_to do |mock| - # mock.send(:get, "/people/1", {}, "JSON1") - # end - # ActiveResource::HttpMock.responses.length #=> 1 - # - # get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil) - # ok_response = ActiveResource::Response.new("", 200, {}) - # - # pairs = {get_matz => ok_response} - # - # ActiveResource::HttpMock.respond_to(pairs, false) - # ActiveResource::HttpMock.responses.length #=> 2 - # - # # If you add a response with an existing request, it will be replaced - # - # fail_response = ActiveResource::Response.new("", 404, {}) - # pairs = {get_matz => fail_response} - # - # ActiveResource::HttpMock.respond_to(pairs, false) - # ActiveResource::HttpMock.responses.length #=> 2 - # - def respond_to(*args) #:yields: mock - pairs = args.first || {} - reset! if args.last.class != FalseClass - - if block_given? - yield Responder.new(responses) - else - delete_responses_to_replace pairs.to_a - responses.concat pairs.to_a - Responder.new(responses) - end - end - - def delete_responses_to_replace(new_responses) - new_responses.each{|nr| - request_to_remove = nr[0] - @@responses = responses.delete_if{|r| r[0] == request_to_remove} - } - end - - # Deletes all logged requests and responses. - def reset! - requests.clear - responses.clear - end - end - - # body? methods - { true => %w(post patch put), - false => %w(get delete head) }.each do |has_body, methods| - methods.each do |method| - # def post(path, body, headers) - # request = ActiveResource::Request.new(:post, path, body, headers) - # self.class.requests << request - # if response = self.class.responses.assoc(request) - # response[1] - # else - # raise InvalidRequestError.new("Could not find a response recorded for #{request.to_s} - Responses recorded are: - #{inspect_responses}") - # end - # end - module_eval <<-EOE, __FILE__, __LINE__ + 1 - def #{method}(path, #{'body, ' if has_body}headers) - request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers) - self.class.requests << request - if response = self.class.responses.assoc(request) - response[1] - else - raise InvalidRequestError.new("Could not find a response recorded for \#{request.to_s} - Responses recorded are: \#{inspect_responses}") - end - end - EOE - end - end - - def initialize(site) #:nodoc: - @site = site - end - - def inspect_responses #:nodoc: - self.class.responses.map { |r| r[0].to_s }.inspect - end - end - - class Request - attr_accessor :path, :method, :body, :headers - - def initialize(method, path, body = nil, headers = {}) - @method, @path, @body, @headers = method, path, body, headers - end - - def ==(req) - path == req.path && method == req.method && headers_match?(req) - end - - def to_s - "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>" - end - - private - - def headers_match?(req) - # Ignore format header on equality if it's not defined - format_header = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] - if headers[format_header].present? || req.headers[format_header].blank? - headers == req.headers - else - headers.dup.merge(format_header => req.headers[format_header]) == req.headers - end - end - end - - class Response - attr_accessor :body, :message, :code, :headers - - def initialize(body, message = 200, headers = {}) - @body, @message, @headers = body, message.to_s, headers - @code = @message[0,3].to_i - - resp_cls = Net::HTTPResponse::CODE_TO_OBJ[@code.to_s] - if resp_cls && !resp_cls.body_permitted? - @body = nil - end - - self['Content-Length'] = @body.nil? ? "0" : body.size.to_s - - end - - # Returns true if code is 2xx, - # false otherwise. - def success? - code.in?(200..299) - end - - def [](key) - headers[key] - end - - def []=(key, value) - headers[key] = value - end - - # Returns true if the other is a Response with an equal body, equal message - # and equal headers. Otherwise it returns false. - def ==(other) - if (other.is_a?(Response)) - other.body == body && other.message == message && other.headers == headers - else - false - end - end - end - - class Connection - private - silence_warnings do - def http - @http ||= HttpMock.new(@site) - end - end - end -end diff --git a/activeresource/lib/active_resource/log_subscriber.rb b/activeresource/lib/active_resource/log_subscriber.rb deleted file mode 100644 index 9e52baf36d..0000000000 --- a/activeresource/lib/active_resource/log_subscriber.rb +++ /dev/null @@ -1,15 +0,0 @@ -module ActiveResource - class LogSubscriber < ActiveSupport::LogSubscriber - def request(event) - result = event.payload[:result] - info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}" - info "--> %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration] - end - - def logger - ActiveResource::Base.logger - end - end -end - -ActiveResource::LogSubscriber.attach_to :active_resource
\ No newline at end of file diff --git a/activeresource/lib/active_resource/observing.rb b/activeresource/lib/active_resource/observing.rb deleted file mode 100644 index 1bfceb8dc8..0000000000 --- a/activeresource/lib/active_resource/observing.rb +++ /dev/null @@ -1,29 +0,0 @@ -module ActiveResource - module Observing - extend ActiveSupport::Concern - include ActiveModel::Observing - - included do - %w( create save update destroy ).each do |method| - # def create_with_notifications(*args, &block) - # notify_observers(:before_create) - # if result = create_without_notifications(*args, &block) - # notify_observers(:after_create) - # end - # result - # end - # alias_method_chain(create, :notifications) - class_eval(<<-EOS, __FILE__, __LINE__ + 1) - def #{method}_with_notifications(*args, &block) - notify_observers(:before_#{method}) - if result = #{method}_without_notifications(*args, &block) - notify_observers(:after_#{method}) - end - result - end - EOS - alias_method_chain(method, :notifications) - end - end - end -end diff --git a/activeresource/lib/active_resource/railtie.rb b/activeresource/lib/active_resource/railtie.rb deleted file mode 100644 index 60f6f88311..0000000000 --- a/activeresource/lib/active_resource/railtie.rb +++ /dev/null @@ -1,14 +0,0 @@ -require "active_resource" -require "rails" - -module ActiveResource - class Railtie < Rails::Railtie - config.active_resource = ActiveSupport::OrderedOptions.new - - initializer "active_resource.set_configs" do |app| - app.config.active_resource.each do |k,v| - ActiveResource::Base.send "#{k}=", v - end - end - end -end
\ No newline at end of file diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb deleted file mode 100644 index 5957969aa2..0000000000 --- a/activeresource/lib/active_resource/schema.rb +++ /dev/null @@ -1,59 +0,0 @@ -require 'active_resource/exceptions' - -module ActiveResource # :nodoc: - class Schema # :nodoc: - # attributes can be known to be one of these types. They are easy to - # cast to/from. - KNOWN_ATTRIBUTE_TYPES = %w( string text integer float decimal datetime timestamp time date binary boolean ) - - # An array of attribute definitions, representing the attributes that - # have been defined. - attr_accessor :attrs - - # The internals of an Active Resource Schema are very simple - - # unlike an Active Record TableDefinition (on which it is based). - # It provides a set of convenience methods for people to define their - # schema using the syntax: - # schema do - # string :foo - # integer :bar - # end - # - # The schema stores the name and type of each attribute. That is then - # read out by the schema method to populate the schema of the actual - # resource. - def initialize - @attrs = {} - end - - def attribute(name, type, options = {}) - raise ArgumentError, "Unknown Attribute type: #{type.inspect} for key: #{name.inspect}" unless type.nil? || Schema::KNOWN_ATTRIBUTE_TYPES.include?(type.to_s) - - the_type = type.to_s - # TODO: add defaults - #the_attr = [type.to_s] - #the_attr << options[:default] if options.has_key? :default - @attrs[name.to_s] = the_type - self - end - - # The following are the attribute types supported by Active Resource - # migrations. - KNOWN_ATTRIBUTE_TYPES.each do |attr_type| - # def string(*args) - # options = args.extract_options! - # attr_names = args - # - # attr_names.each { |name| attribute(name, 'string', options) } - # end - class_eval <<-EOV, __FILE__, __LINE__ + 1 - def #{attr_type.to_s}(*args) - options = args.extract_options! - attr_names = args - - attr_names.each { |name| attribute(name, '#{attr_type}', options) } - end - EOV - end - end -end diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb deleted file mode 100644 index 028acb8bce..0000000000 --- a/activeresource/lib/active_resource/validations.rb +++ /dev/null @@ -1,172 +0,0 @@ -require 'active_support/core_ext/array/wrap' -require 'active_support/core_ext/object/blank' - -module ActiveResource - class ResourceInvalid < ClientError #:nodoc: - end - - # Active Resource validation is reported to and from this object, which is used by Base#save - # to determine whether the object in a valid state to be saved. See usage example in Validations. - class Errors < ActiveModel::Errors - # Grabs errors from an array of messages (like ActiveRecord::Validations). - # The second parameter directs the errors cache to be cleared (default) - # or not (by passing true). - def from_array(messages, save_cache = false) - clear unless save_cache - humanized_attributes = Hash[@base.attributes.keys.map { |attr_name| [attr_name.humanize, attr_name] }] - messages.each do |message| - attr_message = humanized_attributes.keys.detect do |attr_name| - if message[0, attr_name.size + 1] == "#{attr_name} " - add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1] - end - end - - self[:base] << message if attr_message.nil? - end - end - - # Grabs errors from a hash of attribute => array of errors elements - # The second parameter directs the errors cache to be cleared (default) - # or not (by passing true) - # - # Unrecognized attribute names will be humanized and added to the record's - # base errors. - def from_hash(messages, save_cache = false) - clear unless save_cache - - messages.each do |(key,errors)| - errors.each do |error| - if @base.attributes.keys.include?(key) - add key, error - elsif key == 'base' - self[:base] << error - else - # reporting an error on an attribute not in attributes - # format and add them to base - self[:base] << "#{key.humanize} #{error}" - end - end - end - end - - # Grabs errors from a json response. - def from_json(json, save_cache = false) - decoded = ActiveSupport::JSON.decode(json) || {} rescue {} - if decoded.kind_of?(Hash) && (decoded.has_key?('errors') || decoded.empty?) - errors = decoded['errors'] || {} - if errors.kind_of?(Array) - # 3.2.1-style with array of strings - ActiveSupport::Deprecation.warn('Returning errors as an array of strings is deprecated.') - from_array errors, save_cache - else - # 3.2.2+ style - from_hash errors, save_cache - end - else - # <3.2-style respond_with - lacks 'errors' key - ActiveSupport::Deprecation.warn('Returning errors as a hash without a root "errors" key is deprecated.') - from_hash decoded, save_cache - end - end - - # Grabs errors from an XML response. - def from_xml(xml, save_cache = false) - array = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue [] - from_array array, save_cache - end - end - - # Module to support validation and errors with Active Resource objects. The module overrides - # Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned - # in the web service response. The module also adds an +errors+ collection that mimics the interface - # of the errors provided by ActiveModel::Errors. - # - # ==== Example - # - # Consider a Person resource on the server requiring both a +first_name+ and a +last_name+ with a - # <tt>validates_presence_of :first_name, :last_name</tt> declaration in the model: - # - # person = Person.new(:first_name => "Jim", :last_name => "") - # person.save # => false (server returns an HTTP 422 status code and errors) - # person.valid? # => false - # person.errors.empty? # => false - # person.errors.count # => 1 - # person.errors.full_messages # => ["Last name can't be empty"] - # person.errors[:last_name] # => ["can't be empty"] - # person.last_name = "Halpert" - # person.save # => true (and person is now saved to the remote service) - # - module Validations - extend ActiveSupport::Concern - include ActiveModel::Validations - - included do - alias_method_chain :save, :validation - end - - # Validate a resource and save (POST) it to the remote web service. - # If any local validations fail - the save (POST) will not be attempted. - def save_with_validation(options={}) - perform_validation = options[:validate] != false - - # clear the remote validations so they don't interfere with the local - # ones. Otherwise we get an endless loop and can never change the - # fields so as to make the resource valid. - @remote_errors = nil - if perform_validation && valid? || !perform_validation - save_without_validation - true - else - false - end - rescue ResourceInvalid => error - # cache the remote errors because every call to <tt>valid?</tt> clears - # all errors. We must keep a copy to add these back after local - # validations. - @remote_errors = error - load_remote_errors(@remote_errors, true) - false - end - - - # Loads the set of remote errors into the object's Errors based on the - # content-type of the error-block received. - def load_remote_errors(remote_errors, save_cache = false ) #:nodoc: - case self.class.format - when ActiveResource::Formats[:xml] - errors.from_xml(remote_errors.response.body, save_cache) - when ActiveResource::Formats[:json] - errors.from_json(remote_errors.response.body, save_cache) - end - end - - # Checks for errors on an object (i.e., is resource.errors empty?). - # - # Runs all the specified local validations and returns true if no errors - # were added, otherwise false. - # Runs local validations (eg those on your Active Resource model), and - # also any errors returned from the remote system the last time we - # saved. - # Remote errors can only be cleared by trying to re-save the resource. - # - # ==== Examples - # my_person = Person.create(params[:person]) - # my_person.valid? - # # => true - # - # my_person.errors.add('login', 'can not be empty') if my_person.login == '' - # my_person.valid? - # # => false - # - def valid? - super - load_remote_errors(@remote_errors, true) if defined?(@remote_errors) && @remote_errors.present? - errors.empty? - end - - # Returns the Errors object that holds all information about attribute error messages. - def errors - @errors ||= Errors.new(self) - end - end -end diff --git a/activeresource/lib/active_resource/version.rb b/activeresource/lib/active_resource/version.rb deleted file mode 100644 index d02784bd5d..0000000000 --- a/activeresource/lib/active_resource/version.rb +++ /dev/null @@ -1,10 +0,0 @@ -module ActiveResource - module VERSION #:nodoc: - MAJOR = 4 - MINOR = 0 - TINY = 0 - PRE = "beta" - - STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') - end -end diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb deleted file mode 100644 index c68625df4f..0000000000 --- a/activeresource/test/abstract_unit.rb +++ /dev/null @@ -1,143 +0,0 @@ -require File.expand_path('../../../load_paths', __FILE__) - -lib = File.expand_path("#{File.dirname(__FILE__)}/../lib") -$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) - -require 'minitest/autorun' -require 'active_resource' -require 'active_support' -require 'active_support/test_case' -require 'setter_trap' -require 'active_support/logger' - -ActiveResource::Base.logger = ActiveSupport::Logger.new("#{File.dirname(__FILE__)}/debug.log") - -def setup_response - matz_hash = { 'person' => { :id => 1, :name => 'Matz' } } - - @default_request_headers = { 'Content-Type' => 'application/json' } - @matz = matz_hash.to_json - @matz_xml = matz_hash.to_xml - @david = { :person => { :id => 2, :name => 'David' } }.to_json - @greg = { :person => { :id => 3, :name => 'Greg' } }.to_json - @addy = { :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }.to_json - @rick = { :person => { :name => "Rick", :age => 25 } }.to_json - @joe = { :person => { :id => 6, :name => 'Joe', :likes_hats => true }}.to_json - @people = { :people => [ { :person => { :id => 1, :name => 'Matz' } }, { :person => { :id => 2, :name => 'David' } }] }.to_json - @people_david = { :people => [ { :person => { :id => 2, :name => 'David' } }] }.to_json - @addresses = { :addresses => [{ :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }] }.to_json - - # - deep nested resource - - # - Luis (Customer) - # - JK (Customer::Friend) - # - Mateo (Customer::Friend::Brother) - # - Edith (Customer::Friend::Brother::Child) - # - Martha (Customer::Friend::Brother::Child) - # - Felipe (Customer::Friend::Brother) - # - Bryan (Customer::Friend::Brother::Child) - # - Luke (Customer::Friend::Brother::Child) - # - Eduardo (Customer::Friend) - # - Sebas (Customer::Friend::Brother) - # - Andres (Customer::Friend::Brother::Child) - # - Jorge (Customer::Friend::Brother::Child) - # - Elsa (Customer::Friend::Brother) - # - Natacha (Customer::Friend::Brother::Child) - # - Milena (Customer::Friend::Brother) - # - @luis = { - :customer => { - :id => 1, - :name => 'Luis', - :friends => [{ - :name => 'JK', - :brothers => [ - { - :name => 'Mateo', - :children => [{ :name => 'Edith' },{ :name => 'Martha' }] - }, { - :name => 'Felipe', - :children => [{ :name => 'Bryan' },{ :name => 'Luke' }] - } - ] - }, { - :name => 'Eduardo', - :brothers => [ - { - :name => 'Sebas', - :children => [{ :name => 'Andres' },{ :name => 'Jorge' }] - }, { - :name => 'Elsa', - :children => [{ :name => 'Natacha' }] - }, { - :name => 'Milena', - :children => [] - } - ] - }] - } - }.to_json - # - resource with yaml array of strings; for ARs using serialize :bar, Array - @marty = <<-eof.strip - <?xml version=\"1.0\" encoding=\"UTF-8\"?> - <person> - <id type=\"integer\">5</id> - <name>Marty</name> - <colors type=\"yaml\">--- - - \"red\" - - \"green\" - - \"blue\" - </colors> - </person> - eof - - @startup_sound = { - :sound => { - :name => "Mac Startup Sound", :author => { :name => "Jim Reekes" } - } - }.to_json - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, @matz - mock.get "/people/1.xml", {}, @matz_xml - mock.get "/people/2.xml", {}, @david - mock.get "/people/5.xml", {}, @marty - mock.get "/people/Greg.json", {}, @greg - mock.get "/people/6.json", {}, @joe - mock.get "/people/4.json", { 'key' => 'value' }, nil, 404 - mock.put "/people/1.json", {}, nil, 204 - mock.delete "/people/1.json", {}, nil, 200 - mock.delete "/people/2.xml", {}, nil, 400 - mock.get "/people/99.json", {}, nil, 404 - mock.post "/people.json", {}, @rick, 201, 'Location' => '/people/5.xml' - mock.get "/people.json", {}, @people - mock.get "/people/1/addresses.json", {}, @addresses - mock.get "/people/1/addresses/1.json", {}, @addy - mock.get "/people/1/addresses/2.xml", {}, nil, 404 - mock.get "/people/2/addresses.json", {}, nil, 404 - mock.get "/people/2/addresses/1.xml", {}, nil, 404 - mock.get "/people/Greg/addresses/1.json", {}, @addy - mock.put "/people/1/addresses/1.json", {}, nil, 204 - mock.delete "/people/1/addresses/1.json", {}, nil, 200 - mock.post "/people/1/addresses.json", {}, nil, 201, 'Location' => '/people/1/addresses/5' - mock.get "/people/1/addresses/99.json", {}, nil, 404 - mock.get "/people//addresses.xml", {}, nil, 404 - mock.get "/people//addresses/1.xml", {}, nil, 404 - mock.put "/people//addresses/1.xml", {}, nil, 404 - mock.delete "/people//addresses/1.xml", {}, nil, 404 - mock.post "/people//addresses.xml", {}, nil, 404 - mock.head "/people/1.json", {}, nil, 200 - mock.head "/people/Greg.json", {}, nil, 200 - mock.head "/people/99.json", {}, nil, 404 - mock.head "/people/1/addresses/1.json", {}, nil, 200 - mock.head "/people/1/addresses/2.json", {}, nil, 404 - mock.head "/people/2/addresses/1.json", {}, nil, 404 - mock.head "/people/Greg/addresses/1.json", {}, nil, 200 - # customer - mock.get "/customers/1.json", {}, @luis - # sound - mock.get "/sounds/1.json", {}, @startup_sound - end - - Person.user = nil - Person.password = nil -end diff --git a/activeresource/test/cases/authorization_test.rb b/activeresource/test/cases/authorization_test.rb deleted file mode 100644 index fbfe086599..0000000000 --- a/activeresource/test/cases/authorization_test.rb +++ /dev/null @@ -1,251 +0,0 @@ -require 'abstract_unit' - -class AuthorizationTest < ActiveSupport::TestCase - Response = Struct.new(:code) - - def setup - @conn = ActiveResource::Connection.new('http://localhost') - @matz = { :person => { :id => 1, :name => 'Matz' } }.to_json - @david = { :person => { :id => 2, :name => 'David' } }.to_json - @authenticated_conn = ActiveResource::Connection.new("http://david:test123@localhost") - @basic_authorization_request_header = { 'Authorization' => 'Basic ZGF2aWQ6dGVzdDEyMw==' } - end - - private - def decode(response) - @authenticated_conn.format.decode(response.body) - end -end - -class BasicAuthorizationTest < AuthorizationTest - def setup - super - @authenticated_conn.auth_type = :basic - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/2.json", @basic_authorization_request_header, @david - mock.get "/people/1.json", @basic_authorization_request_header, nil, 401, { 'WWW-Authenticate' => 'i_should_be_ignored' } - mock.put "/people/2.json", @basic_authorization_request_header, nil, 204 - mock.delete "/people/2.json", @basic_authorization_request_header, nil, 200 - mock.post "/people/2/addresses.json", @basic_authorization_request_header, nil, 201, 'Location' => '/people/1/addresses/5' - mock.head "/people/2.json", @basic_authorization_request_header, nil, 200 - end - end - - def test_get - david = decode(@authenticated_conn.get("/people/2.json")) - assert_equal "David", david["name"] - end - - def test_post - response = @authenticated_conn.post("/people/2/addresses.json") - assert_equal "/people/1/addresses/5", response["Location"] - end - - def test_put - response = @authenticated_conn.put("/people/2.json") - assert_equal 204, response.code - end - - def test_delete - response = @authenticated_conn.delete("/people/2.json") - assert_equal 200, response.code - end - - def test_head - response = @authenticated_conn.head("/people/2.json") - assert_equal 200, response.code - end - - def test_retry_on_401_doesnt_happen_with_basic_auth - assert_raise(ActiveResource::UnauthorizedAccess) { @authenticated_conn.get("/people/1.json") } - assert_equal "", @authenticated_conn.send(:response_auth_header) - end - - def test_raises_invalid_request_on_unauthorized_requests - assert_raise(ActiveResource::InvalidRequestError) { @conn.get("/people/2.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.put("/people/2.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.head("/people/2.json") } - end - - - def test_authorization_header - authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization'] - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["david", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_authorization_header_with_username_but_no_password - @conn = ActiveResource::Connection.new("http://david:@localhost") - authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["david"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_authorization_header_with_password_but_no_username - @conn = ActiveResource::Connection.new("http://:test123@localhost") - authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_authorization_header_with_decoded_credentials_from_url - @conn = ActiveResource::Connection.new("http://my%40email.com:%31%32%33@localhost") - authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["my@email.com", "123"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_authorization_header_explicitly_setting_username_and_password - @authenticated_conn = ActiveResource::Connection.new("http://@localhost") - @authenticated_conn.user = 'david' - @authenticated_conn.password = 'test123' - authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization'] - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["david", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_authorization_header_explicitly_setting_username_but_no_password - @conn = ActiveResource::Connection.new("http://@localhost") - @conn.user = "david" - authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["david"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_authorization_header_explicitly_setting_password_but_no_username - @conn = ActiveResource::Connection.new("http://@localhost") - @conn.password = "test123" - authorization_header = @conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_authorization_header_if_credentials_supplied_and_auth_type_is_basic - authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - assert_equal @basic_authorization_request_header['Authorization'], authorization_header['Authorization'] - authorization = authorization_header["Authorization"].to_s.split - - assert_equal "Basic", authorization[0] - assert_equal ["david", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1] - end - - def test_client_nonce_is_not_nil - assert_not_nil ActiveResource::Connection.new("http://david:test123@localhost").send(:client_nonce) - end -end - -class DigestAuthorizationTest < AuthorizationTest - def setup - super - @authenticated_conn.auth_type = :digest - - # Make client nonce deterministic - def @authenticated_conn.client_nonce; 'i-am-a-client-nonce' end - - @nonce = "MTI0OTUxMzc4NzpjYWI3NDM3NDNmY2JmODU4ZjQ2ZjcwNGZkMTJiMjE0NA==" - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/2.json", { 'Authorization' => blank_digest_auth_header("/people/2.json", "fad396f6a34aeba28e28b9b96ddbb671") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header } - mock.get "/people/2.json", { 'Authorization' => request_digest_auth_header("/people/2.json", "c064d5ba8891a25290c76c8c7d31fb7b") }, @david, 200 - mock.get "/people/1.json", { 'Authorization' => request_digest_auth_header("/people/1.json", "f9c0b594257bb8422af4abd429c5bb70") }, @matz, 200 - - mock.put "/people/2.json", { 'Authorization' => blank_digest_auth_header("/people/2.json", "50a685d814f94665b9d160fbbaa3958a") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header } - mock.put "/people/2.json", { 'Authorization' => request_digest_auth_header("/people/2.json", "5a75cde841122d8e0f20f8fd1f98a743") }, nil, 204 - - mock.delete "/people/2.json", { 'Authorization' => blank_digest_auth_header("/people/2.json", "846f799107eab5ca4285b909ee299a33") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header } - mock.delete "/people/2.json", { 'Authorization' => request_digest_auth_header("/people/2.json", "9f5b155224edbbb69fd99d8ce094681e") }, nil, 200 - - mock.post "/people/2/addresses.json", { 'Authorization' => blank_digest_auth_header("/people/2/addresses.json", "6984d405ff3d9ed07bbf747dcf16afb0") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header } - mock.post "/people/2/addresses.json", { 'Authorization' => request_digest_auth_header("/people/2/addresses.json", "4bda6a28dbf930b5af9244073623bd04") }, nil, 201, 'Location' => '/people/1/addresses/5' - - mock.head "/people/2.json", { 'Authorization' => blank_digest_auth_header("/people/2.json", "15e5ed84ba5c4cfcd5c98a36c2e4f421") }, nil, 401, { 'WWW-Authenticate' => response_digest_auth_header } - mock.head "/people/2.json", { 'Authorization' => request_digest_auth_header("/people/2.json", "d4c6d2bcc8717abb2e2ccb8c49ee6a91") }, nil, 200 - end - end - - def test_authorization_header_if_credentials_supplied_and_auth_type_is_digest - authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json')) - assert_equal blank_digest_auth_header("/people/2.json", "fad396f6a34aeba28e28b9b96ddbb671"), authorization_header['Authorization'] - end - - def test_authorization_header_with_query_string_if_auth_type_is_digest - authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json?only=name')) - assert_equal blank_digest_auth_header("/people/2.json?only=name", "f8457b0b5d21b6b80737a386217afb24"), authorization_header['Authorization'] - end - - def test_get_with_digest_auth_handles_initial_401_response_and_retries - response = @authenticated_conn.get("/people/2.json") - assert_equal "David", decode(response)["name"] - end - - def test_post_with_digest_auth_handles_initial_401_response_and_retries - response = @authenticated_conn.post("/people/2/addresses.json") - assert_equal "/people/1/addresses/5", response["Location"] - assert_equal 201, response.code - end - - def test_put_with_digest_auth_handles_initial_401_response_and_retries - response = @authenticated_conn.put("/people/2.json") - assert_equal 204, response.code - end - - def test_delete_with_digest_auth_handles_initial_401_response_and_retries - response = @authenticated_conn.delete("/people/2.json") - assert_equal 200, response.code - end - - def test_head_with_digest_auth_handles_initial_401_response_and_retries - response = @authenticated_conn.head("/people/2.json") - assert_equal 200, response.code - end - - def test_get_with_digest_auth_caches_nonce - response = @authenticated_conn.get("/people/2.json") - assert_equal "David", decode(response)["name"] - - # There is no mock for this request with a non-cached nonce. - response = @authenticated_conn.get("/people/1.json") - assert_equal "Matz", decode(response)["name"] - end - - def test_raises_invalid_request_on_unauthorized_requests_with_digest_auth - @conn.auth_type = :digest - assert_raise(ActiveResource::InvalidRequestError) { @conn.get("/people/2.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.put("/people/2.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.json") } - assert_raise(ActiveResource::InvalidRequestError) { @conn.head("/people/2.json") } - end - - private - def blank_digest_auth_header(uri, response) - %Q(Digest username="david", realm="", qop="", uri="#{uri}", nonce="", nc="0", cnonce="i-am-a-client-nonce", opaque="", response="#{response}") - end - - def request_digest_auth_header(uri, response) - %Q(Digest username="david", realm="RailsTestApp", qop="auth", uri="#{uri}", nonce="#{@nonce}", nc="0", cnonce="i-am-a-client-nonce", opaque="ef6dfb078ba22298d366f99567814ffb", response="#{response}") - end - - def response_digest_auth_header - %Q(Digest realm="RailsTestApp", qop="auth", algorithm=MD5, nonce="#{@nonce}", opaque="ef6dfb078ba22298d366f99567814ffb") - end -end diff --git a/activeresource/test/cases/base/custom_methods_test.rb b/activeresource/test/cases/base/custom_methods_test.rb deleted file mode 100644 index f7aa7a4a09..0000000000 --- a/activeresource/test/cases/base/custom_methods_test.rb +++ /dev/null @@ -1,101 +0,0 @@ -require 'abstract_unit' -require 'fixtures/person' -require 'fixtures/street_address' -require 'active_support/core_ext/hash/conversions' - -class CustomMethodsTest < ActiveSupport::TestCase - def setup - @matz = { :person => { :id => 1, :name => 'Matz' } }.to_json - @matz_deep = { :person => { :id => 1, :name => 'Matz', :other => 'other' } }.to_json - @matz_array = { :people => [{ :person => { :id => 1, :name => 'Matz' } }] }.to_json - @ryan = { :person => { :name => 'Ryan' } }.to_json - @addy = { :address => { :id => 1, :street => '12345 Street' } }.to_json - @addy_deep = { :address => { :id => 1, :street => '12345 Street', :zip => "27519" } }.to_json - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, @matz - mock.get "/people/1/shallow.json", {}, @matz - mock.get "/people/1/deep.json", {}, @matz_deep - mock.get "/people/retrieve.json?name=Matz", {}, @matz_array - mock.get "/people/managers.json", {}, @matz_array - mock.post "/people/hire.json?name=Matz", {}, nil, 201 - mock.put "/people/1/promote.json?position=Manager", {}, nil, 204 - mock.put "/people/promote.json?name=Matz", {}, nil, 204, {} - mock.put "/people/sort.json?by=name", {}, nil, 204 - mock.delete "/people/deactivate.json?name=Matz", {}, nil, 200 - mock.delete "/people/1/deactivate.json", {}, nil, 200 - mock.post "/people/new/register.json", {}, @ryan, 201, 'Location' => '/people/5.json' - mock.post "/people/1/register.json", {}, @matz, 201 - mock.get "/people/1/addresses/1.json", {}, @addy - mock.get "/people/1/addresses/1/deep.json", {}, @addy_deep - mock.put "/people/1/addresses/1/normalize_phone.json?locale=US", {}, nil, 204 - mock.put "/people/1/addresses/sort.json?by=name", {}, nil, 204 - mock.post "/people/1/addresses/new/link.json", {}, { :address => { :street => '12345 Street' } }.to_json, 201, 'Location' => '/people/1/addresses/2.json' - end - - Person.user = nil - Person.password = nil - end - - def teardown - ActiveResource::HttpMock.reset! - end - - def test_custom_collection_method - # GET - assert_equal([{ "id" => 1, "name" => 'Matz' }], Person.get(:retrieve, :name => 'Matz')) - - # POST - assert_equal(ActiveResource::Response.new("", 201, {}), Person.post(:hire, :name => 'Matz')) - - # PUT - assert_equal ActiveResource::Response.new("", 204, {}), - Person.put(:promote, {:name => 'Matz'}, 'atestbody') - assert_equal ActiveResource::Response.new("", 204, {}), Person.put(:sort, :by => 'name') - - # DELETE - Person.delete :deactivate, :name => 'Matz' - - # Nested resource - assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.put(:sort, :person_id => 1, :by => 'name') - end - - def test_custom_element_method - # Test GET against an element URL - assert_equal Person.find(1).get(:shallow), {"id" => 1, "name" => 'Matz'} - assert_equal Person.find(1).get(:deep), {"id" => 1, "name" => 'Matz', "other" => 'other'} - - # Test PUT against an element URL - assert_equal ActiveResource::Response.new("", 204, {}), Person.find(1).put(:promote, {:position => 'Manager'}, 'body') - - # Test DELETE against an element URL - assert_equal ActiveResource::Response.new("", 200, {}), Person.find(1).delete(:deactivate) - - # With nested resources - assert_equal StreetAddress.find(1, :params => { :person_id => 1 }).get(:deep), - { "id" => 1, "street" => '12345 Street', "zip" => "27519" } - assert_equal ActiveResource::Response.new("", 204, {}), - StreetAddress.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US') - end - - def test_custom_new_element_method - # Test POST against a new element URL - ryan = Person.new(:name => 'Ryan') - assert_equal ActiveResource::Response.new(@ryan, 201, { 'Location' => '/people/5.json' }), ryan.post(:register) - expected_request = ActiveResource::Request.new(:post, '/people/new/register.json', @ryan) - assert_equal expected_request.body, ActiveResource::HttpMock.requests.first.body - - # Test POST against a nested collection URL - addy = StreetAddress.new(:street => '123 Test Dr.', :person_id => 1) - assert_equal ActiveResource::Response.new({ :address => { :street => '12345 Street' } }.to_json, - 201, { 'Location' => '/people/1/addresses/2.json' }), - addy.post(:link) - - matz = Person.find(1) - assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register) - end - - def test_find_custom_resources - assert_equal 'Matz', Person.find(:all, :from => :managers).first.name - end -end diff --git a/activeresource/test/cases/base/equality_test.rb b/activeresource/test/cases/base/equality_test.rb deleted file mode 100644 index fffd8b75c3..0000000000 --- a/activeresource/test/cases/base/equality_test.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" -require "fixtures/street_address" - -class BaseEqualityTest < ActiveSupport::TestCase - def setup - @new = Person.new - @one = Person.new(:id => 1) - @two = Person.new(:id => 2) - @street = StreetAddress.new(:id => 2) - end - - def test_should_equal_self - assert @new == @new, '@new == @new' - assert @one == @one, '@one == @one' - end - - def test_shouldnt_equal_new_resource - assert @new != @one, '@new != @one' - assert @one != @new, '@one != @new' - end - - def test_shouldnt_equal_different_class - assert @two != @street, 'person != street_address with same id' - assert @street != @two, 'street_address != person with same id' - end - - def test_eql_should_alias_equals_operator - assert_equal @new == @new, @new.eql?(@new) - assert_equal @new == @one, @new.eql?(@one) - - assert_equal @one == @one, @one.eql?(@one) - assert_equal @one == @new, @one.eql?(@new) - - assert_equal @one == @street, @one.eql?(@street) - end - - def test_hash_should_be_id_hash - [@new, @one, @two, @street].each do |resource| - assert_equal resource.id.hash, resource.hash - end - end - - def test_with_prefix_options - assert_equal @one == @one, @one.eql?(@one) - assert_equal @one == @one.dup, @one.eql?(@one.dup) - new_one = @one.dup - new_one.prefix_options = {:foo => 'bar'} - assert_not_equal @one, new_one - end - -end diff --git a/activeresource/test/cases/base/load_test.rb b/activeresource/test/cases/base/load_test.rb deleted file mode 100644 index f07e1ea16b..0000000000 --- a/activeresource/test/cases/base/load_test.rb +++ /dev/null @@ -1,199 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" -require "fixtures/street_address" -require 'active_support/core_ext/hash/conversions' - -module Highrise - class Note < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - class Comment < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - module Deeply - module Nested - class Note < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - class Comment < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - module TestDifferentLevels - class Note < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - end - end - end -end - - -class BaseLoadTest < ActiveSupport::TestCase - def setup - @matz = { :id => 1, :name => 'Matz' } - - @first_address = { :address => { :id => 1, :street => '12345 Street' } } - @addresses = [@first_address, { :address => { :id => 2, :street => '67890 Street' } }] - @addresses_from_json = { :street_addresses => @addresses } - @addresses_from_json_single = { :street_addresses => [ @first_address ] } - - @deep = { :id => 1, :street => { - :id => 1, :state => { :id => 1, :name => 'Oregon', - :notable_rivers => [ - { :id => 1, :name => 'Willamette' }, - { :id => 2, :name => 'Columbia', :rafted_by => @matz }], - :postal_codes => [ 97018, 1234567890 ], - :dates => [ Time.now ], - :votes => [ true, false, true ], - :places => [ "Columbia City", "Unknown" ]}}} - - - # List of books formated as [{timestamp_of_publication => name}, ...] - @books = {:books => [ - {1009839600 => "Ruby in a Nutshell"}, - {1199142000 => "The Ruby Programming Language"} - ]} - - @books_date = {:books => [ - {Time.at(1009839600) => "Ruby in a Nutshell"}, - {Time.at(1199142000) => "The Ruby Programming Language"} - ]} - @person = Person.new - end - - def test_load_hash_with_integers_as_keys - assert_nothing_raised{@person.load(@books)} - end - - def test_load_hash_with_dates_as_keys - assert_nothing_raised{@person.load(@books_date)} - end - - def test_load_expects_hash - assert_raise(ArgumentError) { @person.load nil } - assert_raise(ArgumentError) { @person.load '<person id="1"/>' } - end - - def test_load_simple_hash - assert_equal Hash.new, @person.attributes - assert_equal @matz.stringify_keys, @person.load(@matz).attributes - end - - def test_after_load_attributes_are_accessible - assert_equal Hash.new, @person.attributes - assert_equal @matz.stringify_keys, @person.load(@matz).attributes - assert_equal @matz[:name], @person.attributes['name'] - end - - def test_after_load_attributes_are_accessible_via_indifferent_access - assert_equal Hash.new, @person.attributes - assert_equal @matz.stringify_keys, @person.load(@matz).attributes - assert_equal @matz[:name], @person.attributes['name'] - assert_equal @matz[:name], @person.attributes[:name] - end - - def test_load_one_with_existing_resource - address = @person.load(:street_address => @first_address.values.first).street_address - assert_kind_of StreetAddress, address - assert_equal @first_address.values.first.stringify_keys, address.attributes - end - - def test_load_one_with_unknown_resource - address = silence_warnings { @person.load(@first_address).address } - assert_kind_of Person::Address, address - assert_equal @first_address.values.first.stringify_keys, address.attributes - end - - def test_load_collection_with_existing_resource - addresses = @person.load(@addresses_from_json).street_addresses - assert_kind_of Array, addresses - addresses.each { |address| assert_kind_of StreetAddress, address } - assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes) - end - - def test_load_collection_with_unknown_resource - Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) - assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" - addresses = silence_warnings { @person.load(:addresses => @addresses).addresses } - assert Person.const_defined?(:Address), "Address should have been autocreated" - addresses.each { |address| assert_kind_of Person::Address, address } - assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes) - end - - def test_load_collection_with_single_existing_resource - addresses = @person.load(@addresses_from_json_single).street_addresses - assert_kind_of Array, addresses - addresses.each { |address| assert_kind_of StreetAddress, address } - assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes) - end - - def test_load_collection_with_single_unknown_resource - Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) - assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" - addresses = silence_warnings { @person.load(:addresses => [ @first_address ]).addresses } - assert Person.const_defined?(:Address), "Address should have been autocreated" - addresses.each { |address| assert_kind_of Person::Address, address } - assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes) - end - - def test_recursively_loaded_collections - person = @person.load(@deep) - assert_equal @deep[:id], person.id - - street = person.street - assert_kind_of Person::Street, street - assert_equal @deep[:street][:id], street.id - - state = street.state - assert_kind_of Person::Street::State, state - assert_equal @deep[:street][:state][:id], state.id - - rivers = state.notable_rivers - assert_kind_of Array, rivers - assert_kind_of Person::Street::State::NotableRiver, rivers.first - assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id - assert_equal @matz[:id], rivers.last.rafted_by.id - - postal_codes = state.postal_codes - assert_kind_of Array, postal_codes - assert_equal 2, postal_codes.size - assert_kind_of Fixnum, postal_codes.first - assert_equal @deep[:street][:state][:postal_codes].first, postal_codes.first - assert_kind_of Numeric, postal_codes.last - assert_equal @deep[:street][:state][:postal_codes].last, postal_codes.last - - places = state.places - assert_kind_of Array, places - assert_kind_of String, places.first - assert_equal @deep[:street][:state][:places].first, places.first - - dates = state.dates - assert_kind_of Array, dates - assert_kind_of Time, dates.first - assert_equal @deep[:street][:state][:dates].first, dates.first - - votes = state.votes - assert_kind_of Array, votes - assert_kind_of TrueClass, votes.first - assert_equal @deep[:street][:state][:votes].first, votes.first - end - - def test_nested_collections_within_the_same_namespace - n = Highrise::Note.new(:comments => [{ :comment => { :name => "1" } }]) - assert_kind_of Highrise::Comment, n.comments.first - end - - def test_nested_collections_within_deeply_nested_namespace - n = Highrise::Deeply::Nested::Note.new(:comments => [{ :name => "1" }]) - assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first - end - - def test_nested_collections_in_different_levels_of_namespaces - n = Highrise::Deeply::Nested::TestDifferentLevels::Note.new(:comments => [{ :name => "1" }]) - assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first - end -end diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb deleted file mode 100644 index d29eaf5fb6..0000000000 --- a/activeresource/test/cases/base/schema_test.rb +++ /dev/null @@ -1,411 +0,0 @@ -require 'abstract_unit' -require 'active_support/core_ext/hash/conversions' -require "fixtures/person" -require "fixtures/street_address" - -######################################################################## -# Testing the schema of your Active Resource models -######################################################################## -class SchemaTest < ActiveModel::TestCase - def setup - setup_response # find me in abstract_unit - end - - def teardown - Person.schema = nil # hack to stop test bleedthrough... - end - - ##################################################### - # Passing in a schema directly and returning it - #### - - test "schema on a new model should be empty" do - assert Person.schema.blank?, "should have a blank class schema" - assert Person.new.schema.blank?, "should have a blank instance schema" - end - - test "schema should only accept a hash" do - ["blahblah", ['one','two'], [:age, :name], Person.new].each do |bad_schema| - assert_raises(ArgumentError,"should only accept a hash (or nil), but accepted: #{bad_schema.inspect}") do - Person.schema = bad_schema - end - end - end - - test "schema should accept a simple hash" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - - assert_nothing_raised { Person.schema = new_schema } - assert_equal new_schema, Person.schema - end - - test "schema should accept a hash with simple values" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - assert_equal new_schema, Person.schema - end - - test "schema should accept all known attribute types as values" do - # I'd prefer to use here... - ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - assert_nothing_raised("should have accepted #{the_type.inspect}"){ Person.schema = {'my_key' => the_type }} - end - end - - test "schema should not accept unknown values" do - bad_values = [ :oogle, :blob, 'thing'] - - bad_values.each do |bad_value| - assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do - Person.schema = {'key' => bad_value} - end - end - end - - test "schema should accept nil and remove the schema" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - assert_equal new_schema, Person.schema # sanity check - - - assert_nothing_raised { Person.schema = nil } - assert_nil Person.schema, "should have nulled out the schema, but still had: #{Person.schema.inspect}" - end - - - test "schema should be with indifferent access" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - new_schema_syms = new_schema.keys - - assert_nothing_raised { Person.schema = new_schema } - new_schema_syms.each do |col| - assert Person.new.respond_to?(col.to_s), "should respond to the schema's string key, but failed on: #{col.to_s}" - assert Person.new.respond_to?(col.to_sym), "should respond to the schema's symbol key, but failed on: #{col.to_sym}" - end - end - - - test "schema on a fetched resource should return all the attributes of that model instance" do - p = Person.find(1) - s = p.schema - - assert s.present?, "should have found a non-empty schema!" - - p.attributes.each do |the_attr, val| - assert s.has_key?(the_attr), "should have found attr: #{the_attr} in schema, but only had: #{s.inspect}" - end - end - - test "with two instances, default schema should match the attributes of the individual instances - even if they differ" do - matz = Person.find(1) - rick = Person.find(6) - - m_attrs = matz.attributes.keys.sort - r_attrs = rick.attributes.keys.sort - - assert_not_equal m_attrs, r_attrs, "should have different attributes on each model" - - assert_not_equal matz.schema, rick.schema, "should have had different schemas too" - end - - test "defining a schema should return it when asked" do - assert Person.schema.blank?, "should have a blank class schema" - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { - Person.schema = new_schema - assert_equal new_schema, Person.schema, "should have saved the schema on the class" - assert_equal new_schema, Person.new.schema, "should have made the schema available to every instance" - } - end - - test "defining a schema, then fetching a model should still match the defined schema" do - # sanity checks - assert Person.schema.blank?, "should have a blank class schema" - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - matz = Person.find(1) - assert !matz.schema.blank?, "should have some sort of schema on an instance variable" - assert_not_equal new_schema, matz.schema, "should not have the class-level schema until it's been added to the class!" - - assert_nothing_raised { - Person.schema = new_schema - assert_equal new_schema, matz.schema, "class-level schema should override instance-level schema" - } - end - - - ##################################################### - # Using the schema syntax - #### - - test "should be able to use schema" do - assert_respond_to Person, :schema, "should at least respond to the schema method" - - assert_nothing_raised("Should allow the schema to take a block") do - Person.schema { } - end - end - - test "schema definition should store and return attribute set" do - assert_nothing_raised do - s = nil - Person.schema do - s = self - attribute :foo, :string - end - assert_respond_to s, :attrs, "should return attributes in theory" - assert_equal({'foo' => 'string' }, s.attrs, "should return attributes in practice") - end - end - - test "should be able to add attributes through schema" do - assert_nothing_raised do - s = nil - Person.schema do - s = self - attribute('foo', 'string') - end - assert s.attrs.has_key?('foo'), "should have saved the attribute name" - assert_equal 'string', s.attrs['foo'], "should have saved the attribute type" - end - end - - test "should convert symbol attributes to strings" do - assert_nothing_raised do - s = nil - Person.schema do - attribute(:foo, :integer) - s = self - end - - assert s.attrs.has_key?('foo'), "should have saved the attribute name as a string" - assert_equal 'integer', s.attrs['foo'], "should have saved the attribute type as a string" - end - end - - test "should be able to add all known attribute types" do - assert_nothing_raised do - ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - s = nil - Person.schema do - s = self - attribute('foo', the_type) - end - assert s.attrs.has_key?('foo'), "should have saved the attribute name" - assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" - end - end - end - - test "attributes should not accept unknown values" do - bad_values = [ :oogle, :blob, 'thing'] - - bad_values.each do |bad_value| - s = nil - assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do - Person.schema do - s = self - attribute 'key', bad_value - end - end - assert !self.respond_to?(bad_value), "should only respond to a known attribute type, but accepted: #{bad_value.inspect}" - assert_raises(NoMethodError,"should only have methods for known attribute types, but accepted: #{bad_value.inspect}") do - Person.schema do - send bad_value, 'key' - end - end - end - end - - - test "should accept attribute types as the type's name as the method" do - ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - s = nil - Person.schema do - s = self - send(the_type,'foo') - end - assert s.attrs.has_key?('foo'), "should now have saved the attribute name" - assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" - end - end - - test "should accept multiple attribute names for an attribute method" do - names = ['foo','bar','baz'] - s = nil - Person.schema do - s = self - string(*names) - end - names.each do |the_name| - assert s.attrs.has_key?(the_name), "should now have saved the attribute name: #{the_name}" - assert_equal 'string', s.attrs[the_name], "should have saved the attribute as a string" - end - end - - ##################################################### - # What a schema does for us - #### - - # respond_to? - - test "should respond positively to attributes that are only in the schema" do - new_attr_name = :my_new_schema_attribute - new_attr_name_two = :another_new_schema_attribute - assert Person.schema.blank?, "sanity check - should have a blank class schema" - - assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" - - assert_nothing_raised do - Person.schema = {new_attr_name.to_s => 'string'} - Person.schema { string new_attr_name_two } - end - - assert_respond_to Person.new, new_attr_name, "should respond to the attribute in a passed-in schema, but failed on: #{new_attr_name}" - assert_respond_to Person.new, new_attr_name_two, "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}" - end - - test "should not care about ordering of schema definitions" do - new_attr_name = :my_new_schema_attribute - new_attr_name_two = :another_new_schema_attribute - - assert Person.schema.blank?, "sanity check - should have a blank class schema" - - assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" - - assert_nothing_raised do - Person.schema { string new_attr_name_two } - Person.schema = {new_attr_name.to_s => 'string'} - end - - assert_respond_to Person.new, new_attr_name, "should respond to the attribute in a passed-in schema, but failed on: #{new_attr_name}" - assert_respond_to Person.new, new_attr_name_two, "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}" - end - - # method_missing effects - - test "should not give method_missing for attribute only in schema" do - new_attr_name = :another_new_schema_attribute - new_attr_name_two = :another_new_schema_attribute - - assert Person.schema.blank?, "sanity check - should have a blank class schema" - - assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name} as a method") do - Person.new.send(new_attr_name) - end - assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name_two} as a method") do - Person.new.send(new_attr_name_two) - end - - Person.schema = {new_attr_name.to_s => :float} - Person.schema { string new_attr_name_two } - - assert_nothing_raised do - Person.new.send(new_attr_name) - Person.new.send(new_attr_name_two) - end - end - - - ######## - # Known attributes - # - # Attributes can be known to be attributes even if they aren't actually - # 'set' on a particular instance. - # This will only differ from 'attributes' if a schema has been set. - - test "new model should have no known attributes" do - assert Person.known_attributes.blank?, "should have no known attributes" - assert Person.new.known_attributes.blank?, "should have no known attributes on a new instance" - end - - test "setting schema should set known attributes on class and instance" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - - assert_equal new_schema.keys.sort, Person.known_attributes.sort - assert_equal new_schema.keys.sort, Person.new.known_attributes.sort - end - - test "known attributes on a fetched resource should return all the attributes of the instance" do - p = Person.find(1) - attrs = p.known_attributes - - assert attrs.present?, "should have found some attributes!" - - p.attributes.each do |the_attr, val| - assert attrs.include?(the_attr), "should have found attr: #{the_attr} in known attributes, but only had: #{attrs.inspect}" - end - end - - test "with two instances, known attributes should match the attributes of the individual instances - even if they differ" do - matz = Person.find(1) - rick = Person.find(6) - - m_attrs = matz.attributes.keys.sort - r_attrs = rick.attributes.keys.sort - - assert_not_equal m_attrs, r_attrs, "should have different attributes on each model" - - assert_not_equal matz.known_attributes, rick.known_attributes, "should have had different known attributes too" - end - - test "setting schema then fetching should add schema attributes to the instance attributes" do - # an attribute in common with fetched instance and one that isn't - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - - matz = Person.find(1) - known_attrs = matz.known_attributes - - matz.attributes.keys.each do |the_attr| - assert known_attrs.include?(the_attr), "should have found instance attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}" - end - new_schema.keys.each do |the_attr| - assert known_attrs.include?(the_attr), "should have found schema attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}" - end - end - - -end diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb deleted file mode 100644 index 88ac2de96e..0000000000 --- a/activeresource/test/cases/base_errors_test.rb +++ /dev/null @@ -1,137 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" - -class BaseErrorsTest < ActiveSupport::TestCase - def setup - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.xml", {}, %q(<?xml version="1.0" encoding="UTF-8"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>), 422, {'Content-Type' => 'application/xml; charset=utf-8'} - mock.post "/people.json", {}, %q({"errors":{"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}}), 422, {'Content-Type' => 'application/json; charset=utf-8'} - end - end - - def test_should_mark_as_invalid - [ :json, :xml ].each do |format| - invalid_user_using_format(format) do - assert !@person.valid? - end - end - end - - def test_should_parse_json_and_xml_errors - [ :json, :xml ].each do |format| - invalid_user_using_format(format) do - assert_kind_of ActiveResource::Errors, @person.errors - assert_equal 4, @person.errors.size - end - end - end - - def test_should_parse_json_errors_when_no_errors_key - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.json", {}, '{}', 422, {'Content-Type' => 'application/json; charset=utf-8'} - end - - invalid_user_using_format(:json) do - assert_kind_of ActiveResource::Errors, @person.errors - assert_equal 0, @person.errors.size - end - end - - def test_should_parse_errors_to_individual_attributes - [ :json, :xml ].each do |format| - invalid_user_using_format(format) do - assert @person.errors[:name].any? - assert_equal ["can't be blank"], @person.errors[:age] - assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] - assert_equal ["Person quota full for today."], @person.errors[:base] - end - end - end - - def test_should_iterate_over_errors - [ :json, :xml ].each do |format| - invalid_user_using_format(format) do - errors = [] - @person.errors.each { |attribute, message| errors << [attribute, message] } - assert errors.include?([:name, "can't be blank"]) - end - end - end - - def test_should_iterate_over_full_errors - [ :json, :xml ].each do |format| - invalid_user_using_format(format) do - errors = [] - @person.errors.to_a.each { |message| errors << message } - assert errors.include?("Name can't be blank") - end - end - end - - def test_should_format_full_errors - [ :json, :xml ].each do |format| - invalid_user_using_format(format) do - full = @person.errors.full_messages - assert full.include?("Age can't be blank") - assert full.include?("Name can't be blank") - assert full.include?("Name must start with a letter") - assert full.include?("Person quota full for today.") - end - end - end - - def test_should_mark_as_invalid_when_content_type_is_unavailable_in_response_header - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.xml", {}, %q(<?xml version="1.0" encoding="UTF-8"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>), 422, {} - mock.post "/people.json", {}, %q({"errors":{"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}}), 422, {} - end - - [ :json, :xml ].each do |format| - invalid_user_using_format(format) do - assert !@person.valid? - end - end - end - - def test_should_parse_json_string_errors_with_an_errors_key - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.json", {}, %q({"errors":["Age can't be blank", "Name can't be blank", "Name must start with a letter", "Person quota full for today."]}), 422, {'Content-Type' => 'application/json; charset=utf-8'} - end - - assert_deprecated(/as an array/) do - invalid_user_using_format(:json) do - assert @person.errors[:name].any? - assert_equal ["can't be blank"], @person.errors[:age] - assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] - assert_equal ["Person quota full for today."], @person.errors[:base] - end - end - end - - def test_should_parse_3_1_style_json_errors - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.json", {}, %q({"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}), 422, {'Content-Type' => 'application/json; charset=utf-8'} - end - - assert_deprecated(/without a root/) do - invalid_user_using_format(:json) do - assert @person.errors[:name].any? - assert_equal ["can't be blank"], @person.errors[:age] - assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] - assert_equal ["Person quota full for today."], @person.errors[:base] - end - end - end - - private - def invalid_user_using_format(mime_type_reference) - previous_format = Person.format - Person.format = mime_type_reference - @person = Person.new(:name => '', :age => '') - assert_equal false, @person.save - - yield - ensure - Person.format = previous_format - end -end diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb deleted file mode 100644 index 33a6596602..0000000000 --- a/activeresource/test/cases/base_test.rb +++ /dev/null @@ -1,1177 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" -require "fixtures/customer" -require "fixtures/street_address" -require "fixtures/sound" -require "fixtures/beast" -require "fixtures/proxy" -require "fixtures/address" -require "fixtures/subscription_plan" -require 'active_support/json' -require 'active_support/core_ext/hash/conversions' -require 'mocha' - -class BaseTest < ActiveSupport::TestCase - def setup - setup_response # find me in abstract_unit - @original_person_site = Person.site - end - - def teardown - Person.site = @original_person_site - end - - ######################################################################## - # Tests relating to setting up the API-connection configuration - ######################################################################## - - def test_site_accessor_accepts_uri_or_string_argument - site = URI.parse('http://localhost') - - assert_nothing_raised { Person.site = 'http://localhost' } - assert_equal site, Person.site - - assert_nothing_raised { Person.site = site } - assert_equal site, Person.site - end - - def test_should_use_site_prefix_and_credentials - assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s - assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s - end - - def test_site_variable_can_be_reset - actor = Class.new(ActiveResource::Base) - assert_nil actor.site - actor.site = 'http://localhost:31337' - actor.site = nil - assert_nil actor.site - end - - def test_proxy_accessor_accepts_uri_or_string_argument - proxy = URI.parse('http://localhost') - - assert_nothing_raised { Person.proxy = 'http://localhost' } - assert_equal proxy, Person.proxy - - assert_nothing_raised { Person.proxy = proxy } - assert_equal proxy, Person.proxy - end - - def test_should_use_proxy_prefix_and_credentials - assert_equal 'http://user:password@proxy.local:3000', ProxyResource.proxy.to_s - end - - def test_proxy_variable_can_be_reset - actor = Class.new(ActiveResource::Base) - assert_nil actor.site - actor.proxy = 'http://localhost:31337' - actor.proxy = nil - assert_nil actor.site - end - - def test_should_accept_setting_user - Forum.user = 'david' - assert_equal('david', Forum.user) - assert_equal('david', Forum.connection.user) - end - - def test_should_accept_setting_password - Forum.password = 'test123' - assert_equal('test123', Forum.password) - assert_equal('test123', Forum.connection.password) - end - - def test_should_accept_setting_auth_type - Forum.auth_type = :digest - assert_equal(:digest, Forum.auth_type) - assert_equal(:digest, Forum.connection.auth_type) - end - - def test_should_accept_setting_timeout - Forum.timeout = 5 - assert_equal(5, Forum.timeout) - assert_equal(5, Forum.connection.timeout) - end - - def test_should_accept_setting_ssl_options - expected = {:verify => 1} - Forum.ssl_options= expected - assert_equal(expected, Forum.ssl_options) - assert_equal(expected, Forum.connection.ssl_options) - end - - def test_user_variable_can_be_reset - actor = Class.new(ActiveResource::Base) - actor.site = 'http://cinema' - assert_nil actor.user - actor.user = 'username' - actor.user = nil - assert_nil actor.user - assert_nil actor.connection.user - end - - def test_password_variable_can_be_reset - actor = Class.new(ActiveResource::Base) - actor.site = 'http://cinema' - assert_nil actor.password - actor.password = 'username' - actor.password = nil - assert_nil actor.password - assert_nil actor.connection.password - end - - def test_timeout_variable_can_be_reset - actor = Class.new(ActiveResource::Base) - actor.site = 'http://cinema' - assert_nil actor.timeout - actor.timeout = 5 - actor.timeout = nil - assert_nil actor.timeout - assert_nil actor.connection.timeout - end - - def test_ssl_options_hash_can_be_reset - actor = Class.new(ActiveResource::Base) - actor.site = 'https://cinema' - assert_nil actor.ssl_options - actor.ssl_options = {:foo => 5} - actor.ssl_options = nil - assert_nil actor.ssl_options - assert_nil actor.connection.ssl_options - end - - def test_credentials_from_site_are_decoded - actor = Class.new(ActiveResource::Base) - actor.site = 'http://my%40email.com:%31%32%33@cinema' - assert_equal("my@email.com", actor.user) - assert_equal("123", actor.password) - end - - def test_site_reader_uses_superclass_site_until_written - # Superclass is Object so returns nil. - assert_nil ActiveResource::Base.site - assert_nil Class.new(ActiveResource::Base).site - - # Subclass uses superclass site. - actor = Class.new(Person) - assert_equal Person.site, actor.site - - # Subclass returns frozen superclass copy. - assert !Person.site.frozen? - assert actor.site.frozen? - - # Changing subclass site doesn't change superclass site. - actor.site = 'http://localhost:31337' - assert_not_equal Person.site, actor.site - - # Changed subclass site is not frozen. - assert !actor.site.frozen? - - # Changing superclass site doesn't overwrite subclass site. - Person.site = 'http://somewhere.else' - assert_not_equal Person.site, actor.site - - # Changing superclass site after subclassing changes subclass site. - jester = Class.new(actor) - actor.site = 'http://nomad' - assert_equal actor.site, jester.site - assert jester.site.frozen? - - # Subclasses are always equal to superclass site when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - - fruit.site = 'http://market' - assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class' - - fruit.site = 'http://supermarket' - assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class' - end - - def test_proxy_reader_uses_superclass_site_until_written - # Superclass is Object so returns nil. - assert_nil ActiveResource::Base.proxy - assert_nil Class.new(ActiveResource::Base).proxy - - # Subclass uses superclass proxy. - actor = Class.new(Person) - assert_equal Person.proxy, actor.proxy - - # Subclass returns frozen superclass copy. - assert !Person.proxy.frozen? - assert actor.proxy.frozen? - - # Changing subclass proxy doesn't change superclass site. - actor.proxy = 'http://localhost:31337' - assert_not_equal Person.proxy, actor.proxy - - # Changed subclass proxy is not frozen. - assert !actor.proxy.frozen? - - # Changing superclass proxy doesn't overwrite subclass site. - Person.proxy = 'http://somewhere.else' - assert_not_equal Person.proxy, actor.proxy - - # Changing superclass proxy after subclassing changes subclass site. - jester = Class.new(actor) - actor.proxy = 'http://nomad' - assert_equal actor.proxy, jester.proxy - assert jester.proxy.frozen? - - # Subclasses are always equal to superclass proxy when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - - fruit.proxy = 'http://market' - assert_equal fruit.proxy, apple.proxy, 'subclass did not adopt changes from parent class' - - fruit.proxy = 'http://supermarket' - assert_equal fruit.proxy, apple.proxy, 'subclass did not adopt changes from parent class' - end - - def test_user_reader_uses_superclass_user_until_written - # Superclass is Object so returns nil. - assert_nil ActiveResource::Base.user - assert_nil Class.new(ActiveResource::Base).user - Person.user = 'anonymous' - - # Subclass uses superclass user. - actor = Class.new(Person) - assert_equal Person.user, actor.user - - # Subclass returns frozen superclass copy. - assert !Person.user.frozen? - assert actor.user.frozen? - - # Changing subclass user doesn't change superclass user. - actor.user = 'david' - assert_not_equal Person.user, actor.user - - # Changing superclass user doesn't overwrite subclass user. - Person.user = 'john' - assert_not_equal Person.user, actor.user - - # Changing superclass user after subclassing changes subclass user. - jester = Class.new(actor) - actor.user = 'john.doe' - assert_equal actor.user, jester.user - - # Subclasses are always equal to superclass user when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - - fruit.user = 'manager' - assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class' - - fruit.user = 'client' - assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class' - end - - def test_password_reader_uses_superclass_password_until_written - # Superclass is Object so returns nil. - assert_nil ActiveResource::Base.password - assert_nil Class.new(ActiveResource::Base).password - Person.password = 'my-password' - - # Subclass uses superclass password. - actor = Class.new(Person) - assert_equal Person.password, actor.password - - # Subclass returns frozen superclass copy. - assert !Person.password.frozen? - assert actor.password.frozen? - - # Changing subclass password doesn't change superclass password. - actor.password = 'secret' - assert_not_equal Person.password, actor.password - - # Changing superclass password doesn't overwrite subclass password. - Person.password = 'super-secret' - assert_not_equal Person.password, actor.password - - # Changing superclass password after subclassing changes subclass password. - jester = Class.new(actor) - actor.password = 'even-more-secret' - assert_equal actor.password, jester.password - - # Subclasses are always equal to superclass password when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - - fruit.password = 'mega-secret' - assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class' - - fruit.password = 'ok-password' - assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class' - end - - def test_timeout_reader_uses_superclass_timeout_until_written - # Superclass is Object so returns nil. - assert_nil ActiveResource::Base.timeout - assert_nil Class.new(ActiveResource::Base).timeout - Person.timeout = 5 - - # Subclass uses superclass timeout. - actor = Class.new(Person) - assert_equal Person.timeout, actor.timeout - - # Changing subclass timeout doesn't change superclass timeout. - actor.timeout = 10 - assert_not_equal Person.timeout, actor.timeout - - # Changing superclass timeout doesn't overwrite subclass timeout. - Person.timeout = 15 - assert_not_equal Person.timeout, actor.timeout - - # Changing superclass timeout after subclassing changes subclass timeout. - jester = Class.new(actor) - actor.timeout = 20 - assert_equal actor.timeout, jester.timeout - - # Subclasses are always equal to superclass timeout when not overridden. - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - - fruit.timeout = 25 - assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class' - - fruit.timeout = 30 - assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class' - end - - def test_ssl_options_reader_uses_superclass_ssl_options_until_written - # Superclass is Object so returns nil. - assert_nil ActiveResource::Base.ssl_options - assert_nil Class.new(ActiveResource::Base).ssl_options - Person.ssl_options = {:foo => 'bar'} - - # Subclass uses superclass ssl_options. - actor = Class.new(Person) - assert_equal Person.ssl_options, actor.ssl_options - - # Changing subclass ssl_options doesn't change superclass ssl_options. - actor.ssl_options = {:baz => ''} - assert_not_equal Person.ssl_options, actor.ssl_options - - # Changing superclass ssl_options doesn't overwrite subclass ssl_options. - Person.ssl_options = {:color => 'blue'} - assert_not_equal Person.ssl_options, actor.ssl_options - - # Changing superclass ssl_options after subclassing changes subclass ssl_options. - jester = Class.new(actor) - actor.ssl_options = {:color => 'red'} - assert_equal actor.ssl_options, jester.ssl_options - - # Subclasses are always equal to superclass ssl_options when not overridden. - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - - fruit.ssl_options = {:alpha => 'betas'} - assert_equal fruit.ssl_options, apple.ssl_options, 'subclass did not adopt changes from parent class' - - fruit.ssl_options = {:omega => 'moos'} - assert_equal fruit.ssl_options, apple.ssl_options, 'subclass did not adopt changes from parent class' - end - - def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects - # Subclasses are always equal to superclass site when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - - fruit.site = 'http://market' - assert_equal fruit.connection.site, apple.connection.site - first_connection = apple.connection.object_id - - fruit.site = 'http://supermarket' - assert_equal fruit.connection.site, apple.connection.site - second_connection = apple.connection.object_id - assert_not_equal(first_connection, second_connection, 'Connection should be re-created') - end - - def test_updating_baseclass_user_wipes_descendent_cached_connection_objects - # Subclasses are always equal to superclass user when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - fruit.site = 'http://market' - - fruit.user = 'david' - assert_equal fruit.connection.user, apple.connection.user - first_connection = apple.connection.object_id - - fruit.user = 'john' - assert_equal fruit.connection.user, apple.connection.user - second_connection = apple.connection.object_id - assert_not_equal(first_connection, second_connection, 'Connection should be re-created') - end - - def test_updating_baseclass_password_wipes_descendent_cached_connection_objects - # Subclasses are always equal to superclass password when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - fruit.site = 'http://market' - - fruit.password = 'secret' - assert_equal fruit.connection.password, apple.connection.password - first_connection = apple.connection.object_id - - fruit.password = 'supersecret' - assert_equal fruit.connection.password, apple.connection.password - second_connection = apple.connection.object_id - assert_not_equal(first_connection, second_connection, 'Connection should be re-created') - end - - def test_updating_baseclass_timeout_wipes_descendent_cached_connection_objects - # Subclasses are always equal to superclass timeout when not overridden - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - fruit.site = 'http://market' - - fruit.timeout = 5 - assert_equal fruit.connection.timeout, apple.connection.timeout - first_connection = apple.connection.object_id - - fruit.timeout = 10 - assert_equal fruit.connection.timeout, apple.connection.timeout - second_connection = apple.connection.object_id - assert_not_equal(first_connection, second_connection, 'Connection should be re-created') - end - - def test_header_inheritance - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - fruit.site = 'http://market' - - fruit.headers['key'] = 'value' - assert_equal 'value', apple.headers['key'] - end - - def test_header_inheritance_set_at_multiple_points - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - fruit.site = 'http://market' - - fruit.headers['key'] = 'value' - assert_equal 'value', apple.headers['key'] - - apple.headers['key2'] = 'value2' - fruit.headers['key3'] = 'value3' - - assert_equal 'value', apple.headers['key'] - assert_equal 'value2', apple.headers['key2'] - assert_equal 'value3', apple.headers['key3'] - end - - def test_header_inheritance_should_not_leak_upstream - fruit = Class.new(ActiveResource::Base) - apple = Class.new(fruit) - fruit.site = 'http://market' - - fruit.headers['key'] = 'value' - - apple.headers['key2'] = 'value2' - assert_equal nil, fruit.headers['key2'] - end - - ######################################################################## - # Tests for setting up remote URLs for a given model (including adding - # parameters appropriately) - ######################################################################## - def test_collection_name - assert_equal "people", Person.collection_name - end - - def test_collection_path - assert_equal '/people.json', Person.collection_path - end - - def test_collection_path_with_parameters - assert_equal '/people.json?gender=male', Person.collection_path(:gender => 'male') - assert_equal '/people.json?gender=false', Person.collection_path(:gender => false) - assert_equal '/people.json?gender=', Person.collection_path(:gender => nil) - - assert_equal '/people.json?gender=male', Person.collection_path('gender' => 'male') - - # Use includes? because ordering of param hash is not guaranteed - assert Person.collection_path(:gender => 'male', :student => true).include?('/people.json?') - assert Person.collection_path(:gender => 'male', :student => true).include?('gender=male') - assert Person.collection_path(:gender => 'male', :student => true).include?('student=true') - - assert_equal '/people.json?name%5B%5D=bob&name%5B%5D=your+uncle%2Bme&name%5B%5D=&name%5B%5D=false', Person.collection_path(:name => ['bob', 'your uncle+me', nil, false]) - assert_equal '/people.json?struct%5Ba%5D%5B%5D=2&struct%5Ba%5D%5B%5D=1&struct%5Bb%5D=fred', Person.collection_path(:struct => {:a => [2,1], 'b' => 'fred'}) - end - - def test_custom_element_path - assert_equal '/people/1/addresses/1.json', StreetAddress.element_path(1, :person_id => 1) - assert_equal '/people/1/addresses/1.json', StreetAddress.element_path(1, 'person_id' => 1) - assert_equal '/people/Greg/addresses/1.json', StreetAddress.element_path(1, 'person_id' => 'Greg') - assert_equal '/people/ann%20mary/addresses/ann%20mary.json', StreetAddress.element_path(:'ann mary', 'person_id' => 'ann mary') - end - - def test_custom_element_path_without_required_prefix_param - assert_raise ActiveResource::MissingPrefixParam do - StreetAddress.element_path(1) - end - end - - def test_module_element_path - assert_equal '/sounds/1.json', Asset::Sound.element_path(1) - end - - def test_custom_element_path_with_redefined_to_param - Person.module_eval do - alias_method :original_to_param_element_path, :to_param - def to_param - name - end - end - - # Class method. - assert_equal '/people/Greg.json', Person.element_path('Greg') - - # Protected Instance method. - assert_equal '/people/Greg.json', Person.find('Greg').send(:element_path) - - ensure - # revert back to original - Person.module_eval do - # save the 'new' to_param so we don't get a warning about discarding the method - alias_method :element_path_to_param, :to_param - alias_method :to_param, :original_to_param_element_path - end - end - - def test_custom_element_path_with_parameters - assert_equal '/people/1/addresses/1.json?type=work', StreetAddress.element_path(1, :person_id => 1, :type => 'work') - assert_equal '/people/1/addresses/1.json?type=work', StreetAddress.element_path(1, 'person_id' => 1, :type => 'work') - assert_equal '/people/1/addresses/1.json?type=work', StreetAddress.element_path(1, :type => 'work', :person_id => 1) - assert_equal '/people/1/addresses/1.json?type%5B%5D=work&type%5B%5D=play+time', StreetAddress.element_path(1, :person_id => 1, :type => ['work', 'play time']) - end - - def test_custom_element_path_with_prefix_and_parameters - assert_equal '/people/1/addresses/1.json?type=work', StreetAddress.element_path(1, {:person_id => 1}, {:type => 'work'}) - end - - def test_custom_collection_path_without_required_prefix_param - assert_raise ActiveResource::MissingPrefixParam do - StreetAddress.collection_path - end - end - - def test_custom_collection_path - assert_equal '/people/1/addresses.json', StreetAddress.collection_path(:person_id => 1) - assert_equal '/people/1/addresses.json', StreetAddress.collection_path('person_id' => 1) - end - - def test_custom_collection_path_with_parameters - assert_equal '/people/1/addresses.json?type=work', StreetAddress.collection_path(:person_id => 1, :type => 'work') - assert_equal '/people/1/addresses.json?type=work', StreetAddress.collection_path('person_id' => 1, :type => 'work') - end - - def test_custom_collection_path_with_prefix_and_parameters - assert_equal '/people/1/addresses.json?type=work', StreetAddress.collection_path({:person_id => 1}, {:type => 'work'}) - end - - def test_custom_element_name - assert_equal 'address', StreetAddress.element_name - end - - def test_custom_collection_name - assert_equal 'addresses', StreetAddress.collection_name - end - - def test_prefix - assert_equal "/", Person.prefix - assert_equal Set.new, Person.__send__(:prefix_parameters) - end - - def test_set_prefix - SetterTrap.rollback_sets(Person) do |person_class| - person_class.prefix = "the_prefix" - assert_equal "the_prefix", person_class.prefix - end - end - - def test_set_prefix_with_inline_keys - SetterTrap.rollback_sets(Person) do |person_class| - person_class.prefix = "the_prefix:the_param" - assert_equal "the_prefixthe_param_value", person_class.prefix(:the_param => "the_param_value") - end - end - - def test_set_prefix_twice_should_clear_params - SetterTrap.rollback_sets(Person) do |person_class| - person_class.prefix = "the_prefix/:the_param1" - assert_equal Set.new([:the_param1]), person_class.prefix_parameters - person_class.prefix = "the_prefix/:the_param2" - assert_equal Set.new([:the_param2]), person_class.prefix_parameters - person_class.prefix = "the_prefix/:the_param1/other_prefix/:the_param2" - assert_equal Set.new([:the_param2, :the_param1]), person_class.prefix_parameters - end - end - - def test_set_prefix_with_default_value - SetterTrap.rollback_sets(Person) do |person_class| - person_class.set_prefix - assert_equal "/", person_class.prefix - end - end - - def test_custom_prefix - assert_equal '/people//', StreetAddress.prefix - assert_equal '/people/1/', StreetAddress.prefix(:person_id => 1) - assert_equal [:person_id].to_set, StreetAddress.__send__(:prefix_parameters) - end - - - ######################################################################## - # Tests basic CRUD functions (find/save/create etc) - ######################################################################## - def test_respond_to - matz = Person.find(1) - assert_respond_to matz, :name - assert_respond_to matz, :name= - assert_respond_to matz, :name? - assert !matz.respond_to?(:super_scalable_stuff) - end - - def test_custom_header - Person.headers['key'] = 'value' - assert_raise(ActiveResource::ResourceNotFound) { Person.find(4) } - ensure - Person.headers.delete('key') - end - - def test_save - rick = Person.new - assert rick.save - assert_equal '5', rick.id - end - - def test_save! - rick = Person.new - assert rick.save! - assert_equal '5', rick.id - end - - def test_id_from_response - p = Person.new - resp = {'Location' => '/foo/bar/1'} - assert_equal '1', p.__send__(:id_from_response, resp) - - resp['Location'] << '.json' - assert_equal '1', p.__send__(:id_from_response, resp) - end - - def test_id_from_response_without_location - p = Person.new - resp = {} - assert_nil p.__send__(:id_from_response, resp) - end - - def test_not_persisted_with_no_body_and_positive_content_length - resp = ActiveResource::Response.new(nil) - resp['Content-Length'] = "100" - Person.connection.expects(:post).returns(resp) - assert !Person.create.persisted? - end - - def test_not_persisted_with_body_and_zero_content_length - resp = ActiveResource::Response.new(@rick) - resp['Content-Length'] = "0" - Person.connection.expects(:post).returns(resp) - assert !Person.create.persisted? - end - - # These response codes aren't allowed to have bodies per HTTP spec - def test_not_persisted_with_empty_response_codes - [100,101,204,304].each do |status_code| - resp = ActiveResource::Response.new(@rick, status_code) - Person.connection.expects(:post).returns(resp) - assert !Person.create.persisted? - end - end - - # Content-Length is not required by HTTP 1.1, so we should read - # the body anyway in its absence. - def test_persisted_with_no_content_length - resp = ActiveResource::Response.new(@rick) - resp['Content-Length'] = nil - Person.connection.expects(:post).returns(resp) - assert Person.create.persisted? - end - - def test_create_with_custom_prefix - matzs_house = StreetAddress.new(:person_id => 1) - matzs_house.save - assert_equal '5', matzs_house.id - end - - # Test that loading a resource preserves its prefix_options. - def test_load_preserves_prefix_options - address = StreetAddress.find(1, :params => { :person_id => 1 }) - ryan = Person.new(:id => 1, :name => 'Ryan', :address => address) - assert_equal address.prefix_options, ryan.address.prefix_options - end - - def test_reload_works_with_prefix_options - address = StreetAddress.find(1, :params => { :person_id => 1 }) - assert_equal address, address.reload - end - - def test_reload_with_redefined_to_param - Person.module_eval do - alias_method :original_to_param_reload, :to_param - def to_param - name - end - end - - person = Person.find('Greg') - assert_equal person, person.reload - - ensure - # revert back to original - Person.module_eval do - # save the 'new' to_param so we don't get a warning about discarding the method - alias_method :reload_to_param, :to_param - alias_method :to_param, :original_to_param_reload - end - end - - def test_reload_works_without_prefix_options - person = Person.find(:first) - assert_equal person, person.reload - end - - def test_create - rick = Person.create(:name => 'Rick') - assert rick.valid? - assert !rick.new? - assert_equal '5', rick.id - - # test additional attribute returned on create - assert_equal 25, rick.age - - # Test that save exceptions get bubbled up too - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.json", {}, nil, 409 - end - assert_raise(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') } - end - - def test_create_without_location - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.json", {}, nil, 201 - end - person = Person.create(:name => 'Rick') - assert_nil person.id - end - - def test_clone - matz = Person.find(1) - matz_c = matz.clone - assert matz_c.new? - matz.attributes.each do |k, v| - assert_equal v, matz_c.send(k) if k != Person.primary_key - end - end - - def test_nested_clone - addy = StreetAddress.find(1, :params => {:person_id => 1}) - addy_c = addy.clone - assert addy_c.new? - addy.attributes.each do |k, v| - assert_equal v, addy_c.send(k) if k != StreetAddress.primary_key - end - assert_equal addy.prefix_options, addy_c.prefix_options - end - - def test_complex_clone - matz = Person.find(1) - matz.address = StreetAddress.find(1, :params => {:person_id => matz.id}) - matz.non_ar_hash = {:not => "an ARes instance"} - matz.non_ar_arr = ["not", "ARes"] - matz_c = matz.clone - assert matz_c.new? - assert_raise(NoMethodError) {matz_c.address} - assert_equal matz.non_ar_hash, matz_c.non_ar_hash - assert_equal matz.non_ar_arr, matz_c.non_ar_arr - - # Test that actual copy, not just reference copy - matz.non_ar_hash[:not] = "changed" - assert_not_equal matz.non_ar_hash, matz_c.non_ar_hash - end - - def test_update - matz = Person.find(:first) - matz.name = "David" - assert_kind_of Person, matz - assert_equal "David", matz.name - assert_equal true, matz.save - end - - def test_update_with_custom_prefix_with_specific_id - addy = StreetAddress.find(1, :params => { :person_id => 1 }) - addy.street = "54321 Street" - assert_kind_of StreetAddress, addy - assert_equal "54321 Street", addy.street - addy.save - end - - def test_update_with_custom_prefix_without_specific_id - addy = StreetAddress.find(:first, :params => { :person_id => 1 }) - addy.street = "54321 Lane" - assert_kind_of StreetAddress, addy - assert_equal "54321 Lane", addy.street - addy.save - end - - def test_update_conflict - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/2.json", {}, @david - mock.put "/people/2.json", @default_request_headers, nil, 409 - end - assert_raise(ActiveResource::ResourceConflict) { Person.find(2).save } - end - - - ###### - # update_attribute(s)(!) - - def test_update_attribute_as_symbol - matz = Person.first - matz.expects(:save).returns(true) - - assert_equal "Matz", matz.name - assert matz.update_attribute(:name, "David") - assert_equal "David", matz.name - end - - def test_update_attribute_as_string - matz = Person.first - matz.expects(:save).returns(true) - - assert_equal "Matz", matz.name - assert matz.update_attribute('name', "David") - assert_equal "David", matz.name - end - - - def test_update_attributes_as_symbols - addy = StreetAddress.first(:params => {:person_id => 1}) - addy.expects(:save).returns(true) - - assert_equal "12345 Street", addy.street - assert_equal "Australia", addy.country - assert addy.update_attributes(:street => '54321 Street', :country => 'USA') - assert_equal "54321 Street", addy.street - assert_equal "USA", addy.country - end - - def test_update_attributes_as_strings - addy = StreetAddress.first(:params => {:person_id => 1}) - addy.expects(:save).returns(true) - - assert_equal "12345 Street", addy.street - assert_equal "Australia", addy.country - assert addy.update_attributes('street' => '54321 Street', 'country' => 'USA') - assert_equal "54321 Street", addy.street - assert_equal "USA", addy.country - end - - - ##### - # Mayhem and destruction - - def test_destroy - assert Person.find(1).destroy - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, nil, 404 - end - assert_raise(ActiveResource::ResourceNotFound) { Person.find(1).destroy } - end - - def test_destroy_with_custom_prefix - assert StreetAddress.find(1, :params => { :person_id => 1 }).destroy - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1/addresses/1.json", {}, nil, 404 - end - assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) } - end - - def test_destroy_with_410_gone - assert Person.find(1).destroy - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, nil, 410 - end - assert_raise(ActiveResource::ResourceGone) { Person.find(1).destroy } - end - - def test_delete - assert Person.delete(1) - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, nil, 404 - end - assert_raise(ActiveResource::ResourceNotFound) { Person.find(1) } - end - - def test_delete_with_custom_prefix - assert StreetAddress.delete(1, :person_id => 1) - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1/addresses/1.json", {}, nil, 404 - end - assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) } - end - - def test_delete_with_410_gone - assert Person.delete(1) - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, nil, 410 - end - assert_raise(ActiveResource::ResourceGone) { Person.find(1) } - end - - ######################################################################## - # Tests the more miscellaneous helper methods - ######################################################################## - def test_exists - # Class method. - assert !Person.exists?(nil) - assert Person.exists?(1) - assert !Person.exists?(99) - - # Instance method. - assert !Person.new.exists? - assert Person.find(1).exists? - assert !Person.new(:id => 99).exists? - - # Nested class method. - assert StreetAddress.exists?(1, :params => { :person_id => 1 }) - assert !StreetAddress.exists?(1, :params => { :person_id => 2 }) - assert !StreetAddress.exists?(2, :params => { :person_id => 1 }) - - # Nested instance method. - assert StreetAddress.find(1, :params => { :person_id => 1 }).exists? - assert !StreetAddress.new({:id => 1, :person_id => 2}).exists? - assert !StreetAddress.new({:id => 2, :person_id => 1}).exists? - end - - def test_exists_with_redefined_to_param - Person.module_eval do - alias_method :original_to_param_exists, :to_param - def to_param - name - end - end - - # Class method. - assert Person.exists?('Greg') - - # Instance method. - assert Person.find('Greg').exists? - - # Nested class method. - assert StreetAddress.exists?(1, :params => { :person_id => Person.find('Greg').to_param }) - - # Nested instance method. - assert StreetAddress.find(1, :params => { :person_id => Person.find('Greg').to_param }).exists? - - ensure - # revert back to original - Person.module_eval do - # save the 'new' to_param so we don't get a warning about discarding the method - alias_method :exists_to_param, :to_param - alias_method :to_param, :original_to_param_exists - end - end - - def test_exists_without_http_mock - http = Net::HTTP.new(Person.site.host, Person.site.port) - ActiveResource::Connection.any_instance.expects(:http).returns(http) - http.expects(:request).returns(ActiveResource::Response.new("")) - - assert Person.exists?('not-mocked') - end - - def test_exists_with_410_gone - ActiveResource::HttpMock.respond_to do |mock| - mock.head "/people/1.json", {}, nil, 410 - end - - assert !Person.exists?(1) - end - - def test_to_xml - Person.format = :xml - matz = Person.find(1) - encode = matz.encode - xml = matz.to_xml - - assert_equal encode, xml - assert xml.include?('<?xml version="1.0" encoding="UTF-8"?>') - assert xml.include?('<name>Matz</name>') - assert xml.include?('<id type="integer">1</id>') - ensure - Person.format = :json - end - - def test_to_xml_with_element_name - Person.format = :xml - old_elem_name = Person.element_name - matz = Person.find(1) - Person.element_name = 'ruby_creator' - encode = matz.encode - xml = matz.to_xml - - assert_equal encode, xml - assert xml.include?('<?xml version="1.0" encoding="UTF-8"?>') - assert xml.include?('<ruby-creator>') - assert xml.include?('<name>Matz</name>') - assert xml.include?('<id type="integer">1</id>') - assert xml.include?('</ruby-creator>') - ensure - Person.format = :json - Person.element_name = old_elem_name - end - - def test_to_xml_with_private_method_name_as_attribute - Person.format = :xml - - customer = Customer.new(:foo => "foo") - customer.singleton_class.class_eval do - def foo - "bar" - end - private :foo - end - - assert !customer.to_xml.include?("<foo>bar</foo>") - assert customer.to_xml.include?("<foo>foo</foo>") - ensure - Person.format = :json - end - - def test_to_json - Person.include_root_in_json = true - joe = Person.find(6) - encode = joe.encode - json = joe.to_json - - assert_equal encode, json - assert_match %r{^\{"person":\{}, json - assert_match %r{"id":6}, json - assert_match %r{"name":"Joe"}, json - assert_match %r{\}\}$}, json - end - - def test_to_json_with_element_name - old_elem_name = Person.element_name - Person.include_root_in_json = true - joe = Person.find(6) - Person.element_name = 'ruby_creator' - encode = joe.encode - json = joe.to_json - - assert_equal encode, json - assert_match %r{^\{"ruby_creator":\{}, json - assert_match %r{"id":6}, json - assert_match %r{"name":"Joe"}, json - assert_match %r{\}\}$}, json - ensure - Person.element_name = old_elem_name - end - - def test_to_param_quacks_like_active_record - new_person = Person.new - assert_nil new_person.to_param - matz = Person.find(1) - assert_equal '1', matz.to_param - end - - def test_to_key_quacks_like_active_record - new_person = Person.new - assert_nil new_person.to_key - matz = Person.find(1) - assert_equal [1], matz.to_key - end - - def test_parse_deep_nested_resources - luis = Customer.find(1) - assert_kind_of Customer, luis - luis.friends.each do |friend| - assert_kind_of Customer::Friend, friend - friend.brothers.each do |brother| - assert_kind_of Customer::Friend::Brother, brother - brother.children.each do |child| - assert_kind_of Customer::Friend::Brother::Child, child - end - end - end - end - - def test_load_yaml_array - assert_nothing_raised do - Person.format = :xml - marty = Person.find(5) - assert_equal 3, marty.colors.size - marty.colors.each do |color| - assert_kind_of String, color - end - end - ensure - Person.format = :json - end - - def test_with_custom_formatter - addresses = [{ :id => "1", :street => "1 Infinite Loop", :city => "Cupertino", :state => "CA" }].to_xml(:root => :addresses) - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/addresses.xml", {}, addresses, 200 - end - - # late bind the site - AddressResource.site = "http://localhost" - addresses = AddressResource.find(:all) - - assert_equal "Cupertino, CA", addresses.first.city_state - end - - def test_create_with_custom_primary_key - silver_plan = { :plan => { :code => "silver", :price => 5.00 } }.to_json - - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/plans.json", {}, silver_plan, 201, 'Location' => '/plans/silver.json' - end - - plan = SubscriptionPlan.new(:code => "silver", :price => 5.00) - assert plan.new? - - plan.save! - assert !plan.new? - end - - def test_update_with_custom_primary_key - silver_plan = { :plan => { :code => "silver", :price => 5.00 } }.to_json - silver_plan_updated = { :plan => { :code => "silver", :price => 10.00 } }.to_json - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/plans/silver.json", {}, silver_plan - mock.put "/plans/silver.json", {}, silver_plan_updated, 201, 'Location' => '/plans/silver.json' - end - - plan = SubscriptionPlan.find("silver") - assert !plan.new? - assert_equal 5.00, plan.price - - # update price - plan.price = 10.00 - plan.save! - assert_equal 10.00, plan.price - end - - def test_namespacing - sound = Asset::Sound.find(1) - assert_equal "Asset::Sound::Author", sound.author.class.to_s - end -end diff --git a/activeresource/test/cases/connection_test.rb b/activeresource/test/cases/connection_test.rb deleted file mode 100644 index 0a07ead15e..0000000000 --- a/activeresource/test/cases/connection_test.rb +++ /dev/null @@ -1,275 +0,0 @@ -require 'abstract_unit' - -class ConnectionTest < ActiveSupport::TestCase - ResponseCodeStub = Struct.new(:code) - RedirectResponseStub = Struct.new(:code, :Location) - - def setup - @conn = ActiveResource::Connection.new('http://localhost') - matz = { :person => { :id => 1, :name => 'Matz' } } - david = { :person => { :id => 2, :name => 'David' } } - @people = { :people => [ matz, david ] }.to_json - @people_single = { 'people-single-elements' => [ matz ] }.to_json - @people_empty = { 'people-empty-elements' => [ ] }.to_json - @matz = matz.to_json - @david = david.to_json - @header = { 'key' => 'value' }.freeze - - @default_request_headers = { 'Content-Type' => 'application/json' } - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/2.json", @header, @david - mock.get "/people.json", {}, @people - mock.get "/people_single_elements.json", {}, @people_single - mock.get "/people_empty_elements.json", {}, @people_empty - mock.get "/people/1.json", {}, @matz - mock.put "/people/1.json", {}, nil, 204 - mock.put "/people/2.json", {}, @header, 204 - mock.delete "/people/1.json", {}, nil, 200 - mock.delete "/people/2.json", @header, nil, 200 - mock.post "/people.json", {}, nil, 201, 'Location' => '/people/5.json' - mock.post "/members.json", {}, @header, 201, 'Location' => '/people/6.json' - mock.head "/people/1.json", {}, nil, 200 - end - end - - def test_handle_response - # 2xx and 3xx are valid responses. - [200, 299, 300, 399].each do |code| - expected = ResponseCodeStub.new(code) - assert_equal expected, handle_response(expected) - end - - # 301 is moved permanently (redirect) - assert_redirect_raises 301 - - # 302 is found (redirect) - assert_redirect_raises 302 - - # 303 is see other (redirect) - assert_redirect_raises 303 - - # 307 is temporary redirect - assert_redirect_raises 307 - - # 400 is a bad request (e.g. malformed URI or missing request parameter) - assert_response_raises ActiveResource::BadRequest, 400 - - # 401 is an unauthorized request - assert_response_raises ActiveResource::UnauthorizedAccess, 401 - - # 403 is a forbidden request (and authorizing will not help) - assert_response_raises ActiveResource::ForbiddenAccess, 403 - - # 404 is a missing resource. - assert_response_raises ActiveResource::ResourceNotFound, 404 - - # 405 is a method not allowed error - assert_response_raises ActiveResource::MethodNotAllowed, 405 - - # 409 is an optimistic locking error - assert_response_raises ActiveResource::ResourceConflict, 409 - - # 410 is a removed resource - assert_response_raises ActiveResource::ResourceGone, 410 - - # 422 is a validation error - assert_response_raises ActiveResource::ResourceInvalid, 422 - - # 4xx are client errors. - [402, 499].each do |code| - assert_response_raises ActiveResource::ClientError, code - end - - # 5xx are server errors. - [500, 599].each do |code| - assert_response_raises ActiveResource::ServerError, code - end - - # Others are unknown. - [199, 600].each do |code| - assert_response_raises ActiveResource::ConnectionError, code - end - end - - ResponseHeaderStub = Struct.new(:code, :message, 'Allow') - def test_should_return_allowed_methods_for_method_no_allowed_exception - begin - handle_response ResponseHeaderStub.new(405, "HTTP Failed...", "GET, POST") - rescue ActiveResource::MethodNotAllowed => e - assert_equal "Failed. Response code = 405. Response message = HTTP Failed....", e.message - assert_equal [:get, :post], e.allowed_methods - end - end - - def test_initialize_raises_argument_error_on_missing_site - assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) } - end - - def test_site_accessor_accepts_uri_or_string_argument - site = URI.parse("http://localhost") - - assert_raise(URI::InvalidURIError) { @conn.site = nil } - - assert_nothing_raised { @conn.site = "http://localhost" } - assert_equal site, @conn.site - - assert_nothing_raised { @conn.site = site } - assert_equal site, @conn.site - end - - def test_proxy_accessor_accepts_uri_or_string_argument - proxy = URI.parse("http://proxy_user:proxy_password@proxy.local:4242") - - assert_nothing_raised { @conn.proxy = "http://proxy_user:proxy_password@proxy.local:4242" } - assert_equal proxy, @conn.proxy - - assert_nothing_raised { @conn.proxy = proxy } - assert_equal proxy, @conn.proxy - end - - def test_timeout_accessor - @conn.timeout = 5 - assert_equal 5, @conn.timeout - end - - def test_get - matz = decode(@conn.get("/people/1.json")) - assert_equal "Matz", matz["name"] - end - - def test_head - response = @conn.head("/people/1.json") - assert response.body.blank? - assert_equal 200, response.code - end - - def test_get_with_header - david = decode(@conn.get("/people/2.json", @header)) - assert_equal "David", david["name"] - end - - def test_get_collection - people = decode(@conn.get("/people.json")) - assert_equal "Matz", people[0]["person"]["name"] - assert_equal "David", people[1]["person"]["name"] - end - - def test_get_collection_single - people = decode(@conn.get("/people_single_elements.json")) - assert_equal "Matz", people[0]["person"]["name"] - end - - def test_get_collection_empty - people = decode(@conn.get("/people_empty_elements.json")) - assert_equal [], people - end - - def test_post - response = @conn.post("/people.json") - assert_equal "/people/5.json", response["Location"] - end - - def test_post_with_header - response = @conn.post("/members.json", @header) - assert_equal "/people/6.json", response["Location"] - end - - def test_put - response = @conn.put("/people/1.json") - assert_equal 204, response.code - end - - def test_put_with_header - response = @conn.put("/people/2.json", @header) - assert_equal 204, response.code - end - - def test_delete - response = @conn.delete("/people/1.json") - assert_equal 200, response.code - end - - def test_delete_with_header - response = @conn.delete("/people/2.json", @header) - assert_equal 200, response.code - end - - def test_timeout - @http = mock('new Net::HTTP') - @conn.expects(:http).returns(@http) - @http.expects(:get).raises(Timeout::Error, 'execution expired') - assert_raise(ActiveResource::TimeoutError) { @conn.get('/people_timeout.json') } - end - - def test_setting_timeout - http = Net::HTTP.new('') - - [10, 20].each do |timeout| - @conn.timeout = timeout - @conn.send(:configure_http, http) - assert_equal timeout, http.open_timeout - assert_equal timeout, http.read_timeout - end - end - - def test_accept_http_header - @http = mock('new Net::HTTP') - @conn.expects(:http).returns(@http) - path = '/people/1.xml' - @http.expects(:get).with(path, { 'Accept' => 'application/xhtml+xml' }).returns(ActiveResource::Response.new(@matz, 200, { 'Content-Type' => 'text/xhtml' })) - assert_nothing_raised(Mocha::ExpectationError) { @conn.get(path, { 'Accept' => 'application/xhtml+xml' }) } - end - - def test_ssl_options_get_applied_to_http - http = Net::HTTP.new('') - @conn.site="https://secure" - @conn.ssl_options={:verify_mode => OpenSSL::SSL::VERIFY_PEER} - @conn.send(:configure_http, http) - - assert http.use_ssl? - assert_equal http.verify_mode, OpenSSL::SSL::VERIFY_PEER - end - - def test_ssl_error - http = Net::HTTP.new('') - @conn.expects(:http).returns(http) - http.expects(:get).raises(OpenSSL::SSL::SSLError, 'Expired certificate') - assert_raise(ActiveResource::SSLError) { @conn.get('/people/1.json') } - end - - def test_auth_type_can_be_string - @conn.auth_type = 'digest' - assert_equal(:digest, @conn.auth_type) - end - - def test_auth_type_defaults_to_basic - @conn.auth_type = nil - assert_equal(:basic, @conn.auth_type) - end - - def test_auth_type_ignores_nonsensical_values - @conn.auth_type = :wibble - assert_equal(:basic, @conn.auth_type) - end - - protected - def assert_response_raises(klass, code) - assert_raise(klass, "Expected response code #{code} to raise #{klass}") do - handle_response ResponseCodeStub.new(code) - end - end - - def assert_redirect_raises(code) - assert_raise(ActiveResource::Redirection, "Expected response code #{code} to raise ActiveResource::Redirection") do - handle_response RedirectResponseStub.new(code, 'http://example.com/') - end - end - - def handle_response(response) - @conn.__send__(:handle_response, response) - end - - def decode(response) - @conn.format.decode(response.body) - end -end diff --git a/activeresource/test/cases/finder_test.rb b/activeresource/test/cases/finder_test.rb deleted file mode 100644 index 3e8550d356..0000000000 --- a/activeresource/test/cases/finder_test.rb +++ /dev/null @@ -1,139 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" -require "fixtures/customer" -require "fixtures/street_address" -require "fixtures/beast" -require "fixtures/proxy" -require 'active_support/core_ext/hash/conversions' - -class FinderTest < ActiveSupport::TestCase - def setup - setup_response # find me in abstract_unit - end - - def test_find_by_id - matz = Person.find(1) - assert_kind_of Person, matz - assert_equal "Matz", matz.name - assert matz.name? - end - - def test_find_by_id_with_custom_prefix - addy = StreetAddress.find(1, :params => { :person_id => 1 }) - assert_kind_of StreetAddress, addy - assert_equal '12345 Street', addy.street - end - - def test_find_all - all = Person.find(:all) - assert_equal 2, all.size - assert_kind_of Person, all.first - assert_equal "Matz", all.first.name - assert_equal "David", all.last.name - end - - def test_all - all = Person.all - assert_equal 2, all.size - assert_kind_of Person, all.first - assert_equal "Matz", all.first.name - assert_equal "David", all.last.name - end - - def test_all_with_params - all = StreetAddress.all(:params => { :person_id => 1 }) - assert_equal 1, all.size - assert_kind_of StreetAddress, all.first - end - - def test_find_first - matz = Person.find(:first) - assert_kind_of Person, matz - assert_equal "Matz", matz.name - end - - def test_first - matz = Person.first - assert_kind_of Person, matz - assert_equal "Matz", matz.name - end - - def test_first_with_params - addy = StreetAddress.first(:params => { :person_id => 1 }) - assert_kind_of StreetAddress, addy - assert_equal '12345 Street', addy.street - end - - def test_find_last - david = Person.find(:last) - assert_kind_of Person, david - assert_equal 'David', david.name - end - - def test_last - david = Person.last - assert_kind_of Person, david - assert_equal 'David', david.name - end - - def test_last_with_params - addy = StreetAddress.last(:params => { :person_id => 1 }) - assert_kind_of StreetAddress, addy - assert_equal '12345 Street', addy.street - end - - def test_find_by_id_not_found - assert_raise(ActiveResource::ResourceNotFound) { Person.find(99) } - assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(99, :params => {:person_id => 1}) } - end - - def test_find_all_sub_objects - all = StreetAddress.find(:all, :params => { :person_id => 1 }) - assert_equal 1, all.size - assert_kind_of StreetAddress, all.first - end - - def test_find_all_sub_objects_not_found - assert_nothing_raised do - StreetAddress.find(:all, :params => { :person_id => 2 }) - end - end - - def test_find_all_by_from - ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.json", {}, @people_david } - - people = Person.find(:all, :from => "/companies/1/people.json") - assert_equal 1, people.size - assert_equal "David", people.first.name - end - - def test_find_all_by_from_with_options - ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.json", {}, @people_david } - - people = Person.find(:all, :from => "/companies/1/people.json") - assert_equal 1, people.size - assert_equal "David", people.first.name - end - - def test_find_all_by_symbol_from - ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.json", {}, @people_david } - - people = Person.find(:all, :from => :managers) - assert_equal 1, people.size - assert_equal "David", people.first.name - end - - def test_find_single_by_from - ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/manager.json", {}, @david } - - david = Person.find(:one, :from => "/companies/1/manager.json") - assert_equal "David", david.name - end - - def test_find_single_by_symbol_from - ActiveResource::HttpMock.respond_to { |m| m.get "/people/leader.json", {}, @david } - - david = Person.find(:one, :from => :leader) - assert_equal "David", david.name - end -end diff --git a/activeresource/test/cases/format_test.rb b/activeresource/test/cases/format_test.rb deleted file mode 100644 index 315f9db1eb..0000000000 --- a/activeresource/test/cases/format_test.rb +++ /dev/null @@ -1,118 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" -require "fixtures/street_address" - -class FormatTest < ActiveSupport::TestCase - def setup - @matz = { :id => 1, :name => 'Matz' } - @david = { :id => 2, :name => 'David' } - - @programmers = [ @matz, @david ] - end - - def test_http_format_header_name - [:get, :head].each do |verb| - header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[verb] - assert_equal 'Accept', header_name - end - - [:patch, :put, :post].each do |verb| - header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[verb] - assert_equal 'Content-Type', header_name - end - end - - def test_formats_on_single_element - [ :json, :xml ].each do |format| - using_format(Person, format) do - ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david) - assert_equal @david[:name], Person.find(1).name - end - end - end - - def test_formats_on_collection - [ :json, :xml ].each do |format| - using_format(Person, format) do - ActiveResource::HttpMock.respond_to.get "/people.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@programmers) - remote_programmers = Person.find(:all) - assert_equal 2, remote_programmers.size - assert remote_programmers.find { |p| p.name == 'David' } - end - end - end - - def test_formats_on_custom_collection_method - [ :json, :xml ].each do |format| - using_format(Person, format) do - ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode([@david]) - remote_programmers = Person.get(:retrieve, :name => 'David') - assert_equal 1, remote_programmers.size - assert_equal @david[:id], remote_programmers[0]['id'] - assert_equal @david[:name], remote_programmers[0]['name'] - end - end - end - - def test_formats_on_custom_element_method - [:json, :xml].each do |format| - using_format(Person, format) do - david = (format == :json ? { :person => @david } : @david) - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/2.#{format}", { 'Accept' => ActiveResource::Formats[format].mime_type }, ActiveResource::Formats[format].encode(david) - mock.get "/people/2/shallow.#{format}", { 'Accept' => ActiveResource::Formats[format].mime_type }, ActiveResource::Formats[format].encode(david) - end - - remote_programmer = Person.find(2).get(:shallow) - assert_equal @david[:id], remote_programmer['id'] - assert_equal @david[:name], remote_programmer['name'] - end - - ryan_hash = { :name => 'Ryan' } - ryan_hash = (format == :json ? { :person => ryan_hash } : ryan_hash) - ryan = ActiveResource::Formats[format].encode(ryan_hash) - using_format(Person, format) do - remote_ryan = Person.new(:name => 'Ryan') - ActiveResource::HttpMock.respond_to.post "/people.#{format}", { 'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, { 'Location' => "/people/5.#{format}" } - remote_ryan.save - - remote_ryan = Person.new(:name => 'Ryan') - ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", { 'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, { 'Location' => "/people/5.#{format}" } - assert_equal ActiveResource::Response.new(ryan, 201, { 'Location' => "/people/5.#{format}" }), remote_ryan.post(:register) - end - end - end - - def test_setting_format_before_site - resource = Class.new(ActiveResource::Base) - resource.format = :json - resource.site = 'http://37s.sunrise.i:3000' - assert_equal ActiveResource::Formats[:json], resource.connection.format - end - - def test_serialization_of_nested_resource - address = { :street => '12345 Street' } - person = { :name => 'Rus', :address => address} - - [:json, :xml].each do |format| - encoded_person = ActiveResource::Formats[format].encode(person) - assert_match(/12345 Street/, encoded_person) - remote_person = Person.new(person.update({:address => StreetAddress.new(address)})) - assert_kind_of StreetAddress, remote_person.address - using_format(Person, format) do - ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"} - remote_person.save - end - end - end - - private - def using_format(klass, mime_type_reference) - previous_format = klass.format - klass.format = mime_type_reference - - yield - ensure - klass.format = previous_format - end -end diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb deleted file mode 100644 index d13d9258ce..0000000000 --- a/activeresource/test/cases/http_mock_test.rb +++ /dev/null @@ -1,202 +0,0 @@ -require 'abstract_unit' -require 'active_support/core_ext/object/inclusion' - -class HttpMockTest < ActiveSupport::TestCase - setup do - @http = ActiveResource::HttpMock.new("http://example.com") - end - - FORMAT_HEADER = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES - - [:post, :patch, :put, :get, :delete, :head].each do |method| - test "responds to simple #{method} request" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Response") - end - - assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body - end - - test "adds format header by default to #{method} request" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(method, "/people/1", {}, "Response") - end - - assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body - end - - test "respond only when headers match header by default to #{method} request" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(method, "/people/1", {"X-Header" => "X"}, "Response") - end - - assert_equal "Response", request(method, "/people/1", "X-Header" => "X").body - assert_raise(ActiveResource::InvalidRequestError) { request(method, "/people/1") } - end - - test "does not overwrite format header to #{method} request" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Response") - end - - assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body - end - - test "ignores format header when there is only one response to same url in a #{method} request" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(method, "/people/1", {}, "Response") - end - - assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body - assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body - end - - test "responds correctly when format header is given to #{method} request" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/xml" }, "XML") - mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Json") - end - - assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body - assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body - end - - test "raises InvalidRequestError if no response found for the #{method} request" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "json") - end - - assert_raise(::ActiveResource::InvalidRequestError) do - request(method, "/people/1", FORMAT_HEADER[method] => "application/xml") - end - end - - end - - test "allows you to send in pairs directly to the respond_to method" do - matz = { :person => { :id => 1, :name => "Matz" } }.to_json - - create_matz = ActiveResource::Request.new(:post, '/people.json', matz, {}) - created_response = ActiveResource::Response.new("", 201, { "Location" => "/people/1.json" }) - get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil) - ok_response = ActiveResource::Response.new(matz, 200, {}) - - pairs = {create_matz => created_response, get_matz => ok_response} - - ActiveResource::HttpMock.respond_to(pairs) - assert_equal 2, ActiveResource::HttpMock.responses.length - assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body - assert_equal matz, ActiveResource::HttpMock.responses.assoc(get_matz)[1].body - end - - test "resets all mocked responses on each call to respond_to with a block by default" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/2", {}, "JSON2") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - end - - test "resets all mocked responses on each call to respond_to by passing pairs by default" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - matz = { :person => { :id => 1, :name => "Matz" } }.to_json - get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil) - ok_response = ActiveResource::Response.new(matz, 200, {}) - ActiveResource::HttpMock.respond_to({get_matz => ok_response}) - - assert_equal 1, ActiveResource::HttpMock.responses.length - end - - test "allows you to add new responses to the existing responses by calling a block" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - ActiveResource::HttpMock.respond_to(false) do |mock| - mock.send(:get, "/people/2", {}, "JSON2") - end - assert_equal 2, ActiveResource::HttpMock.responses.length - end - - test "allows you to add new responses to the existing responses by passing pairs" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - matz = { :person => { :id => 1, :name => "Matz" } }.to_json - get_matz = ActiveResource::Request.new(:get, '/people/1.json', nil) - ok_response = ActiveResource::Response.new(matz, 200, {}) - ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false) - - assert_equal 2, ActiveResource::HttpMock.responses.length - end - - test "allows you to replace the existing reponse with the same request by calling a block" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - ActiveResource::HttpMock.respond_to(false) do |mock| - mock.send(:get, "/people/1", {}, "JSON2") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - end - - test "allows you to replace the existing reponse with the same request by passing pairs" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - matz = { :person => { :id => 1, :name => "Matz" } }.to_json - get_matz = ActiveResource::Request.new(:get, '/people/1', nil) - ok_response = ActiveResource::Response.new(matz, 200, {}) - - ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false) - assert_equal 1, ActiveResource::HttpMock.responses.length - end - - test "do not replace the response with the same path but different method by calling a block" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - ActiveResource::HttpMock.respond_to(false) do |mock| - mock.send(:put, "/people/1", {}, "JSON2") - end - assert_equal 2, ActiveResource::HttpMock.responses.length - end - - test "do not replace the response with the same path but different method by passing pairs" do - ActiveResource::HttpMock.respond_to do |mock| - mock.send(:get, "/people/1", {}, "JSON1") - end - assert_equal 1, ActiveResource::HttpMock.responses.length - - put_matz = ActiveResource::Request.new(:put, '/people/1', nil) - ok_response = ActiveResource::Response.new("", 200, {}) - - ActiveResource::HttpMock.respond_to({put_matz => ok_response}, false) - assert_equal 2, ActiveResource::HttpMock.responses.length - end - - def request(method, path, headers = {}, body = nil) - if method.in?([:patch, :put, :post]) - @http.send(method, path, body, headers) - else - @http.send(method, path, headers) - end - end -end diff --git a/activeresource/test/cases/log_subscriber_test.rb b/activeresource/test/cases/log_subscriber_test.rb deleted file mode 100644 index ab5c22a783..0000000000 --- a/activeresource/test/cases/log_subscriber_test.rb +++ /dev/null @@ -1,32 +0,0 @@ -require "abstract_unit" -require "fixtures/person" -require "active_support/log_subscriber/test_helper" -require "active_resource/log_subscriber" -require "active_support/core_ext/hash/conversions" - -class LogSubscriberTest < ActiveSupport::TestCase - include ActiveSupport::LogSubscriber::TestHelper - - def setup - super - - @matz = { :person => { :id => 1, :name => 'Matz' } }.to_json - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, @matz - end - - ActiveResource::LogSubscriber.attach_to :active_resource - end - - def set_logger(logger) - ActiveResource::Base.logger = logger - end - - def test_request_notification - Person.find(1) - wait - assert_equal 2, @logger.logged(:info).size - assert_equal "GET http://37s.sunrise.i:3000/people/1.json", @logger.logged(:info)[0] - assert_match(/\-\-\> 200 200 33/, @logger.logged(:info)[1]) - end -end diff --git a/activeresource/test/cases/observing_test.rb b/activeresource/test/cases/observing_test.rb deleted file mode 100644 index b2371a1bdf..0000000000 --- a/activeresource/test/cases/observing_test.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'abstract_unit' -require 'fixtures/person' -require 'active_support/core_ext/hash/conversions' - -class ObservingTest < ActiveSupport::TestCase - cattr_accessor :history - - class PersonObserver < ActiveModel::Observer - observe :person - - %w( after_create after_destroy after_save after_update - before_create before_destroy before_save before_update).each do |method| - define_method(method) { |*| log method } - end - - private - def log(method) - (ObservingTest.history ||= []) << method.to_sym - end - end - - def setup - @matz = { 'person' => { :id => 1, :name => 'Matz' } }.to_json - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, @matz - mock.post "/people.json", {}, @matz, 201, 'Location' => '/people/1.json' - mock.put "/people/1.json", {}, nil, 204 - mock.delete "/people/1.json", {}, nil, 200 - end - - PersonObserver.instance - end - - def teardown - self.history = nil - end - - def test_create_fires_save_and_create_notifications - Person.create(:name => 'Rick') - assert_equal [:before_save, :before_create, :after_create, :after_save], self.history - end - - def test_update_fires_save_and_update_notifications - person = Person.find(1) - person.save - assert_equal [:before_save, :before_update, :after_update, :after_save], self.history - end - - def test_destroy_fires_destroy_notifications - person = Person.find(1) - person.destroy - assert_equal [:before_destroy, :after_destroy], self.history - end -end diff --git a/activeresource/test/cases/validations_test.rb b/activeresource/test/cases/validations_test.rb deleted file mode 100644 index c6c6e1d786..0000000000 --- a/activeresource/test/cases/validations_test.rb +++ /dev/null @@ -1,67 +0,0 @@ -require 'abstract_unit' -require 'fixtures/project' -require 'active_support/core_ext/hash/conversions' - -# The validations are tested thoroughly under ActiveModel::Validations -# This test case simply makes sure that they are all accessible by -# Active Resource objects. -class ValidationsTest < ActiveModel::TestCase - VALID_PROJECT_HASH = { :name => "My Project", :description => "A project" } - def setup - @my_proj = { "person" => VALID_PROJECT_HASH }.to_json - ActiveResource::HttpMock.respond_to do |mock| - mock.post "/projects.json", {}, @my_proj, 201, 'Location' => '/projects/5.json' - end - end - - def test_validates_presence_of - p = new_project(:name => nil) - assert !p.valid?, "should not be a valid record without name" - assert !p.save, "should not have saved an invalid record" - assert_equal ["can't be blank"], p.errors[:name], "should have an error on name" - - p.name = "something" - - assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}" - end - - def test_fails_save! - p = new_project(:name => nil) - assert_raise(ActiveResource::ResourceInvalid) { p.save! } - end - - def test_save_without_validation - p = new_project(:name => nil) - assert !p.save - assert p.save(:validate => false) - end - - def test_validate_callback - # we have a callback ensuring the description is longer than three letters - p = new_project(:description => 'a') - assert !p.valid?, "should not be a valid record when it fails a validation callback" - assert !p.save, "should not have saved an invalid record" - assert_equal ["must be greater than three letters long"], p.errors[:description], "should be an error on description" - - # should now allow this description - p.description = 'abcd' - assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}" - end - - def test_client_side_validation_maximum - project = Project.new(:description => '123456789012345') - assert ! project.valid? - assert_equal ['is too long (maximum is 10 characters)'], project.errors[:description] - end - - protected - - # quickie helper to create a new project with all the required - # attributes. - # Pass in any params you specifically want to override - def new_project(opts = {}) - Project.new(VALID_PROJECT_HASH.merge(opts)) - end - -end - diff --git a/activeresource/test/fixtures/address.rb b/activeresource/test/fixtures/address.rb deleted file mode 100644 index 7a73ecb52a..0000000000 --- a/activeresource/test/fixtures/address.rb +++ /dev/null @@ -1,19 +0,0 @@ -# turns everything into the same object -class AddressXMLFormatter - include ActiveResource::Formats::XmlFormat - - def decode(xml) - data = ActiveResource::Formats::XmlFormat.decode(xml) - # process address fields - data.each do |address| - address['city_state'] = "#{address['city']}, #{address['state']}" - end - data - end - -end - -class AddressResource < ActiveResource::Base - self.element_name = "address" - self.format = AddressXMLFormatter.new -end
\ No newline at end of file diff --git a/activeresource/test/fixtures/beast.rb b/activeresource/test/fixtures/beast.rb deleted file mode 100644 index e31ec58346..0000000000 --- a/activeresource/test/fixtures/beast.rb +++ /dev/null @@ -1,14 +0,0 @@ -class BeastResource < ActiveResource::Base - self.site = 'http://beast.caboo.se' - site.user = 'foo' - site.password = 'bar' -end - -class Forum < BeastResource - # taken from BeastResource - # self.site = 'http://beast.caboo.se' -end - -class Topic < BeastResource - self.site += '/forums/:forum_id' -end diff --git a/activeresource/test/fixtures/customer.rb b/activeresource/test/fixtures/customer.rb deleted file mode 100644 index 845d5d11cb..0000000000 --- a/activeresource/test/fixtures/customer.rb +++ /dev/null @@ -1,3 +0,0 @@ -class Customer < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" -end diff --git a/activeresource/test/fixtures/person.rb b/activeresource/test/fixtures/person.rb deleted file mode 100644 index e88bb69310..0000000000 --- a/activeresource/test/fixtures/person.rb +++ /dev/null @@ -1,3 +0,0 @@ -class Person < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" -end diff --git a/activeresource/test/fixtures/project.rb b/activeresource/test/fixtures/project.rb deleted file mode 100644 index 53de666601..0000000000 --- a/activeresource/test/fixtures/project.rb +++ /dev/null @@ -1,18 +0,0 @@ -# used to test validations -class Project < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - schema do - string :email - string :name - end - - validates :name, :presence => true - validates :description, :presence => false, :length => {:maximum => 10} - validate :description_greater_than_three_letters - - # to test the validate *callback* works - def description_greater_than_three_letters - errors.add :description, 'must be greater than three letters long' if description.length < 3 unless description.blank? - end -end - diff --git a/activeresource/test/fixtures/proxy.rb b/activeresource/test/fixtures/proxy.rb deleted file mode 100644 index bb8e015df0..0000000000 --- a/activeresource/test/fixtures/proxy.rb +++ /dev/null @@ -1,4 +0,0 @@ -class ProxyResource < ActiveResource::Base - self.site = "http://localhost" - self.proxy = "http://user:password@proxy.local:3000" -end
\ No newline at end of file diff --git a/activeresource/test/fixtures/sound.rb b/activeresource/test/fixtures/sound.rb deleted file mode 100644 index d9d2b99fcd..0000000000 --- a/activeresource/test/fixtures/sound.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Asset - class Sound < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end -end - -# to test namespacing in a module -class Author -end
\ No newline at end of file diff --git a/activeresource/test/fixtures/street_address.rb b/activeresource/test/fixtures/street_address.rb deleted file mode 100644 index 6a8adb98b5..0000000000 --- a/activeresource/test/fixtures/street_address.rb +++ /dev/null @@ -1,4 +0,0 @@ -class StreetAddress < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000/people/:person_id" - self.element_name = 'address' -end diff --git a/activeresource/test/fixtures/subscription_plan.rb b/activeresource/test/fixtures/subscription_plan.rb deleted file mode 100644 index e3c2dd9a74..0000000000 --- a/activeresource/test/fixtures/subscription_plan.rb +++ /dev/null @@ -1,5 +0,0 @@ -class SubscriptionPlan < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - self.element_name = 'plan' - self.primary_key = :code -end diff --git a/activeresource/test/setter_trap.rb b/activeresource/test/setter_trap.rb deleted file mode 100644 index 7cfd9ca111..0000000000 --- a/activeresource/test/setter_trap.rb +++ /dev/null @@ -1,26 +0,0 @@ -class SetterTrap < ActiveSupport::BasicObject - class << self - def rollback_sets(obj) - trapped = new(obj) - yield(trapped).tap { trapped.rollback_sets } - end - end - - def initialize(obj) - @cache = {} - @obj = obj - end - - def respond_to?(method) - @obj.respond_to?(method) - end - - def method_missing(method, *args, &proc) - @cache[method] ||= @obj.send($`) if method.to_s =~ /=$/ - @obj.send method, *args, &proc - end - - def rollback_sets - @cache.each { |k, v| @obj.send k, v } - end -end diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 3e40e08ca2..8165b89cde 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -27,6 +27,11 @@ * Unicode database updated to 6.1.0. +## Rails 3.2.2 (March 1, 2012) ## + +* No changes. + + ## Rails 3.2.1 (January 26, 2012) ## * Documentation fixes and improvements. @@ -111,7 +116,7 @@ filehandle, or tune your filesystem. -## Rails 3.1.4 (unreleased) ## +## Rails 3.1.4 (March 1, 2012) ## * No changes @@ -183,7 +188,7 @@ * JSON decoding now uses the multi_json gem which also vendors a json engine called OkJson. The yaml backend has been removed in favor of OkJson as a default engine for 1.8.x, while the built in 1.9.x json implementation will be used by default. *Josh Kalderimis* -## Rails 3.0.12 (unreleased) ## +## Rails 3.0.12 (March 1, 2012) ## * No changes. diff --git a/activesupport/lib/active_support/basic_object.rb b/activesupport/lib/active_support/basic_object.rb index c3c7ab0112..6ccb0cd525 100644 --- a/activesupport/lib/active_support/basic_object.rb +++ b/activesupport/lib/active_support/basic_object.rb @@ -10,5 +10,4 @@ module ActiveSupport ::Object.send(:raise, *args) end end - end diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index d7408eff9f..b9f196d7a9 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -91,6 +91,7 @@ module ActiveSupport case when key.respond_to?(:cache_key) then key.cache_key when key.is_a?(Array) then key.map { |element| retrieve_cache_key(element) }.to_param + when key.respond_to?(:to_a) then retrieve_cache_key(key.to_a) else key.to_param end.to_s end diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index b7712a4a15..8e6a3bc5a8 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -13,7 +13,7 @@ module ActiveSupport attr_reader :cache_path DIR_FORMATTER = "%03X" - FILENAME_MAX_SIZE = 230 # max filename size on file system is 255, minus room for timestamp and random characters appended by Tempfile (used by atomic write) + FILENAME_MAX_SIZE = 228 # max filename size on file system is 255, minus room for timestamp and random characters appended by Tempfile (used by atomic write) EXCLUDED_DIRS = ['.', '..'].freeze def initialize(cache_path, options = nil) diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index 268303aaf2..95eb94fdf6 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -29,6 +29,7 @@ class Class def cattr_reader(*syms) options = syms.extract_options! syms.each do |sym| + raise NameError.new("invalid attribute name") unless sym =~ /^[_A-Za-z]\w*$/ class_eval(<<-EOS, __FILE__, __LINE__ + 1) unless defined? @@#{sym} @@#{sym} = nil @@ -52,6 +53,7 @@ class Class def cattr_writer(*syms) options = syms.extract_options! syms.each do |sym| + raise NameError.new("invalid attribute name") unless sym =~ /^[_A-Za-z]\w*$/ class_eval(<<-EOS, __FILE__, __LINE__ + 1) unless defined? @@#{sym} @@#{sym} = nil diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb index 29bf7c0f3d..0634f20e3c 100644 --- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -1,5 +1,3 @@ -require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/remove_method' diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index af78226c21..6d4270f8b0 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -182,6 +182,13 @@ class Date result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day] self.acts_like?(:time) ? result.change(:hour => 0) : result end + alias :last_week :prev_week + + # Alias of prev_month + alias :last_month :prev_month + + # Alias of prev_year + alias :last_year :prev_year # Returns a new Date/DateTime representing the start of the given day in next week (default is :monday). def next_week(day = :monday) diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb index 3645597301..fc3277f4d2 100644 --- a/activesupport/lib/active_support/core_ext/file/atomic.rb +++ b/activesupport/lib/active_support/core_ext/file/atomic.rb @@ -17,6 +17,7 @@ class File require 'fileutils' unless defined?(FileUtils) temp_file = Tempfile.new(basename(file_name), temp_dir) + temp_file.binmode yield temp_file temp_file.close diff --git a/activesupport/lib/active_support/core_ext/hash/deep_dup.rb b/activesupport/lib/active_support/core_ext/hash/deep_dup.rb index 447142605c..9ab179c566 100644 --- a/activesupport/lib/active_support/core_ext/hash/deep_dup.rb +++ b/activesupport/lib/active_support/core_ext/hash/deep_dup.rb @@ -3,8 +3,7 @@ class Hash def deep_dup duplicate = self.dup duplicate.each_pair do |k,v| - tv = duplicate[k] - duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v + duplicate[k] = v.is_a?(Hash) ? v.deep_dup : v end duplicate end diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 0484d8e5d8..45181f0e16 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -13,17 +13,15 @@ class Hash # valid_keys = [:mass, :velocity, :time] # search(options.slice(*valid_keys)) def slice(*keys) - keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) - hash = self.class.new - keys.each { |k| hash[k] = self[k] if has_key?(k) } - hash + keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) + keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) } end # Replaces the hash with only the given keys. # Returns a hash contained the removed key/value pairs # {:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d => 4} def slice!(*keys) - keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) + keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) omit = slice(*self.keys - keys) hash = slice(*keys) replace(hash) @@ -33,8 +31,6 @@ class Hash # Removes and returns the key/value pairs matching the given keys. # {:a => 1, :b => 2, :c => 3, :d => 4}.extract!(:a, :b) # => {:a => 1, :b => 2} def extract!(*keys) - result = {} - keys.each {|key| result[key] = delete(key) } - result + keys.each_with_object({}) {|key, result| result[key] = delete(key) } end end diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb index be94ae1565..84acb629ad 100644 --- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb @@ -4,6 +4,7 @@ class Module def mattr_reader(*syms) options = syms.extract_options! syms.each do |sym| + raise NameError.new("invalid attribute name") unless sym =~ /^[_A-Za-z]\w*$/ class_eval(<<-EOS, __FILE__, __LINE__ + 1) @@#{sym} = nil unless defined? @@#{sym} @@ -25,6 +26,7 @@ class Module def mattr_writer(*syms) options = syms.extract_options! syms.each do |sym| + raise NameError.new("invalid attribute name") unless sym =~ /^[_A-Za-z]\w*$/ class_eval(<<-EOS, __FILE__, __LINE__ + 1) def self.#{sym}=(obj) @@#{sym} = obj diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index ac2a63d3a1..af92b869fd 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -106,9 +106,11 @@ class Module unless options.is_a?(Hash) && to = options[:to] raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)." end - prefix, to, allow_nil = options[:prefix], options[:to], options[:allow_nil] - if prefix == true && to.to_s =~ /^[^a-z_]/ + to = to.to_s + prefix, allow_nil = options.values_at(:prefix, :allow_nil) + + if prefix == true && to =~ /^[^a-z_]/ raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method." end @@ -122,10 +124,8 @@ class Module file, line = caller.first.split(':', 2) line = line.to_i - methods.each do |method| - method = method.to_s - - if allow_nil + if allow_nil + methods.each do |method| module_eval(<<-EOS, file, line - 2) def #{method_prefix}#{method}(*args, &block) # def customer_name(*args, &block) if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name) @@ -133,7 +133,9 @@ class Module end # end end # end EOS - else + end + else + methods.each do |method| exception = %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}") module_eval(<<-EOS, file, line - 1) diff --git a/activesupport/lib/active_support/core_ext/module/remove_method.rb b/activesupport/lib/active_support/core_ext/module/remove_method.rb index b76bc16ee1..719071d1c2 100644 --- a/activesupport/lib/active_support/core_ext/module/remove_method.rb +++ b/activesupport/lib/active_support/core_ext/module/remove_method.rb @@ -1,12 +1,8 @@ class Module def remove_possible_method(method) if method_defined?(method) || private_method_defined?(method) - remove_method(method) + undef_method(method) end - rescue NameError - # If the requested method is defined on a superclass or included module, - # method_defined? returns true but remove_method throws a NameError. - # Ignore this. end def redefine_method(method, &block) diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb index 9d044eba71..9d1630bb7c 100644 --- a/activesupport/lib/active_support/core_ext/object/duplicable.rb +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -104,3 +104,16 @@ class Module false end end + +require 'bigdecimal' +class BigDecimal + begin + BigDecimal.new('4.56').dup + + def duplicable? + true + end + rescue TypeError + # can't dup, so use superclass implementation + end +end diff --git a/activesupport/lib/active_support/core_ext/proc.rb b/activesupport/lib/active_support/core_ext/proc.rb index 94bb5fb0cb..cd63740940 100644 --- a/activesupport/lib/active_support/core_ext/proc.rb +++ b/activesupport/lib/active_support/core_ext/proc.rb @@ -1,7 +1,10 @@ require "active_support/core_ext/kernel/singleton_class" +require "active_support/deprecation" class Proc #:nodoc: def bind(object) + ActiveSupport::Deprecation.warn 'Proc#bind is deprecated and will be removed in future versions', caller + block, time = self, Time.now object.class_eval do method_name = "__bind_#{time.to_i}_#{time.usec}" diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb index dd780da157..4903687b73 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -103,29 +103,39 @@ module ActiveSupport #:nodoc: end end - def[](*args) - new_safe_buffer = super - new_safe_buffer.instance_eval { @dirty = false } - new_safe_buffer + def [](*args) + return super if args.size < 2 + + if html_safe? + new_safe_buffer = super + new_safe_buffer.instance_eval { @html_safe = true } + new_safe_buffer + else + to_str[*args] + end end def safe_concat(value) - raise SafeConcatError if dirty? + raise SafeConcatError unless html_safe? original_concat(value) end def initialize(*) - @dirty = false + @html_safe = true super end def initialize_copy(other) super - @dirty = other.dirty? + @html_safe = other.html_safe? + end + + def clone_empty + self[0, 0] end def concat(value) - if dirty? || value.html_safe? + if !html_safe? || value.html_safe? super(value) else super(ERB::Util.h(value)) @@ -138,7 +148,7 @@ module ActiveSupport #:nodoc: end def html_safe? - !dirty? + defined?(@html_safe) && @html_safe end def to_s @@ -161,18 +171,12 @@ module ActiveSupport #:nodoc: end # end def #{unsafe_method}!(*args) # def capitalize!(*args) - @dirty = true # @dirty = true + @html_safe = false # @html_safe = false super # super end # end EOT end end - - protected - - def dirty? - @dirty - end end end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index dd2a21fe2a..5076697c04 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -145,6 +145,7 @@ class Time def prev_year years_ago(1) end + alias_method :last_year, :prev_year # Short-hand for years_since(1) def next_year @@ -155,6 +156,7 @@ class Time def prev_month months_ago(1) end + alias_method :last_month, :prev_month # Short-hand for months_since(1) def next_month @@ -199,6 +201,7 @@ class Time def prev_week(day = :monday) ago(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0) end + alias_method :last_week, :prev_week # Returns a new Time representing the start of the given day in next week (default is :monday). def next_week(day = :monday) diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 2c5950edf5..745a131524 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -129,16 +129,14 @@ module ActiveSupport #:nodoc: # Add a set of modules to the watch stack, remembering the initial constants def watch_namespaces(namespaces) - watching = [] - namespaces.map do |namespace| + @watching << namespaces.map do |namespace| module_name = Dependencies.to_constant_name(namespace) original_constants = Dependencies.qualified_const_defined?(module_name) ? Inflector.constantize(module_name).local_constants : [] - watching << module_name @stack[module_name] << original_constants + module_name end - @watching << watching end private @@ -365,7 +363,7 @@ module ActiveSupport #:nodoc: # Record history *after* loading so first load gets warnings. history << expanded - return result + result end # Is the provided constant path defined? @@ -434,7 +432,7 @@ module ActiveSupport #:nodoc: mod = Module.new into.const_set const_name, mod autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path) - return mod + mod end # Load the file at the provided path. +const_paths+ is a set of qualified @@ -458,7 +456,7 @@ module ActiveSupport #:nodoc: autoloaded_constants.concat newly_defined_paths unless load_once_path?(path) autoloaded_constants.uniq! log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty? - return result + result end # Return the constant path for the provided parent and constant name. @@ -505,7 +503,7 @@ module ActiveSupport #:nodoc: raise NameError, "uninitialized constant #{qualified_name}", - caller.reject {|l| l.starts_with? __FILE__ } + caller.reject { |l| l.starts_with? __FILE__ } end # Remove the constants that have been autoloaded, and those that have been @@ -543,10 +541,7 @@ module ActiveSupport #:nodoc: def safe_get(key) key = key.name if key.respond_to?(:name) - @store[key] || begin - klass = Inflector.safe_constantize(key) - @store[key] = klass - end + @store[key] ||= Inflector.safe_constantize(key) end def store(klass) @@ -600,10 +595,10 @@ module ActiveSupport #:nodoc: def mark_for_unload(const_desc) name = to_constant_name const_desc if explicitly_unloadable_constants.include? name - return false + false else explicitly_unloadable_constants << name - return true + true end end @@ -631,10 +626,10 @@ module ActiveSupport #:nodoc: return new_constants unless aborting log "Error during loading, removing partially loaded constants " - new_constants.each {|c| remove_constant(c) }.clear + new_constants.each { |c| remove_constant(c) }.clear end - return [] + [] end # Convert the provided const desc to a qualified constant name (as a string). @@ -663,7 +658,7 @@ module ActiveSupport #:nodoc: constantized.before_remove_const if constantized.respond_to?(:before_remove_const) parent.instance_eval { remove_const to_remove } - return true + true end protected diff --git a/activesupport/lib/active_support/dependencies/autoload.rb b/activesupport/lib/active_support/dependencies/autoload.rb index 4c771da096..a1626ebeba 100644 --- a/activesupport/lib/active_support/dependencies/autoload.rb +++ b/activesupport/lib/active_support/dependencies/autoload.rb @@ -9,13 +9,16 @@ module ActiveSupport @@eager_autoload = false def autoload(const_name, path = @@at_path) - full = [self.name, @@under_path, const_name.to_s, path].compact.join("::") - location = path || Inflector.underscore(full) + unless path + full = [name, @@under_path, const_name.to_s, path].compact.join("::") + path = Inflector.underscore(full) + end if @@eager_autoload - @@autoloads[const_name] = location + @@autoloads[const_name] = path end - super const_name, location + + super const_name, path end def autoload_under(path) diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb index 2ede084e95..c99da22cd6 100644 --- a/activesupport/lib/active_support/file_update_checker.rb +++ b/activesupport/lib/active_support/file_update_checker.rb @@ -1,5 +1,3 @@ -require "active_support/core_ext/array/extract_options" - module ActiveSupport # \FileUpdateChecker specifies the API used by Rails to watch files # and control reloading. The API depends on four methods: @@ -11,7 +9,7 @@ module ActiveSupport # the filesystem or not; # # * +execute+ which executes the given block on initialization - # and updates the counter to the latest timestamp; + # and updates the latest watched files and timestamp; # # * +execute_if_updated+ which just executes the block if it was updated; # @@ -41,13 +39,13 @@ module ActiveSupport # # == Implementation details # - # This particular implementation checks for added and updated files, - # but not removed files. Directories lookup are compiled to a glob for - # performance. Therefore, while someone can add new files to the +files+ - # array after initialization (and parts of Rails do depend on this feature), - # adding new directories after initialization is not allowed. + # This particular implementation checks for added, updated, and removed + # files. Directories lookup are compiled to a glob for performance. + # Therefore, while someone can add new files to the +files+ array after + # initialization (and parts of Rails do depend on this feature), adding + # new directories after initialization is not supported. # - # Notice that other objects that implements FileUpdateChecker API may + # Notice that other objects that implement the FileUpdateChecker API may # not even allow new files to be added after initialization. If this # is the case, we recommend freezing the +files+ after initialization to # avoid changes that won't make effect. @@ -55,27 +53,38 @@ module ActiveSupport @files = files @glob = compile_glob(dirs) @block = block - @updated_at = nil - @last_update_at = updated_at + + @last_watched = watched + @last_update_at = updated_at(@last_watched) end - # Check if any of the entries were updated. If so, the updated_at - # value is cached until the block is executed via +execute+ or +execute_if_updated+ + # Check if any of the entries were updated. If so, the watched and/or + # updated_at values are cached until the block is executed via +execute+ + # or +execute_if_updated+ def updated? - current_updated_at = updated_at - if @last_update_at < current_updated_at - @updated_at = updated_at + current_watched = watched + if @last_watched.size != current_watched.size + @watched = current_watched true else - false + current_updated_at = updated_at(current_watched) + if @last_update_at < current_updated_at + @watched = current_watched + @updated_at = current_updated_at + true + else + false + end end end - # Executes the given block and updates the counter to latest timestamp. + # Executes the given block and updates the latest watched files and timestamp. def execute - @last_update_at = updated_at + @last_watched = watched + @last_update_at = updated_at(@last_watched) @block.call ensure + @watched = nil @updated_at = nil end @@ -91,27 +100,33 @@ module ActiveSupport private - def updated_at #:nodoc: - @updated_at || begin - all = [] - all.concat @files.select { |f| File.exists?(f) } - all.concat Dir[@glob] if @glob - all.map { |path| File.mtime(path) }.max || Time.at(0) + def watched + @watched || begin + all = @files.select { |f| File.exists?(f) } + all.concat(Dir[@glob]) if @glob + all end end - def compile_glob(hash) #:nodoc: + def updated_at(paths) + @updated_at || paths.map { |path| File.mtime(path) }.max || Time.at(0) + end + + def compile_glob(hash) hash.freeze # Freeze so changes aren't accidently pushed return if hash.empty? - globs = [] - hash.each do |key, value| - globs << "#{key}/**/*#{compile_ext(value)}" + globs = hash.map do |key, value| + "#{escape(key)}/**/*#{compile_ext(value)}" end "{#{globs.join(",")}}" end - def compile_ext(array) #:nodoc: + def escape(key) + key.gsub(',','\,') + end + + def compile_ext(array) array = Array(array) return if array.empty? ".{#{array.join(",")}}" diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 674e4acfd6..91459f3e5b 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -1,12 +1,11 @@ require 'active_support/core_ext/hash/keys' -# This class has dubious semantics and we only have it so that -# people can write <tt>params[:key]</tt> instead of <tt>params['key']</tt> -# and they get the same value for both keys. - module ActiveSupport + # This class has dubious semantics and we only have it so that + # people can write <tt>params[:key]</tt> instead of <tt>params['key']</tt> + # and they get the same value for both keys. class HashWithIndifferentAccess < Hash - + # Always returns true, so that <tt>Array#extract_options!</tt> finds members of this class. def extractable_options? true @@ -43,6 +42,10 @@ module ActiveSupport end end + def self.[](*args) + new.merge(Hash[*args]) + end + alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) alias_method :regular_update, :update unless method_defined?(:regular_update) diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index c630bd21b1..19a6c51516 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require 'active_support/inflector/inflections' module ActiveSupport @@ -112,7 +114,7 @@ module ActiveSupport # "TheManWithoutAPast".titleize # => "The Man Without A Past" # "raiders_of_the_lost_ark".titleize # => "Raiders Of The Lost Ark" def titleize(word) - humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize } + humanize(underscore(word)).gsub(/\b(['’`]?[a-z])/) { $1.capitalize } end # Create the name of a table like Rails does for models to table names. This method @@ -144,7 +146,7 @@ module ActiveSupport # Replaces underscores with dashes in the string. # # Example: - # "puni_puni" # => "puni-puni" + # "puni_puni".dasherize # => "puni-puni" def dasherize(underscored_word) underscored_word.tr('_', '-') end @@ -210,11 +212,9 @@ module ActiveSupport names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? - constant = Object - names.each do |name| - constant = constant.const_get(name, false) + names.inject(Object) do |constant, name| + constant.const_get(name, false) end - constant end # Tries to find a constant with the name specified in the argument string: diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb index 82507c1e03..c167efc1a7 100644 --- a/activesupport/lib/active_support/lazy_load_hooks.rb +++ b/activesupport/lib/active_support/lazy_load_hooks.rb @@ -1,32 +1,32 @@ -# lazy_load_hooks allows rails to lazily load a lot of components and thus making the app boot faster. Because of -# this feature now there is no need to require <tt>ActiveRecord::Base</tt> at boot time purely to apply configuration. Instead -# a hook is registered that applies configuration once <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is used -# as example but this feature can be applied elsewhere too. -# -# Here is an example where +on_load+ method is called to register a hook. -# -# initializer "active_record.initialize_timezone" do -# ActiveSupport.on_load(:active_record) do -# self.time_zone_aware_attributes = true -# self.default_timezone = :utc -# end -# end -# -# When the entirety of +activerecord/lib/active_record/base.rb+ has been evaluated then +run_load_hooks+ is invoked. -# The very last line of +activerecord/lib/active_record/base.rb+ is: -# -# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base) -# module ActiveSupport - @load_hooks = Hash.new {|h,k| h[k] = [] } - @loaded = {} + # lazy_load_hooks allows rails to lazily load a lot of components and thus making the app boot faster. Because of + # this feature now there is no need to require <tt>ActiveRecord::Base</tt> at boot time purely to apply configuration. Instead + # a hook is registered that applies configuration once <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is used + # as example but this feature can be applied elsewhere too. + # + # Here is an example where +on_load+ method is called to register a hook. + # + # initializer "active_record.initialize_timezone" do + # ActiveSupport.on_load(:active_record) do + # self.time_zone_aware_attributes = true + # self.default_timezone = :utc + # end + # end + # + # When the entirety of +activerecord/lib/active_record/base.rb+ has been evaluated then +run_load_hooks+ is invoked. + # The very last line of +activerecord/lib/active_record/base.rb+ is: + # + # ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base) + # + @load_hooks = Hash.new { |h,k| h[k] = [] } + @loaded = Hash.new { |h,k| h[k] = [] } def self.on_load(name, options = {}, &block) - if base = @loaded[name] + @loaded[name].each do |base| execute_hook(base, options, block) - else - @load_hooks[name] << [block, options] end + + @load_hooks[name] << [block, options] end def self.execute_hook(base, options, block) @@ -38,7 +38,7 @@ module ActiveSupport end def self.run_load_hooks(name, base = Object) - @loaded[name] = base + @loaded[name] << base @load_hooks[name].each do |hook, options| execute_hook(base, options, hook) end diff --git a/activesupport/lib/active_support/log_subscriber.rb b/activesupport/lib/active_support/log_subscriber.rb index 58938cdc3d..d2a6e1bd82 100644 --- a/activesupport/lib/active_support/log_subscriber.rb +++ b/activesupport/lib/active_support/log_subscriber.rb @@ -3,7 +3,7 @@ require 'active_support/core_ext/class/attribute' module ActiveSupport # ActiveSupport::LogSubscriber is an object set to consume ActiveSupport::Notifications - # with the sole purpose of logging them. The log subscriber dispatches notifications to + # with the sole purpose of logging them. The log subscriber dispatches notifications to # a registered object based on its given namespace. # # An example would be Active Record log subscriber responsible for logging queries: @@ -75,7 +75,8 @@ module ActiveSupport @@flushable_loggers ||= begin loggers = log_subscribers.map(&:logger) loggers.uniq! - loggers.select { |l| l.respond_to?(:flush) } + loggers.select! { |l| l.respond_to?(:flush) } + loggers end end @@ -101,8 +102,7 @@ module ActiveSupport %w(info debug warn error fatal unknown).each do |level| class_eval <<-METHOD, __FILE__, __LINE__ + 1 def #{level}(progname = nil, &block) - return unless logger - logger.#{level}(progname, &block) + logger.#{level}(progname, &block) if logger end METHOD end diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index 13f675c654..8cf7bdafda 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -1,3 +1,6 @@ +require 'active_support/notifications/instrumenter' +require 'active_support/notifications/fanout' + module ActiveSupport # = Notifications # @@ -105,10 +108,6 @@ module ActiveSupport # to log subscribers in a thread. You can use any queue implementation you want. # module Notifications - autoload :Instrumenter, 'active_support/notifications/instrumenter' - autoload :Event, 'active_support/notifications/instrumenter' - autoload :Fanout, 'active_support/notifications/fanout' - @instrumenters = Hash.new { |h,k| h[k] = notifier.listening?(k) } class << self diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index a9aa5464e9..17c99089c1 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -9,18 +9,25 @@ module ActiveSupport end def subscribe(pattern = nil, block = Proc.new) - subscriber = Subscriber.new(pattern, block).tap do |s| - @subscribers << s - end + subscriber = Subscribers.new pattern, block + @subscribers << subscriber @listeners_for.clear subscriber end def unsubscribe(subscriber) - @subscribers.reject! {|s| s.matches?(subscriber)} + @subscribers.reject! { |s| s.matches?(subscriber) } @listeners_for.clear end + def start(name, id, payload) + listeners_for(name).each { |s| s.start(name, id, payload) } + end + + def finish(name, id, payload) + listeners_for(name).each { |s| s.finish(name, id, payload) } + end + def publish(name, *args) listeners_for(name).each { |s| s.publish(name, *args) } end @@ -37,23 +44,89 @@ module ActiveSupport def wait end - class Subscriber #:nodoc: - def initialize(pattern, delegate) - @pattern = pattern - @delegate = delegate + module Subscribers # :nodoc: + def self.new(pattern, listener) + if listener.respond_to?(:call) + subscriber = Timed.new pattern, listener + else + subscriber = Evented.new pattern, listener + end + + unless pattern + AllMessages.new(subscriber) + else + subscriber + end end - def publish(message, *args) - @delegate.call(message, *args) + class Evented #:nodoc: + def initialize(pattern, delegate) + @pattern = pattern + @delegate = delegate + end + + def start(name, id, payload) + @delegate.start name, id, payload + end + + def finish(name, id, payload) + @delegate.finish name, id, payload + end + + def subscribed_to?(name) + @pattern === name.to_s + end + + def matches?(subscriber_or_name) + self === subscriber_or_name || + @pattern && @pattern === subscriber_or_name + end end - def subscribed_to?(name) - !@pattern || @pattern === name.to_s + class Timed < Evented + def initialize(pattern, delegate) + @timestack = Hash.new { |h,id| + h[id] = Hash.new { |ids,name| ids[name] = [] } + } + super + end + + def publish(name, *args) + @delegate.call name, *args + end + + def start(name, id, payload) + @timestack[id][name].push Time.now + end + + def finish(name, id, payload) + started = @timestack[id][name].pop + @delegate.call(name, started, Time.now, id, payload) + end end - def matches?(subscriber_or_name) - self === subscriber_or_name || - @pattern && @pattern === subscriber_or_name + class AllMessages # :nodoc: + def initialize(delegate) + @delegate = delegate + end + + def start(name, id, payload) + @delegate.start name, id, payload + end + + def finish(name, id, payload) + @delegate.finish name, id, payload + end + + def publish(name, *args) + @delegate.publish name, *args + end + + def subscribed_to?(name) + true + end + + alias :matches? :=== end end end diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index 3941c285a2..58e292c658 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -1,7 +1,6 @@ -require 'active_support/core_ext/module/delegation' - module ActiveSupport module Notifications + # Instrumentors are stored in a thread local. class Instrumenter attr_reader :id @@ -14,15 +13,14 @@ module ActiveSupport # and publish it. Notice that events get sent even if an error occurs # in the passed-in block def instrument(name, payload={}) - started = Time.now - + @notifier.start(name, @id, payload) begin yield rescue Exception => e payload[:exception] = [e.class.name, e.message] raise e ensure - @notifier.publish(name, started, Time.now, @id, payload) + @notifier.finish(name, @id, payload) end end diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index f696716cc8..d1c62c5087 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -8,30 +8,6 @@ module ActiveSupport initializer "active_support.deprecation_behavior" do |app| if deprecation = app.config.active_support.deprecation ActiveSupport::Deprecation.behavior = deprecation - else - defaults = {"development" => :log, - "production" => :notify, - "test" => :stderr} - - env = Rails.env - - if defaults.key?(env) - msg = "You did not specify how you would like Rails to report " \ - "deprecation notices for your #{env} environment, please " \ - "set config.active_support.deprecation to :#{defaults[env]} " \ - "at config/environments/#{env}.rb" - - warn msg - ActiveSupport::Deprecation.behavior = defaults[env] - else - msg = "You did not specify how you would like Rails to report " \ - "deprecation notices for your #{env} environment, please " \ - "set config.active_support.deprecation to :log, :notify or " \ - ":stderr at config/environments/#{env}.rb" - - warn msg - ActiveSupport::Deprecation.behavior = :stderr - end end end @@ -42,8 +18,7 @@ module ActiveSupport zone_default = Time.find_zone!(app.config.time_zone) unless zone_default - raise \ - 'Value assigned to config.time_zone not recognized.' + + raise 'Value assigned to config.time_zone not recognized. ' \ 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.' end diff --git a/activesupport/lib/active_support/rescuable.rb b/activesupport/lib/active_support/rescuable.rb index 0f4a06468a..85e84bc203 100644 --- a/activesupport/lib/active_support/rescuable.rb +++ b/activesupport/lib/active_support/rescuable.rb @@ -108,7 +108,11 @@ module ActiveSupport when Symbol method(rescuer) when Proc - rescuer.bind(self) + if rescuer.arity == 0 + Proc.new { instance_exec(&rescuer) } + else + Proc.new { |_exception| instance_exec(_exception, &rescuer) } + end end end end diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb index f6ad861353..538a36f6d9 100644 --- a/activesupport/lib/active_support/tagged_logging.rb +++ b/activesupport/lib/active_support/tagged_logging.rb @@ -13,7 +13,7 @@ module ActiveSupport # This is used by the default Rails.logger as configured by Railties to make it easy to stamp log lines # with subdomains, request ids, and anything else to aid debugging of multi-user production applications. module TaggedLogging - class Formatter < ActiveSupport::Logger::SimpleFormatter # :nodoc: + module Formatter # :nodoc: # This method is invoked when a log event occurs def call(severity, timestamp, progname, msg) super(severity, timestamp, progname, "#{tags_text}#{msg}") @@ -37,7 +37,7 @@ module ActiveSupport end def self.new(logger) - logger.formatter = Formatter.new + logger.formatter.extend Formatter logger.extend(self) end @@ -45,7 +45,7 @@ module ActiveSupport tags = formatter.current_tags new_tags = new_tags.flatten.reject(&:blank?) tags.concat new_tags - yield + yield self ensure tags.pop(new_tags.size) end diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 35f400f9df..ce46c46092 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -1,28 +1,28 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/try' -# The TimeZone class serves as a wrapper around TZInfo::Timezone instances. It allows us to do the following: -# -# * Limit the set of zones provided by TZInfo to a meaningful subset of 142 zones. -# * Retrieve and display zones with a friendlier name (e.g., "Eastern Time (US & Canada)" instead of "America/New_York"). -# * Lazily load TZInfo::Timezone instances only when they're needed. -# * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+, +parse+, +at+ and +now+ methods. -# -# If you set <tt>config.time_zone</tt> in the Rails Application, you can access this TimeZone object via <tt>Time.zone</tt>: -# -# # application.rb: -# class Application < Rails::Application -# config.time_zone = "Eastern Time (US & Canada)" -# end -# -# Time.zone # => #<TimeZone:0x514834...> -# Time.zone.name # => "Eastern Time (US & Canada)" -# Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00 -# -# The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones -# defined by the TimeZone class. If you need to use zones that aren't defined by TimeZone, you'll need to install the TZInfo gem -# (if a recent version of the gem is installed locally, this will be used instead of the bundled version.) module ActiveSupport + # The TimeZone class serves as a wrapper around TZInfo::Timezone instances. It allows us to do the following: + # + # * Limit the set of zones provided by TZInfo to a meaningful subset of 142 zones. + # * Retrieve and display zones with a friendlier name (e.g., "Eastern Time (US & Canada)" instead of "America/New_York"). + # * Lazily load TZInfo::Timezone instances only when they're needed. + # * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+, +parse+, +at+ and +now+ methods. + # + # If you set <tt>config.time_zone</tt> in the Rails Application, you can access this TimeZone object via <tt>Time.zone</tt>: + # + # # application.rb: + # class Application < Rails::Application + # config.time_zone = "Eastern Time (US & Canada)" + # end + # + # Time.zone # => #<TimeZone:0x514834...> + # Time.zone.name # => "Eastern Time (US & Canada)" + # Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00 + # + # The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones + # defined by the TimeZone class. If you need to use zones that aren't defined by TimeZone, you'll need to install the TZInfo gem + # (if a recent version of the gem is installed locally, this will be used instead of the bundled version.) class TimeZone # Keys are Rails TimeZone names, values are TZInfo identifiers MAPPING = { @@ -168,8 +168,7 @@ module ActiveSupport "Auckland" => "Pacific/Auckland", "Wellington" => "Pacific/Auckland", "Nuku'alofa" => "Pacific/Tongatapu" - }.each { |name, zone| name.freeze; zone.freeze } - MAPPING.freeze + } UTC_OFFSET_WITH_COLON = '%s%02d:%02d' UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '') @@ -267,7 +266,7 @@ module ActiveSupport # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 def parse(str, now=now) date_parts = Date._parse(str) - return if date_parts.blank? + return if date_parts.empty? time = Time.parse(str, now) rescue DateTime.parse(str) if date_parts[:offset].nil? ActiveSupport::TimeWithZone.new(nil, self, time) @@ -282,7 +281,7 @@ module ActiveSupport # Time.zone = 'Hawaii' # => "Hawaii" # Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00 def now - Time.now.utc.in_time_zone(self) + time_now.utc.in_time_zone(self) end # Return the current date in this time zone. @@ -391,5 +390,11 @@ module ActiveSupport end end end + + private + + def time_now + Time.now + end end end diff --git a/activesupport/lib/active_support/xml_mini/jdom.rb b/activesupport/lib/active_support/xml_mini/jdom.rb index dbb6c71907..4551dd2f2d 100644 --- a/activesupport/lib/active_support/xml_mini/jdom.rb +++ b/activesupport/lib/active_support/xml_mini/jdom.rb @@ -12,7 +12,6 @@ java_import org.xml.sax.InputSource unless defined? InputSource java_import org.xml.sax.Attributes unless defined? Attributes java_import org.w3c.dom.Node unless defined? Node -# = XmlMini JRuby JDOM implementation module ActiveSupport module XmlMini_JDOM #:nodoc: extend self diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb index 16570c6aea..26556598fd 100644 --- a/activesupport/lib/active_support/xml_mini/libxml.rb +++ b/activesupport/lib/active_support/xml_mini/libxml.rb @@ -2,7 +2,6 @@ require 'libxml' require 'active_support/core_ext/object/blank' require 'stringio' -# = XmlMini LibXML implementation module ActiveSupport module XmlMini_LibXML #:nodoc: extend self diff --git a/activesupport/lib/active_support/xml_mini/libxmlsax.rb b/activesupport/lib/active_support/xml_mini/libxmlsax.rb index 2536b1f33e..acc018fd2d 100644 --- a/activesupport/lib/active_support/xml_mini/libxmlsax.rb +++ b/activesupport/lib/active_support/xml_mini/libxmlsax.rb @@ -2,9 +2,8 @@ require 'libxml' require 'active_support/core_ext/object/blank' require 'stringio' -# = XmlMini LibXML implementation using a SAX-based parser module ActiveSupport - module XmlMini_LibXMLSAX + module XmlMini_LibXMLSAX #:nodoc: extend self # Class that will build the hash while the XML document diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb index 04ec9e8ab8..bb0a52bdcf 100644 --- a/activesupport/lib/active_support/xml_mini/nokogiri.rb +++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb @@ -7,7 +7,6 @@ end require 'active_support/core_ext/object/blank' require 'stringio' -# = XmlMini Nokogiri implementation module ActiveSupport module XmlMini_Nokogiri #:nodoc: extend self diff --git a/activesupport/lib/active_support/xml_mini/nokogirisax.rb b/activesupport/lib/active_support/xml_mini/nokogirisax.rb index 93fd3dfe57..30b94aac47 100644 --- a/activesupport/lib/active_support/xml_mini/nokogirisax.rb +++ b/activesupport/lib/active_support/xml_mini/nokogirisax.rb @@ -7,9 +7,8 @@ end require 'active_support/core_ext/object/blank' require 'stringio' -# = XmlMini Nokogiri implementation using a SAX-based parser module ActiveSupport - module XmlMini_NokogiriSAX + module XmlMini_NokogiriSAX #:nodoc: extend self # Class that will build the hash while the XML document diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb index a13ad10118..a2a87337a6 100644 --- a/activesupport/lib/active_support/xml_mini/rexml.rb +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -2,7 +2,6 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/object/blank' require 'stringio' -# = XmlMini ReXML implementation module ActiveSupport module XmlMini_REXML #:nodoc: extend self diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index b03865da93..5365713e6e 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -69,6 +69,10 @@ class CacheKeyTest < ActiveSupport::TestCase def test_expand_cache_key_of_true assert_equal 'true', ActiveSupport::Cache.expand_cache_key(true) end + + def test_expand_cache_key_of_array_like_object + assert_equal 'foo/bar/baz', ActiveSupport::Cache.expand_cache_key(%w{foo bar baz}.to_enum) + end end class CacheStoreSettingTest < ActiveSupport::TestCase @@ -575,6 +579,16 @@ class FileStoreTest < ActiveSupport::TestCase assert_equal "views/index?id=1", @cache_with_pathname.send(:file_path_key, key) end + # Test that generated cache keys are short enough to have Tempfile stuff added to them and + # remain valid + def test_filename_max_size + key = "#{'A' * ActiveSupport::Cache::FileStore::FILENAME_MAX_SIZE}" + path = @cache.send(:key_file_path, key) + Dir::Tmpname.create(path) do |tmpname, n, opts| + assert File.basename(tmpname+'.lock').length <= 255, "Temp filename too long: #{File.basename(tmpname+'.lock').length}" + end + end + # Because file systems have a maximum filename size, filenames > max size should be split in to directories # If filename is 'AAAAB', where max size is 4, the returned path should be AAAA/B def test_key_transformation_max_filename_size diff --git a/activesupport/test/core_ext/class/attribute_accessor_test.rb b/activesupport/test/core_ext/class/attribute_accessor_test.rb index 3822e7af66..8d827f054e 100644 --- a/activesupport/test/core_ext/class/attribute_accessor_test.rb +++ b/activesupport/test/core_ext/class/attribute_accessor_test.rb @@ -42,4 +42,18 @@ class ClassAttributeAccessorTest < ActiveSupport::TestCase assert !@object.respond_to?(:camp) assert !@object.respond_to?(:camp=) end + + def test_should_raise_name_error_if_attribute_name_is_invalid + assert_raises NameError do + Class.new do + cattr_reader "invalid attribute name" + end + end + + assert_raises NameError do + Class.new do + cattr_writer "invalid attribute name" + end + end + end end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 6e91fdedce..760d138623 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -175,6 +175,18 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,4), Date.new(1583,10,14).prev_year end + def test_last_year + assert_equal Date.new(2004,6,5), Date.new(2005,6,5).last_year + end + + def test_last_year_in_leap_years + assert_equal Date.new(1999,2,28), Date.new(2000,2,29).last_year + end + + def test_last_year_in_calendar_reform + assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year + end + def test_next_year assert_equal Date.new(2006,6,5), Date.new(2005,6,5).next_year end @@ -245,6 +257,14 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday) end + def test_last_week + assert_equal Date.new(2005,5,9), Date.new(2005,5,17).last_week + assert_equal Date.new(2006,12,25), Date.new(2007,1,7).last_week + assert_equal Date.new(2010,2,12), Date.new(2010,2,19).last_week(:friday) + assert_equal Date.new(2010,2,13), Date.new(2010,2,19).last_week(:saturday) + assert_equal Date.new(2010,2,27), Date.new(2010,3,4).last_week(:saturday) + end + def test_next_week assert_equal Date.new(2005,2,28), Date.new(2005,2,22).next_week assert_equal Date.new(2005,3,4), Date.new(2005,2,22).next_week(:friday) @@ -265,6 +285,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month end + def test_last_month_on_31st + assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month + end + def test_yesterday_constructor assert_equal Date.current - 1, Date.yesterday end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 57b5b95a66..cd8cb5d18b 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -159,6 +159,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year end + def test_last_year + assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year + end + def test_next_year assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year end @@ -232,6 +236,14 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday) end + def test_last_week + assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).last_week + assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).last_week(:tuesday) + assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).last_week(:friday) + assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).last_week + assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).last_week(:wednesday) + end + def test_next_week assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday) @@ -247,6 +259,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month end + def test_last_month_on_31st + assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month + end + def test_xmlschema assert_match(/^1880-02-28T15:15:10\+00:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema) assert_match(/^1980-02-28T15:15:10\+00:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema) diff --git a/activesupport/test/core_ext/duplicable_test.rb b/activesupport/test/core_ext/duplicable_test.rb index 3e54266051..1105353e45 100644 --- a/activesupport/test/core_ext/duplicable_test.rb +++ b/activesupport/test/core_ext/duplicable_test.rb @@ -4,17 +4,25 @@ require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/numeric/time' class DuplicableTest < ActiveSupport::TestCase - RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, BigDecimal.new('4.56'), 5.seconds] + RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, 5.seconds] YES = ['1', Object.new, /foo/, [], {}, Time.now] NO = [Class.new, Module.new] + begin + bd = BigDecimal.new('4.56') + YES << bd.dup + rescue TypeError + RAISE_DUP << bd + end + + def test_duplicable (RAISE_DUP + NO).each do |v| assert !v.duplicable? end YES.each do |v| - assert v.duplicable? + assert v.duplicable?, "#{v.class} should be duplicable" end (YES + NO).each do |v| diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index a0f261ebdb..80b3c16328 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -388,6 +388,15 @@ class HashExtTest < ActiveSupport::TestCase assert_equal expected, hash end + def test_constructor_on_indifferent_access + hash = HashWithIndifferentAccess[:foo, 1] + assert_equal 1, hash[:foo] + assert_equal 1, hash['foo'] + hash[:foo] = 3 + assert_equal 3, hash[:foo] + assert_equal 3, hash['foo'] + end + def test_reverse_merge defaults = { :a => "x", :b => "y", :c => 10 }.freeze options = { :a => 1, :b => 2 } @@ -486,6 +495,13 @@ class HashExtTest < ActiveSupport::TestCase assert_equal 'bender', slice['login'] end + def test_extract + original = {:a => 1, :b => 2, :c => 3, :d => 4} + expected = {:a => 1, :b => 2} + + assert_equal expected, original.extract!(:a, :b) + end + def test_except original = { :a => 'x', :b => 'y', :c => 10 } expected = { :a => 'x', :b => 'y' } @@ -550,7 +566,7 @@ class HashExtToParamTests < ActiveSupport::TestCase end def test_to_param_orders_by_key_in_ascending_order - assert_equal 'a=2&b=1&c=0', ActiveSupport::OrderedHash[*%w(b 1 c 0 a 2)].to_param + assert_equal 'a=2&b=1&c=0', Hash[*%w(b 1 c 0 a 2)].to_param end end diff --git a/activesupport/test/core_ext/module/attribute_accessor_test.rb b/activesupport/test/core_ext/module/attribute_accessor_test.rb index 6a2ad2f241..a577f90bdd 100644 --- a/activesupport/test/core_ext/module/attribute_accessor_test.rb +++ b/activesupport/test/core_ext/module/attribute_accessor_test.rb @@ -44,4 +44,18 @@ class ModuleAttributeAccessorTest < ActiveSupport::TestCase assert !@object.respond_to?(:camp) assert !@object.respond_to?(:camp=) end + + def test_should_raise_name_error_if_attribute_name_is_invalid + assert_raises NameError do + Class.new do + mattr_reader "invalid attribute name" + end + end + + assert_raises NameError do + Class.new do + mattr_writer "invalid attribute name" + end + end + end end diff --git a/activesupport/test/core_ext/object/to_query_test.rb b/activesupport/test/core_ext/object/to_query_test.rb index 6a26e1fa4f..c34647c1df 100644 --- a/activesupport/test/core_ext/object/to_query_test.rb +++ b/activesupport/test/core_ext/object/to_query_test.rb @@ -28,12 +28,12 @@ class ToQueryTest < ActiveSupport::TestCase def test_nested_conversion assert_query_equal 'person%5Blogin%5D=seckar&person%5Bname%5D=Nicholas', - :person => ActiveSupport::OrderedHash[:login, 'seckar', :name, 'Nicholas'] + :person => Hash[:login, 'seckar', :name, 'Nicholas'] end def test_multiple_nested assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10', - ActiveSupport::OrderedHash[:account, {:person => {:id => 20}}, :person, {:id => 10}] + Hash[:account, {:person => {:id => 20}}, :person, {:id => 10}] end def test_array_values diff --git a/activesupport/test/core_ext/proc_test.rb b/activesupport/test/core_ext/proc_test.rb index 690bfd3bf8..c4d5592196 100644 --- a/activesupport/test/core_ext/proc_test.rb +++ b/activesupport/test/core_ext/proc_test.rb @@ -3,10 +3,12 @@ require 'active_support/core_ext/proc' class ProcTests < ActiveSupport::TestCase def test_bind_returns_method_with_changed_self - block = Proc.new { self } - assert_equal self, block.call - bound_block = block.bind("hello") - assert_not_equal block, bound_block - assert_equal "hello", bound_block.call + assert_deprecated do + block = Proc.new { self } + assert_equal self, block.call + bound_block = block.bind("hello") + assert_not_equal block, bound_block + assert_equal "hello", bound_block.call + end end end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index eda8066579..c542acca68 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -198,6 +198,10 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year end + def test_last_year + assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year + end + def test_next_year assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year end @@ -505,6 +509,16 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday) end end + + def test_last_week + with_env_tz 'US/Eastern' do + assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week + assert_equal Time.local(2005,2,22), Time.local(2005,3,1,15,15,10).last_week(:tuesday) + assert_equal Time.local(2005,2,25), Time.local(2005,3,1,15,15,10).last_week(:friday) + assert_equal Time.local(2006,10,30), Time.local(2006,11,6,0,0,0).last_week + assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).last_week(:wednesday) + end + end def test_next_week with_env_tz 'US/Eastern' do @@ -662,6 +676,10 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month end + def test_last_month_on_31st + assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month + end + def test_xmlschema_is_available assert_nothing_raised { Time.now.xmlschema } end diff --git a/activesupport/test/file_update_checker_test.rb b/activesupport/test/file_update_checker_test.rb index dd2483287b..8adff5de8d 100644 --- a/activesupport/test/file_update_checker_test.rb +++ b/activesupport/test/file_update_checker_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'fileutils' +require 'thread' MTIME_FIXTURES_PATH = File.expand_path("../fixtures", __FILE__) @@ -43,8 +44,8 @@ class FileUpdateCheckerWithEnumerableTest < ActiveSupport::TestCase i = 0 checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 } FileUtils.rm(FILES) - assert !checker.execute_if_updated - assert_equal 0, i + assert checker.execute_if_updated + assert_equal 1, i end def test_should_cache_updated_result_until_execute @@ -79,4 +80,18 @@ class FileUpdateCheckerWithEnumerableTest < ActiveSupport::TestCase assert !checker.execute_if_updated assert_equal 0, i end + + def test_should_not_block_if_a_strange_filename_used + FileUtils.mkdir_p("tmp_watcher/valid,yetstrange,path,") + FileUtils.touch(FILES.map { |file_name| "tmp_watcher/valid,yetstrange,path,/#{file_name}" }) + + test = Thread.new do + ActiveSupport::FileUpdateChecker.new([],"tmp_watcher/valid,yetstrange,path," => :txt) { i += 1 } + Thread.exit + end + test.priority = -1 + test.join(5) + + assert !test.alive? + end end diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index 809b8b46c9..879c3c1125 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -220,7 +220,9 @@ module InflectorTestCases 'Actionwebservice' => 'Actionwebservice', "david's code" => "David's Code", "David's code" => "David's Code", - "david's Code" => "David's Code" + "david's Code" => "David's Code", + "Fred’s" => "Fred’s", + "Fred`s" => "Fred`s" } OrdinalNumbers = { diff --git a/activesupport/test/lazy_load_hooks_test.rb b/activesupport/test/lazy_load_hooks_test.rb index 58ccc14324..7851634dbf 100644 --- a/activesupport/test/lazy_load_hooks_test.rb +++ b/activesupport/test/lazy_load_hooks_test.rb @@ -8,6 +8,16 @@ class LazyLoadHooksTest < ActiveSupport::TestCase assert_equal 1, i end + def test_basic_hook_with_two_registrations + i = 0 + ActiveSupport.on_load(:basic_hook_with_two) { i += incr } + assert_equal 0, i + ActiveSupport.run_load_hooks(:basic_hook_with_two, FakeContext.new(2)) + assert_equal 2, i + ActiveSupport.run_load_hooks(:basic_hook_with_two, FakeContext.new(5)) + assert_equal 7, i + end + def test_hook_registered_after_run i = 0 ActiveSupport.run_load_hooks(:registered_after) @@ -16,6 +26,25 @@ class LazyLoadHooksTest < ActiveSupport::TestCase assert_equal 1, i end + def test_hook_registered_after_run_with_two_registrations + i = 0 + ActiveSupport.run_load_hooks(:registered_after_with_two, FakeContext.new(2)) + ActiveSupport.run_load_hooks(:registered_after_with_two, FakeContext.new(5)) + assert_equal 0, i + ActiveSupport.on_load(:registered_after_with_two) { i += incr } + assert_equal 7, i + end + + def test_hook_registered_interleaved_run_with_two_registrations + i = 0 + ActiveSupport.run_load_hooks(:registered_interleaved_with_two, FakeContext.new(2)) + assert_equal 0, i + ActiveSupport.on_load(:registered_interleaved_with_two) { i += incr } + assert_equal 2, i + ActiveSupport.run_load_hooks(:registered_interleaved_with_two, FakeContext.new(5)) + assert_equal 7, i + end + def test_hook_receives_a_context i = 0 ActiveSupport.on_load(:contextual) { i += incr } diff --git a/activesupport/test/buffered_logger_test.rb b/activesupport/test/logger_test.rb index 615635607c..eedeca30a8 100644 --- a/activesupport/test/buffered_logger_test.rb +++ b/activesupport/test/logger_test.rb @@ -3,11 +3,9 @@ require 'multibyte_test_helpers' require 'stringio' require 'fileutils' require 'tempfile' -require 'active_support/testing/deprecation' -class BufferedLoggerTest < ActiveSupport::TestCase +class LoggerTest < ActiveSupport::TestCase include MultibyteTestHelpers - include ActiveSupport::Testing::Deprecation Logger = ActiveSupport::Logger diff --git a/activesupport/test/notifications/evented_notification_test.rb b/activesupport/test/notifications/evented_notification_test.rb new file mode 100644 index 0000000000..f77a0eb3fa --- /dev/null +++ b/activesupport/test/notifications/evented_notification_test.rb @@ -0,0 +1,67 @@ +require 'abstract_unit' + +module ActiveSupport + module Notifications + class EventedTest < ActiveSupport::TestCase + class Listener + attr_reader :events + + def initialize + @events = [] + end + + def start(name, id, payload) + @events << [:start, name, id, payload] + end + + def finish(name, id, payload) + @events << [:finish, name, id, payload] + end + end + + def test_evented_listener + notifier = Fanout.new + listener = Listener.new + notifier.subscribe 'hi', listener + notifier.start 'hi', 1, {} + notifier.start 'hi', 2, {} + notifier.finish 'hi', 2, {} + notifier.finish 'hi', 1, {} + + assert_equal 4, listener.events.length + assert_equal [ + [:start, 'hi', 1, {}], + [:start, 'hi', 2, {}], + [:finish, 'hi', 2, {}], + [:finish, 'hi', 1, {}], + ], listener.events + end + + def test_evented_listener_no_events + notifier = Fanout.new + listener = Listener.new + notifier.subscribe 'hi', listener + notifier.start 'world', 1, {} + assert_equal 0, listener.events.length + end + + def test_listen_to_everything + notifier = Fanout.new + listener = Listener.new + notifier.subscribe nil, listener + notifier.start 'hello', 1, {} + notifier.start 'world', 1, {} + notifier.finish 'world', 1, {} + notifier.finish 'hello', 1, {} + + assert_equal 4, listener.events.length + assert_equal [ + [:start, 'hello', 1, {}], + [:start, 'world', 1, {}], + [:finish, 'world', 1, {}], + [:finish, 'hello', 1, {}], + ], listener.events + end + end + end +end diff --git a/activesupport/test/ordered_options_test.rb b/activesupport/test/ordered_options_test.rb index 0eee991e20..3526c7a366 100644 --- a/activesupport/test/ordered_options_test.rb +++ b/activesupport/test/ordered_options_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'active_support/ordered_options' class OrderedOptionsTest < ActiveSupport::TestCase def test_usage diff --git a/activesupport/test/safe_buffer_test.rb b/activesupport/test/safe_buffer_test.rb index bdde5141a9..047b89be2a 100644 --- a/activesupport/test/safe_buffer_test.rb +++ b/activesupport/test/safe_buffer_test.rb @@ -84,13 +84,13 @@ class SafeBufferTest < ActiveSupport::TestCase assert_equal "hello<>", clean + @buffer end - test "Should concat as a normal string when dirty" do + test "Should concat as a normal string when safe" do clean = "hello".html_safe @buffer.gsub!('', '<>') assert_equal "<>hello", @buffer + clean end - test "Should preserve dirty? status on copy" do + test "Should preserve html_safe? status on copy" do @buffer.gsub!('', '<>') assert !@buffer.dup.html_safe? end @@ -102,20 +102,42 @@ class SafeBufferTest < ActiveSupport::TestCase assert_equal "<script>", result_buffer end - test "Should raise an error when safe_concat is called on dirty buffers" do + test "Should raise an error when safe_concat is called on unsafe buffers" do @buffer.gsub!('', '<>') assert_raise ActiveSupport::SafeBuffer::SafeConcatError do @buffer.safe_concat "BUSTED" end end - test "should not fail if the returned object is not a string" do + test "Should not fail if the returned object is not a string" do assert_kind_of NilClass, @buffer.slice("chipchop") end - test "Should initialize @dirty to false for new instance when sliced" do - dirty = @buffer[0,0].send(:dirty?) - assert_not_nil dirty - assert !dirty + test "clone_empty returns an empty buffer" do + assert_equal '', ActiveSupport::SafeBuffer.new('foo').clone_empty + end + + test "clone_empty keeps the original dirtyness" do + assert @buffer.clone_empty.html_safe? + assert !@buffer.gsub!('', '').clone_empty.html_safe? + end + + test "Should be safe when sliced if original value was safe" do + new_buffer = @buffer[0,0] + assert_not_nil new_buffer + assert new_buffer.html_safe?, "should be safe" + end + + test "Should continue unsafe on slice" do + x = 'foo'.html_safe.gsub!('f', '<script>alert("lolpwnd");</script>') + + # calling gsub! makes the dirty flag true + assert !x.html_safe?, "should not be safe" + + # getting a slice of it + y = x[0..-1] + + # should still be unsafe + assert !y.html_safe?, "should not be safe" end end diff --git a/activesupport/test/tagged_logging_test.rb b/activesupport/test/tagged_logging_test.rb index dd4ae319e5..0751c2469e 100644 --- a/activesupport/test/tagged_logging_test.rb +++ b/activesupport/test/tagged_logging_test.rb @@ -18,7 +18,7 @@ class TaggedLoggingTest < ActiveSupport::TestCase @logger.tagged("BCX") { @logger.info "Funky time" } assert_equal "[BCX] Funky time\n", @output.string end - + test "tagged twice" do @logger.tagged("BCX") { @logger.tagged("Jason") { @logger.info "Funky time" } } assert_equal "[BCX] [Jason] Funky time\n", @output.string @@ -29,6 +29,11 @@ class TaggedLoggingTest < ActiveSupport::TestCase assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string end + test "provides access to the logger instance" do + @logger.tagged("BCX") { |logger| logger.info "Funky time" } + assert_equal "[BCX] Funky time\n", @output.string + end + test "tagged once with blank and nil" do @logger.tagged(nil, "", "New") { @logger.info "Funky time" } assert_equal "[New] Funky time\n", @output.string diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index e26256f9c6..d14d01dc30 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -48,8 +48,8 @@ class TimeZoneTest < ActiveSupport::TestCase def test_now with_env_tz 'US/Eastern' do - Time.stubs(:now).returns(Time.local(2000)) - zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'].dup + def zone.time_now; Time.local(2000); end assert_instance_of ActiveSupport::TimeWithZone, zone.now assert_equal Time.utc(2000,1,1,5), zone.now.utc assert_equal Time.utc(2000), zone.now.time @@ -59,8 +59,11 @@ class TimeZoneTest < ActiveSupport::TestCase def test_now_enforces_spring_dst_rules with_env_tz 'US/Eastern' do - Time.stubs(:now).returns(Time.local(2006,4,2,2)) # 2AM springs forward to 3AM - zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'].dup + def zone.time_now + Time.local(2006,4,2,2) # 2AM springs forward to 3AM + end + assert_equal Time.utc(2006,4,2,3), zone.now.time assert_equal true, zone.now.dst? end @@ -68,8 +71,10 @@ class TimeZoneTest < ActiveSupport::TestCase def test_now_enforces_fall_dst_rules with_env_tz 'US/Eastern' do - Time.stubs(:now).returns(Time.at(1162098000)) # equivalent to 1AM DST - zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'].dup + def zone.time_now + Time.at(1162098000) # equivalent to 1AM DST + end assert_equal Time.utc(2006,10,29,1), zone.now.time assert_equal true, zone.now.dst? end diff --git a/ci/travis.rb b/ci/travis.rb index 52e146df70..fc120f80ba 100755 --- a/ci/travis.rb +++ b/ci/travis.rb @@ -19,7 +19,6 @@ class Build 'ap' => 'actionpack', 'am' => 'actionmailer', 'amo' => 'activemodel', - 'ares' => 'activeresource', 'as' => 'activesupport', 'ar' => 'activerecord' } @@ -35,7 +34,6 @@ class Build self.options.update(options) Dir.chdir(dir) do announce(heading) - ENV['IM'] = identity_map?.inspect rake(*tasks) end end @@ -46,7 +44,7 @@ class Build def heading heading = [gem] - heading << "with #{adapter} IM #{identity_map? ? 'enabled' : 'disabled'}" if activerecord? + heading << "with #{adapter}" if activerecord? heading << "in isolation" if isolated? heading.join(' ') end @@ -62,7 +60,6 @@ class Build def key key = [gem] key << adapter if activerecord? - key << 'IM' if identity_map? key << 'isolated' if isolated? key.join(':') end @@ -71,10 +68,6 @@ class Build gem == 'activerecord' end - def identity_map? - options[:identity_map] - end - def isolated? options[:isolated] end @@ -108,7 +101,6 @@ ENV['GEM'].split(',').each do |gem| results[build.key] = build.run! if build.activerecord? - build.options[:identity_map] = true results[build.key] = build.run! end end diff --git a/guides/Rakefile b/guides/Rakefile new file mode 100644 index 0000000000..ad4ff91fe6 --- /dev/null +++ b/guides/Rakefile @@ -0,0 +1,11 @@ +desc 'Generate guides (for authors), use ONLY=foo to process just "foo.textile"' +task :generate_guides do + ENV["WARN_BROKEN_LINKS"] = "1" # authors can't disable this + ruby "rails_guides.rb" +end + +# Validate guides ------------------------------------------------------------------------- +desc 'Validate guides, use ONLY=foo to process just "foo.html"' +task :validate_guides do + ruby "w3c_validator.rb" +end diff --git a/railties/guides/assets/images/belongs_to.png b/guides/assets/images/belongs_to.png Binary files differindex 44243edbca..44243edbca 100644 --- a/railties/guides/assets/images/belongs_to.png +++ b/guides/assets/images/belongs_to.png diff --git a/railties/guides/assets/images/book_icon.gif b/guides/assets/images/book_icon.gif Binary files differindex c81d5db520..c81d5db520 100644 --- a/railties/guides/assets/images/book_icon.gif +++ b/guides/assets/images/book_icon.gif diff --git a/railties/guides/assets/images/bullet.gif b/guides/assets/images/bullet.gif Binary files differindex 95a26364a4..95a26364a4 100644 --- a/railties/guides/assets/images/bullet.gif +++ b/guides/assets/images/bullet.gif diff --git a/railties/guides/assets/images/challenge.png b/guides/assets/images/challenge.png Binary files differindex d163748640..d163748640 100644 --- a/railties/guides/assets/images/challenge.png +++ b/guides/assets/images/challenge.png diff --git a/railties/guides/assets/images/chapters_icon.gif b/guides/assets/images/chapters_icon.gif Binary files differindex 06fb415f4a..06fb415f4a 100644 --- a/railties/guides/assets/images/chapters_icon.gif +++ b/guides/assets/images/chapters_icon.gif diff --git a/railties/guides/assets/images/check_bullet.gif b/guides/assets/images/check_bullet.gif Binary files differindex 1fcfeba250..1fcfeba250 100644 --- a/railties/guides/assets/images/check_bullet.gif +++ b/guides/assets/images/check_bullet.gif diff --git a/railties/guides/assets/images/credits_pic_blank.gif b/guides/assets/images/credits_pic_blank.gif Binary files differindex f6f654fc65..f6f654fc65 100644 --- a/railties/guides/assets/images/credits_pic_blank.gif +++ b/guides/assets/images/credits_pic_blank.gif diff --git a/railties/guides/assets/images/csrf.png b/guides/assets/images/csrf.png Binary files differindex ab73baafe8..ab73baafe8 100644 --- a/railties/guides/assets/images/csrf.png +++ b/guides/assets/images/csrf.png diff --git a/railties/guides/assets/images/customized_error_messages.png b/guides/assets/images/customized_error_messages.png Binary files differindex fa676991e3..fa676991e3 100644 --- a/railties/guides/assets/images/customized_error_messages.png +++ b/guides/assets/images/customized_error_messages.png diff --git a/railties/guides/assets/images/edge_badge.png b/guides/assets/images/edge_badge.png Binary files differindex cddd46c4b8..cddd46c4b8 100644 --- a/railties/guides/assets/images/edge_badge.png +++ b/guides/assets/images/edge_badge.png diff --git a/railties/guides/assets/images/error_messages.png b/guides/assets/images/error_messages.png Binary files differindex 428892194a..428892194a 100644 --- a/railties/guides/assets/images/error_messages.png +++ b/guides/assets/images/error_messages.png diff --git a/railties/guides/assets/images/feature_tile.gif b/guides/assets/images/feature_tile.gif Binary files differindex 75469361db..75469361db 100644 --- a/railties/guides/assets/images/feature_tile.gif +++ b/guides/assets/images/feature_tile.gif diff --git a/railties/guides/assets/images/footer_tile.gif b/guides/assets/images/footer_tile.gif Binary files differindex bb33fc1ff0..bb33fc1ff0 100644 --- a/railties/guides/assets/images/footer_tile.gif +++ b/guides/assets/images/footer_tile.gif diff --git a/railties/guides/assets/images/fxn.png b/guides/assets/images/fxn.png Binary files differindex 9b531ee584..9b531ee584 100644 --- a/railties/guides/assets/images/fxn.png +++ b/guides/assets/images/fxn.png diff --git a/guides/assets/images/getting_started/new_post.png b/guides/assets/images/getting_started/new_post.png Binary files differnew file mode 100644 index 0000000000..dc9459032a --- /dev/null +++ b/guides/assets/images/getting_started/new_post.png diff --git a/guides/assets/images/getting_started/routing_error_no_controller.png b/guides/assets/images/getting_started/routing_error_no_controller.png Binary files differnew file mode 100644 index 0000000000..92a39efd78 --- /dev/null +++ b/guides/assets/images/getting_started/routing_error_no_controller.png diff --git a/guides/assets/images/getting_started/routing_error_no_route_matches.png b/guides/assets/images/getting_started/routing_error_no_route_matches.png Binary files differnew file mode 100644 index 0000000000..bc768a94a2 --- /dev/null +++ b/guides/assets/images/getting_started/routing_error_no_route_matches.png diff --git a/guides/assets/images/getting_started/template_is_missing_posts_new.png b/guides/assets/images/getting_started/template_is_missing_posts_new.png Binary files differnew file mode 100644 index 0000000000..9f070d59db --- /dev/null +++ b/guides/assets/images/getting_started/template_is_missing_posts_new.png diff --git a/guides/assets/images/getting_started/unknown_action_create_for_posts.png b/guides/assets/images/getting_started/unknown_action_create_for_posts.png Binary files differnew file mode 100644 index 0000000000..03d92dfb7d --- /dev/null +++ b/guides/assets/images/getting_started/unknown_action_create_for_posts.png diff --git a/guides/assets/images/getting_started/unknown_action_new_for_posts.png b/guides/assets/images/getting_started/unknown_action_new_for_posts.png Binary files differnew file mode 100644 index 0000000000..b63883d922 --- /dev/null +++ b/guides/assets/images/getting_started/unknown_action_new_for_posts.png diff --git a/railties/guides/assets/images/grey_bullet.gif b/guides/assets/images/grey_bullet.gif Binary files differindex e75e8e93a1..e75e8e93a1 100644 --- a/railties/guides/assets/images/grey_bullet.gif +++ b/guides/assets/images/grey_bullet.gif diff --git a/railties/guides/assets/images/habtm.png b/guides/assets/images/habtm.png Binary files differindex fea78b0b5c..fea78b0b5c 100644 --- a/railties/guides/assets/images/habtm.png +++ b/guides/assets/images/habtm.png diff --git a/railties/guides/assets/images/has_many.png b/guides/assets/images/has_many.png Binary files differindex 6cff58460d..6cff58460d 100644 --- a/railties/guides/assets/images/has_many.png +++ b/guides/assets/images/has_many.png diff --git a/railties/guides/assets/images/has_many_through.png b/guides/assets/images/has_many_through.png Binary files differindex 85d7599925..85d7599925 100644 --- a/railties/guides/assets/images/has_many_through.png +++ b/guides/assets/images/has_many_through.png diff --git a/railties/guides/assets/images/has_one.png b/guides/assets/images/has_one.png Binary files differindex a70ddaaa86..a70ddaaa86 100644 --- a/railties/guides/assets/images/has_one.png +++ b/guides/assets/images/has_one.png diff --git a/railties/guides/assets/images/has_one_through.png b/guides/assets/images/has_one_through.png Binary files differindex 89a7617a30..89a7617a30 100644 --- a/railties/guides/assets/images/has_one_through.png +++ b/guides/assets/images/has_one_through.png diff --git a/railties/guides/assets/images/header_backdrop.png b/guides/assets/images/header_backdrop.png Binary files differindex ff2982175e..ff2982175e 100644 --- a/railties/guides/assets/images/header_backdrop.png +++ b/guides/assets/images/header_backdrop.png diff --git a/railties/guides/assets/images/header_tile.gif b/guides/assets/images/header_tile.gif Binary files differindex e2c878d492..e2c878d492 100644 --- a/railties/guides/assets/images/header_tile.gif +++ b/guides/assets/images/header_tile.gif diff --git a/railties/guides/assets/images/i18n/demo_html_safe.png b/guides/assets/images/i18n/demo_html_safe.png Binary files differindex f881f60dac..f881f60dac 100644 --- a/railties/guides/assets/images/i18n/demo_html_safe.png +++ b/guides/assets/images/i18n/demo_html_safe.png diff --git a/railties/guides/assets/images/i18n/demo_localized_pirate.png b/guides/assets/images/i18n/demo_localized_pirate.png Binary files differindex 9134709573..9134709573 100644 --- a/railties/guides/assets/images/i18n/demo_localized_pirate.png +++ b/guides/assets/images/i18n/demo_localized_pirate.png diff --git a/railties/guides/assets/images/i18n/demo_translated_en.png b/guides/assets/images/i18n/demo_translated_en.png Binary files differindex ecdd878d38..ecdd878d38 100644 --- a/railties/guides/assets/images/i18n/demo_translated_en.png +++ b/guides/assets/images/i18n/demo_translated_en.png diff --git a/railties/guides/assets/images/i18n/demo_translated_pirate.png b/guides/assets/images/i18n/demo_translated_pirate.png Binary files differindex 41c580923a..41c580923a 100644 --- a/railties/guides/assets/images/i18n/demo_translated_pirate.png +++ b/guides/assets/images/i18n/demo_translated_pirate.png diff --git a/railties/guides/assets/images/i18n/demo_translation_missing.png b/guides/assets/images/i18n/demo_translation_missing.png Binary files differindex af9e2d0427..af9e2d0427 100644 --- a/railties/guides/assets/images/i18n/demo_translation_missing.png +++ b/guides/assets/images/i18n/demo_translation_missing.png diff --git a/railties/guides/assets/images/i18n/demo_untranslated.png b/guides/assets/images/i18n/demo_untranslated.png Binary files differindex 3603f43463..3603f43463 100644 --- a/railties/guides/assets/images/i18n/demo_untranslated.png +++ b/guides/assets/images/i18n/demo_untranslated.png diff --git a/railties/guides/assets/images/icons/README b/guides/assets/images/icons/README index f12b2a730c..f12b2a730c 100644 --- a/railties/guides/assets/images/icons/README +++ b/guides/assets/images/icons/README diff --git a/railties/guides/assets/images/icons/callouts/1.png b/guides/assets/images/icons/callouts/1.png Binary files differindex 7d473430b7..7d473430b7 100644 --- a/railties/guides/assets/images/icons/callouts/1.png +++ b/guides/assets/images/icons/callouts/1.png diff --git a/railties/guides/assets/images/icons/callouts/10.png b/guides/assets/images/icons/callouts/10.png Binary files differindex 997bbc8246..997bbc8246 100644 --- a/railties/guides/assets/images/icons/callouts/10.png +++ b/guides/assets/images/icons/callouts/10.png diff --git a/railties/guides/assets/images/icons/callouts/11.png b/guides/assets/images/icons/callouts/11.png Binary files differindex ce47dac3f5..ce47dac3f5 100644 --- a/railties/guides/assets/images/icons/callouts/11.png +++ b/guides/assets/images/icons/callouts/11.png diff --git a/railties/guides/assets/images/icons/callouts/12.png b/guides/assets/images/icons/callouts/12.png Binary files differindex 31daf4e2f2..31daf4e2f2 100644 --- a/railties/guides/assets/images/icons/callouts/12.png +++ b/guides/assets/images/icons/callouts/12.png diff --git a/railties/guides/assets/images/icons/callouts/13.png b/guides/assets/images/icons/callouts/13.png Binary files differindex 14021a89c2..14021a89c2 100644 --- a/railties/guides/assets/images/icons/callouts/13.png +++ b/guides/assets/images/icons/callouts/13.png diff --git a/railties/guides/assets/images/icons/callouts/14.png b/guides/assets/images/icons/callouts/14.png Binary files differindex 64014b75fe..64014b75fe 100644 --- a/railties/guides/assets/images/icons/callouts/14.png +++ b/guides/assets/images/icons/callouts/14.png diff --git a/railties/guides/assets/images/icons/callouts/15.png b/guides/assets/images/icons/callouts/15.png Binary files differindex 0d65765fcf..0d65765fcf 100644 --- a/railties/guides/assets/images/icons/callouts/15.png +++ b/guides/assets/images/icons/callouts/15.png diff --git a/railties/guides/assets/images/icons/callouts/2.png b/guides/assets/images/icons/callouts/2.png Binary files differindex 5d09341b2f..5d09341b2f 100644 --- a/railties/guides/assets/images/icons/callouts/2.png +++ b/guides/assets/images/icons/callouts/2.png diff --git a/railties/guides/assets/images/icons/callouts/3.png b/guides/assets/images/icons/callouts/3.png Binary files differindex ef7b700471..ef7b700471 100644 --- a/railties/guides/assets/images/icons/callouts/3.png +++ b/guides/assets/images/icons/callouts/3.png diff --git a/railties/guides/assets/images/icons/callouts/4.png b/guides/assets/images/icons/callouts/4.png Binary files differindex adb8364eb5..adb8364eb5 100644 --- a/railties/guides/assets/images/icons/callouts/4.png +++ b/guides/assets/images/icons/callouts/4.png diff --git a/railties/guides/assets/images/icons/callouts/5.png b/guides/assets/images/icons/callouts/5.png Binary files differindex 4d7eb46002..4d7eb46002 100644 --- a/railties/guides/assets/images/icons/callouts/5.png +++ b/guides/assets/images/icons/callouts/5.png diff --git a/railties/guides/assets/images/icons/callouts/6.png b/guides/assets/images/icons/callouts/6.png Binary files differindex 0ba694af6c..0ba694af6c 100644 --- a/railties/guides/assets/images/icons/callouts/6.png +++ b/guides/assets/images/icons/callouts/6.png diff --git a/railties/guides/assets/images/icons/callouts/7.png b/guides/assets/images/icons/callouts/7.png Binary files differindex 472e96f8ac..472e96f8ac 100644 --- a/railties/guides/assets/images/icons/callouts/7.png +++ b/guides/assets/images/icons/callouts/7.png diff --git a/railties/guides/assets/images/icons/callouts/8.png b/guides/assets/images/icons/callouts/8.png Binary files differindex 5e60973c21..5e60973c21 100644 --- a/railties/guides/assets/images/icons/callouts/8.png +++ b/guides/assets/images/icons/callouts/8.png diff --git a/railties/guides/assets/images/icons/callouts/9.png b/guides/assets/images/icons/callouts/9.png Binary files differindex a0676d26cc..a0676d26cc 100644 --- a/railties/guides/assets/images/icons/callouts/9.png +++ b/guides/assets/images/icons/callouts/9.png diff --git a/railties/guides/assets/images/icons/caution.png b/guides/assets/images/icons/caution.png Binary files differindex cb9d5ea0df..cb9d5ea0df 100644 --- a/railties/guides/assets/images/icons/caution.png +++ b/guides/assets/images/icons/caution.png diff --git a/railties/guides/assets/images/icons/example.png b/guides/assets/images/icons/example.png Binary files differindex bba1c0010d..bba1c0010d 100644 --- a/railties/guides/assets/images/icons/example.png +++ b/guides/assets/images/icons/example.png diff --git a/railties/guides/assets/images/icons/home.png b/guides/assets/images/icons/home.png Binary files differindex 37a5231bac..37a5231bac 100644 --- a/railties/guides/assets/images/icons/home.png +++ b/guides/assets/images/icons/home.png diff --git a/railties/guides/assets/images/icons/important.png b/guides/assets/images/icons/important.png Binary files differindex 1096c23295..1096c23295 100644 --- a/railties/guides/assets/images/icons/important.png +++ b/guides/assets/images/icons/important.png diff --git a/railties/guides/assets/images/icons/next.png b/guides/assets/images/icons/next.png Binary files differindex 64e126bdda..64e126bdda 100644 --- a/railties/guides/assets/images/icons/next.png +++ b/guides/assets/images/icons/next.png diff --git a/railties/guides/assets/images/icons/note.png b/guides/assets/images/icons/note.png Binary files differindex 841820f7c4..841820f7c4 100644 --- a/railties/guides/assets/images/icons/note.png +++ b/guides/assets/images/icons/note.png diff --git a/railties/guides/assets/images/icons/prev.png b/guides/assets/images/icons/prev.png Binary files differindex 3e8f12fe24..3e8f12fe24 100644 --- a/railties/guides/assets/images/icons/prev.png +++ b/guides/assets/images/icons/prev.png diff --git a/railties/guides/assets/images/icons/tip.png b/guides/assets/images/icons/tip.png Binary files differindex a3a029d898..a3a029d898 100644 --- a/railties/guides/assets/images/icons/tip.png +++ b/guides/assets/images/icons/tip.png diff --git a/railties/guides/assets/images/icons/up.png b/guides/assets/images/icons/up.png Binary files differindex 2db1ce62fa..2db1ce62fa 100644 --- a/railties/guides/assets/images/icons/up.png +++ b/guides/assets/images/icons/up.png diff --git a/railties/guides/assets/images/icons/warning.png b/guides/assets/images/icons/warning.png Binary files differindex 0b0c419df2..0b0c419df2 100644 --- a/railties/guides/assets/images/icons/warning.png +++ b/guides/assets/images/icons/warning.png diff --git a/railties/guides/assets/images/jaimeiniesta.jpg b/guides/assets/images/jaimeiniesta.jpg Binary files differindex 445f048d92..445f048d92 100644 --- a/railties/guides/assets/images/jaimeiniesta.jpg +++ b/guides/assets/images/jaimeiniesta.jpg diff --git a/railties/guides/assets/images/nav_arrow.gif b/guides/assets/images/nav_arrow.gif Binary files differindex c4f57658d7..c4f57658d7 100644 --- a/railties/guides/assets/images/nav_arrow.gif +++ b/guides/assets/images/nav_arrow.gif diff --git a/railties/guides/assets/images/polymorphic.png b/guides/assets/images/polymorphic.png Binary files differindex ff2fd9f76d..ff2fd9f76d 100644 --- a/railties/guides/assets/images/polymorphic.png +++ b/guides/assets/images/polymorphic.png diff --git a/railties/guides/assets/images/posts_index.png b/guides/assets/images/posts_index.png Binary files differindex f6cd2f9b80..f6cd2f9b80 100644 --- a/railties/guides/assets/images/posts_index.png +++ b/guides/assets/images/posts_index.png diff --git a/railties/guides/assets/images/radar.png b/guides/assets/images/radar.png Binary files differindex f61e08763f..f61e08763f 100644 --- a/railties/guides/assets/images/radar.png +++ b/guides/assets/images/radar.png diff --git a/railties/guides/assets/images/rails_guides_kindle_cover.jpg b/guides/assets/images/rails_guides_kindle_cover.jpg Binary files differindex 9eb16720a9..9eb16720a9 100644 --- a/railties/guides/assets/images/rails_guides_kindle_cover.jpg +++ b/guides/assets/images/rails_guides_kindle_cover.jpg diff --git a/railties/guides/assets/images/rails_guides_logo.gif b/guides/assets/images/rails_guides_logo.gif Binary files differindex a24683a34e..a24683a34e 100644 --- a/railties/guides/assets/images/rails_guides_logo.gif +++ b/guides/assets/images/rails_guides_logo.gif diff --git a/railties/guides/assets/images/rails_logo_remix.gif b/guides/assets/images/rails_logo_remix.gif Binary files differindex 58960ee4f9..58960ee4f9 100644 --- a/railties/guides/assets/images/rails_logo_remix.gif +++ b/guides/assets/images/rails_logo_remix.gif diff --git a/railties/guides/assets/images/rails_welcome.png b/guides/assets/images/rails_welcome.png Binary files differindex f2aa210d19..f2aa210d19 100644 --- a/railties/guides/assets/images/rails_welcome.png +++ b/guides/assets/images/rails_welcome.png diff --git a/railties/guides/assets/images/session_fixation.png b/guides/assets/images/session_fixation.png Binary files differindex 6b084508db..6b084508db 100644 --- a/railties/guides/assets/images/session_fixation.png +++ b/guides/assets/images/session_fixation.png diff --git a/railties/guides/assets/images/tab_grey.gif b/guides/assets/images/tab_grey.gif Binary files differindex e9680b7136..e9680b7136 100644 --- a/railties/guides/assets/images/tab_grey.gif +++ b/guides/assets/images/tab_grey.gif diff --git a/railties/guides/assets/images/tab_info.gif b/guides/assets/images/tab_info.gif Binary files differindex 458fea9a61..458fea9a61 100644 --- a/railties/guides/assets/images/tab_info.gif +++ b/guides/assets/images/tab_info.gif diff --git a/railties/guides/assets/images/tab_note.gif b/guides/assets/images/tab_note.gif Binary files differindex 1d5c171ed6..1d5c171ed6 100644 --- a/railties/guides/assets/images/tab_note.gif +++ b/guides/assets/images/tab_note.gif diff --git a/railties/guides/assets/images/tab_red.gif b/guides/assets/images/tab_red.gif Binary files differindex daf140b5a8..daf140b5a8 100644 --- a/railties/guides/assets/images/tab_red.gif +++ b/guides/assets/images/tab_red.gif diff --git a/railties/guides/assets/images/tab_yellow.gif b/guides/assets/images/tab_yellow.gif Binary files differindex dc961c99dd..dc961c99dd 100644 --- a/railties/guides/assets/images/tab_yellow.gif +++ b/guides/assets/images/tab_yellow.gif diff --git a/railties/guides/assets/images/tab_yellow.png b/guides/assets/images/tab_yellow.png Binary files differindex cceea6581f..cceea6581f 100644 --- a/railties/guides/assets/images/tab_yellow.png +++ b/guides/assets/images/tab_yellow.png diff --git a/railties/guides/assets/images/validation_error_messages.png b/guides/assets/images/validation_error_messages.png Binary files differindex 622d35da5d..622d35da5d 100644 --- a/railties/guides/assets/images/validation_error_messages.png +++ b/guides/assets/images/validation_error_messages.png diff --git a/railties/guides/assets/images/vijaydev.jpg b/guides/assets/images/vijaydev.jpg Binary files differindex e21d3cabfc..e21d3cabfc 100644 --- a/railties/guides/assets/images/vijaydev.jpg +++ b/guides/assets/images/vijaydev.jpg diff --git a/railties/guides/assets/javascripts/guides.js b/guides/assets/javascripts/guides.js index c4e4d459ea..c4e4d459ea 100644 --- a/railties/guides/assets/javascripts/guides.js +++ b/guides/assets/javascripts/guides.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js b/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js index 8aa3ed2732..8aa3ed2732 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js b/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js index d40bbd7dd2..d40bbd7dd2 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js b/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js index 8c296969ff..8c296969ff 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js b/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js index 079214efe1..079214efe1 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js b/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js index 627dbb9b76..627dbb9b76 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js b/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js index 9f70d3aed6..9f70d3aed6 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js b/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js index 4297a9a648..4297a9a648 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js b/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js index e1060d4468..e1060d4468 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js b/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js index e9b14fc580..e9b14fc580 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js b/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js index 6ba7d9da87..6ba7d9da87 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js b/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js index 6ec5c18521..6ec5c18521 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js b/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js index ff98daba16..ff98daba16 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js b/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js index d692fd6382..d692fd6382 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js b/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js index 1a150a6ad3..1a150a6ad3 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js b/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js index d94a2e0ec5..d94a2e0ec5 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js b/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js index 95e6e4325b..95e6e4325b 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js b/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js index 9f7d9e90c3..9f7d9e90c3 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js b/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js index 0be1752968..0be1752968 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js b/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js index ce77462975..ce77462975 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js b/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js index ff82130a7a..ff82130a7a 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js b/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js index aa04da0996..aa04da0996 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js b/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js index 4b0b6f04d2..4b0b6f04d2 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js b/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js index 5c2cd8806f..5c2cd8806f 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js b/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js index be845dc0b3..be845dc0b3 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js b/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js index 69d9fd0b1f..69d9fd0b1f 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +++ b/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js diff --git a/railties/guides/assets/javascripts/syntaxhighlighter/shCore.js b/guides/assets/javascripts/syntaxhighlighter/shCore.js index b47b645472..b47b645472 100644 --- a/railties/guides/assets/javascripts/syntaxhighlighter/shCore.js +++ b/guides/assets/javascripts/syntaxhighlighter/shCore.js diff --git a/railties/guides/assets/stylesheets/fixes.css b/guides/assets/stylesheets/fixes.css index bf86b29efa..bf86b29efa 100644 --- a/railties/guides/assets/stylesheets/fixes.css +++ b/guides/assets/stylesheets/fixes.css diff --git a/railties/guides/assets/stylesheets/kindle.css b/guides/assets/stylesheets/kindle.css index b26cd1786a..b26cd1786a 100644 --- a/railties/guides/assets/stylesheets/kindle.css +++ b/guides/assets/stylesheets/kindle.css diff --git a/railties/guides/assets/stylesheets/main.css b/guides/assets/stylesheets/main.css index 90723cc8e1..42b85fefa3 100644 --- a/railties/guides/assets/stylesheets/main.css +++ b/guides/assets/stylesheets/main.css @@ -80,7 +80,7 @@ body { font-family: Helvetica, Arial, sans-serif; font-size: 87.5%; line-height: 1.5em; - background: #222; + background: #fff; min-width: 69em; color: #999; } @@ -94,6 +94,7 @@ body { #topNav { padding: 1em 0; color: #565656; + background: #222; } #header { @@ -111,7 +112,6 @@ body { } #container { - background: #FFF; color: #333; padding: 0.5em 0 1.5em 0; } @@ -137,7 +137,7 @@ body { #footer { padding: 2em 0; - background: url(../images/footer_tile.gif) repeat-x; + background: #222 url(../images/footer_tile.gif) repeat-x; } #footer .wrapper { padding-left: 2em; diff --git a/railties/guides/assets/stylesheets/print.css b/guides/assets/stylesheets/print.css index 628da105d4..628da105d4 100644 --- a/railties/guides/assets/stylesheets/print.css +++ b/guides/assets/stylesheets/print.css diff --git a/railties/guides/assets/stylesheets/reset.css b/guides/assets/stylesheets/reset.css index cb14fbcc55..cb14fbcc55 100644 --- a/railties/guides/assets/stylesheets/reset.css +++ b/guides/assets/stylesheets/reset.css diff --git a/railties/guides/assets/stylesheets/style.css b/guides/assets/stylesheets/style.css index 89b2ab885a..89b2ab885a 100644 --- a/railties/guides/assets/stylesheets/style.css +++ b/guides/assets/stylesheets/style.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCore.css b/guides/assets/stylesheets/syntaxhighlighter/shCore.css index 34f6864a15..34f6864a15 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCore.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCore.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css index 08f9e10e4e..08f9e10e4e 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css index 1db1f70cb0..1db1f70cb0 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css index a45de9fd8e..a45de9fd8e 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css index 706c77a0a8..706c77a0a8 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css index 6101eba51f..6101eba51f 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css index 2923ce7367..2923ce7367 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css index e3733eed56..e3733eed56 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css b/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css index d09368384d..d09368384d 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css index 136541172d..136541172d 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css index d8b4313433..d8b4313433 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css index 77377d9533..77377d9533 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css index dae5053fea..dae5053fea 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css index 8fbd871fb5..8fbd871fb5 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css index f4db39cd83..f4db39cd83 100755 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css index c49563cc9d..c49563cc9d 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css index 6305a10e4e..6305a10e4e 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css diff --git a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css b/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css index 6d2edb2eb8..6d2edb2eb8 100644 --- a/railties/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +++ b/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css diff --git a/railties/guides/code/getting_started/Gemfile b/guides/code/getting_started/Gemfile index 768985070c..768985070c 100644 --- a/railties/guides/code/getting_started/Gemfile +++ b/guides/code/getting_started/Gemfile diff --git a/railties/guides/code/getting_started/README.rdoc b/guides/code/getting_started/README.rdoc index d2014bd35f..d2014bd35f 100644 --- a/railties/guides/code/getting_started/README.rdoc +++ b/guides/code/getting_started/README.rdoc diff --git a/railties/guides/code/getting_started/Rakefile b/guides/code/getting_started/Rakefile index e1d1ec8615..e1d1ec8615 100644 --- a/railties/guides/code/getting_started/Rakefile +++ b/guides/code/getting_started/Rakefile diff --git a/railties/guides/code/getting_started/app/assets/images/rails.png b/guides/code/getting_started/app/assets/images/rails.png Binary files differindex d5edc04e65..d5edc04e65 100644 --- a/railties/guides/code/getting_started/app/assets/images/rails.png +++ b/guides/code/getting_started/app/assets/images/rails.png diff --git a/railties/guides/code/getting_started/app/assets/javascripts/application.js b/guides/code/getting_started/app/assets/javascripts/application.js index 9097d830e2..9097d830e2 100644 --- a/railties/guides/code/getting_started/app/assets/javascripts/application.js +++ b/guides/code/getting_started/app/assets/javascripts/application.js diff --git a/railties/guides/code/getting_started/app/assets/javascripts/comments.js.coffee b/guides/code/getting_started/app/assets/javascripts/comments.js.coffee index 761567942f..761567942f 100644 --- a/railties/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +++ b/guides/code/getting_started/app/assets/javascripts/comments.js.coffee diff --git a/railties/guides/code/getting_started/app/assets/javascripts/home.js.coffee b/guides/code/getting_started/app/assets/javascripts/home.js.coffee index 761567942f..761567942f 100644 --- a/railties/guides/code/getting_started/app/assets/javascripts/home.js.coffee +++ b/guides/code/getting_started/app/assets/javascripts/home.js.coffee diff --git a/railties/guides/code/getting_started/app/assets/javascripts/posts.js.coffee b/guides/code/getting_started/app/assets/javascripts/posts.js.coffee index 761567942f..761567942f 100644 --- a/railties/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +++ b/guides/code/getting_started/app/assets/javascripts/posts.js.coffee diff --git a/railties/guides/code/getting_started/app/assets/stylesheets/application.css b/guides/code/getting_started/app/assets/stylesheets/application.css index 3b5cc6648e..3b5cc6648e 100644 --- a/railties/guides/code/getting_started/app/assets/stylesheets/application.css +++ b/guides/code/getting_started/app/assets/stylesheets/application.css diff --git a/railties/guides/code/getting_started/app/assets/stylesheets/comments.css.scss b/guides/code/getting_started/app/assets/stylesheets/comments.css.scss index e730912783..e730912783 100644 --- a/railties/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +++ b/guides/code/getting_started/app/assets/stylesheets/comments.css.scss diff --git a/railties/guides/code/getting_started/app/assets/stylesheets/home.css.scss b/guides/code/getting_started/app/assets/stylesheets/home.css.scss index f0ddc6846a..f0ddc6846a 100644 --- a/railties/guides/code/getting_started/app/assets/stylesheets/home.css.scss +++ b/guides/code/getting_started/app/assets/stylesheets/home.css.scss diff --git a/railties/guides/code/getting_started/app/assets/stylesheets/posts.css.scss b/guides/code/getting_started/app/assets/stylesheets/posts.css.scss index ed4dfd10f2..ed4dfd10f2 100644 --- a/railties/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +++ b/guides/code/getting_started/app/assets/stylesheets/posts.css.scss diff --git a/railties/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss b/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss index 05188f08ed..05188f08ed 100644 --- a/railties/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +++ b/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss diff --git a/railties/guides/code/getting_started/app/controllers/application_controller.rb b/guides/code/getting_started/app/controllers/application_controller.rb index e8065d9505..e8065d9505 100644 --- a/railties/guides/code/getting_started/app/controllers/application_controller.rb +++ b/guides/code/getting_started/app/controllers/application_controller.rb diff --git a/railties/guides/code/getting_started/app/controllers/comments_controller.rb b/guides/code/getting_started/app/controllers/comments_controller.rb index 7447fd078b..7447fd078b 100644 --- a/railties/guides/code/getting_started/app/controllers/comments_controller.rb +++ b/guides/code/getting_started/app/controllers/comments_controller.rb diff --git a/railties/guides/code/getting_started/app/controllers/home_controller.rb b/guides/code/getting_started/app/controllers/home_controller.rb index 6cc31c1ca3..6cc31c1ca3 100644 --- a/railties/guides/code/getting_started/app/controllers/home_controller.rb +++ b/guides/code/getting_started/app/controllers/home_controller.rb diff --git a/railties/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb index 1581d4eb16..1581d4eb16 100644 --- a/railties/guides/code/getting_started/app/controllers/posts_controller.rb +++ b/guides/code/getting_started/app/controllers/posts_controller.rb diff --git a/railties/guides/code/getting_started/app/helpers/application_helper.rb b/guides/code/getting_started/app/helpers/application_helper.rb index de6be7945c..de6be7945c 100644 --- a/railties/guides/code/getting_started/app/helpers/application_helper.rb +++ b/guides/code/getting_started/app/helpers/application_helper.rb diff --git a/railties/guides/code/getting_started/app/helpers/comments_helper.rb b/guides/code/getting_started/app/helpers/comments_helper.rb index 0ec9ca5f2d..0ec9ca5f2d 100644 --- a/railties/guides/code/getting_started/app/helpers/comments_helper.rb +++ b/guides/code/getting_started/app/helpers/comments_helper.rb diff --git a/railties/guides/code/getting_started/app/helpers/home_helper.rb b/guides/code/getting_started/app/helpers/home_helper.rb index 23de56ac60..23de56ac60 100644 --- a/railties/guides/code/getting_started/app/helpers/home_helper.rb +++ b/guides/code/getting_started/app/helpers/home_helper.rb diff --git a/railties/guides/code/getting_started/app/helpers/posts_helper.rb b/guides/code/getting_started/app/helpers/posts_helper.rb index b6e8e67894..b6e8e67894 100644 --- a/railties/guides/code/getting_started/app/helpers/posts_helper.rb +++ b/guides/code/getting_started/app/helpers/posts_helper.rb diff --git a/railties/guides/code/getting_started/app/mailers/.gitkeep b/guides/code/getting_started/app/mailers/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/app/mailers/.gitkeep +++ b/guides/code/getting_started/app/mailers/.gitkeep diff --git a/railties/guides/code/getting_started/app/models/.gitkeep b/guides/code/getting_started/app/models/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/app/models/.gitkeep +++ b/guides/code/getting_started/app/models/.gitkeep diff --git a/railties/guides/code/getting_started/app/models/comment.rb b/guides/code/getting_started/app/models/comment.rb index 4e76c5b5b0..4e76c5b5b0 100644 --- a/railties/guides/code/getting_started/app/models/comment.rb +++ b/guides/code/getting_started/app/models/comment.rb diff --git a/railties/guides/code/getting_started/app/models/post.rb b/guides/code/getting_started/app/models/post.rb index 61c2b5ae44..61c2b5ae44 100644 --- a/railties/guides/code/getting_started/app/models/post.rb +++ b/guides/code/getting_started/app/models/post.rb diff --git a/railties/guides/code/getting_started/app/models/tag.rb b/guides/code/getting_started/app/models/tag.rb index 30992e8ba9..30992e8ba9 100644 --- a/railties/guides/code/getting_started/app/models/tag.rb +++ b/guides/code/getting_started/app/models/tag.rb diff --git a/railties/guides/code/getting_started/app/views/comments/_comment.html.erb b/guides/code/getting_started/app/views/comments/_comment.html.erb index 4c3fbf26cd..4c3fbf26cd 100644 --- a/railties/guides/code/getting_started/app/views/comments/_comment.html.erb +++ b/guides/code/getting_started/app/views/comments/_comment.html.erb diff --git a/railties/guides/code/getting_started/app/views/comments/_form.html.erb b/guides/code/getting_started/app/views/comments/_form.html.erb index d15bdd6b59..d15bdd6b59 100644 --- a/railties/guides/code/getting_started/app/views/comments/_form.html.erb +++ b/guides/code/getting_started/app/views/comments/_form.html.erb diff --git a/railties/guides/code/getting_started/app/views/home/index.html.erb b/guides/code/getting_started/app/views/home/index.html.erb index bb4f3dcd1f..bb4f3dcd1f 100644 --- a/railties/guides/code/getting_started/app/views/home/index.html.erb +++ b/guides/code/getting_started/app/views/home/index.html.erb diff --git a/railties/guides/code/getting_started/app/views/layouts/application.html.erb b/guides/code/getting_started/app/views/layouts/application.html.erb index 6578a41da2..6578a41da2 100644 --- a/railties/guides/code/getting_started/app/views/layouts/application.html.erb +++ b/guides/code/getting_started/app/views/layouts/application.html.erb diff --git a/railties/guides/code/getting_started/app/views/posts/_form.html.erb b/guides/code/getting_started/app/views/posts/_form.html.erb index e27da7f413..e27da7f413 100644 --- a/railties/guides/code/getting_started/app/views/posts/_form.html.erb +++ b/guides/code/getting_started/app/views/posts/_form.html.erb diff --git a/railties/guides/code/getting_started/app/views/posts/edit.html.erb b/guides/code/getting_started/app/views/posts/edit.html.erb index 720580236b..720580236b 100644 --- a/railties/guides/code/getting_started/app/views/posts/edit.html.erb +++ b/guides/code/getting_started/app/views/posts/edit.html.erb diff --git a/railties/guides/code/getting_started/app/views/posts/index.html.erb b/guides/code/getting_started/app/views/posts/index.html.erb index 45dee1b25f..45dee1b25f 100644 --- a/railties/guides/code/getting_started/app/views/posts/index.html.erb +++ b/guides/code/getting_started/app/views/posts/index.html.erb diff --git a/railties/guides/code/getting_started/app/views/posts/new.html.erb b/guides/code/getting_started/app/views/posts/new.html.erb index 36ad7421f9..36ad7421f9 100644 --- a/railties/guides/code/getting_started/app/views/posts/new.html.erb +++ b/guides/code/getting_started/app/views/posts/new.html.erb diff --git a/railties/guides/code/getting_started/app/views/posts/show.html.erb b/guides/code/getting_started/app/views/posts/show.html.erb index da78a9527b..da78a9527b 100644 --- a/railties/guides/code/getting_started/app/views/posts/show.html.erb +++ b/guides/code/getting_started/app/views/posts/show.html.erb diff --git a/railties/guides/code/getting_started/app/views/tags/_form.html.erb b/guides/code/getting_started/app/views/tags/_form.html.erb index 7e424b0e20..7e424b0e20 100644 --- a/railties/guides/code/getting_started/app/views/tags/_form.html.erb +++ b/guides/code/getting_started/app/views/tags/_form.html.erb diff --git a/railties/guides/code/getting_started/config.ru b/guides/code/getting_started/config.ru index ddf869e921..ddf869e921 100644 --- a/railties/guides/code/getting_started/config.ru +++ b/guides/code/getting_started/config.ru diff --git a/railties/guides/code/getting_started/config/application.rb b/guides/code/getting_started/config/application.rb index d2cd5c028b..d2cd5c028b 100644 --- a/railties/guides/code/getting_started/config/application.rb +++ b/guides/code/getting_started/config/application.rb diff --git a/railties/guides/code/getting_started/config/boot.rb b/guides/code/getting_started/config/boot.rb index 4489e58688..4489e58688 100644 --- a/railties/guides/code/getting_started/config/boot.rb +++ b/guides/code/getting_started/config/boot.rb diff --git a/railties/guides/code/getting_started/config/database.yml b/guides/code/getting_started/config/database.yml index 32a998ad72..51a4dd459d 100644 --- a/railties/guides/code/getting_started/config/database.yml +++ b/guides/code/getting_started/config/database.yml @@ -6,9 +6,7 @@ development: adapter: sqlite3 database: db/development.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 timeout: 5000 # Warning: The database defined as "test" will be erased and @@ -17,15 +15,11 @@ development: test: adapter: sqlite3 database: db/test.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 timeout: 5000 diff --git a/railties/guides/code/getting_started/config/environment.rb b/guides/code/getting_started/config/environment.rb index 8f728b7ce7..8f728b7ce7 100644 --- a/railties/guides/code/getting_started/config/environment.rb +++ b/guides/code/getting_started/config/environment.rb diff --git a/railties/guides/code/getting_started/config/environments/development.rb b/guides/code/getting_started/config/environments/development.rb index cec2b20c0b..cec2b20c0b 100644 --- a/railties/guides/code/getting_started/config/environments/development.rb +++ b/guides/code/getting_started/config/environments/development.rb diff --git a/railties/guides/code/getting_started/config/environments/production.rb b/guides/code/getting_started/config/environments/production.rb index cfb8c960d6..ecc35b030b 100644 --- a/railties/guides/code/getting_started/config/environments/production.rb +++ b/guides/code/getting_started/config/environments/production.rb @@ -20,7 +20,7 @@ Blog::Application.configure do # Generate digests for assets URLs. config.assets.digest = true - # Defaults to Rails.root.join("public/assets"). + # Defaults to nil # config.assets.manifest = YOUR_PATH # Specifies the header that your server uses for sending files. diff --git a/railties/guides/code/getting_started/config/environments/test.rb b/guides/code/getting_started/config/environments/test.rb index f2bc932fb3..f2bc932fb3 100644 --- a/railties/guides/code/getting_started/config/environments/test.rb +++ b/guides/code/getting_started/config/environments/test.rb diff --git a/railties/guides/code/getting_started/config/initializers/backtrace_silencers.rb b/guides/code/getting_started/config/initializers/backtrace_silencers.rb index 59385cdf37..59385cdf37 100644 --- a/railties/guides/code/getting_started/config/initializers/backtrace_silencers.rb +++ b/guides/code/getting_started/config/initializers/backtrace_silencers.rb diff --git a/railties/guides/code/getting_started/config/initializers/inflections.rb b/guides/code/getting_started/config/initializers/inflections.rb index 5d8d9be237..5d8d9be237 100644 --- a/railties/guides/code/getting_started/config/initializers/inflections.rb +++ b/guides/code/getting_started/config/initializers/inflections.rb diff --git a/railties/guides/code/getting_started/config/initializers/mime_types.rb b/guides/code/getting_started/config/initializers/mime_types.rb index 72aca7e441..72aca7e441 100644 --- a/railties/guides/code/getting_started/config/initializers/mime_types.rb +++ b/guides/code/getting_started/config/initializers/mime_types.rb diff --git a/railties/guides/code/getting_started/config/initializers/secret_token.rb b/guides/code/getting_started/config/initializers/secret_token.rb index b0c8ee23c1..f36ebdda18 100644 --- a/railties/guides/code/getting_started/config/initializers/secret_token.rb +++ b/guides/code/getting_started/config/initializers/secret_token.rb @@ -4,4 +4,6 @@ # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. +# Make sure your secret key is kept private +# if you're sharing your code publicly. Blog::Application.config.secret_token = '685a9bf865b728c6549a191c90851c1b5ec41ecb60b9e94ad79dd3f824749798aa7b5e94431901960bee57809db0947b481570f7f13376b7ca190fa28099c459' diff --git a/railties/guides/code/getting_started/config/initializers/session_store.rb b/guides/code/getting_started/config/initializers/session_store.rb index 1a67af58b5..1a67af58b5 100644 --- a/railties/guides/code/getting_started/config/initializers/session_store.rb +++ b/guides/code/getting_started/config/initializers/session_store.rb diff --git a/railties/guides/code/getting_started/config/initializers/wrap_parameters.rb b/guides/code/getting_started/config/initializers/wrap_parameters.rb index 999df20181..999df20181 100644 --- a/railties/guides/code/getting_started/config/initializers/wrap_parameters.rb +++ b/guides/code/getting_started/config/initializers/wrap_parameters.rb diff --git a/railties/guides/code/getting_started/config/locales/en.yml b/guides/code/getting_started/config/locales/en.yml index 179c14ca52..179c14ca52 100644 --- a/railties/guides/code/getting_started/config/locales/en.yml +++ b/guides/code/getting_started/config/locales/en.yml diff --git a/railties/guides/code/getting_started/config/routes.rb b/guides/code/getting_started/config/routes.rb index b048ac68f1..b048ac68f1 100644 --- a/railties/guides/code/getting_started/config/routes.rb +++ b/guides/code/getting_started/config/routes.rb diff --git a/railties/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb b/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb index d45a961523..d45a961523 100644 --- a/railties/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +++ b/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb diff --git a/railties/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb b/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb index adda8078c1..adda8078c1 100644 --- a/railties/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +++ b/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb diff --git a/railties/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb b/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb index cf95b1c3d0..cf95b1c3d0 100644 --- a/railties/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +++ b/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb diff --git a/railties/guides/code/getting_started/db/schema.rb b/guides/code/getting_started/db/schema.rb index 9db4fbe4b6..9db4fbe4b6 100644 --- a/railties/guides/code/getting_started/db/schema.rb +++ b/guides/code/getting_started/db/schema.rb diff --git a/railties/guides/code/getting_started/db/seeds.rb b/guides/code/getting_started/db/seeds.rb index 4edb1e857e..4edb1e857e 100644 --- a/railties/guides/code/getting_started/db/seeds.rb +++ b/guides/code/getting_started/db/seeds.rb diff --git a/railties/guides/code/getting_started/doc/README_FOR_APP b/guides/code/getting_started/doc/README_FOR_APP index fe41f5cc24..fe41f5cc24 100644 --- a/railties/guides/code/getting_started/doc/README_FOR_APP +++ b/guides/code/getting_started/doc/README_FOR_APP diff --git a/railties/guides/code/getting_started/lib/assets/.gitkeep b/guides/code/getting_started/lib/assets/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/lib/assets/.gitkeep +++ b/guides/code/getting_started/lib/assets/.gitkeep diff --git a/railties/guides/code/getting_started/lib/tasks/.gitkeep b/guides/code/getting_started/lib/tasks/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/lib/tasks/.gitkeep +++ b/guides/code/getting_started/lib/tasks/.gitkeep diff --git a/railties/guides/code/getting_started/public/404.html b/guides/code/getting_started/public/404.html index 9a48320a5f..9a48320a5f 100644 --- a/railties/guides/code/getting_started/public/404.html +++ b/guides/code/getting_started/public/404.html diff --git a/railties/guides/code/getting_started/public/422.html b/guides/code/getting_started/public/422.html index 83660ab187..83660ab187 100644 --- a/railties/guides/code/getting_started/public/422.html +++ b/guides/code/getting_started/public/422.html diff --git a/railties/guides/code/getting_started/public/500.html b/guides/code/getting_started/public/500.html index f3648a0dbc..f3648a0dbc 100644 --- a/railties/guides/code/getting_started/public/500.html +++ b/guides/code/getting_started/public/500.html diff --git a/railties/guides/code/getting_started/public/favicon.ico b/guides/code/getting_started/public/favicon.ico index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/public/favicon.ico +++ b/guides/code/getting_started/public/favicon.ico diff --git a/railties/guides/code/getting_started/public/robots.txt b/guides/code/getting_started/public/robots.txt index 085187fa58..085187fa58 100644 --- a/railties/guides/code/getting_started/public/robots.txt +++ b/guides/code/getting_started/public/robots.txt diff --git a/railties/guides/code/getting_started/script/rails b/guides/code/getting_started/script/rails index f8da2cffd4..f8da2cffd4 100755 --- a/railties/guides/code/getting_started/script/rails +++ b/guides/code/getting_started/script/rails diff --git a/railties/guides/code/getting_started/test/fixtures/.gitkeep b/guides/code/getting_started/test/fixtures/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/test/fixtures/.gitkeep +++ b/guides/code/getting_started/test/fixtures/.gitkeep diff --git a/railties/guides/code/getting_started/test/fixtures/comments.yml b/guides/code/getting_started/test/fixtures/comments.yml index d33da386bf..d33da386bf 100644 --- a/railties/guides/code/getting_started/test/fixtures/comments.yml +++ b/guides/code/getting_started/test/fixtures/comments.yml diff --git a/railties/guides/code/getting_started/test/fixtures/posts.yml b/guides/code/getting_started/test/fixtures/posts.yml index 8b0f75a33d..8b0f75a33d 100644 --- a/railties/guides/code/getting_started/test/fixtures/posts.yml +++ b/guides/code/getting_started/test/fixtures/posts.yml diff --git a/railties/guides/code/getting_started/test/fixtures/tags.yml b/guides/code/getting_started/test/fixtures/tags.yml index 8485668908..8485668908 100644 --- a/railties/guides/code/getting_started/test/fixtures/tags.yml +++ b/guides/code/getting_started/test/fixtures/tags.yml diff --git a/railties/guides/code/getting_started/test/functional/.gitkeep b/guides/code/getting_started/test/functional/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/test/functional/.gitkeep +++ b/guides/code/getting_started/test/functional/.gitkeep diff --git a/railties/guides/code/getting_started/test/functional/comments_controller_test.rb b/guides/code/getting_started/test/functional/comments_controller_test.rb index 2ec71b4ec5..2ec71b4ec5 100644 --- a/railties/guides/code/getting_started/test/functional/comments_controller_test.rb +++ b/guides/code/getting_started/test/functional/comments_controller_test.rb diff --git a/railties/guides/code/getting_started/test/functional/home_controller_test.rb b/guides/code/getting_started/test/functional/home_controller_test.rb index 0d9bb47c3e..0d9bb47c3e 100644 --- a/railties/guides/code/getting_started/test/functional/home_controller_test.rb +++ b/guides/code/getting_started/test/functional/home_controller_test.rb diff --git a/railties/guides/code/getting_started/test/functional/posts_controller_test.rb b/guides/code/getting_started/test/functional/posts_controller_test.rb index b8f7b07820..b8f7b07820 100644 --- a/railties/guides/code/getting_started/test/functional/posts_controller_test.rb +++ b/guides/code/getting_started/test/functional/posts_controller_test.rb diff --git a/railties/guides/code/getting_started/test/integration/.gitkeep b/guides/code/getting_started/test/integration/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/test/integration/.gitkeep +++ b/guides/code/getting_started/test/integration/.gitkeep diff --git a/railties/guides/code/getting_started/test/performance/browsing_test.rb b/guides/code/getting_started/test/performance/browsing_test.rb index 3fea27b916..3fea27b916 100644 --- a/railties/guides/code/getting_started/test/performance/browsing_test.rb +++ b/guides/code/getting_started/test/performance/browsing_test.rb diff --git a/railties/guides/code/getting_started/test/test_helper.rb b/guides/code/getting_started/test/test_helper.rb index 8bf1192ffe..8bf1192ffe 100644 --- a/railties/guides/code/getting_started/test/test_helper.rb +++ b/guides/code/getting_started/test/test_helper.rb diff --git a/railties/guides/code/getting_started/test/unit/.gitkeep b/guides/code/getting_started/test/unit/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/test/unit/.gitkeep +++ b/guides/code/getting_started/test/unit/.gitkeep diff --git a/railties/guides/code/getting_started/test/unit/comment_test.rb b/guides/code/getting_started/test/unit/comment_test.rb index b6d6131a96..b6d6131a96 100644 --- a/railties/guides/code/getting_started/test/unit/comment_test.rb +++ b/guides/code/getting_started/test/unit/comment_test.rb diff --git a/railties/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb b/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb index 2518c16bd5..2518c16bd5 100644 --- a/railties/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +++ b/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb diff --git a/railties/guides/code/getting_started/test/unit/helpers/home_helper_test.rb b/guides/code/getting_started/test/unit/helpers/home_helper_test.rb index 4740a18dac..4740a18dac 100644 --- a/railties/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +++ b/guides/code/getting_started/test/unit/helpers/home_helper_test.rb diff --git a/railties/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb b/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb index 48549c2ea1..48549c2ea1 100644 --- a/railties/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +++ b/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb diff --git a/railties/guides/code/getting_started/test/unit/post_test.rb b/guides/code/getting_started/test/unit/post_test.rb index 6d9d463a71..6d9d463a71 100644 --- a/railties/guides/code/getting_started/test/unit/post_test.rb +++ b/guides/code/getting_started/test/unit/post_test.rb diff --git a/railties/guides/code/getting_started/test/unit/tag_test.rb b/guides/code/getting_started/test/unit/tag_test.rb index b8498a117c..b8498a117c 100644 --- a/railties/guides/code/getting_started/test/unit/tag_test.rb +++ b/guides/code/getting_started/test/unit/tag_test.rb diff --git a/railties/guides/code/getting_started/vendor/assets/stylesheets/.gitkeep b/guides/code/getting_started/vendor/assets/stylesheets/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/vendor/assets/stylesheets/.gitkeep +++ b/guides/code/getting_started/vendor/assets/stylesheets/.gitkeep diff --git a/railties/guides/code/getting_started/vendor/plugins/.gitkeep b/guides/code/getting_started/vendor/plugins/.gitkeep index e69de29bb2..e69de29bb2 100644 --- a/railties/guides/code/getting_started/vendor/plugins/.gitkeep +++ b/guides/code/getting_started/vendor/plugins/.gitkeep diff --git a/railties/guides/rails_guides.rb b/guides/rails_guides.rb index feb5fe3937..e662ad2ed9 100644 --- a/railties/guides/rails_guides.rb +++ b/guides/rails_guides.rb @@ -13,8 +13,8 @@ require 'rubygems' begin # Guides generation in the Rails repo. - as_lib = File.join(pwd, "../../activesupport/lib") - ap_lib = File.join(pwd, "../../actionpack/lib") + as_lib = File.join(pwd, "../activesupport/lib") + ap_lib = File.join(pwd, "../actionpack/lib") $:.unshift as_lib if File.directory?(as_lib) $:.unshift ap_lib if File.directory?(ap_lib) diff --git a/railties/guides/rails_guides/generator.rb b/guides/rails_guides/generator.rb index d6a98f9ac4..d6a98f9ac4 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/guides/rails_guides/generator.rb diff --git a/railties/guides/rails_guides/helpers.rb b/guides/rails_guides/helpers.rb index e6ef656474..e6ef656474 100644 --- a/railties/guides/rails_guides/helpers.rb +++ b/guides/rails_guides/helpers.rb diff --git a/railties/guides/rails_guides/indexer.rb b/guides/rails_guides/indexer.rb index 89fbccbb1d..89fbccbb1d 100644 --- a/railties/guides/rails_guides/indexer.rb +++ b/guides/rails_guides/indexer.rb diff --git a/railties/guides/rails_guides/levenshtein.rb b/guides/rails_guides/levenshtein.rb index 489aa3ea7a..489aa3ea7a 100644 --- a/railties/guides/rails_guides/levenshtein.rb +++ b/guides/rails_guides/levenshtein.rb diff --git a/railties/guides/rails_guides/textile_extensions.rb b/guides/rails_guides/textile_extensions.rb index 4677fae504..4677fae504 100644 --- a/railties/guides/rails_guides/textile_extensions.rb +++ b/guides/rails_guides/textile_extensions.rb diff --git a/railties/guides/source/2_2_release_notes.textile b/guides/source/2_2_release_notes.textile index 8e2d528eee..3a0f2efbaf 100644 --- a/railties/guides/source/2_2_release_notes.textile +++ b/guides/source/2_2_release_notes.textile @@ -229,7 +229,7 @@ This will enable recognition of (among others) these routes: * Lead Contributor: "S. Brent Faulkner":http://www.unwwwired.net/ * More information: -** "Rails Routing from the Outside In":http://guides.rubyonrails.org/routing.html#_nested_resources +** "Rails Routing from the Outside In":http://guides.rubyonrails.org/routing.html#nested-resources ** "What's New in Edge Rails: Shallow Routes":http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes h4. Method Arrays for Member or Collection Routes diff --git a/railties/guides/source/2_3_release_notes.textile b/guides/source/2_3_release_notes.textile index 15abba66ab..15abba66ab 100644 --- a/railties/guides/source/2_3_release_notes.textile +++ b/guides/source/2_3_release_notes.textile diff --git a/railties/guides/source/3_0_release_notes.textile b/guides/source/3_0_release_notes.textile index d22c76dd81..d22c76dd81 100644 --- a/railties/guides/source/3_0_release_notes.textile +++ b/guides/source/3_0_release_notes.textile diff --git a/railties/guides/source/3_1_release_notes.textile b/guides/source/3_1_release_notes.textile index f88d8624ba..f88d8624ba 100644 --- a/railties/guides/source/3_1_release_notes.textile +++ b/guides/source/3_1_release_notes.textile diff --git a/railties/guides/source/3_2_release_notes.textile b/guides/source/3_2_release_notes.textile index d669a7fdfa..0f8fea2bf6 100644 --- a/railties/guides/source/3_2_release_notes.textile +++ b/guides/source/3_2_release_notes.textile @@ -49,6 +49,18 @@ The <tt>mass_assignment_sanitizer</tt> config also needs to be added in <tt>conf config.active_record.mass_assignment_sanitizer = :strict </ruby> +h4. What to update in your engines + +Replace the code beneath the comment in <tt>script/rails</tt> with the following content: + +<ruby> +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/your_engine_name/engine', __FILE__) + +require 'rails/all' +require 'rails/engine/commands' +</ruby> + h3. Creating a Rails 3.2 application <shell> diff --git a/railties/guides/source/_license.html.erb b/guides/source/_license.html.erb index 00b4466f50..00b4466f50 100644 --- a/railties/guides/source/_license.html.erb +++ b/guides/source/_license.html.erb diff --git a/railties/guides/source/_welcome.html.erb b/guides/source/_welcome.html.erb index 9d2e9c1d68..9d2e9c1d68 100644 --- a/railties/guides/source/_welcome.html.erb +++ b/guides/source/_welcome.html.erb diff --git a/railties/guides/source/action_controller_overview.textile b/guides/source/action_controller_overview.textile index 52d134ace5..52d134ace5 100644 --- a/railties/guides/source/action_controller_overview.textile +++ b/guides/source/action_controller_overview.textile diff --git a/railties/guides/source/action_mailer_basics.textile b/guides/source/action_mailer_basics.textile index 26c95be031..c277f764e7 100644 --- a/railties/guides/source/action_mailer_basics.textile +++ b/guides/source/action_mailer_basics.textile @@ -244,7 +244,7 @@ It is possible to send email to one or more recipients in one email (for e.g. in <ruby> class AdminMailer < ActionMailer::Base - default :to => Admin.all.map(&:email), + default :to => Proc.new { Admin.pluck(:email) }, :from => "notification@example.com" def new_registration(user) diff --git a/railties/guides/source/action_view_overview.textile b/guides/source/action_view_overview.textile index 2c0b81121f..42120e9bad 100644 --- a/railties/guides/source/action_view_overview.textile +++ b/guides/source/action_view_overview.textile @@ -431,11 +431,11 @@ form("post") <form action='/posts/create' method='post'> <p> <label for="post_title">Title</label><br /> - <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /> + <input id="post_title" name="post[title]" type="text" value="Hello World" /> </p> <p> <label for="post_body">Body</label><br /> - <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea> + <textarea id="post_body" name="post[body]"></textarea> </p> <input name="commit" type="submit" value="Create" /> </form> @@ -451,7 +451,7 @@ For example, if +@post+ has an attribute +title+ mapped to a +String+ column tha <ruby> input("post", "title") # => - <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /> + <input id="post_title" name="post[title]" type="text" value="Hello World" /> </ruby> h4. RecordTagHelper @@ -535,10 +535,10 @@ h4. AssetTagHelper This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds. -By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting +ActionController::Base.asset_host+ in the application configuration, typically in +config/environments/production.rb+. For example, let's say your asset host is +assets.example.com+: +By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting +config.action_controller.asset_host+ in the application configuration, typically in +config/environments/production.rb+. For example, let's say your asset host is +assets.example.com+: <ruby> -ActionController::Base.asset_host = "assets.example.com" +config.action_controller.asset_host = "assets.example.com" image_tag("rails.png") # => <img src="http://assets.example.com/images/rails.png" alt="Rails" /> </ruby> @@ -987,8 +987,8 @@ The HTML generated for this would be: <html> <form action="/persons/create" method="post"> - <input id="person_first_name" name="person[first_name]" size="30" type="text" /> - <input id="person_last_name" name="person[last_name]" size="30" type="text" /> + <input id="person_first_name" name="person[first_name]" type="text" /> + <input id="person_last_name" name="person[last_name]" type="text" /> <input name="commit" type="submit" value="Create" /> </form> </html> diff --git a/railties/guides/source/active_model_basics.textile b/guides/source/active_model_basics.textile index 98b3533000..98b3533000 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/guides/source/active_model_basics.textile diff --git a/railties/guides/source/active_record_basics.textile b/guides/source/active_record_basics.textile index 487f8b70f9..487f8b70f9 100644 --- a/railties/guides/source/active_record_basics.textile +++ b/guides/source/active_record_basics.textile diff --git a/railties/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index 21bbc64255..14d0ba9b28 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -94,7 +94,7 @@ client = Client.find(10) The SQL equivalent of the above is: <sql> -SELECT * FROM clients WHERE (clients.id = 10) +SELECT * FROM clients WHERE (clients.id = 10) LIMIT 1 </sql> <tt>Model.find(primary_key)</tt> will raise an +ActiveRecord::RecordNotFound+ exception if no matching record is found. @@ -133,6 +133,24 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 <tt>Model.last</tt> returns +nil+ if no matching record is found. No exception will be raised. +h5. +find_by+ + +<tt>Model.find_by</tt> finds the first record matching some conditions. For example: + +<ruby> +Client.find_by first_name: 'Lifo' +# => #<Client id: 1, first_name: "Lifo"> + +Client.find_by first_name: 'Jon' +# => nil +</ruby> + +It is equivalent to writing: + +<ruby> +Client.where(first_name: 'Lifo').first +</ruby> + h5(#first_1). +first!+ <tt>Model.first!</tt> finds the first record. For example: @@ -167,6 +185,24 @@ SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 <tt>Model.last!</tt> raises +RecordNotFound+ if no matching record is found. +h5(#find_by_1). +find_by!+ + +<tt>Model.find_by!</tt> finds the first record matching some conditions. It raises +RecordNotFound+ if no matching record is found. For example: + +<ruby> +Client.find_by! first_name: 'Lifo' +# => #<Client id: 1, first_name: "Lifo"> + +Client.find_by! first_name: 'Jon' +# => RecordNotFound +</ruby> + +It is equivalent to writing: + +<ruby> +Client.where(first_name: 'Lifo').first! +</ruby> + h4. Retrieving Multiple Objects h5. Using Multiple Primary Keys @@ -943,21 +979,23 @@ If, in the case of this +includes+ query, there were no comments for any posts, h3. Scopes -Scoping allows you to specify commonly-used ARel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as +where+, +joins+ and +includes+. All scope methods will return an +ActiveRecord::Relation+ object which will allow for further methods (such as other scopes) to be called on it. +Scoping allows you to specify commonly-used queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as +where+, +joins+ and +includes+. All scope methods will return an +ActiveRecord::Relation+ object which will allow for further methods (such as other scopes) to be called on it. -To define a simple scope, we use the +scope+ method inside the class, passing the ARel query that we'd like run when this scope is called: +To define a simple scope, we use the +scope+ method inside the class, passing the query that we'd like run when this scope is called: <ruby> class Post < ActiveRecord::Base - scope :published, where(:published => true) + scope :published, -> { where(published: true) } end </ruby> -Just like before, these methods are also chainable: +This is exactly the same as defining a class method, and which you use is a matter of personal preference: <ruby> class Post < ActiveRecord::Base - scope :published, where(:published => true).joins(:category) + def self.published + where(published: true) + end end </ruby> @@ -965,8 +1003,8 @@ Scopes are also chainable within scopes: <ruby> class Post < ActiveRecord::Base - scope :published, where(:published => true) - scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0)) + scope :published, -> { where(:published => true) } + scope :published_and_commented, -> { published.and(self.arel_table[:comments_count].gt(0)) } end </ruby> @@ -983,25 +1021,13 @@ category = Category.first category.posts.published # => [published posts belonging to this category] </ruby> -h4. Working with times - -If you're working with dates or times within scopes, due to how they are evaluated, you will need to use a lambda so that the scope is evaluated every time. - -<ruby> -class Post < ActiveRecord::Base - scope :created_before_now, lambda { where("created_at < ?", Time.zone.now ) } -end -</ruby> - -Without the +lambda+, this +Time.zone.now+ will only be called once. - h4. Passing in arguments -When a +lambda+ is used for a +scope+, it can take arguments: +Your scope can take arguments: <ruby> class Post < ActiveRecord::Base - scope :created_before, lambda { |time| where("created_at < ?", time) } + scope :created_before, ->(time) { where("created_at < ?", time) } end </ruby> @@ -1048,7 +1074,7 @@ If we wish for a scope to be applied across all queries to the model we can use <ruby> class Client < ActiveRecord::Base - default_scope where("removed_at IS NULL") + default_scope { where("removed_at IS NULL") } end </ruby> @@ -1133,6 +1159,8 @@ Client.where(:first_name => 'Andy').first_or_create!(:locked => false) # => ActiveRecord::RecordInvalid: Validation failed: Orders count can't be blank </ruby> +As with +first_or_create+ there is a +find_or_create_by!+ method but the +first_or_create!+ method is preferred for clarity. + h4. +first_or_initialize+ The +first_or_initialize+ method will work just like +first_or_create+ but it will not call +create+ but +new+. This means that a new model instance will be created in memory but won't be saved to the database. Continuing with the +first_or_create+ example, we now want the client named 'Nick': diff --git a/railties/guides/source/active_record_validations_callbacks.textile b/guides/source/active_record_validations_callbacks.textile index 72ac8d2db9..88c4481e5e 100644 --- a/railties/guides/source/active_record_validations_callbacks.textile +++ b/guides/source/active_record_validations_callbacks.textile @@ -517,9 +517,21 @@ class Person < ActiveRecord::Base end </ruby> +h3. Strict Validations + +You can also specify validations to be strict and raise +ActiveModel::StrictValidationFailed+ when the object is invalid. + +<ruby> +class Person < ActiveRecord::Base + validates :name, :presence => { :strict => true } +end + +Person.new.valid? => ActiveModel::StrictValidationFailed: Name can't be blank +</ruby> + h3. Conditional Validation -Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a +Proc+. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option. +Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string, a +Proc+ or an +Array+. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option. h4. Using a Symbol with +:if+ and +:unless+ @@ -571,6 +583,20 @@ end All validations inside of +with_options+ block will have automatically passed the condition +:if => :is_admin?+ +h4. Combining validation conditions + +On the other hand, when multiple conditions define whether or not a validation should happen, an +Array+ can be used. Moreover, you can apply both +:if:+ and +:unless+ to the same validation. + +<ruby> +class Computer < ActiveRecord::Base + validates :mouse, :presence => true, + :if => ["market.retail?", :desktop?] + :unless => Proc.new { |c| c.trackpad.present? } +end +</ruby> + +The validation only runs when all the +:if+ conditions and none of the +:unless+ conditions are evaluated to +true+. + h3. Performing Custom Validations When the built-in validation helpers are not enough for your needs, you can write your own validators or validation methods as you prefer. @@ -1095,7 +1121,7 @@ Post destroyed h3. Conditional Callbacks -As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the +:if+ and +:unless+ options, which can take a symbol, a string or a +Proc+. You may use the +:if+ option when you want to specify under which conditions the callback *should* be called. If you want to specify the conditions under which the callback *should not* be called, then you may use the +:unless+ option. +As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the +:if+ and +:unless+ options, which can take a symbol, a string, a +Proc+ or an +Array+. You may use the +:if+ option when you want to specify under which conditions the callback *should* be called. If you want to specify the conditions under which the callback *should not* be called, then you may use the +:unless+ option. h4. Using +:if+ and +:unless+ with a +Symbol+ diff --git a/railties/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile index 61fdb5ccc6..5d0a3f82e8 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/guides/source/active_support_core_extensions.textile @@ -509,55 +509,6 @@ end NOTE: Defined in +active_support/core_ext/module/aliasing.rb+. -h5. +attr_accessor_with_default+ - -The method +attr_accessor_with_default+ serves the same purpose as the Ruby macro +attr_accessor+ but allows you to set a default value for the attribute: - -<ruby> -class Url - attr_accessor_with_default :port, 80 -end - -Url.new.port # => 80 -</ruby> - -The default value can be also specified with a block, which is called in the context of the corresponding object: - -<ruby> -class User - attr_accessor :name, :surname - attr_accessor_with_default(:full_name) do - [name, surname].compact.join(" ") - end -end - -u = User.new -u.name = 'Xavier' -u.surname = 'Noria' -u.full_name # => "Xavier Noria" -</ruby> - -The result is not cached, the block is invoked in each call to the reader. - -You can overwrite the default with the writer: - -<ruby> -url = Url.new -url.host # => 80 -url.host = 8080 -url.host # => 8080 -</ruby> - -The default value is returned as long as the attribute is unset. The reader does not rely on the value of the attribute to know whether it has to return the default. It rather monitors the writer: if there's any assignment the value is no longer considered to be unset. - -Active Resource uses this macro to set a default value for the +:primary_key+ attribute: - -<ruby> -attr_accessor_with_default :primary_key, 'id' -</ruby> - -NOTE: Defined in +active_support/core_ext/module/attr_accessor_with_default.rb+. - h5. Internal Attributes When you are defining an attribute in a class that is meant to be subclassed, name collisions are a risk. That's remarkably important for libraries. @@ -2869,6 +2820,8 @@ d.next_year # => Wed, 28 Feb 2001 Active Support defines these methods as well for Ruby 1.8. ++prev_year+ is aliased to +last_year+. + h6. +prev_month+, +next_month+ In Ruby 1.9 +prev_month+ and +next_month+ return the date with the same day in the last or next month: @@ -2890,6 +2843,8 @@ Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000 Active Support defines these methods as well for Ruby 1.8. ++prev_month+ is aliased to +last_month+. + h6. +beginning_of_week+, +end_of_week+ The methods +beginning_of_week+ and +end_of_week+ return the dates for the @@ -2935,6 +2890,8 @@ d.prev_week(:saturday) # => Sat, 01 May 2010 d.prev_week(:friday) # => Fri, 30 Apr 2010 </ruby> ++prev_week+ is aliased to +last_week+. + h6. +beginning_of_month+, +end_of_month+ The methods +beginning_of_month+ and +end_of_month+ return the dates for the beginning and end of the month: @@ -3145,13 +3102,13 @@ end_of_week (at_end_of_week) monday sunday weeks_ago -prev_week +prev_week (last_week) next_week months_ago months_since beginning_of_month (at_beginning_of_month) end_of_month (at_end_of_month) -prev_month +prev_month (last_month) next_month beginning_of_quarter (at_beginning_of_quarter) end_of_quarter (at_end_of_quarter) @@ -3159,7 +3116,7 @@ beginning_of_year (at_beginning_of_year) end_of_year (at_end_of_year) years_ago years_since -prev_year +prev_year (last_year) next_year </ruby> @@ -3321,13 +3278,13 @@ end_of_week (at_end_of_week) monday sunday weeks_ago -prev_week +prev_week (last_week) next_week months_ago months_since beginning_of_month (at_beginning_of_month) end_of_month (at_end_of_month) -prev_month +prev_month (last_month) next_month beginning_of_quarter (at_beginning_of_quarter) end_of_quarter (at_end_of_quarter) @@ -3335,7 +3292,7 @@ beginning_of_year (at_beginning_of_year) end_of_year (at_end_of_year) years_ago years_since -prev_year +prev_year (last_year) next_year </ruby> diff --git a/guides/source/active_support_instrumentation.textile b/guides/source/active_support_instrumentation.textile new file mode 100644 index 0000000000..430549fba4 --- /dev/null +++ b/guides/source/active_support_instrumentation.textile @@ -0,0 +1,448 @@ +h2. Active Support Instrumentation + +Active Support is a part of core Rails that provides Ruby language extensions, utilities and other things. One of the things it includes is an instrumentation API that can be used inside an application to measure certain actions that occur within Ruby code, such as that inside a Rails application or the framework itself. It is not limited to Rails, however. It can be used independently in other Ruby scripts if it is so desired. + +In this guide, you will learn how to use the instrumentation API inside of ActiveSupport to measure events inside of Rails and other Ruby code. We cover: + +* What instrumentation can provide +* The hooks inside the Rails framework for instrumentation +* Adding a subscriber to a hook +* Building a custom instrumentation implementation + +endprologue. + +h3. Introduction to instrumentation + +The instrumentation API provided by ActiveSupport allows developers to provide hooks which other developers may hook into. There are several of these within the Rails framework, as described below in <TODO: link to section detailing each hook point>. With this API, developers can choose to be notified when certain events occur inside their application or another piece of Ruby code. + +For example, there is a hook provided within Active Record that is called every time Active Record uses a SQL query on a database. This hook could be *subscribed* to, and used to track the number of queries during a certain action. There's another hook around the processing of an action of a controller. This could be used, for instance, to track how long a specific action has taken. + +You are even able to create your own events inside your application which you can later subscribe to. + +h3. Rails framework hooks + +Within the Ruby on Rails framework, there are a number of hooks provided for common events. These are detailed below. + +h3. ActionController + +h4. write_fragment.action_controller + +|_.Key |_.Value| +|+:key+ |The complete key| + +<ruby> +{ + :key => 'posts/1-dasboard-view' +} +</ruby> + +h4. read_fragment.action_controller + +|_.Key |_.Value| +|+:key+ |The complete key| + +<ruby> +{ + :key => 'posts/1-dasboard-view' +} +</ruby> + +h4. expire_fragment.action_controller + +|_.Key |_.Value| +|+:key+ |The complete key| + +<ruby> +{ + :key => 'posts/1-dasboard-view' +} +</ruby> + +h4. exist_fragment?.action_controller + +|_.Key |_.Value| +|+:key+ |The complete key| + +<ruby> +{ + :key => 'posts/1-dasboard-view' +} +</ruby> + +h4. write_page.action_controller + +|_.Key |_.Value| +|+:path+ |The complete path| + +<ruby> +{ + :path => '/users/1' +} +</ruby> + +h4. expire_page.action_controller + +|_.Key |_.Value| +|+:path+ |The complete path| + +<ruby> +{ + :path => '/users/1' +} +</ruby> + +h4. start_processing.action_controller + +|_.Key |_.Value | +|+:controller+ |The controller name| +|+:action+ |The action| +|+:params+ |Hash of request parameters without any filtered parameter| +|+:format+ |html/js/json/xml etc| +|+:method+ |HTTP request verb| +|+:path+ |Request path| + +<ruby> +{ + :controller => "PostsController", + :action => "new", + :params => { "action" => "new", "controller" => "posts" }, + :format => :html, + :method => "GET", + :path => "/posts/new" +} +</ruby> + +h4. process_action.action_controller + +|_.Key |_.Value | +|+:controller+ |The controller name| +|+:action+ |The action| +|+:params+ |Hash of request parameters without any filtered parameter| +|+:format+ |html/js/json/xml etc| +|+:method+ |HTTP request verb| +|+:path+ |Request path| +|+:view_runtime+ |Amount spent in view in ms| + +<ruby> +{ + :controller => "PostsController", + :action => "index", + :params => {"action" => "index", "controller" => "posts"}, + :format => :html, + :method => "GET", + :path => "/posts", + :status => 200, + :view_runtime => 46.848, + :db_runtime => 0.157 +} +</ruby> + +h4. send_file.action_controller + +|_.Key |_.Value | +|+:path+ |Complete path to the file| + +INFO. Additional keys may be added by the caller. + +h4. send_data.action_controller + ++ActionController+ does not had any specific information to the payload. All options are passed through to the payload. + +h4. redirect_to.action_controller + +|_.Key |_.Value | +|+:status+ |HTTP response code| +|+:location+ |URL to redirect to| + +<ruby> +{ + :status => 302, + :location => "http://localhost:3000/posts/new" +} +</ruby> + +h4. halted_callback.action_controller + +|_.Key |_.Value | +|+:filter+ |Filter that halted the action| + +<ruby> +{ + :filter => ":halting_filter" +} +</ruby> + +h3. ActionView + +h4. render_template.action_view + +|_.Key |_.Value | +|+:identifier+ |Full path to template| +|+:layout+ |Applicable layout| + +<ruby> +{ + :identifier => "/Users/adam/projects/notifications/app/views/posts/index.html.erb", + :layout => "layouts/application" +} +</ruby> + +h4. render_partial.action_view + +|_.Key |_.Value | +|+:identifier+ |Full path to template| + +<ruby> +{ + :identifier => "/Users/adam/projects/notifications/app/views/posts/_form.html.erb", +} +</ruby> + +h3. ActiveRecord + +h4. sql.active_record + +|_.Key |_.Value | +|+:sql+ |SQL statement| +|+:name+ |Name of the operation| +|+:object_id+ |+self.object_id+| + +INFO. The adapters will add their own data as well. + +<ruby> +{ + :sql => "SELECT \"posts\".* FROM \"posts\" ", + :name => "Post Load", + :connection_id => 70307250813140, + :binds => [] +} +</ruby> + +h4. identity.active_record + +|_.Key |_.Value | +|+:line+ |Primary Key of object in the identity map| +|+:name+ |Record's class| +|+:connection_id+ |+self.object_id+| + +h3. ActionMailer + +h4. receive.action_mailer + +|_.Key |_.Value| +|+:mailer+ |Name of the mailer class| +|+:message_id+ |ID of the message, generated by the Mail gem| +|+:subject+ |Subject of the mail| +|+:to+ |To address(es) of the mail| +|+:from+ |From address of the mail| +|+:bcc+ |BCC addresses of the mail| +|+:cc+ |CC addresses of the mail| +|+:date+ |Date of the mail| +|+:mail+ |The encoded form of the mail| + +<ruby> +{ + :mailer => "Notification", + :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", + :subject => "Rails Guides", + :to => ["users@rails.com", "ddh@rails.com"], + :from => ["me@rails.com"], + :date => Sat, 10 Mar 2012 14:18:09 +0100, + :mail=> "..." # ommitted for beverity +} +</ruby> + +h4. deliver.action_mailer + +|_.Key |_.Value| +|+:mailer+ |Name of the mailer class| +|+:message_id+ |ID of the message, generated by the Mail gem| +|+:subject+ |Subject of the mail| +|+:to+ |To address(es) of the mail| +|+:from+ |From address of the mail| +|+:bcc+ |BCC addresses of the mail| +|+:cc+ |CC addresses of the mail| +|+:date+ |Date of the mail| +|+:mail+ |The encoded form of the mail| + +<ruby> +{ + :mailer => "Notification", + :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", + :subject => "Rails Guides", + :to => ["users@rails.com", "ddh@rails.com"], + :from => ["me@rails.com"], + :date => Sat, 10 Mar 2012 14:18:09 +0100, + :mail=> "..." # ommitted for beverity +} +</ruby> + +h3. ActiveResource + +h4. request.active_resource + +|_.Key |_.Value| +|+:method+ |HTTP method| +|+:request_uri+ |Complete URI| +|+:result+ |HTTP response object| + +h3. ActiveSupport + +h4. cache_read.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| +|+:hit+ |If this read is a hit| +|+:super_operation+ |:fetch is added when a read is used with +#fetch+| + +h4. cache_generate.active_support + +This event is only used when +#fetch+ is called with a block. + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +INFO. Options passed to fetch will be merged with the payload when writing to the store + +<ruby> +{ + :key => 'name-of-complicated-computation' +} +</ruby> + + +h4. cache_fetch_hit.active_support + +This event is only used when +#fetch+ is called with a block. + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +INFO. Options passed to fetch will be merged with the payload. + +<ruby> +{ + :key => 'name-of-complicated-computation' +} +</ruby> + +h4. cache_write.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +INFO. Cache stores my add their own keys + +<ruby> +{ + :key => 'name-of-complicated-computation' +} +</ruby> + +h4. cache_delete.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +<ruby> +{ + :key => 'name-of-complicated-computation' +} +</ruby> + +h4. cache_exist?.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +<ruby> +{ + :key => 'name-of-complicated-computation' +} +</ruby> + +h3. Rails + +h4. deprecation.rails + +|_.Key |_.Value| +|+:message+ |The deprecation warning| +|+:callstack+ |Where the deprecation came from| + +h3. Subscribing to an event + +Subscribing to an event is easy. Use +ActiveSupport::Notifications.subscribe+ with a block to +listen to any notification. + +The block receives the following arguments: + +# The name of the event +# Time when is started +# Time when it finished +# An unique ID for this event +# The payload (described in previous sections) + +<ruby> +ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data| + # your own custom stuff + Rails.logger.info "#{name} Received!" +end +</ruby> + +Defining all those block arguments each time can be tedious. You can easily create an +ActiveSupport::Notifications::Event+ +from block args like this: + +<ruby> +ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| + event = ActiveSupport::Notification::Event.new args + + event.name # => "process_action.action_controller" + event.duration # => 10 (in milliseconds) + event.payload # => { :extra => :information } + + Rails.logger.info "#{event} Received!" +end +</ruby> + +Most times you only care about the data itself. Here is a shortuct to just get the data. + +<ruby> +ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| + data = args.extract_options! + data # { :extra => :information } +</ruby> + +You may also subscribe to events matching a regular expresssion. This enables you to subscribe to +multiple events at once. Here's you could subscribe to everything from +ActionController+. + +<ruby> +ActiveSupport::Notifications.subscribe /action_controller/ do |*args| + # inspect all ActionController events +end +</ruby> + +h3. Creating custom events + +Adding your own events is easy as well. +ActiveSupport::Notifications+ will take care of +all the heavy lifting for you. Simply call +instrument+ with a +name+, +payload+ and a block. +The notification will be sent after the block returns. +ActiveSupport+ will generate the start and end times +as well as the unique ID. All data passed into the +insturment+ call will make it into the payload. + +Here's an example: + +<ruby> +ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do + # do your custom stuff here +end +</ruby> + +Now you can listen to this event with: + +<ruby> +ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data| + puts data.inspect # { :this => :data } +end +</ruby> + +You should follow Rails conventions when defining your own events. The format is: +event.library+. +If you application is sending Tweets, you should create an event named +tweet.twitter+. diff --git a/railties/guides/source/ajax_on_rails.textile b/guides/source/ajax_on_rails.textile index 5913a472fd..cda9c64460 100644 --- a/railties/guides/source/ajax_on_rails.textile +++ b/guides/source/ajax_on_rails.textile @@ -134,7 +134,7 @@ If the server returns 200, the output of the above example is equivalent to our ** *position* By default (i.e. when not specifying this option, like in the examples before) the response is injected into the element with the specified DOM id, replacing the original content of the element (if there was any). You might want to alter this behavior by keeping the original content - the only question is where to place the new content? This can specified by the +position+ parameter, with four possibilities: *** +:before+ Inserts the response text just before the target element. More precisely, it creates a text node from the response and inserts it as the left sibling of the target element. *** +:after+ Similar behavior to +:before+, but in this case the response is inserted after the target element. -*** +:top+ Inserts the text into the target element, before it's original content. If the target element was empty, this is equivalent with not specifying +:position+ at all. +*** +:top+ Inserts the text into the target element, before its original content. If the target element was empty, this is equivalent with not specifying +:position+ at all. *** +:bottom+ The counterpart of +:top+: the response is inserted after the target element's original content. A typical example of using +:bottom+ is inserting a new <li> element into an existing list: @@ -174,7 +174,7 @@ link_to_remote "Update record", This generates a remote link which adds 2 parameters to the standard URL generated by Rails, taken from the page (contained in the elements matched by the 'status' and 'completed' DOM id). -** *Callbacks* Since an AJAX call is typically asynchronous, as it's name suggests (this is not a rule, and you can fire a synchronous request - see the last option, +:type+) your only way of communicating with a request once it is fired is via specifying callbacks. There are six options at your disposal (in fact 508, counting all possible response types, but these six are the most frequent and therefore specified by a constant): +** *Callbacks* Since an AJAX call is typically asynchronous, as its name suggests (this is not a rule, and you can fire a synchronous request - see the last option, +:type+) your only way of communicating with a request once it is fired is via specifying callbacks. There are six options at your disposal (in fact 508, counting all possible response types, but these six are the most frequent and therefore specified by a constant): *** +:loading:+ => +code+ The request is in the process of receiving the data, but the transfer is not completed yet. *** +:loaded:+ => +code+ The transfer is completed, but the data is not processed and returned yet *** +:interactive:+ => +code+ One step after +:loaded+: The data is fully received and being processed @@ -203,7 +203,7 @@ link_to_remote "Add new item", ** *:type* If you want to fire a synchronous request for some obscure reason (blocking the browser while the request is processed and doesn't return a status code), you can use the +:type+ option with the value of +:synchronous+. * Finally, using the +html_options+ parameter you can add HTML attributes to the generated tag. It works like the same parameter of the +link_to+ helper. There are interesting side effects for the +href+ and +onclick+ parameters though: ** If you specify the +href+ parameter, the AJAX link will degrade gracefully, i.e. the link will point to the URL even if JavaScript is disabled in the client browser -** +link_to_remote+ gains it's AJAX behavior by specifying the remote call in the onclick handler of the link. If you supply +html_options[:onclick]+ you override the default behavior, so use this with care! +** +link_to_remote+ gains its AJAX behavior by specifying the remote call in the onclick handler of the link. If you supply +html_options[:onclick]+ you override the default behavior, so use this with care! We are finished with +link_to_remote+. I know this is quite a lot to digest for one helper function, but remember, these options are common for all the rest of the Rails view helpers, so we will take a look at the differences / additional parameters in the next sections. @@ -211,8 +211,8 @@ h4. AJAX Forms There are three different ways of adding AJAX forms to your view using Rails Prototype helpers. They are slightly different, but striving for the same goal: instead of submitting the form using the standard HTTP request/response cycle, it is submitted asynchronously, thus not reloading the page. These methods are the following: -* +remote_form_for+ (and it's alias +form_remote_for+) is tied to Rails most tightly of the three since it takes a resource, model or array of resources (in case of a nested resource) as a parameter. -* +form_remote_tag+ AJAXifies the form by serializing and sending it's data in the background +* +remote_form_for+ (and its alias +form_remote_for+) is tied to Rails most tightly of the three since it takes a resource, model or array of resources (in case of a nested resource) as a parameter. +* +form_remote_tag+ AJAXifies the form by serializing and sending its data in the background * +submit_to_remote+ and +button_to_remote+ is more rarely used than the previous two. Rather than creating an AJAX form, you add a button/input Let's see them in action one by one! diff --git a/railties/guides/source/api_documentation_guidelines.textile b/guides/source/api_documentation_guidelines.textile index 93120c15a7..c6aa1f0a2b 100644 --- a/railties/guides/source/api_documentation_guidelines.textile +++ b/guides/source/api_documentation_guidelines.textile @@ -6,7 +6,7 @@ endprologue. h3. RDoc -The Rails API documentation is generated with RDoc 2.5. Please consult the documentation for help with the "markup":http://rdoc.rubyforge.org/RDoc/Markup.html, and take into account also these "additional directives":http://rdoc.rubyforge.org/RDoc/Parser/Ruby.html. +The Rails API documentation is generated with RDoc. Please consult the documentation for help with the "markup":http://rdoc.rubyforge.org/RDoc/Markup.html, and also take into account these "additional directives":http://rdoc.rubyforge.org/RDoc/Parser/Ruby.html. h3. Wording @@ -14,7 +14,7 @@ Write simple, declarative sentences. Brevity is a plus: get to the point. Write in present tense: "Returns a hash that...", rather than "Returned a hash that..." or "Will return a hash that...". -Start comments in upper case, follow regular punctuation rules: +Start comments in upper case. Follow regular punctuation rules: <ruby> # Declares an attribute reader backed by an internally-named instance variable. @@ -23,7 +23,7 @@ def attr_internal_reader(*attrs) end </ruby> -Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the recommended idioms in edge, reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage. +Communicate to the reader the current way of doing things, both explicitly and implicitly. Use the idioms recommended in edge. Reorder sections to emphasize favored approaches if needed, etc. The documentation should be a model for best practices and canonical, modern Rails usage. Documentation has to be concise but comprehensive. Explore and document edge cases. What happens if a module is anonymous? What if a collection is empty? What if an argument is nil? @@ -41,10 +41,9 @@ h3. Example Code Choose meaningful examples that depict and cover the basics as well as interesting points or gotchas. -Use two spaces to indent chunks of code--that is two spaces with respect to the left margin; the examples -themselves should use "Rails coding conventions":contributing_to_ruby_on_rails.html#follow-the-coding-conventions. +Use two spaces to indent chunks of code--that is, for markup purposes, two spaces with respect to the left margin. The examples themselves should use "Rails coding conventions":contributing_to_ruby_on_rails.html#follow-the-coding-conventions. -Short docs do not need an explicit "Examples" label to introduce snippets, they just follow paragraphs: +Short docs do not need an explicit "Examples" label to introduce snippets; they just follow paragraphs: <ruby> # Converts a collection of elements into a formatted string by calling @@ -64,7 +63,7 @@ On the other hand, big chunks of structured documentation may have a separate "E # Person.exists?(['name LIKE ?', "%#{query}%"]) </ruby> -The result of expressions follow them and are introduced by "# => ", vertically aligned: +The results of expressions follow them and are introduced by "# => ", vertically aligned: <ruby> # For checking if a fixnum is even or odd. @@ -98,7 +97,7 @@ On the other hand, regular comments do not use an arrow: h3. Filenames -As a rule of thumb use filenames relative to the application root: +As a rule of thumb, use filenames relative to the application root: <plain> config/routes.rb # YES @@ -111,12 +110,12 @@ h3. Fonts h4. Fixed-width Font Use fixed-width fonts for: -* constants, in particular class and module names -* method names -* literals like +nil+, +false+, +true+, +self+ -* symbols -* method parameters -* file names +* Constants, in particular class and module names. +* Method names. +* Literals like +nil+, +false+, +true+, +self+. +* Symbols. +* Method parameters. +* File names. <ruby> class Array @@ -134,6 +133,20 @@ h4. Regular Font When "true" and "false" are English words rather than Ruby keywords use a regular font: +<ruby> +# Runs all the validations within the specified context. Returns true if no errors are found, +# false otherwise. +# +# If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if +# <tt>new_record?</tt> is true, and to <tt>:update</tt> if it is not. +# +# Validations with no <tt>:on</tt> option will run no matter the context. Validations with +# some <tt>:on</tt> option will only run in the specified context. +def valid?(context = nil) + ... +end +</ruby> + h3. Description Lists In lists of options, parameters, etc. use a hyphen between the item and its description (reads better than a colon because normally options are symbols): @@ -146,7 +159,7 @@ The description starts in upper case and ends with a full stop—it's standard E h3. Dynamically Generated Methods -Methods created with +(module|class)_eval(STRING)+ have a comment by their side with an instance of the generated code. That comment is 2 spaces apart from the template: +Methods created with +(module|class)_eval(STRING)+ have a comment by their side with an instance of the generated code. That comment is 2 spaces away from the template: <ruby> for severity in Severity.constants @@ -162,7 +175,7 @@ for severity in Severity.constants end </ruby> -If the resulting lines are too wide, say 200 columns or more, we put the comment above the call: +If the resulting lines are too wide, say 200 columns or more, put the comment above the call: <ruby> # def self.find_by_login_and_activated(*args) diff --git a/railties/guides/source/asset_pipeline.textile b/guides/source/asset_pipeline.textile index ff2bd08602..464788f30f 100644 --- a/railties/guides/source/asset_pipeline.textile +++ b/guides/source/asset_pipeline.textile @@ -128,7 +128,7 @@ For example, these files: <plain> app/assets/javascripts/home.js lib/assets/javascripts/moovinator.js -vendor/assets/javascript/slider.js +vendor/assets/javascripts/slider.js </plain> would be referenced in a manifest like this: @@ -409,7 +409,9 @@ cannot see application objects or methods. *Heroku requires this to be false.* WARNING: If you set +config.assets.initialize_on_precompile+ to false, be sure to test +rake assets:precompile+ locally before deploying. It may expose bugs where your assets reference application objects or methods, since those are still -in scope in development mode regardless of the value of this flag. +in scope in development mode regardless of the value of this flag. Changing this flag also effects +engines. Engines can define assets for precompilation as well. Since the complete environment is not loaded, +engines (or other gems) will not be loaded which can cause missing assets. Capistrano (v2.8.0 and above) includes a recipe to handle this in deployment. Add the following line to +Capfile+: @@ -423,12 +425,14 @@ It is important that this folder is shared between deployments so that remotely NOTE. If you are precompiling your assets locally, you can use +bundle install --without assets+ on the server to avoid installing the assets gems (the gems in the assets group in the Gemfile). -The default matcher for compiling files includes +application.js+, +application.css+ and all non-JS/CSS files (i.e., +.coffee+ and +.scss+ files are *not* automatically included as they compile to JS/CSS): +The default matcher for compiling files includes +application.js+, +application.css+ and all non-JS/CSS files (this will include all image assets automatically): <ruby> [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) }, /application.(css|js)$/ ] </ruby> +NOTE. The matcher (and other members of the precompile array; see below) is applied to final compiled file names. This means that anything that compiles to JS/CSS is excluded, as well as raw JS/CSS files; for example, +.coffee+ and +.scss+ files are *not* automatically included as they compile to JS/CSS. + If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the +precompile+ array: <erb> @@ -456,7 +460,7 @@ config.assets.manifest = '/path/to/some/other/location' NOTE: If there are missing precompiled files in production you will get an <tt>Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError</tt> exception indicating the name of the missing file(s). -h5. Server Configuration +h5. Far-future Expires header Precompiled assets exist on the filesystem and are served directly by your web server. They do not have far-future headers by default, so to get the benefit of fingerprinting you'll have to update your server configuration to add them. @@ -464,6 +468,7 @@ For Apache: <plain> <LocationMatch "^/assets/.*$"> + # Use of ETag is discouraged when Last-Modified is present Header unset ETag FileETag None # RFC says only cache for 1 year @@ -484,6 +489,8 @@ location ~ ^/assets/ { } </plain> +h5. GZip compression + When files are precompiled, Sprockets also creates a "gzipped":http://en.wikipedia.org/wiki/Gzip (.gz) version of your assets. Web servers are typically configured to use a moderate compression ratio as a compromise, but since precompilation happens once, Sprockets uses the maximum compression ratio, thus reducing the size of the data transfer to the minimum. On the other hand, web servers can be configured to serve compressed content directly from disk, rather than deflating non-compressed files themselves. Nginx is able to do this automatically enabling +gzip_static+: @@ -666,7 +673,7 @@ config.assets.compile = false # Generate digests for assets URLs. config.assets.digest = true -# Defaults to Rails.root.join("public/assets") +# Defaults to nil and saved in location specified by config.assets.prefix # config.assets.manifest = YOUR_PATH # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) diff --git a/railties/guides/source/association_basics.textile b/guides/source/association_basics.textile index ba92aedbd0..8ddc56bef1 100644 --- a/railties/guides/source/association_basics.textile +++ b/guides/source/association_basics.textile @@ -342,9 +342,9 @@ In designing a data model, you will sometimes find a model that should have a re <ruby> class Employee < ActiveRecord::Base - has_many :subordinates, :class_name => "Employee" - belongs_to :manager, :class_name => "Employee", + has_many :subordinates, :class_name => "Employee", :foreign_key => "manager_id" + belongs_to :manager, :class_name => "Employee" end </ruby> @@ -1322,7 +1322,7 @@ If you need to evaluate conditions dynamically at runtime, use a proc: <ruby> class Customer < ActiveRecord::Base has_many :latest_orders, :class_name => "Order", - :conditions => proc { ["orders.created_at > ?, 10.hours.ago] } + :conditions => proc { ["orders.created_at > ?", 10.hours.ago] } end </ruby> diff --git a/railties/guides/source/caching_with_rails.textile b/guides/source/caching_with_rails.textile index 6419d32c13..0e811a2527 100644 --- a/railties/guides/source/caching_with_rails.textile +++ b/guides/source/caching_with_rails.textile @@ -257,7 +257,9 @@ However, it's important to note that query caches are created at the start of an h3. Cache Stores -Rails provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk. +Rails provides different stores for the cached data created by <b>action</b> and <b>fragment</b> caches. + +TIP: Page caches are always stored on disk. h4. Configuration @@ -267,7 +269,7 @@ You can set up your application's default cache store by calling +config.cache_s config.cache_store = :memory_store </ruby> -Alternatively, you can call +ActionController::Base.cache_store+ outside of a configuration block. +NOTE: Alternatively, you can call +ActionController::Base.cache_store+ outside of a configuration block. You can access the cache by calling +Rails.cache+. @@ -294,7 +296,7 @@ h4. ActiveSupport::Cache::MemoryStore This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the +:size+ options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed. <ruby> -ActionController::Base.cache_store = :memory_store, :size => 64.megabytes +config.cache_store = :memory_store, :size => 64.megabytes </ruby> If you're running multiple Ruby on Rails server processes (which is the case if you're using mongrel_cluster or Phusion Passenger), then your Rails server process instances won't be able to share cache data with each other. This cache store is not appropriate for large application deployments, but can work well for small, low traffic sites with only a couple of server processes or for development and test environments. @@ -306,7 +308,7 @@ h4. ActiveSupport::Cache::FileStore This cache store uses the file system to store entries. The path to the directory where the store files will be stored must be specified when initializing the cache. <ruby> -ActionController::Base.cache_store = :file_store, "/path/to/cache/directory" +config.cache_store = :file_store, "/path/to/cache/directory" </ruby> With this cache store, multiple server processes on the same host can share a cache. Servers processes running on different hosts could share a cache by using a shared file system, but that set up would not be ideal and is not recommended. The cache store is appropriate for low to medium traffic sites that are served off one or two hosts. @@ -322,7 +324,7 @@ When initializing the cache, you need to specify the addresses for all memcached The +write+ and +fetch+ methods on this cache accept two additional options that take advantage of features specific to memcached. You can specify +:raw+ to send a value directly to the server with no serialization. The value must be a string or number. You can use memcached direct operation like +increment+ and +decrement+ only on raw values. You can also specify +:unless_exist+ if you don't want memcached to overwrite an existing entry. <ruby> -ActionController::Base.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com" +config.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com" </ruby> h4. ActiveSupport::Cache::EhcacheStore @@ -330,7 +332,7 @@ h4. ActiveSupport::Cache::EhcacheStore If you are using JRuby you can use Terracotta's Ehcache as the cache store for your application. Ehcache is an open source Java cache that also offers an enterprise version with increased scalability, management, and commercial support. You must first install the jruby-ehcache-rails3 gem (version 1.1.0 or later) to use this cache store. <ruby> -ActionController::Base.cache_store = :ehcache_store +config.cache_store = :ehcache_store </ruby> When initializing the cache, you may use the +:ehcache_config+ option to specify the Ehcache config file to use (where the default is "ehcache.xml" in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache). @@ -359,7 +361,7 @@ h4. ActiveSupport::Cache::NullStore This cache store implementation is meant to be used only in development or test environments and it never stores anything. This can be very useful in development when you have code that interacts directly with +Rails.cache+, but caching may interfere with being able to see the results of code changes. With this cache store, all +fetch+ and +read+ operations will result in a miss. <ruby> -ActionController::Base.cache_store = :null +config.cache_store = :null_store </ruby> h4. Custom Cache Stores @@ -369,7 +371,7 @@ You can create your own custom cache store by simply extending +ActiveSupport::C To use a custom cache store, simple set the cache store to a new instance of the class. <ruby> -ActionController::Base.cache_store = MyCacheStore.new +config.cache_store = MyCacheStore.new </ruby> h4. Cache Keys diff --git a/railties/guides/source/command_line.textile b/guides/source/command_line.textile index 8ae8c61ae6..858ce47db1 100644 --- a/railties/guides/source/command_line.textile +++ b/guides/source/command_line.textile @@ -278,6 +278,12 @@ The +console+ command lets you interact with your Rails application from the com You can also use the alias "c" to invoke the console: <tt>rails c</tt>. +You can specify the environment in which the +console+ command should operate using the +-e+ switch. + +<shell> +$ rails console -e staging +</shell> + If you wish to test out some code without changing any data, you can do that by invoking +rails console --sandbox+. <shell> @@ -374,7 +380,6 @@ Rails version 4.0.0.beta JavaScript Runtime Node.js (V8) Active Record version 4.0.0.beta Action Pack version 4.0.0.beta -Active Resource version 4.0.0.beta Action Mailer version 4.0.0.beta Active Support version 4.0.0.beta Middleware ActionDispatch::Static, Rack::Lock, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport @@ -521,9 +526,7 @@ development: adapter: postgresql encoding: unicode database: gitapp_development - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 username: gitapp password: ... diff --git a/railties/guides/source/configuring.textile b/guides/source/configuring.textile index e796f44606..717654d5d8 100644 --- a/railties/guides/source/configuring.textile +++ b/guides/source/configuring.textile @@ -88,7 +88,7 @@ NOTE. The +config.asset_path+ configuration is ignored if the asset pipeline is * +config.filter_parameters+ used for filtering out the parameters that you don't want shown in the logs, such as passwords or credit card numbers. -* +config.force_ssl+ forces all requests to be under HTTPS protocol by using +Rack::SSL+ middleware. +* +config.force_ssl+ forces all requests to be under HTTPS protocol by using +ActionDispatch::SSL+ middleware. * +config.log_level+ defines the verbosity of the Rails logger. This option defaults to +:debug+ for all modes except production, where it defaults to +:info+. @@ -197,7 +197,7 @@ h4. Configuring Middleware Every Rails application comes with a standard set of middleware which it uses in this order in the development environment: -* +Rack::SSL+ forces every request to be under HTTPS protocol. Will be available if +config.force_ssl+ is set to +true+. Options passed to this can be configured by using +config.ssl_options+. +* +ActionDispatch::SSL+ forces every request to be under HTTPS protocol. Will be available if +config.force_ssl+ is set to +true+. Options passed to this can be configured by using +config.ssl_options+. * +ActionDispatch::Static+ is used to serve static assets. Disabled if +config.serve_static_assets+ is +true+. * +Rack::Lock+ wraps the app in mutex so it can only be called by a single thread at a time. Only enabled if +config.action_controller.allow_concurrency+ is set to +false+, which it is by default. * +ActiveSupport::Cache::Strategy::LocalCache+ serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread. @@ -248,6 +248,14 @@ They can also be removed from the stack completely: config.middleware.delete ActionDispatch::BestStandardsSupport </ruby> +In addition to these methods to handle the stack, if your application is going to be used as an API endpoint only, the middleware stack can be configured like this: + +<ruby> +config.middleware.http_only! +</ruby> + +By doing this, Rails will create a smaller middleware stack, by not adding some middlewares that are usually useful for browser access only, such as Cookies, Session and Flash, BestStandardsSupport, and MethodOverride. You can always add any of them later manually if you want. Refer to the "API App docs":api_app.html for more info on how to setup your application for API only apps. + h4. Configuring i18n * +config.i18n.default_locale+ sets the default locale of an application used for i18n. Defaults to +:en+. @@ -280,12 +288,12 @@ h4. Configuring Active Record * +config.active_record.whitelist_attributes+ will create an empty whitelist of attributes available for mass-assignment security for all models in your app. -* +config.active_record.identity_map+ controls whether the identity map is enabled, and is false by default. - * +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.dependent_restrict_raises+ will control the behavior when an object with a <tt>:dependent => :restrict</tt> association is deleted. Setting this to false will prevent +DeleteRestrictionError+ from being raised and instead will add an error on the model object. Defaults to false in the development mode. +* +config.active_record.mass_assignment_sanitizer+ will determine the strictness of the mass assignment sanitization within Rails. Defaults to +:strict+. In this mode, mass assigning any non-+attr_accessible+ attribute in a +create+ or +update_attributes+ call will raise an exception. Setting this option to +:logger+ will only print to the log file when an attribute is being assigned and will not raise an exception. + 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. @@ -346,8 +354,7 @@ h4. Configuring Action Dispatch h4. Configuring Action View -There are only a few configuration options for Action View, starting with six on +ActionView::Base+: - +<tt>config.action_view</tt> includes a small number of configuration settings: * +config.action_view.field_error_proc+ provides an HTML generator for displaying errors that come from Active Record. The default is @@ -387,6 +394,16 @@ And can reference in the view with the following code: * +config.action_view.cache_asset_ids+ With the cache enabled, the asset tag helper methods will make fewer expensive file system calls (the default implementation checks the file system timestamp). However this prevents you from modifying any asset files while the server is running. +* +config.action_view.embed_authenticity_token_in_remote_forms+ allows you to set the default behavior for +authenticity_token+ in forms with +:remote => true+. By default it's set to false, which means that remote forms will not include +authenticity_token+, which is helpful when you're fragment-caching the form. Remote forms get the authenticity from the +meta+ tag, so embedding is unnecessary unless you support browsers without JavaScript. In such case you can either pass +:authenticity_token => true+ as a form option or set this config setting to +true+ + +* +config.action_view.prefix_partial_path_with_controller_namespace+ determines whether or not partials are looked up from a subdirectory in templates rendered from namespaced controllers. For example, consider a controller named +Admin::PostsController+ which renders this template: + +<erb> +<%= render @post %> +<erb> + +The default setting is +true+, which uses the partial at +/admin/posts/_post.erb+. Setting the value to +false+ would render +/posts/_post.erb+, which is the same behavior as rendering from a non-namespaced controller such as +PostsController+. + h4. Configuring Action Mailer There are a number of settings available on +config.action_mailer+: @@ -429,12 +446,6 @@ config.action_mailer.observers = ["MailObserver"] config.action_mailer.interceptors = ["MailInterceptor"] </ruby> -h4. Configuring Active Resource - -There is a single configuration setting available on +config.active_resource+: - -* +config.active_resource.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Active Resource. Set to +nil+ to disable logging. - h4. Configuring Active Support There are a few configuration options available in Active Support: @@ -457,6 +468,99 @@ There are a few configuration options available in Active Support: * +ActiveSupport::Logger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+. +h4. Configuring a Database + +Just about every Rails application will interact with a database. The database to use is specified in a configuration file called +config/database.yml+. If you open this file in a new Rails application, you'll see a default database configured to use SQLite3. The file contains sections for three different environments in which Rails can run by default: + +* The +development+ environment is used on your development/local computer as you interact manually with the application. +* The +test+ environment is used when running automated tests. +* The +production+ environment is used when you deploy your application for the world to use. + +TIP: You don't have to update the database configurations manually. If you look at the options of the application generator, you will see that one of the options is named <tt>--database</tt>. This option allows you to choose an adapter from a list of the most used relational databases. You can even run the generator repeatedly: <tt>cd .. && rails new blog --database=mysql</tt>. When you confirm the overwriting of the +config/database.yml+ file, your application will be configured for MySQL instead of SQLite. Detailed examples of the common database connections are below. + +h5. Configuring an SQLite3 Database + +Rails comes with built-in support for "SQLite3":http://www.sqlite.org, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using an SQLite database when creating a new project, but you can always change it later. + +Here's the section of the default configuration file (<tt>config/database.yml</tt>) with connection information for the development environment: + +<yaml> +development: + adapter: sqlite3 + database: db/development.sqlite3 + pool: 5 + timeout: 5000 +</yaml> + +NOTE: Rails uses an SQLite3 database for data storage by default because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL "out of the box", and has plugins for many database systems. If you are using a database in a production environment Rails most likely has an adapter for it. + +h5. Configuring a MySQL Database + +If you choose to use MySQL instead of the shipped SQLite3 database, your +config/database.yml+ will look a little different. Here's the development section: + +<yaml> +development: + adapter: mysql2 + encoding: utf8 + database: blog_development + pool: 5 + username: root + password: + socket: /tmp/mysql.sock +</yaml> + +If your development computer's MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the +development+ section as appropriate. + +h5. Configuring a PostgreSQL Database + +If you choose to use PostgreSQL, your +config/database.yml+ will be customized to use PostgreSQL databases: + +<yaml> +development: + adapter: postgresql + encoding: unicode + database: blog_development + pool: 5 + username: blog + password: +</yaml> + +h5. Configuring an SQLite3 Database for JRuby Platform + +If you choose to use SQLite3 and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section: + +<yaml> +development: + adapter: jdbcsqlite3 + database: db/development.sqlite3 +</yaml> + +h5. Configuring a MySQL Database for JRuby Platform + +If you choose to use MySQL and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section: + +<yaml> +development: + adapter: jdbcmysql + database: blog_development + username: root + password: +</yaml> + +h5. Configuring a PostgreSQL Database for JRuby Platform + +If you choose to use PostgreSQL and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section: + +<yaml> +development: + adapter: jdbcpostgresql + encoding: unicode + database: blog_development + username: blog + password: +</yaml> + +Change the username and password in the +development+ section as appropriate. h3. Rails Environment Settings @@ -493,7 +597,7 @@ Rails has 5 initialization events which can be hooked into (listed in the order * +after_initialize+: Run directly after the initialization of the application, but before the application initializers are run. -To define an event for these hooks, use the block syntax within a +Rails::Aplication+, +Rails::Railtie+ or +Rails::Engine+ subclass: +To define an event for these hooks, use the block syntax within a +Rails::Application+, +Rails::Railtie+ or +Rails::Engine+ subclass: <ruby> module YourApp @@ -610,8 +714,6 @@ The error occurred while evaluating nil.each *+action_mailer.compile_config_methods+* Initializes methods for the config settings specified so that they are quicker to access. -*+active_resource.set_configs+* Sets up Active Resource by using the settings in +config.active_resource+ by +send+'ing the method names as setters to +ActiveResource::Base+ and passing the values through. - *+set_load_path+* This initializer runs before +bootstrap_hook+. Adds the +vendor+, +lib+, all directories of +app+ and any paths specified by +config.load_paths+ to +$LOAD_PATH+. *+set_autoload_paths+* This initializer runs before +bootstrap_hook+. Adds all sub-directories of +app+ and paths specified by +config.autoload_paths+ to +ActiveSupport::Dependencies.autoload_paths+. @@ -652,19 +754,17 @@ The error occurred while evaluating nil.each h3. Database pooling -Active Record database connections are managed by +ActiveRecord::ConnectionAdapters::ConnectionPool+ which ensures that a connection pool synchronizes the amount of thread access to a limited number of database connections. This limit defaults to 1 and can be configured in +database.yml+. +Active Record database connections are managed by +ActiveRecord::ConnectionAdapters::ConnectionPool+ which ensures that a connection pool synchronizes the amount of thread access to a limited number of database connections. This limit defaults to 5 and can be configured in +database.yml+. <ruby> development: adapter: sqlite3 database: db/development.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 timeout: 5000 </ruby> -Since the connection pooling is handled inside of Active Record by default, all application servers (Thin, Mongrel, Unicorn etc.) should behave the same. Initially, the database connection pool is empty and it will create additional connections as the demand for them increases, until it reaches the connection pool limit. +Since the connection pooling is handled inside of ActiveRecord by default, all application servers (Thin, mongrel, Unicorn etc.) should behave the same. Initially, the database connection pool is empty and it will create additional connections as the demand for them increases, until it reaches the connection pool limit. Any one request will check out a connection the first time it requires access to the database, after which it will check the connection back in, at the end of the request, meaning that the additional connection slot will be available again for the next request in the queue. diff --git a/railties/guides/source/contributing_to_ruby_on_rails.textile b/guides/source/contributing_to_ruby_on_rails.textile index aac5e13978..d0dbb1555a 100644 --- a/railties/guides/source/contributing_to_ruby_on_rails.textile +++ b/guides/source/contributing_to_ruby_on_rails.textile @@ -8,29 +8,29 @@ This guide covers ways in which _you_ can become a part of the ongoing developme * Contributing to the Ruby on Rails documentation * Contributing to the Ruby on Rails code -Ruby on Rails is not "someone else's framework." Over the years, hundreds of people have contributed to Ruby on Rails ranging from a single character to massive architectural changes or significant documentation. All with the goal of making Ruby on Rails better for everyone. Even if you don't feel up to writing code or documentation yet, there are a variety of other ways that you can contribute, from reporting issues to testing patches. +Ruby on Rails is not "someone else's framework." Over the years, hundreds of people have contributed to Ruby on Rails ranging from a single character to massive architectural changes or significant documentation -- all with the goal of making Ruby on Rails better for everyone. Even if you don't feel up to writing code or documentation yet, there are a variety of other ways that you can contribute, from reporting issues to testing patches. endprologue. h3. Reporting an Issue -Ruby on Rails uses "GitHub Issue Tracking":https://github.com/rails/rails/issues to track issues (primarily bugs and contributions of new code). If you've found a bug in Ruby on Rails, this is the place to start. You'll need to create a (free) GitHub account in order to either submit an issue, comment on them or create pull requests. +Ruby on Rails uses "GitHub Issue Tracking":https://github.com/rails/rails/issues to track issues (primarily bugs and contributions of new code). If you've found a bug in Ruby on Rails, this is the place to start. You'll need to create a (free) GitHub account in order to submit an issue, to comment on them or to create pull requests. NOTE: Bugs in the most recent released version of Ruby on Rails are likely to get the most attention. Also, the Rails core team is always interested in feedback from those who can take the time to test _edge Rails_ (the code for the version of Rails that is currently under development). Later in this guide you'll find out how to get edge Rails for testing. h4. Creating a Bug Report -If you've found a problem in Ruby on Rails which is not a security risk do a search in GitHub Issues in case it was already reported. If you find no issue addressing it you can "add a new one":https://github.com/rails/rails/issues/new. (See the next section for reporting security issues.) +If you've found a problem in Ruby on Rails which is not a security risk, do a search in GitHub under "Issues":https://github.com/rails/rails/issues in case it was already reported. If you find no issue addressing it you can "add a new one":https://github.com/rails/rails/issues/new. (See the next section for reporting security issues.) -At the minimum, your issue report needs a title and descriptive text. But that's only a minimum. You should include as much relevant information as possible. You need to at least post the code sample that has the issue. Even better is to include a unit test that shows how the expected behavior is not occurring. Your goal should be to make it easy for yourself - and others - to replicate the bug and figure out a fix. +At the minimum, your issue report needs a title and descriptive text. But that's only a minimum. You should include as much relevant information as possible. You need at least to post the code sample that has the issue. Even better is to include a unit test that shows how the expected behavior is not occurring. Your goal should be to make it easy for yourself -- and others -- to replicate the bug and figure out a fix. -Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kind of bug, you're creating this issue report in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the issue report will automatically see any activity or that others will jump to fix it. Creating an issue like this is mostly to help yourself start on the path of fixing the problem and for others to confirm it with a "I'm having this problem too" comment. +Then, don't get your hopes up! Unless you have a "Code Red, Mission Critical, the World is Coming to an End" kind of bug, you're creating this issue report in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the issue report will automatically see any activity or that others will jump to fix it. Creating an issue like this is mostly to help yourself start on the path of fixing the problem and for others to confirm it with an "I'm having this problem too" comment. h4. Special Treatment for Security Issues WARNING: Please do not report security vulnerabilities with public GitHub issue reports. The "Rails security policy page":http://rubyonrails.org/security details the procedure to follow for security issues. -h4. What About Feature Requests? +h4. What about Feature Requests? Please don't put "feature request" items into GitHub Issues. If there's a new feature that you want to see added to Ruby on Rails, you'll need to write the code yourself - or convince someone else to partner with you to write the code. Later in this guide you'll find detailed instructions for proposing a patch to Ruby on Rails. If you enter a wishlist item in GitHub Issues with no code, you can expect it to be marked "invalid" as soon as it's reviewed. @@ -38,7 +38,7 @@ h3. Running the Test Suite To move on from submitting bugs to helping resolve existing issues or contributing your own code to Ruby on Rails, you _must_ be able to run its test suite. In this section of the guide you'll learn how to set up the tests on your own computer. -h4. Install git +h4. Install Git Ruby on Rails uses git for source code control. The "git homepage":http://git-scm.com/ has installation instructions. There are a variety of resources on the net that will help you get familiar with git: @@ -66,7 +66,7 @@ Install first libxml2 and libxslt together with their development files for Noko $ sudo apt-get install libxml2 libxml2-dev libxslt1-dev </shell> -Also, SQLite3 and its development files for the +sqlite3-ruby+ gem, in Ubuntu you're done with +Also, SQLite3 and its development files for the +sqlite3-ruby+ gem -- in Ubuntu you're done with just <shell> $ sudo apt-get install sqlite3 libsqlite3-dev @@ -76,6 +76,7 @@ Get a recent version of "Bundler":http://gembundler.com/: <shell> $ gem install bundler +$ gem update bundler </shell> and run: @@ -84,20 +85,20 @@ and run: $ bundle install --without db </shell> -This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back at these soon. With dependencies installed, you can run the test suite with: +This command will install all dependencies except the MySQL and PostgreSQL Ruby drivers. We will come back to these soon. With dependencies installed, you can run the test suite with: <shell> $ bundle exec rake test </shell> -You can also run tests for a specific framework, like Action Pack, by going into its directory and executing the same command: +You can also run tests for a specific component, like Action Pack, by going into its directory and executing the same command: <shell> $ cd actionpack $ bundle exec rake test </shell> -If you want to run tests from the specific directory use the +TEST_DIR+ environment variable. For example, this will run tests inside +railties/test/generators+ directory only: +If you want to run the tests located in a specific directory use the +TEST_DIR+ environment variable. For example, this will run the tests of the +railties/test/generators+ directory only: <shell> $ cd railties @@ -106,9 +107,9 @@ $ TEST_DIR=generators bundle exec rake test h4. Warnings -The test suite runs with warnings enabled. Ideally Ruby on Rails should issue no warning, but there may be a few, and also some from third-party libraries. Please ignore (or fix!) them if any, and submit patches that do not issue new warnings. +The test suite runs with warnings enabled. Ideally, Ruby on Rails should issue no warnings, but there may be a few, as well as some from third-party libraries. Please ignore (or fix!) them, if any, and submit patches that do not issue new warnings. -As of this writing they are specially noisy with Ruby 1.9. If you are sure about what you are doing and would like to have a more clear output, there's a way to override the flag: +As of this writing (December, 2010) they are specially noisy with Ruby 1.9. If you are sure about what you are doing and would like to have a more clear output, there's a way to override the flag: <shell> $ RUBYOPT=-W0 bundle exec rake test @@ -116,17 +117,17 @@ $ RUBYOPT=-W0 bundle exec rake test h4. Testing Active Record -The test suite of Active Record attempts to run four times, once for SQLite3, once for each of the two MySQL gems (+mysql+ and +mysql2+), and once for PostgreSQL. We are going to see now how to setup the environment for them. +The test suite of Active Record attempts to run four times: once for SQLite3, once for each of the two MySQL gems (+mysql+ and +mysql2+), and once for PostgreSQL. We are going to see now how to set up the environment for them. WARNING: If you're working with Active Record code, you _must_ ensure that the tests pass for at least MySQL, PostgreSQL, and SQLite3. Subtle differences between the various adapters have been behind the rejection of many patches that looked OK when tested only against MySQL. -h5. Set up Database Configuration +h5. Database Configuration The Active Record test suite requires a custom config file: +activerecord/test/config.yml+. An example is provided in +activerecord/test/config.example.yml+ which can be copied and used as needed for your environment. h5. SQLite3 -The gem +sqlite3-ruby+ does not belong to the "db" group indeed, if you followed the instructions above you're ready. This is how you run the Active Record test suite only for SQLite3: +The gem +sqlite3-ruby+ does not belong to the "db" group. Indeed, if you followed the instructions above you're ready. This is how you run the Active Record test suite only for SQLite3: <shell> $ cd activerecord @@ -167,13 +168,13 @@ $ cd activerecord $ rake mysql:build_databases </shell> -PostgreSQL's authentication works differently. A simple way to setup the development environment for example is to run with your development account +PostgreSQL's authentication works differently. A simple way to set up the development environment for example is to run with your development account <shell> $ sudo -u postgres createuser --superuser $USER </shell> -and after that create the test databases with +and then create the test databases with <shell> $ cd activerecord @@ -182,9 +183,9 @@ $ rake postgresql:build_databases NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation. -If you’re using another database, check the files under +activerecord/test/connections+ for default connection information. You can edit these files if you _must_ on your machine to provide different credentials, but obviously you should not push any such changes back to Rails. +If you’re using another database, check the files under +activerecord/test/connections+ for default connection information. You can edit these files to provide different credentials on your machine if you must, but obviously you should not push any such changes back to Rails. -You can now run tests as you did for +sqlite3+, the tasks are +You can now run the tests as you did for +sqlite3+. The tasks are respectively <shell> test_mysql @@ -192,7 +193,7 @@ test_mysql2 test_postgresql </shell> -respectively. As we mentioned before +As we mentioned before <shell> $ bundle exec rake test @@ -200,9 +201,9 @@ $ bundle exec rake test will now run the four of them in turn. -You can also invoke +test_jdbcmysql+, +test_jdbcsqlite3+ or +test_jdbcpostgresql+. Check out the file +activerecord/RUNNING_UNIT_TESTS+ for information on running more targeted database tests, or the file +ci/travis.rb+ to see the test suite that the continuous integration server runs. +You can invoke +test_jdbcmysql+, +test_jdbcsqlite3+ or +test_jdbcpostgresql+ also. See the file +activerecord/RUNNING_UNIT_TESTS+ for information on running more targeted database tests, or the file +ci/travis.rb+ for the test suite run by the continuous integration server. -h4. Older versions of Ruby on Rails +h4. Older Versions of Ruby on Rails If you want to add a fix to older versions of Ruby on Rails, you'll need to set up and switch to your own local tracking branch. Here is an example to switch to the 3-0-stable branch: @@ -219,9 +220,9 @@ As a next step beyond reporting issues, you can help the core team resolve exist h4. Verifying Bug Reports -For starters, it helps to just verify bug reports. Can you reproduce the reported issue on your own computer? If so, you can add a comment to the issue saying that you're seeing the same thing. +For starters, it helps just to verify bug reports. Can you reproduce the reported issue on your own computer? If so, you can add a comment to the issue saying that you're seeing the same thing. -If something is very vague, can you help squish it down into something specific? Maybe you can provide additional information to help reproduce a bug, or eliminate needless steps that aren't required to help demonstrate the problem. +If something is very vague, can you help squash it down into something specific? Maybe you can provide additional information to help reproduce a bug, or help by eliminating needless steps that aren't required to demonstrate the problem. If you find a bug report without a test, it's very useful to contribute a failing test. This is also a great way to get started exploring the source code: looking at the existing test files will teach you how to write more tests. New tests are best contributed in the form of a patch, as explained later on in the "Contributing to the Rails Code" section. @@ -229,51 +230,51 @@ Anything you can do to make bug reports more succinct or easier to reproduce is h4. Testing Patches -You can also help out by examining pull requests that have been submitted to Ruby on Rails via GitHub. To apply someone's changes you need to first create a dedicated branch: +You can also help out by examining pull requests that have been submitted to Ruby on Rails via GitHub. To apply someone's changes you need first to create a dedicated branch: <shell> $ git checkout -b testing_branch </shell> -Then you can use their remote branch to update your codebase. For example, let's say the GitHub user JohnSmith has forked and pushed to the topic branch located at https://github.com/JohnSmith/rails. +Then you can use their remote branch to update your codebase. For example, let's say the GitHub user JohnSmith has forked and pushed to a topic branch "orange" located at https://github.com/JohnSmith/rails. <shell> $ git remote add JohnSmith git://github.com/JohnSmith/rails.git -$ git pull JohnSmith topic +$ git pull JohnSmith orange </shell> After applying their branch, test it out! Here are some things to think about: * Does the change actually work? * Are you happy with the tests? Can you follow what they're testing? Are there any tests missing? -* Does it have proper documentation coverage? Should documentation elsewhere be updated? +* Does it have the proper documentation coverage? Should documentation elsewhere be updated? * Do you like the implementation? Can you think of a nicer or faster way to implement a part of their change? Once you're happy that the pull request contains a good change, comment on the GitHub issue indicating your approval. Your comment should indicate that you like the change and what you like about it. Something like: <blockquote> -I like the way you've restructured that code in generate_finder_sql, much nicer. The tests look good too. +I like the way you've restructured that code in generate_finder_sql -- much nicer. The tests look good too. </blockquote> If your comment simply says "+1", then odds are that other reviewers aren't going to take it too seriously. Show that you took the time to review the pull request. h3. Contributing to the Rails Documentation -Ruby on Rails has two main sets of documentation: The guides help you to learn Ruby on Rails, and the API is a reference. +Ruby on Rails has two main sets of documentation: the guides help you in learning about Ruby on Rails, and the API is a reference. -You can help improve the Rails guides by making them more coherent, adding missing information, correcting factual errors, fixing typos, bringing it up to date with the latest edge Rails. To get involved in the translation of Rails guides, please see "Translating Rails Guides":https://wiki.github.com/lifo/docrails/translating-rails-guides. +You can help improve the Rails guides by making them more coherent, consistent or readable, adding missing information, correcting factual errors, fixing typos, or bringing it up to date with the latest edge Rails. To get involved in the translation of Rails guides, please see "Translating Rails Guides":https://wiki.github.com/lifo/docrails/translating-rails-guides. -If you're confident about your changes, you can push them yourself directly via "docrails":https://github.com/lifo/docrails. docrails is a branch with an *open commit policy* and public write access. Commits to docrails are still reviewed, but that happens after they are pushed. docrails is merged with master regularly, so you are effectively editing the Ruby on Rails documentation. +If you're confident about your changes, you can push them directly yourself via "docrails":https://github.com/lifo/docrails. Docrails is a branch with an *open commit policy* and public write access. Commits to docrails are still reviewed, but this happens after they are pushed. Docrails is merged with master regularly, so you are effectively editing the Ruby on Rails documentation. If you are unsure of the documentation changes, you can create an issue in the "Rails":https://github.com/rails/rails/issues issues tracker on GitHub. When working with documentation, please take into account the "API Documentation Guidelines":api_documentation_guidelines.html and the "Ruby on Rails Guides Guidelines":ruby_on_rails_guides_guidelines.html. -NOTE: As explained earlier, ordinary code patches should have proper documentation coverage. docrails is only used for isolated documentation improvements. +NOTE: As explained earlier, ordinary code patches should have proper documentation coverage. Docrails is only used for isolated documentation improvements. -NOTE: To help our CI servers you can add [ci skip] tag to your documentation commit message to skip build on that commit. Please remember to use it for commits containing only documentation changes. +NOTE: To help our CI servers you can add [ci skip] to your documentation commit message to skip build on that commit. Please remember to use it for commits containing only documentation changes. -WARNING: docrails has a very strict policy: no code can be touched whatsoever, no matter how trivial or small the change. Only RDoc and guides can be edited via docrails. Also, CHANGELOGs should never be edited in docrails. +WARNING: Docrails has a very strict policy: no code can be touched whatsoever, no matter how trivial or small the change. Only RDoc and guides can be edited via docrails. Also, CHANGELOGs should never be edited in docrails. h3. Contributing to the Rails Code @@ -292,37 +293,37 @@ $ cd rails $ git checkout -b my_new_branch </shell> -It doesn’t really matter what name you use, because this branch will only exist on your local computer and your personal repository on Github. It won't be part of the Rails git repository. +It doesn’t matter much what name you use, because this branch will only exist on your local computer and your personal repository on Github. It won't be part of the Rails git repository. h4. Write Your Code Now get busy and add or edit code. You’re on your branch now, so you can write whatever you want (you can check to make sure you’re on the right branch with +git branch -a+). But if you’re planning to submit your change back for inclusion in Rails, keep a few things in mind: -* Get the code right -* Use Rails idioms and helpers -* Include tests that fail without your code, and pass with it -* Update the documentation, the surrounding one, examples elsewhere, guides, whatever is affected by your contribution +* Get the code right. +* Use Rails idioms and helpers. +* Include tests that fail without your code, and pass with it. +* Update the (surrounding) documentation, examples elsewhere, and the guides: whatever is affected by your contribution. h4. Follow the Coding Conventions Rails follows a simple set of coding style conventions. -* Two spaces, no tabs. -* No trailing whitespace. Blank lines should not have any space. -* Outdent private/protected from method definitions. Same indentation as the class/module. +* Two spaces, no tabs (for indentation). +* No trailing whitespace. Blank lines should not have any spaces. +* Indent after private/protected. * Prefer +&&+/+||+ over +and+/+or+. -* Prefer class << self block over self.method for class methods. -* +MyClass.my_method(my_arg)+ not +my_method( my_arg )+ or +my_method my_arg+. -* a = b and not a=b. -* Follow the conventions you see used in the source already. +* Prefer class << self over self.method for class methods. +* Use +MyClass.my_method(my_arg)+ not +my_method( my_arg )+ or +my_method my_arg+. +* Use a = b and not a=b. +* Follow the conventions in the source you see used already. -These are some guidelines and please use your best judgment in using them. +The above are guidelines -- please use your best judgment in using them. h4. Sanity Check You should not be the only person who looks at the code before you submit it. You know at least one other Rails developer, right? Show them what you’re doing and ask for feedback. Doing this in private before you push a patch out publicly is the “smoke test” for a patch: if you can’t convince one other developer of the beauty of your code, you’re unlikely to convince the core team either. -You might also want to check out the "RailsBridge BugMash":http://wiki.railsbridge.org/projects/railsbridge/wiki/BugMash as a way to get involved in a group effort to improve Rails. This can help you get started and help check your code when you're writing your first patches. +You might want also to check out the "RailsBridge BugMash":http://wiki.railsbridge.org/projects/railsbridge/wiki/BugMash as a way to get involved in a group effort to improve Rails. This can help you get started and help you check your code when you're writing your first patches. h4. Commit Your Changes @@ -332,7 +333,9 @@ When you're happy with the code on your computer, you need to commit the changes $ git commit -a -m "Here is a commit message on what I changed in this commit" </shell> -h4. Update master +TIP. Please squash your commits into a single commit when appropriate. This simplifies future cherry picks, and also keeps the git log clean. + +h4. Update Master It’s pretty likely that other changes to master have happened while you were working. Go get them: @@ -370,13 +373,13 @@ h4. Issue a Pull Request Navigate to the Rails repository you just pushed to (e.g. https://github.com/your-user-name/rails) and press "Pull Request" in the upper right hand corner. -Write your branch name in branch field (is filled with master by default) and press "Update Commit Range" +Write your branch name in the branch field (this is filled with "master" by default) and press "Update Commit Range". -Ensure the changesets you introduced are included in the "Commits" tab and that the "Files Changed" incorporate all of your changes. +Ensure the changesets you introduced are included in the "Commits" tab. Ensure that the "Files Changed" incorporate all of your changes. -Fill in some details about your potential patch including a meaningful title. When finished, press "Send pull request." Rails Core will be notified about your submission. +Fill in some details about your potential patch including a meaningful title. When finished, press "Send pull request". The Rails core team will be notified about your submission. -h4. Get Some Feedback +h4. Get some Feedback Now you need to get other people to look at your patch, just as you've looked at other people's patches. You can use the "rubyonrails-core mailing list":http://groups.google.com/group/rubyonrails-core/ or the #rails-contrib channel on IRC freenode for this. You might also try just talking to Rails developers that you know. diff --git a/railties/guides/source/credits.html.erb b/guides/source/credits.html.erb index da6bd6acdf..da6bd6acdf 100644 --- a/railties/guides/source/credits.html.erb +++ b/guides/source/credits.html.erb diff --git a/railties/guides/source/debugging_rails_applications.textile b/guides/source/debugging_rails_applications.textile index 57c7786636..57c7786636 100644 --- a/railties/guides/source/debugging_rails_applications.textile +++ b/guides/source/debugging_rails_applications.textile diff --git a/railties/guides/source/documents.yaml b/guides/source/documents.yaml index 08aafda288..2acdcca39c 100644 --- a/railties/guides/source/documents.yaml +++ b/guides/source/documents.yaml @@ -97,6 +97,11 @@ url: asset_pipeline.html description: This guide documents the asset pipeline. - + name: Getting Started with Engines + url: engines.html + description: This guide explains how to write a mountable engine. + work_in_progress: true + - name: The Rails Initialization Process work_in_progress: true url: initialization.html diff --git a/railties/guides/source/engines.textile b/guides/source/engines.textile index e6058e7581..36210aedb0 100644 --- a/railties/guides/source/engines.textile +++ b/guides/source/engines.textile @@ -16,7 +16,7 @@ Engines can be considered miniature applications that provide functionality to t Therefore, engines and applications can be thought of almost the same thing, just with very minor differences, as you'll see throughout this guide. Engines and applications also share a common structure. -Engines are also closely related to plugins where the two share a common +lib+ directory structure and are both generated using the +rails plugin new+ generator. The difference being that an engine is considered a "full plugin" by Rails -- as indicated by the +--full+ option that's passed to the generator command -- but this guide will refer to them simply as "engines" throughout. An engine *can* be a plugin, and a plugin *can* be an engine. +Engines are also closely related to plugins where the two share a common +lib+ directory structure and are both generated using the +rails plugin new+ generator. The difference being that an engine is considered a "full plugin" by Rails as indicated by the +--full+ option that's passed to the generator command, but this guide will refer to them simply as "engines" throughout. An engine *can* be a plugin, and a plugin *can* be an engine. The engine that will be created in this guide will be called "blorgh". The engine will provide blogging functionality to its host applications, allowing for new posts and comments to be created. At the beginning of this guide, you will be working solely within the engine itself, but in later sections you'll see how to hook it into an application. @@ -51,7 +51,7 @@ h5. Critical files At the root of this brand new engine's directory, lives a +blorgh.gemspec+ file. When you include the engine into the application later on, you will do so with this line in a Rails application's +Gemfile+: <ruby> - gem 'blorgh', :path => "vendor/engines/blorgh" +gem 'blorgh', :path => "vendor/engines/blorgh" </ruby> By specifying it as a gem within the +Gemfile+, Bundler will load it as such, parsing this +blorgh.gemspec+ file and requiring a file within the +lib+ directory called +lib/blorgh.rb+. This file requires the +blorgh/engine.rb+ file (located at +lib/blorgh/engine.rb+) and defines a base module called +Blorgh+. @@ -77,7 +77,7 @@ end By inheriting from the +Rails::Engine+ class, this gem notifies Rails that there's an engine at the specified path, and will correctly mount the engine inside the application, performing tasks such as adding the +app+ directory of the engine to the load path for models, mailers, controllers and views. -The +isolate_namespace+ method here deserves special notice. This call is responsible for isolating the controllers, models, routes and other things into their own namespace, away from similar components inside hte application. Without this, there is a possibility that the engine's components could "leak" into the application, causing unwanted disruption, or that important engine components could be overriden by similarly named things within the application. +The +isolate_namespace+ method here deserves special notice. This call is responsible for isolating the controllers, models, routes and other things into their own namespace, away from similar components inside the application. Without this, there is a possibility that the engine's components could "leak" into the application, causing unwanted disruption, or that important engine components could be overridden by similarly named things within the application. One of the examples of such conflicts are helpers. Without calling +isolate_namespace+, engine's helpers would be included in application's controllers. NOTE: It is *highly* recommended that the +isolate_namespace+ line be left within the +Engine+ class definition. Without it, classes generated in an engine *may* conflict with an application. @@ -115,7 +115,6 @@ The +test+ directory is where tests for the engine will go. To test the engine, <ruby> Rails.application.routes.draw do - mount Blorgh::Engine => "/blorgh" end </ruby> @@ -126,7 +125,7 @@ Also in the test directory is the +test/integration+ directory, where integratio h3. Providing engine functionality -The engine that this guide covers will provide posting and commenting functionality and follows a similar thread to the "Getting Started Guide":getting-started.html, with some new twists. +The engine that this guide covers will provide posting and commenting functionality and follows a similar thread to the "Getting Started Guide":getting_started.html, with some new twists. h4. Generating a post resource @@ -179,7 +178,6 @@ After that, a line for the resource is inserted into the +config/routes.rb+ file <ruby> Blorgh::Engine.routes.draw do resources :posts - end </ruby> @@ -219,17 +217,13 @@ By default, the scaffold styling is not applied to the engine as the engine's la <%= stylesheet_link_tag "scaffold" %> </erb> -You can see what the engine has so far by running +rake db:migrate+ at the root of our engine to run the migration generated by the scaffold generator, and then running +rails server+. When you open +http://localhost:3000/blorgh/posts+ you will see the default scaffold that has been generated. - -!images/engines_scaffold.png(Blank engine scaffold)! - -Click around! You've just generated your first engine's first functions. +You can see what the engine has so far by running +rake db:migrate+ at the root of our engine to run the migration generated by the scaffold generator, and then running +rails server+ in +test/dummy+. When you open +http://localhost:3000/blorgh/posts+ you will see the default scaffold that has been generated. Click around! You've just generated your first engine's first functions. If you'd rather play around in the console, +rails console+ will also work just like a Rails application. Remember: the +Post+ model is namespaced, so to reference it you must call it as +Blorgh::Post+. <ruby> - >> Blorgh::Post.find(1) - => #<Blorgh::Post id: 1 ...> +>> Blorgh::Post.find(1) +=> #<Blorgh::Post id: 1 ...> </ruby> One final thing is that the +posts+ resource for this engine should be the root of the engine. Whenever someone goes to the root path where the engine is mounted, they should be shown a list of posts. This can be made to happen if this line is inserted into the +config/routes.rb+ file inside the engine: @@ -263,7 +257,7 @@ create test/fixtures/blorgh/comments.yml This generator call will generate just the necessary model files it needs, namespacing the files under a +blorgh+ directory and creating a model class called +Blorgh::Comment+. -To show the comments on a post, edit +app/views/posts/show.html.erb+ and add this line before the "Edit" link: +To show the comments on a post, edit +app/views/blorgh/posts/show.html.erb+ and add this line before the "Edit" link: <erb> <h3>Comments</h3> @@ -315,7 +309,7 @@ resources :posts do end </ruby> -This creates a nested route for the comments, which is what the form requires. +This creates a nested route for the comments, which is what the form requires. The route now exists, but the controller that this route goes to does not. To create it, run this command: @@ -355,13 +349,13 @@ end This is the final part required to get the new comment form working. Displaying the comments however, is not quite right yet. If you were to create a comment right now you would see this error: -<text> - Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder], :formats=>[:html], :locale=>[:en, :en]}. Searched in: - * "/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views" - * "/Users/ryan/Sites/side_projects/blorgh/app/views" -</text> +<plain> +Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder], :formats=>[:html], :locale=>[:en, :en]}. Searched in: + * "/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views" + * "/Users/ryan/Sites/side_projects/blorgh/app/views" +</plain> -The engine is unable to find the partial required for rendering the comments. Rails has looked firstly in the application's (+test/dummy+) +app/views+ directory and then in the engine's +app/views+ directory. When it can't find it, it will throw this error. The engine knows to look for +blorgh/comments/comment+ because the model object it is receiving is from the +Blorgh::Comment+ class. +The engine is unable to find the partial required for rendering the comments. Rails has looked firstly in the application's (+test/dummy+) +app/views+ directory and then in the engine's +app/views+ directory. When it can't find it, it will throw this error. The engine knows to look for +blorgh/comments/comment+ because the model object it is receiving is from the +Blorgh::Comment+ class. This partial will be responsible for rendering just the comment text, for now. Create a new file at +app/views/blorgh/comments/_comment.html.erb+ and put this line inside it: @@ -440,6 +434,18 @@ The first timestamp (+\[timestamp_1\]+) will be the current time and the second To run these migrations within the context of the application, simply run +rake db:migrate+. When accessing the engine through +http://localhost:3000/blog+, the posts will be empty. This is because the table created inside the application is different from the one created within the engine. Go ahead, play around with the newly mounted engine. You'll find that it's the same as when it was only an engine. +If you would like to run migrations only from one engine, you can do it by specifying +SCOPE+: + +<shell> +rake db:migrate SCOPE=blorgh +</shell> + +This may be useful if you want to revert engine's migrations before removing it. In order to revert all migrations from blorgh engine you can run such code: + +<shell> +rake db:migrate SCOPE=blorgh VERSION=0 +</shell> + h4. Using a class provided by the application When an engine is created, it may want to use specific classes from an application to provide links between the pieces of the engine and the pieces of the application. In the case of the +blorgh+ engine, making posts and comments have authors would make a lot of sense. @@ -481,12 +487,12 @@ private end </ruby> -By defining that the +author+ association's object is represented by the +User+ class a link is established between the engine and the application. There needs to be a way of associating the records in the +blorgh_posts+ table with the records in the +users+ table. Because the association is called +author+, there should be an +author_id+ column added to the +blorgh_posts+ table. +By defining that the +author+ association's object is represented by the +User+ class a link is established between the engine and the application. There needs to be a way of associating the records in the +blorgh_posts+ table with the records in the +users+ table. Because the association is called +author+, there should be an +author_id+ column added to the +blorgh_posts+ table. To generate this new column, run this command within the engine: <shell> -$ rails g migration add_author_id_to_blorgh_posts author_id:integer +$ rails g migration add_author_id_to_blorgh_posts author_id:integer </shell> NOTE: Due to the migration's name and the column specification after it, Rails will automatically know that you want to add a column to a specific table and write that into the migration for you. You don't need to tell it any more than this. @@ -499,11 +505,11 @@ $ rake blorgh:install:migrations Notice here that only _one_ migration was copied over here. This is because the first two migrations were copied over the first time this command was run. -<shell> - NOTE: Migration [timestamp]_create_blorgh_posts.rb from blorgh has been skipped. Migration with the same name already exists. - NOTE: Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists. - Copied migration [timestamp]_add_author_id_to_blorgh_posts.rb from blorgh -</shell> +<plain> +NOTE Migration [timestamp]_create_blorgh_posts.rb from blorgh has been skipped. Migration with the same name already exists. +NOTE Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists. +Copied migration [timestamp]_add_author_id_to_blorgh_posts.rb from blorgh +</plain> Run this migration using this command: @@ -524,9 +530,9 @@ Finally, the author's name should be displayed on the post's page. Add this code By outputting +@post.author+ using the +<%=+ tag the +to_s+ method will be called on the object. By default, this will look quite ugly: -<text> +<plain> #<User:0x00000100ccb3b0> -</text> +</plain> This is undesirable and it would be much better to have the user's name there. To do this, add a +to_s+ method to the +User+ class within the application: @@ -569,9 +575,9 @@ self.author = Blorgh.user_class.constantize.find_or_create_by_name(author_name) To save having to call +constantize+ on the +user_class+ result all the time, you could instead just override the +user_class+ getter method inside the +Blorgh+ module in the +lib/blorgh.rb+ file to always call +constantize+ on the saved value before returning the result: <ruby> - def self.user_class - @@user_class.constantize - end +def self.user_class + @@user_class.constantize +end </ruby> This would then turn the above code for +self.author=+ into this: @@ -651,10 +657,6 @@ Try this now by creating a new file at +app/views/blorgh/posts/index.html.erb+ a <% end %> </erb> -Rather than looking like the default scaffold, the page will now look like this: - -!images/engines_post_override.png(Engine scaffold overriden)! - h4. Routes Routes inside an engine are, by default, isolated from the application. This is done by the +isolate_namespace+ call inside the +Engine+ class. This essentially means that the application and its engines can have identically named routes, and that they will not clash. @@ -662,9 +664,9 @@ Routes inside an engine are, by default, isolated from the application. This is Routes inside an engine are drawn on the +Engine+ class within +config/routes.rb+, like this: <ruby> - Blorgh::Engine.routes.draw do - resources :posts - end +Blorgh::Engine.routes.draw do + resources :posts +end </ruby> By having isolated routes such as this, if you wish to link to an area of an engine from within an application, you will need to use the engine's routing proxy method. Calls to normal routing methods such as +posts_path+ may end up going to undesired locations if both the application and the engine both have such a helper defined. @@ -705,11 +707,26 @@ Imagine that you did have an asset located at +app/assets/stylesheets/blorgh/sty You can also specify these assets as dependencies of other assets using the Asset Pipeline require statements in processed files: -<css> +<plain> /* *= require blorgh/style */ -</css> +</plain> + +h4. Separate Assets & Precompiling + +There are some situations where your engine's assets not required by the host application. For example, say that you've created +an admin functionality that only exists for your engine. In this case, the host application doesn't need to require +admin.css+ +or +admin.js+. Only the gem's admin layout needs these assets. It doesn't make sense for the host app to include +"blorg/admin.css"+ in it's stylesheets. In this situation, you should explicitly define these assets for precompilation. +This tells sprockets to add you engine assets when +rake assets:precompile+ is ran. + +You can define assets for precompilation in +engine.rb+ + +<ruby> +initializer do |app| + app.config.assets.precompile += %w(admin.css admin.js) +end +</ruby> For more information, read the "Asset Pipeline guide":http://guides.rubyonrails.org/asset_pipeline.html diff --git a/railties/guides/source/form_helpers.textile b/guides/source/form_helpers.textile index a696e4f8ae..8934667c5e 100644 --- a/railties/guides/source/form_helpers.textile +++ b/guides/source/form_helpers.textile @@ -39,7 +39,7 @@ When called without arguments like this, it creates a +<form>+ tag which, </form> </html> -Now, you'll notice that the HTML contains something extra: a +div+ element with two hidden input elements inside. This div is important, because the form cannot be successfully submitted without it. The first input element with name +utf8+ enforces browsers to properly respect your form's character encoding and is generated for all forms whether their actions are "GET" or "POST". The second input element with name +authenticity_token+ is a security feature of Rails called *cross-site request forgery protection*, and form helpers generate it for every non-GET form (provided that this security feature is enabled). You can read more about this in the "Security Guide":./security.html#_cross_site_reference_forgery_csrf. +Now, you'll notice that the HTML contains something extra: a +div+ element with two hidden input elements inside. This div is important, because the form cannot be successfully submitted without it. The first input element with name +utf8+ enforces browsers to properly respect your form's character encoding and is generated for all forms whether their actions are "GET" or "POST". The second input element with name +authenticity_token+ is a security feature of Rails called *cross-site request forgery protection*, and form helpers generate it for every non-GET form (provided that this security feature is enabled). You can read more about this in the "Security Guide":./security.html#cross-site-request-forgery-csrf. NOTE: Throughout this guide, the +div+ with the hidden input elements will be excluded from code samples for brevity. @@ -169,11 +169,11 @@ Output: <textarea id="message" name="message" cols="24" rows="6">Hi, nice site</textarea> <input id="password" name="password" type="password" /> <input id="parent_id" name="parent_id" type="hidden" value="5" /> -<input id="user_name" name="user[name]" size="30" type="search" /> -<input id="user_phone" name="user[phone]" size="30" type="tel" /> +<input id="user_name" name="user[name]" type="search" /> +<input id="user_phone" name="user[phone]" type="tel" /> <input id="user_born_on" name="user[born_on]" type="date" /> -<input id="user_homepage" size="30" name="user[homepage]" type="url" /> -<input id="user_address" size="30" name="user[address]" type="email" /> +<input id="user_homepage" name="user[homepage]" type="url" /> +<input id="user_address" name="user[address]" type="email" /> </html> Hidden inputs are not shown to the user but instead hold data like any textual input. Values inside them can be changed with JavaScript. @@ -239,7 +239,7 @@ The resulting HTML is: <html> <form accept-charset="UTF-8" action="/articles/create" method="post" class="nifty_form"> - <input id="article_title" name="article[title]" size="30" type="text" /> + <input id="article_title" name="article[title]" type="text" /> <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea> <input name="commit" type="submit" value="Create" /> </form> @@ -264,8 +264,8 @@ which produces the following output: <html> <form accept-charset="UTF-8" action="/people/create" class="new_person" id="new_person" method="post"> - <input id="person_name" name="person[name]" size="30" type="text" /> - <input id="contact_detail_phone_number" name="contact_detail[phone_number]" size="30" type="text" /> + <input id="person_name" name="person[name]" type="text" /> + <input id="contact_detail_phone_number" name="contact_detail[phone_number]" type="text" /> </form> </html> @@ -428,7 +428,7 @@ As with other helpers, if you were to use the +select+ helper on a form builder <%= f.select(:city_id, ...) %> </erb> -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 <tt> ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) </tt> 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 <tt> ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) </tt> 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. h4. Option Tags from a Collection of Arbitrary Objects @@ -714,9 +714,9 @@ Assuming the person had two addresses, with ids 23 and 45 this would create outp <html> <form accept-charset="UTF-8" action="/people/1" class="edit_person" id="edit_person_1" method="post"> - <input id="person_name" name="person[name]" size="30" type="text" /> - <input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" /> - <input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" /> + <input id="person_name" name="person[name]" type="text" /> + <input id="person_address_23_city" name="person[address][23][city]" type="text" /> + <input id="person_address_45_city" name="person[address][45][city]" type="text" /> </form> </html> @@ -739,7 +739,7 @@ To create more intricate nestings, you can specify the first part of the input n will create inputs like <html> -<input id="person_address_primary_1_city" name="person[address][primary][1][city]" size="30" type="text" value="bologna" /> +<input id="person_address_primary_1_city" name="person[address][primary][1][city]" type="text" value="bologna" /> </html> As a general rule the final input name is the concatenation of the name given to +fields_for+/+form_for+, the index value and the name of the attribute. You can also pass an +:index+ option directly to helpers such as +text_field+, but it is usually less repetitive to specify this at the form builder level rather than on individual input controls. diff --git a/railties/guides/source/generators.textile b/guides/source/generators.textile index 920ff997ae..920ff997ae 100644 --- a/railties/guides/source/generators.textile +++ b/guides/source/generators.textile diff --git a/railties/guides/source/getting_started.textile b/guides/source/getting_started.textile index d6f3c3e217..d12606c5de 100644 --- a/railties/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -13,6 +13,8 @@ endprologue. WARNING. This Guide is based on Rails 3.1. Some of the code shown here will not work in earlier versions of Rails. +WARNING: The Edge version of this guide is currently being re-worked. Please excuse us while we re-arrange the place. + h3. Guide Assumptions This guide is designed for beginners who want to get started with a Rails @@ -61,164 +63,17 @@ tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience. -The Rails philosophy includes several guiding principles: +The Rails philosophy includes two major guiding principles: * DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing. * Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to do it, rather than requiring you to specify every little thing through endless configuration files. -* REST is the best pattern for web applications - organizing your application around resources and standard HTTP verbs -is the fastest way to go. - -h4. The MVC Architecture - -At the core of Rails is the Model, View, Controller architecture, usually just -called MVC. MVC benefits include: - -* Isolation of business logic from the user interface -* Ease of keeping code DRY -* Making it clear where different types of code belong for easier maintenance - -h5. Models - -A model represents the information (data) of the application and the rules to -manipulate that data. In the case of Rails, models are primarily used for -managing the rules of interaction with a corresponding database table. In most -cases, each table in your database will correspond to one model in your -application. The bulk of your application's business logic will be concentrated -in the models. - -h5. Views - -Views represent the user interface of your application. In Rails, views are -often HTML files with embedded Ruby code that perform tasks related solely to -the presentation of the data. Views handle the job of providing data to the web -browser or other tool that is used to make requests from your application. - -h5. Controllers - -Controllers provide the "glue" between models and views. In Rails, controllers -are responsible for processing the incoming requests from the web browser, -interrogating the models for data, and passing that data on to the views for -presentation. - -h4. The Components of Rails - -Rails ships as many individual components. Each of these components are briefly -explained below. If you are new to Rails, as you read this section, don't get -hung up on the details of each component, as they will be explained in further -detail later. For instance, we will bring up Rack applications, but you don't -need to know anything about them to continue with this guide. - -* Action Pack - ** Action Controller - ** Action Dispatch - ** Action View -* Action Mailer -* Active Model -* Active Record -* Active Resource -* Active Support -* Railties - -h5. Action Pack - -Action Pack is a single gem that contains Action Controller, Action View and -Action Dispatch. The "VC" part of "MVC". - -h6. Action Controller - -Action Controller is the component that manages the controllers in a Rails -application. The Action Controller framework processes incoming requests to a -Rails application, extracts parameters, and dispatches them to the intended -action. Services provided by Action Controller include session management, -template rendering, and redirect management. - -h6. Action View - -Action View manages the views of your Rails application. It can create both HTML -and XML output by default. Action View manages rendering templates, including -nested and partial templates, and includes built-in AJAX support. View -templates are covered in more detail in another guide called "Layouts and -Rendering":layouts_and_rendering.html. - -h6. Action Dispatch - -Action Dispatch handles routing of web requests and dispatches them as you want, -either to your application or any other Rack application. Rack applications are -a more advanced topic and are covered in a separate guide called "Rails on -Rack":rails_on_rack.html. - -h5. Action Mailer - -Action Mailer is a framework for building e-mail services. You can use Action -Mailer to receive and process incoming email and send simple plain text or -complex multipart emails based on flexible templates. - -h5. Active Model - -Active Model provides a defined interface between the Action Pack gem services -and Object Relationship Mapping gems such as Active Record. Active Model allows -Rails to utilize other ORM frameworks in place of Active Record if your -application needs this. - -h5. Active Record - -Active Record is the base for the models in a Rails application. It provides -database independence, basic CRUD functionality, advanced finding capabilities, -and the ability to relate models to one another, among other services. - -h5. Active Resource - -Active Resource provides a framework for managing the connection between -business objects and RESTful web services. It implements a way to map web-based -resources to local objects with CRUD semantics. - -h5. Active Support - -Active Support is an extensive collection of utility classes and standard Ruby -library extensions that are used in Rails, both by the core code and by your -applications. - -h5. Railties - -Railties is the core Rails code that builds new Rails applications and glues the -various frameworks and plugins together in any Rails application. - -h4. REST - -Rest stands for Representational State Transfer and is the foundation of the -RESTful architecture. This is generally considered to be Roy Fielding's doctoral -thesis, "Architectural Styles and the Design of Network-based Software -Architectures":http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm. While -you can read through the thesis, REST in terms of Rails boils down to two main -principles: - -* Using resource identifiers such as URLs to represent resources. -* Transferring representations of the state of that resource between system components. - -For example, the following HTTP request: - -<tt>DELETE /photos/17</tt> - -would be understood to refer to a photo resource with the ID of 17, and to -indicate a desired action - deleting that resource. REST is a natural style for -the architecture of web applications, and Rails hooks into this shielding you -from many of the RESTful complexities and browser quirks. - -If you'd like more details on REST as an architectural style, these resources -are more approachable than Fielding's thesis: - -* "A Brief Introduction to REST":http://www.infoq.com/articles/rest-introduction by Stefan Tilkov -* "An Introduction to REST":http://bitworking.org/news/373/An-Introduction-to-REST (video tutorial) by Joe Gregorio -* "Representational State Transfer":http://en.wikipedia.org/wiki/Representational_State_Transfer article in Wikipedia -* "How to GET a Cup of Coffee":http://www.infoq.com/articles/webber-rest-workflow by Jim Webber, Savas Parastatidis & -Ian Robinson h3. Creating a New Rails Project The best way to use this guide is to follow each step as it happens, no code or step needed to make this example application has been left out, so you can -literally follow along step by step. You can get the complete code "here":https://github.com/lifo/docrails/tree/master/railties/guides/code/getting_started. +literally follow along step by step. You can get the complete code "here":https://github.com/lifo/docrails/tree/master/guides/code/getting_started. By following along with this guide, you'll create a Rails project called <tt>blog</tt>, a (very) simple weblog. Before you can start building the application, you need to @@ -228,29 +83,27 @@ TIP: The examples below use # and $ to denote terminal prompts. If you are using h4. Installing Rails -In most cases, the easiest way to install Rails is to take advantage of RubyGems: +To install Rails, use the +gem install+ command provided by RubyGems: <shell> -Usually run this as the root user: # gem install rails </shell> -TIP. If you're working on Windows, you can quickly install Ruby and Rails with -"Rails Installer":http://railsinstaller.org. +TIP. If you're working on Windows, you can quickly install Ruby and Rails with "Rails Installer":http://railsinstaller.org. -To verify that you have everything installed correctly, you should be able to run -the following: +To verify that you have everything installed correctly, you should be able to run the following: <shell> $ rails --version </shell> -If it says something like "Rails 3.1.3" you are ready to continue. +If it says something like "Rails 3.2.2" you are ready to continue. h4. Creating the Blog Application -To begin, open a terminal, navigate to a folder where you have rights to create -files, and type: +Rails comes with a number of generators that are designed to make your development life easier. One of these is the new application generator, which will provide you with the foundation of a Rails application so that you don't have to write it yourself. + +To use this generator, open a terminal, navigate to a directory where you have rights to create files, and type: <shell> $ rails new blog @@ -258,22 +111,15 @@ $ rails new blog This will create a Rails application called Blog in a directory called blog. -TIP: You can see all of the switches that the Rails application builder accepts -by running -<tt>rails new -h</tt>. +TIP: You can see all of the switches that the Rails application builder accepts by running <tt>rails new -h</tt>. -After you create the blog application, switch to its folder to continue work -directly in that application: +After you create the blog application, switch to its folder to continue work directly in that application: <shell> $ cd blog </shell> -The 'rails new blog' command we ran above created a folder in your working directory -called <tt>blog</tt>. The <tt>blog</tt> folder has a number of auto-generated folders -that make up the structure of a Rails application. Most of the work in -this tutorial will happen in the <tt>app/</tt> folder, but here's a basic -rundown on the function of each of the files and folders that Rails created by default: +The +rails new blog+ command we ran above created a folder in your working directory called <tt>blog</tt>. The <tt>blog</tt> directory has a number of auto-generated folders that make up the structure of a Rails application. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here's a basic rundown on the function of each of the files and folders that Rails created by default: |_.File/Folder|_.Purpose| |app/|Contains the controllers, models, views and assets for your application. You'll focus on this folder for the remainder of this guide.| @@ -281,7 +127,7 @@ rundown on the function of each of the files and folders that Rails created by d |config.ru|Rack configuration for Rack based servers used to start the application.| |db/|Contains your current database schema, as well as the database migrations.| |doc/|In-depth documentation for your application.| -|Gemfile<BR />Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application.| +|Gemfile<BR />Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see "the Bundler website":http://gembundler.com | |lib/|Extended modules for your application.| |log/|Application log files.| |public/|The only folder seen to the world as-is. Contains the static files and compiled assets.| @@ -292,209 +138,45 @@ rundown on the function of each of the files and folders that Rails created by d |tmp/|Temporary files| |vendor/|A place for all third-party code. In a typical Rails application, this includes Ruby Gems and the Rails source code (if you optionally install it into your project).| -h4. Configuring a Database - -Just about every Rails application will interact with a database. The database -to use is specified in a configuration file, +config/database.yml+. If you open -this file in a new Rails application, you'll see a default database -configured to use SQLite3. The file contains sections for three different -environments in which Rails can run by default: - -* The +development+ environment is used on your development/local computer as you interact -manually with the application. -* The +test+ environment is used when running automated tests. -* The +production+ environment is used when you deploy your application for the world to use. - -TIP: You don't have to update the database configurations manually. If you look at the -options of the application generator, you will see that one of the options -is named <tt>--database</tt>. This option allows you to choose an adapter from a -list of the most used relational databases. You can even run the generator -repeatedly: <tt>cd .. && rails new blog --database=mysql</tt>. When you confirm the overwriting - of the +config/database.yml+ file, your application will be configured for MySQL -instead of SQLite. Detailed examples of the common database connections are below. - -h5. Configuring an SQLite3 Database - -Rails comes with built-in support for "SQLite3":http://www.sqlite.org, which is -a lightweight serverless database application. While a busy production -environment may overload SQLite, it works well for development and testing. -Rails defaults to using an SQLite database when creating a new project, but you -can always change it later. - -Here's the section of the default configuration file -(<tt>config/database.yml</tt>) with connection information for the development -environment: - -<yaml> -development: - adapter: sqlite3 - database: db/development.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 - timeout: 5000 -</yaml> - -NOTE: In this guide we are using an SQLite3 database for data storage, because -it is a zero configuration database that just works. Rails also supports MySQL -and PostgreSQL "out of the box", and has plugins for many database systems. If -you are using a database in a production environment Rails most likely has an -adapter for it. - -h5. Configuring a MySQL Database - -If you choose to use MySQL instead of the shipped SQLite3 database, your -+config/database.yml+ will look a little different. Here's the development -section: - -<yaml> -development: - adapter: mysql2 - encoding: utf8 - database: blog_development - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 - username: root - password: - socket: /tmp/mysql.sock -</yaml> - -If your development computer's MySQL installation includes a root user with an -empty password, this configuration should work for you. Otherwise, change the -username and password in the +development+ section as appropriate. - -h5. Configuring a PostgreSQL Database - -If you choose to use PostgreSQL, your +config/database.yml+ will be customized -to use PostgreSQL databases: - -<yaml> -development: - adapter: postgresql - encoding: unicode - database: blog_development - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 - username: blog - password: -</yaml> - -h5. Configuring an SQLite3 Database for JRuby Platform - -If you choose to use SQLite3 and are using JRuby, your +config/database.yml+ will -look a little different. Here's the development section: - -<yaml> -development: - adapter: jdbcsqlite3 - database: db/development.sqlite3 -</yaml> - -h5. Configuring a MySQL Database for JRuby Platform - -If you choose to use MySQL and are using JRuby, your +config/database.yml+ will look -a little different. Here's the development section: - -<yaml> -development: - adapter: jdbcmysql - database: blog_development - username: root - password: -</yaml> - -h5. Configuring a PostgreSQL Database for JRuby Platform - -Finally if you choose to use PostgreSQL and are using JRuby, your -+config/database.yml+ will look a little different. Here's the development -section: - -<yaml> -development: - adapter: jdbcpostgresql - encoding: unicode - database: blog_development - username: blog - password: -</yaml> - -Change the username and password in the +development+ section as appropriate. - -h4. Creating the Database - -Now that you have your database configured, it's time to have Rails create an -empty database for you. You can do this by running a rake command: - -<shell> -$ rake db:create -</shell> - -This will create your development and test SQLite3 databases inside the -<tt>db/</tt> folder. - -TIP: Rake is a general-purpose command-runner that Rails uses for many things. -You can see the list of available rake commands in your application by running -+rake -T+. - h3. Hello, Rails! -One of the traditional places to start with a new language is by getting some -text up on screen quickly. To do this, you need to get your Rails application -server running. +One of the traditional places to start with a new language is by getting some text up on screen quickly. To do this, you need to get your Rails application server running. h4. Starting up the Web Server -You actually have a functional Rails application already. To see it, you need to -start a web server on your development machine. You can do this by running: +You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running: <shell> $ rails server </shell> -TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and -the absence of a runtime will give you an +execjs+ error. Usually Mac OS X -and Windows come with a JavaScript runtime installed. Rails adds the +therubyracer+ gem -to Gemfile in a commented line for new apps and you can uncomment if you need it. -+therubyrhino+ is the recommended runtime for JRuby users and is added by default -to Gemfile in apps generated under JRuby. You can investigate about all the -supported runtimes at "ExecJS":https://github.com/sstephenson/execjs#readme. +TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an +execjs+ error. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the +therubyracer+ gem to Gemfile in a commented line for new apps and you can uncomment if you need it. +therubyrhino+ is the recommended runtime for JRuby users and is added by default to Gemfile in apps generated under JRuby. You can investigate about all the supported runtimes at "ExecJS":https://github.com/sstephenson/execjs#readme. -This will fire up an instance of the WEBrick web server by default (Rails can -also use several other web servers). To see your application in action, open a -browser window and navigate to "http://localhost:3000":http://localhost:3000. -You should see Rails' default information page: +This will fire up an instance of a webserver built into Ruby called WEBrick by default. To see your application in action, open a browser window and navigate to "http://localhost:3000":http://localhost:3000. You should see the Rails default information page: !images/rails_welcome.png(Welcome Aboard screenshot)! -TIP: To stop the web server, hit Ctrl+C in the terminal window where it's -running. In development mode, Rails does not generally require you to stop the -server; changes you make in files will be automatically picked up by the server. +TIP: To stop the web server, hit Ctrl+C in the terminal window where it's running. In development mode, Rails does not generally require you to restart the server; changes you make in files will be automatically picked up by the server. -The "Welcome Aboard" page is the _smoke test_ for a new Rails application: it -makes sure that you have your software configured correctly enough to serve a -page. You can also click on the _About your application’s environment_ link to -see a summary of your application's environment. +The "Welcome Aboard" page is the _smoke test_ for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. You can also click on the _About your application’s environment_ link to see a summary of your application's environment. h4. Say "Hello", Rails -To get Rails saying "Hello", you need to create at minimum a controller and a -view. Fortunately, you can do that in a single command. Enter this command in -your terminal: +To get Rails saying "Hello", you need to create at minimum a _controller_ and a _view_. + +A controller's purpose is to receive specific requests for the application. What controller receives what request is determined by the _routing_. There is very often more than one route to each controller, and different routes can be served by different _actions_. Each action's purpose is to collect information to provide it to a view. + +A view's purpose is to display this information in a human readable format. An important distinction to make is that it is the _controller_, not the view, where information is collected. The view should just display that information. By default, view templates are written in a language called ERB (Embedded Ruby) which is converted by the request cycle in Rails before being sent to the user. + +To create a new controller, you will need to run the "controller" generator and tell it you want a controller called "welcome" with an action called "index", just like this: <shell> -$ rails generate controller home index +$ rails generate controller welcome index </shell> -TIP: If you get a command not found error when running this command, you -need to explicitly pass Rails +rails+ commands to Ruby: <tt>ruby -\path\to\your\application\script\rails generate controller home index</tt>. +Rails will create several files for you. Most important of these are of course the controller, located at +app/controllers/welcome_controller.rb+ and the view, located at +app/views/welcome/index.html.erb+. -Rails will create several files for you, including -+app/views/home/index.html.erb+. This is the template that will be used to -display the results of the +index+ action (method) in the +home+ controller. -Open this file in your text editor and edit it to contain a single line of code: +Open the +app/views/welcome/index.html.erb+ file in your text editor and edit it to contain a single line of code: <code class="html"> <h1>Hello, Rails!</h1> @@ -502,29 +184,15 @@ Open this file in your text editor and edit it to contain a single line of code: h4. 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":http://localhost:3000, instead of the "Welcome Aboard" -smoke test. +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":http://localhost:3000. At the moment, however, the "Welcome Aboard" smoke test is occupying that spot. -The first step to doing this is to delete the default page from your -application: +To fix this, delete the +index.html+ file located inside the +public+ directory of the application. -<shell> -$ rm public/index.html -</shell> +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. -We need to do this as Rails will deliver any static file in the +public+ -directory in preference to any dynamic content we generate from the controllers. +Next, you have to tell Rails where your actual home page is located. -Now, you have to tell Rails where your actual home page is located. Open the -file +config/routes.rb+ in your editor. This is your application's _routing -file_ which holds entries in a special DSL (domain-specific language) that tells -Rails how to connect incoming requests to controllers and actions. This file -contains many sample routes on commented lines, and one of them actually shows -you how to connect the root of your site to a specific controller and action. -Find the line beginning with +root :to+ and uncomment it. It should look something like the following: +Open the file +config/routes.rb+ in your editor. This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with +root :to+ and uncomment it. It should look something like the following: <ruby> Blog::Application.routes.draw do @@ -532,64 +200,190 @@ Blog::Application.routes.draw do #... # You can have the root of your site routed with "root" # just remember to delete public/index.html. - root :to => "home#index" + root :to => "welcome#index" </ruby> -The +root :to => "home#index"+ tells Rails to map the root action to the home -controller's index action. +The +root :to => "welcome#index"+ tells Rails to map requests to the root of the application to the welcome controller's index action. This was created earlier when you ran the controller generator (+rails generate controller welcome index+). + +If you navigate to "http://localhost:3000":http://localhost:3000 in your browser, you'll see +Hello, Rails!+. + +NOTE. For more information about routing, refer to "Rails Routing from the Outside In":routing.html. + +h3. Getting Up and Running + +Now that you've seen how to create a controller, an action and a view, let's create something with a bit more substance. + +In the Blog application, you will now create a new _resource_. A resource is the term used for a collection of similar objects, such as posts, people or animals. You can create, read, update and destroy items for a resource and these operations are referred to as _CRUD_ operations. -Now if you navigate to "http://localhost:3000":http://localhost:3000 in your -browser, you'll see +Hello, Rails!+. +In the next section, you will add the ability to create new posts in your application and be able to view them. This is the "CR" from CRUD. The form for doing this will look like this: -NOTE. For more information about routing, refer to "Rails Routing from the -Outside In":routing.html. +!images/getting_started/new_post.png(The new post form)! -h3. Getting Up and Running Quickly with Scaffolding +It will look a little basic for now, but that's ok. We'll look at improving the styling for it afterwards. -Rails _scaffolding_ is a quick way to generate some of the major pieces of an -application. If you want to create the models, views, and controllers for a new -resource in a single operation, scaffolding is the tool for the job. +h4. Laying down the ground work -h3. Creating a Resource +The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at +/posts/new+. If you attempt to navigate to that now -- by visiting "http://localhost:3000/posts/new":http://localhost:3000/posts/new -- Rails will give you a routing error: -In the case of the blog application, you can start by generating a scaffold for the -Post resource: this will represent a single blog posting. To do this, enter this -command in your terminal: + +!images/getting_started/routing_error_no_route_matches.png(A routing error, no route matches /posts/new)! + +This is because there is nowhere inside the routes for the application -- defined inside +config/routes.rb+ -- that defines this route. By default, Rails has no routes configured at all, and so you must define your routes as you need them. + + To do this, you're going to need to create a route inside +config/routes.rb+ file, on a new line between the +do+ and the +end+ for the +draw+ method: + +<ruby> +get "posts/new" +</ruby> + +This route is a super-simple route: it defines a new route that only responds to +GET+ requests, and that the route is at +posts/new+. But how does it know where to go without the use of the +:to+ option? Well, Rails uses a sensible default here: Rails will assume that you want this route to go to the new action inside the posts controller. + +With the route defined, requests can now be made to +/posts/new+ in the application. Navigate to "http://localhost:3000/posts/new":http://localhost:3000/posts/new and you'll see another routing error: + +!images/getting_started/routing_error_no_controller.png(Another routing error, uninitialized constant PostsController) + +This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. The solution to this particular problem is simple: you need to create a controller called +PostsController+. You can do this by running this command: <shell> -$ rails generate scaffold Post name:string title:string content:text +$ rails g controller posts </shell> -The scaffold generator will build several files in your application, along with some -folders, and edit <tt>config/routes.rb</tt>. Here's a quick overview of what it creates: +If you open up the newly generated +app/controllers/posts_controller.rb+ you'll see a fairly empty controller: -|_.File |_.Purpose| -|db/migrate/20100207214725_create_posts.rb |Migration to create the posts table in your database (your name will include a different timestamp)| -|app/models/post.rb |The Post model| -|test/unit/post_test.rb |Unit testing harness for the posts model| -|test/fixtures/posts.yml |Sample posts for use in testing| -|config/routes.rb |Edited to include routing information for posts| -|app/controllers/posts_controller.rb |The Posts controller| -|app/views/posts/index.html.erb |A view to display an index of all posts | -|app/views/posts/edit.html.erb |A view to edit an existing post| -|app/views/posts/show.html.erb |A view to display a single post| -|app/views/posts/new.html.erb |A view to create a new post| -|app/views/posts/_form.html.erb |A partial to control the overall look and feel of the form used in edit and new views| -|test/functional/posts_controller_test.rb |Functional testing harness for the posts controller| -|app/helpers/posts_helper.rb |Helper functions to be used from the post views| -|test/unit/helpers/posts_helper_test.rb |Unit testing harness for the posts helper| -|app/assets/javascripts/posts.js.coffee |CoffeeScript for the posts controller| -|app/assets/stylesheets/posts.css.scss |Cascading style sheet for the posts controller| -|app/assets/stylesheets/scaffolds.css.scss |Cascading style sheet to make the scaffolded views look better| - -NOTE. While scaffolding will get you up and running quickly, the code it -generates is unlikely to be a perfect fit for your application. You'll most -probably want to customize the generated code. Many experienced Rails developers -avoid scaffolding entirely, preferring to write all or most of their source code -from scratch. Rails, however, makes it really simple to customize templates for -generated models, controllers, views and other source files. You'll find more -information in the "Creating and Customizing Rails Generators & -Templates":generators.html guide. +<ruby> +class PostsController < ApplicationController +end +</ruby> + +A controller is simply a class that is defined to inherit from +ApplicationController+. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the posts within our system. + +If you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new now, you'll get a new error: + +!images/getting_started/unknown_action_new_for_posts.png(Unknown action new for PostsController!) + +This error indicates that Rails cannot find the +new+ action inside the +PostsController+ that you just generated. This is because when controllers are generated in Rails they are empty by default, unless you tell it you wanted actions during the generation process. + +To manually define an action inside a controller, all you need to do is to define a new method inside the controller. Open +app/controllers/posts_controller.rb+ and inside the +PostsController+ class, define a +new+ method like this: + +<ruby> +def new +end +</ruby> + +With the +new+ method defined in +PostsController+, if you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll see another error: + +!images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new) + +You're getting this error now because Rails expects plain actions like this one to have views associated with them to display their information. With no view available, Rails errors out. + +In the above image, the bottom line has been truncated. Let's see what the full thing looks like: + +<text> +Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/path/to/blog/app/views" +</text> + +That's quite a lot of text! Let's quickly go through and understand what each part of it does. + +The first part identifies what template is missing. In this case, it's the +posts/new+ template. Rails will first look for this template. If it can't find it, then it will attempt to load a template called +application/new+. It looks for one here because the +PostsController+ inherits from +ApplicationController+. + +The next part of the message contains a hash. The +:locale+ key in this hash simply indicates what spoken language template should be retrieved. By default, this is the English -- or "en" -- template. The next key, +:formats+ shows what formats of template Rails is after. The default is +:html+, and so Rails is looking for an HTML template. The final key, +:handlers+, is telling us what _template handlers_ could be used to render our template. +:erb+ is most commonly used for HTML templates, +:builder+ is used for XML templates, and +:coffee+ uses CoffeeScript to build JavaScript templates. + +The final part of this message tells us where Rails has looked for the templates. Templates within a basic Rails application like this are kept in a single location, but in more complex applications it could be many different paths. + +The simplest template that would work in this case would be one located at +app/views/posts/new.html.erb+. The extension of this file name is key: the first extension is the _format_ of the template, and the second extension is the _handler_ that will be used. Rails is attempting to find a template called +posts/new+ within +app/views+ for the application. The format for this template can only be +html+ and the handler must be one of +erb+, +builder+ or +coffee+. Because you want to create a new HTML form, you will be using the +ERB+ language. Therefore the file should be called +posts/new.html.erb+ and be located inside the +app/views+ directory of the application. + +Go ahead now and create a new file at +app/views/posts/new.html.erb+ and write this content in it: + +<erb> +<h1>New Post</h1> +</erb> + +When you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll now see that the page has a title. The route, controller, action and view are now working harmoniously! It's time to create the form for a new post. + +h4. The first form + +To create a form within this template, you will use a _form builder_. The primary form builder for Rails is provided by a helper method called +form_for+. To use this method, write this code into +app/views/posts/new.html.erb+: + +<erb> +<%= form_for :post do |f| %> + <p> + <%= f.label :title %><br> + <%= f.text_field :title %> + </p> + + <p> + <%= f.label :text %><br> + <%= f.text_area :text %> + </p> + + <p> + <%= f.submit %> + </p> +<% end %> +</erb> + +If you refresh the page now, you'll see the exact same form as in the example. Building forms in Rails is really just that easy! + +When you call +form_for+, you pass it an identifying object for this form. In this case, it's the symbol +:post+. This tells the +form_for+ helper what this form is for. Inside the block for this method, the FormBuilder object -- represented by +f+ -- is used to build two labels and two text fields, one each for the title and text of a post. Finally, a call to +submit+ on the +f+ object will create a submit button for the form. + +There's one problem with this form though. If you inspect the HTML that is generated, by viewing the source of the page, you will see that the +action+ attribute for the form is pointing at +/posts/new+. This is a problem because this route goes to the very page that you're on right at the moment, and that route should only be used to display the form for a new post. + +So the form needs to use a different URL in order to go somewhere else. This can be done quite simply with the +:url+ option of +form_for+. Typically in Rails, the action that is used for new form submissions like this is called "create", and so the form should be pointed to this action. + +Edit the +form_for+ line inside +app/views/posts/new.html.erb+ to look like this: + +<erb> +<%= form_for :post, :url => { :action => :create } do |f| %> +</erb> + +In this example, a +Hash+ object is passed to the +:url+ option. What Rails will do with this is that it will point the form to the +create+ action of the current controller, the +PostsController+, and will send a +POST+ request to that route. For this to work, you will need to add a route to +config/routes.rb+, right underneath the one for "posts/new": + +<ruby> +post "posts/create" +</ruby> + +By using the +post+ method rather than the +get+ method, Rails will define a route that will only respond to POST methods. The POST method is the typical method used by forms all over the web. + +With the form and the route for it defined now, you will be able to fill in the form and then click the submit button to begin the process of creating a new post, so go ahead and do that. When you submit the form, you should see a familiar error: + +!images/getting_started/unknown_action_create_for_posts(Unknown action create for PostsController)! + +You will now need to create the +create+ action within the +PostsController+ for this to work. + +h4. Creating posts + +To make the "Unknown action" go away, you can define a +create+ action within the +PostsController+ class in +app/controllers/posts_controller.rb+, underneath the +new+ action: + +<ruby> +class PostsController < ApplicationController + def new + end + + def create + end + +end +</ruby> + +If you re-submit the form now, you'll see another familiar error: a template is missing. That's ok, we can ignore that for now. What the +create+ action should be doing is saving our new post to a database. + +When a form is submitted, the fields of the form are sent to Rails as _parameters_. These parameters can then be referenced inside the controller actions, typically to perform a particular task. To see what these parameters look like, change the +create+ action to this: + +<ruby> +def create + render :text => params[:post].inspect +end +</ruby> + +The +render+ method here is taking a very simple hash with the key of +text+ and the value of +params[:post].inspect+. The +params+ method here is the object which represents the parameters (or fields) coming in from the form. The +params+ method returns a +HashWithIndifferentAccess+ object, which allows you to access the keys of the hash using either strings or symbols. In this situation, the only parameters that matter are the ones from the form. + +If you re-submit the form one more time you'll now no longer get the missing template error. Instead, you'll see something that looks like the following: + +<ruby> +{"title"=>"First post!", "text"=>"This is my first post."} +</ruby> + +This action is now displaying the parameters for the post that are coming in from the form. However, this isn't really all that helpful. Yes, you can see the parameters but nothing in particular is being done with them. h4. Running a Migration @@ -651,7 +445,7 @@ invoking the command: <tt>rake db:migrate RAILS_ENV=production</tt>. h4. Adding a Link To hook the posts up to the home page you've already created, you can add a link -to the home page. Open +app/views/home/index.html.erb+ and modify it as follows: +to the home page. Open +app/views/welcome/index.html.erb+ and modify it as follows: <ruby> <h1>Hello, Rails!</h1> @@ -1238,7 +1032,7 @@ Associations":association_basics.html guide. h4. Adding a Route for Comments -As with the +home+ controller, we will need to add a route so that Rails knows +As with the +welcome+ controller, we will need to add a route so that Rails knows where we would like to navigate to see +comments+. Open up the +config/routes.rb+ file again. Near the top, you will see the entry for +posts+ that was added automatically by the scaffold generator: <tt>resources diff --git a/railties/guides/source/i18n.textile b/guides/source/i18n.textile index 25201888e7..320f1e9d20 100644 --- a/railties/guides/source/i18n.textile +++ b/guides/source/i18n.textile @@ -521,7 +521,7 @@ h5. Bulk and Namespace Lookup To look up multiple translations at once, an array of keys can be passed: <ruby> -I18n.t [:odd, :even], :scope => 'activerecord.errors.messages' +I18n.t [:odd, :even], :scope => 'errors.messages' # => ["must be odd", "must be even"] </ruby> diff --git a/railties/guides/source/index.html.erb b/guides/source/index.html.erb index 5439459b42..5439459b42 100644 --- a/railties/guides/source/index.html.erb +++ b/guides/source/index.html.erb diff --git a/railties/guides/source/initialization.textile b/guides/source/initialization.textile index 5ae9cf0f2b..69e5c1edcc 100644 --- a/railties/guides/source/initialization.textile +++ b/guides/source/initialization.textile @@ -159,7 +159,6 @@ In a standard Rails application, there's a +Gemfile+ which declares all dependen * actionpack (3.1.0.beta) * activemodel (3.1.0.beta) * activerecord (3.1.0.beta) -* activeresource (3.1.0.beta) * activesupport (3.1.0.beta) * arel (2.0.7) * builder (3.0.0) @@ -491,7 +490,6 @@ require "rails" active_record action_controller action_mailer - active_resource rails/test_unit ).each do |framework| begin diff --git a/railties/guides/source/kindle/KINDLE.md b/guides/source/kindle/KINDLE.md index a7d9a4e4cf..a7d9a4e4cf 100644 --- a/railties/guides/source/kindle/KINDLE.md +++ b/guides/source/kindle/KINDLE.md diff --git a/railties/guides/source/kindle/copyright.html.erb b/guides/source/kindle/copyright.html.erb index bd51d87383..bd51d87383 100644 --- a/railties/guides/source/kindle/copyright.html.erb +++ b/guides/source/kindle/copyright.html.erb diff --git a/railties/guides/source/kindle/layout.html.erb b/guides/source/kindle/layout.html.erb index f0a286210b..f0a286210b 100644 --- a/railties/guides/source/kindle/layout.html.erb +++ b/guides/source/kindle/layout.html.erb diff --git a/railties/guides/source/kindle/rails_guides.opf.erb b/guides/source/kindle/rails_guides.opf.erb index 4e07664fd0..4e07664fd0 100644 --- a/railties/guides/source/kindle/rails_guides.opf.erb +++ b/guides/source/kindle/rails_guides.opf.erb diff --git a/railties/guides/source/kindle/toc.html.erb b/guides/source/kindle/toc.html.erb index e013797dee..e013797dee 100644 --- a/railties/guides/source/kindle/toc.html.erb +++ b/guides/source/kindle/toc.html.erb diff --git a/railties/guides/source/kindle/toc.ncx.erb b/guides/source/kindle/toc.ncx.erb index 2c6d8e3bdf..2c6d8e3bdf 100644 --- a/railties/guides/source/kindle/toc.ncx.erb +++ b/guides/source/kindle/toc.ncx.erb diff --git a/railties/guides/source/kindle/welcome.html.erb b/guides/source/kindle/welcome.html.erb index e30704c4e6..e30704c4e6 100644 --- a/railties/guides/source/kindle/welcome.html.erb +++ b/guides/source/kindle/welcome.html.erb diff --git a/railties/guides/source/layout.html.erb b/guides/source/layout.html.erb index 35b6fc7014..35b6fc7014 100644 --- a/railties/guides/source/layout.html.erb +++ b/guides/source/layout.html.erb diff --git a/railties/guides/source/layouts_and_rendering.textile b/guides/source/layouts_and_rendering.textile index be352cfe3d..7c7fc7044c 100644 --- a/railties/guides/source/layouts_and_rendering.textile +++ b/guides/source/layouts_and_rendering.textile @@ -359,7 +359,7 @@ class ProductsController < ApplicationController end </ruby> -With this declaration, all of the methods within +ProductsController+ will use +app/views/layouts/inventory.html.erb+ for their layout. +With this declaration, all of the views rendered by the products controller will use +app/views/layouts/inventory.html.erb+ as their layout. To assign a specific layout for the entire application, use a +layout+ declaration in your +ApplicationController+ class: @@ -1134,13 +1134,6 @@ In Rails 3.0, there is also a shorthand for this. Assuming +@products+ is a coll Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection: -In the event that the collection is empty, +render+ will return nil, so it should be fairly simple to provide alternative content. - -<erb> -<h1>Products</h1> -<%= render(@products) || 'There are no products available.' %> -</erb> - * +index.html.erb+ <erb> @@ -1162,6 +1155,13 @@ In the event that the collection is empty, +render+ will return nil, so it shoul In this case, Rails will use the customer or employee partials as appropriate for each member of the collection. +In the event that the collection is empty, +render+ will return nil, so it should be fairly simple to provide alternative content. + +<erb> +<h1>Products</h1> +<%= render(@products) || 'There are no products available.' %> +</erb> + h5. Local Variables To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial: diff --git a/railties/guides/source/migrations.textile b/guides/source/migrations.textile index c11f8e221b..cb7ee1e6a2 100644 --- a/railties/guides/source/migrations.textile +++ b/guides/source/migrations.textile @@ -497,7 +497,7 @@ and h4. Using the +change+ Method The +change+ method removes the need to write both +up+ and +down+ methods in -those cases that Rails know how to revert the changes automatically. Currently, +those cases that Rails knows how to revert the changes automatically. Currently, the +change+ method supports only these migration definitions: * +add_column+ diff --git a/railties/guides/source/nested_model_forms.textile b/guides/source/nested_model_forms.textile index 4b1fd2e0ac..82c9ab9d36 100644 --- a/railties/guides/source/nested_model_forms.textile +++ b/guides/source/nested_model_forms.textile @@ -131,7 +131,7 @@ This will generate the following html: <html> <form action="/people" class="new_person" id="new_person" method="post"> - <input id="person_name" name="person[name]" size="30" type="text" /> + <input id="person_name" name="person[name]" type="text" /> </form> </html> @@ -153,9 +153,9 @@ This generates: <html> <form action="/people" class="new_person" id="new_person" method="post"> - <input id="person_name" name="person[name]" size="30" type="text" /> + <input id="person_name" name="person[name]" type="text" /> - <input id="person_address_attributes_street" name="person[address_attributes][street]" size="30" type="text" /> + <input id="person_address_attributes_street" name="person[address_attributes][street]" type="text" /> </form> </html> @@ -194,10 +194,10 @@ Which generates: <html> <form action="/people" class="new_person" id="new_person" method="post"> - <input id="person_name" name="person[name]" size="30" type="text" /> + <input id="person_name" name="person[name]" type="text" /> - <input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" size="30" type="text" /> - <input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" size="30" type="text" /> + <input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" type="text" /> + <input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" type="text" /> </form> </html> diff --git a/railties/guides/source/performance_testing.textile b/guides/source/performance_testing.textile index 958b13cd9e..958b13cd9e 100644 --- a/railties/guides/source/performance_testing.textile +++ b/guides/source/performance_testing.textile diff --git a/railties/guides/source/plugins.textile b/guides/source/plugins.textile index 07fd95c825..97b4eca779 100644 --- a/railties/guides/source/plugins.textile +++ b/guides/source/plugins.textile @@ -176,11 +176,11 @@ require 'test_helper' class ActsAsYaffleTest < Test::Unit::TestCase def test_a_hickwalls_yaffle_text_field_should_be_last_squawk - assert_equal "last_squawk", Hickwall.yaffle_text_field + assert_equal :last_squawk, Hickwall.yaffle_text_field end def test_a_wickwalls_yaffle_text_field_should_be_last_tweet - assert_equal "last_tweet", Wickwall.yaffle_text_field + assert_equal :last_tweet, Wickwall.yaffle_text_field end end @@ -362,13 +362,16 @@ module Yaffle def acts_as_yaffle(options = {}) cattr_accessor :yaffle_text_field self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s + + include Yaffle::ActsAsYaffle::LocalInstanceMethods end end - def squawk(string) - write_attribute(self.class.yaffle_text_field, string.to_squawk) + module LocalInstanceMethods + def squawk(string) + write_attribute(self.class.yaffle_text_field, string.to_squawk) + end end - end end diff --git a/railties/guides/source/rails_application_templates.textile b/guides/source/rails_application_templates.textile index f50ced3307..f50ced3307 100644 --- a/railties/guides/source/rails_application_templates.textile +++ b/guides/source/rails_application_templates.textile diff --git a/railties/guides/source/rails_on_rack.textile b/guides/source/rails_on_rack.textile index 9526526bc7..73e0f242cc 100644 --- a/railties/guides/source/rails_on_rack.textile +++ b/guides/source/rails_on_rack.textile @@ -91,13 +91,15 @@ For a freshly generated Rails application, this might produce something like: <ruby> use ActionDispatch::Static use Rack::Lock -use ActiveSupport::Cache::Strategy::LocalCache +use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000000029a0838> use Rack::Runtime +use Rack::MethodOverride +use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp -use Rack::Sendfile +use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache @@ -105,8 +107,9 @@ use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser -use Rack::MethodOverride use ActionDispatch::Head +use Rack::ConditionalGet +use Rack::ETag use ActionDispatch::BestStandardsSupport run Blog::Application.routes </ruby> @@ -161,18 +164,73 @@ config.middleware.delete(middleware) h4. Internal Middleware Stack -Much of Action Controller's functionality is implemented as Middlewares. The following table explains the purpose of each of them: +Much of Action Controller's functionality is implemented as Middlewares. The following list explains the purpose of each of them: -|_.Middleware|_.Purpose| -|+Rack::Lock+|Sets <tt>env["rack.multithread"]</tt> flag to +true+ and wraps the application within a Mutex.| -|+ActionController::Failsafe+|Returns HTTP Status +500+ to the client if an exception gets raised while dispatching.| -|+ActiveRecord::QueryCache+|Enables the Active Record query cache.| -|+ActionDispatch::Session::CookieStore+|Uses the cookie based session store.| -|+ActionDispatch::Session::CacheStore+|Uses the Rails cache based session store.| -|+ActionDispatch::Session::MemCacheStore+|Uses the memcached based session store.| -|+ActiveRecord::SessionStore+|Uses the database based session store.| -|+Rack::MethodOverride+|Sets HTTP method based on +_method+ parameter or <tt>env["HTTP_X_HTTP_METHOD_OVERRIDE"]</tt>.| -|+Rack::Head+|Discards the response body if the client sends a +HEAD+ request.| + *+ActionDispatch::Static+* +* Used to serve static assets. Disabled if <tt>config.serve_static_assets</tt> is true. + + *+Rack::Lock+* +* Sets <tt>env["rack.multithread"]</tt> flag to +true+ and wraps the application within a Mutex. + + *+ActiveSupport::Cache::Strategy::LocalCache::Middleware+* +* Used for memory caching. This cache is not thread safe. + + *+Rack::Runtime+* +* Sets an X-Runtime header, containing the time (in seconds) taken to execute the request. + + *+Rack::MethodOverride+* +* Allows the method to be overridden if <tt>params[:_method]</tt> is set. This is the middleware which supports the PUT and DELETE HTTP method types. + + *+ActionDispatch::RequestId+* +* Makes a unique +X-Request-Id+ header available to the response and enables the <tt>ActionDispatch::Request#uuid</tt> method. + + *+Rails::Rack::Logger+* +* Notifies the logs that the request has began. After request is complete, flushes all the logs. + + *+ActionDispatch::ShowExceptions+* +* Rescues any exception returned by the application and calls an exceptions app that will wrap it in a format for the end user. + + *+ActionDispatch::DebugExceptions+* +* Responsible for logging exceptions and showing a debugging page in case the request is local. + + *+ActionDispatch::RemoteIp+* +* Checks for IP spoofing attacks. + + *+ActionDispatch::Reloader+* +* Provides prepare and cleanup callbacks, intended to assist with code reloading during development. + + *+ActionDispatch::Callbacks+* +* Runs the prepare callbacks before serving the request. + + *+ActiveRecord::ConnectionAdapters::ConnectionManagement+* +* Cleans active connections after each request, unless the <tt>rack.test</tt> key in the request environment is set to +true+. + + *+ActiveRecord::QueryCache+* +* Enables the Active Record query cache. + + *+ActionDispatch::Cookies+* +* Sets cookies for the request. + + *+ActionDispatch::Session::CookieStore+* +* Responsible for storing the session in cookies. + + *+ActionDispatch::Flash+* +* Sets up the flash keys. Only available if <tt>config.action_controller.session_store</tt> is set to a value. + + *+ActionDispatch::ParamsParser+* +* Parses out parameters from the request into <tt>params</tt>. + + *+ActionDispatch::Head+* +* Converts HEAD requests to +GET+ requests and serves them as so. + + *+Rack::ConditionalGet+* +* Adds support for "Conditional +GET+" so that server responds with nothing if page wasn't changed. + + *+Rack::ETag+* +* Adds ETag header on all String bodies. ETags are used to validate cache. + + *+ActionDispatch::BestStandardsSupport+* +* Enables “best standards support” so that IE8 renders some elements correctly. TIP: It's possible to use any of the above middlewares in your custom Rack stack. diff --git a/railties/guides/source/routing.textile b/guides/source/routing.textile index df2bd9d0c9..75f4e82918 100644 --- a/railties/guides/source/routing.textile +++ b/guides/source/routing.textile @@ -234,14 +234,14 @@ end In addition to the routes for magazines, this declaration will also route ads to an +AdsController+. The ad URLs require a magazine: -|_.HTTP Verb |_.Path |_.action |_.used for | -|GET |/magazines/:id/ads |index |display a list of all ads for a specific magazine | -|GET |/magazines/:id/ads/new |new |return an HTML form for creating a new ad belonging to a specific magazine | -|POST |/magazines/:id/ads |create |create a new ad belonging to a specific magazine | -|GET |/magazines/:id/ads/:id |show |display a specific ad belonging to a specific magazine | -|GET |/magazines/:id/ads/:id/edit |edit |return an HTML form for editing an ad belonging to a specific magazine | -|PATCH/PUT |/magazines/:id/ads/:id |update |update a specific ad belonging to a specific magazine | -|DELETE |/magazines/:id/ads/:id |destroy |delete a specific ad belonging to a specific magazine | +|_.HTTP Verb |_.Path |_.action |_.used for | +|GET |/magazines/:magazine_id/ads |index |display a list of all ads for a specific magazine | +|GET |/magazines/:magazine_id/ads/new |new |return an HTML form for creating a new ad belonging to a specific magazine | +|POST |/magazines/:magazine_id/ads |create |create a new ad belonging to a specific magazine | +|GET |/magazines/:magazine_id/ads/:id |show |display a specific ad belonging to a specific magazine | +|GET |/magazines/:magazine_id/ads/:id/edit |edit |return an HTML form for editing an ad belonging to a specific magazine | +|PATCH/PUT |/magazines/:magazine_id/ads/:id |update |update a specific ad belonging to a specific magazine | +|DELETE |/magazines/:magazine_id/ads/:id |destroy |delete a specific ad belonging to a specific magazine | This will also create routing helpers such as +magazine_ads_url+ and +edit_magazine_ad_path+. These helpers take an instance of Magazine as the first parameter (+magazine_ads_url(@magazine)+). @@ -295,12 +295,18 @@ In this case, Rails will see that +@magazine+ is a +Magazine+ and +@ad+ is an +A <%= link_to "Ad details", [@magazine, @ad] %> </erb> -If you wanted to link to just a magazine, you could leave out the +Array+: +If you wanted to link to just a magazine: <erb> <%= link_to "Magazine details", @magazine %> </erb> +For other actions, you just need to insert the action name as the first element of the array: + +<erb> +<%= link_to "Edit Ad", [:edit, @magazine, @ad] %> +</erb> + This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style. h4. Adding More RESTful Actions @@ -383,7 +389,7 @@ match ':controller/:action/:id/:user_id' An incoming path of +/photos/show/1/2+ will be dispatched to the +show+ action of the +PhotosController+. +params[:id]+ will be +"1"+, and +params[:user_id]+ will be +"2"+. -NOTE: You can't use +namespace+ or +:module+ with a +:controller+ path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g: +NOTE: You can't use +:namespace+ or +:module+ with a +:controller+ path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g: <ruby> match ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/ @@ -529,6 +535,17 @@ TwitterClone::Application.routes.draw do end </ruby> +You can also specify constraints as a lambda: + +<ruby> +TwitterClone::Application.routes.draw do + match "*path" => "blacklist#index", + :constraints => lambda { |request| Blacklist.retrieve_ips.include?(request.remote_ip) } +end +</ruby> + +Both the +matches?+ method and the lambda gets the +request+ object as an argument. + h4. Route Globbing Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example @@ -616,6 +633,7 @@ You can specify what Rails should route +"/"+ to with the +root+ method: <ruby> root :to => 'pages#main' +root 'pages#main' # shortcut for the above </ruby> 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. diff --git a/railties/guides/source/ruby_on_rails_guides_guidelines.textile b/guides/source/ruby_on_rails_guides_guidelines.textile index f3c8fa654d..f3e934d38c 100644 --- a/railties/guides/source/ruby_on_rails_guides_guidelines.textile +++ b/guides/source/ruby_on_rails_guides_guidelines.textile @@ -1,20 +1,20 @@ h2. Ruby on Rails Guides Guidelines -This guide documents guidelines for writing guides. This guide follows itself in a gracile loop. +This guide documents guidelines for writing Ruby on Rails Guides. This guide follows itself in a graceful loop, serving itself as an example. endprologue. h3. Textile -Guides are written in "Textile":http://www.textism.com/tools/textile/. There's comprehensive documentation "here":http://redcloth.org/hobix.com/textile/ and a cheatsheet for markup "here":http://redcloth.org/hobix.com/textile/quick.html. +Guides are written in "Textile":http://www.textism.com/tools/textile/. There is comprehensive "documentation":http://redcloth.org/hobix.com/textile/ and a "cheatsheet":http://redcloth.org/hobix.com/textile/quick.html for markup. h3. Prologue -Each guide should start with motivational text at the top (that's the little introduction in the blue area.) The prologue should tell the reader what the guide is about, and what they will learn. See for example the "Routing Guide":routing.html. +Each guide should start with motivational text at the top (that's the little introduction in the blue area). The prologue should tell the reader what the guide is about, and what they will learn. See for example the "Routing Guide":routing.html. h3. Titles -The title of every guide uses +h2+, guide sections use +h3+, subsections +h4+, etc. +The title of every guide uses +h2+; guide sections use +h3+; subsections +h4+; etc. Capitalize all words except for internal articles, prepositions, conjunctions, and forms of the verb to be: @@ -31,7 +31,7 @@ h6. The <tt>:content_type</tt> Option h3. API Documentation Guidelines -The guides and the API should be coherent where appropriate. Please have a look at these particular sections of the "API Documentation Guidelines":api_documentation_guidelines.html: +The guides and the API should be coherent and consistent where appropriate. Please have a look at these particular sections of the "API Documentation Guidelines":api_documentation_guidelines.html: * "Wording":api_documentation_guidelines.html#wording * "Example Code":api_documentation_guidelines.html#example-code @@ -44,7 +44,7 @@ h3. HTML Guides h4. Generation -To generate all the guides, just +cd+ into the +railties+ directory and execute: +To generate all the guides, just +cd+ into the *+guides+* directory and execute: <plain> bundle exec rake generate_guides @@ -55,16 +55,17 @@ bundle exec rake generate_guides To process +my_guide.textile+ and nothing else use the +ONLY+ environment variable: <plain> +touch my_guide.textile bundle exec rake generate_guides ONLY=my_guide </plain> By default, guides that have not been modified are not processed, so +ONLY+ is rarely needed in practice. -To force process of all the guides, pass +ALL=1+. +To force processing all the guides, pass +ALL=1+. It is also recommended that you work with +WARNINGS=1+. This detects duplicate IDs and warns about broken internal links. -If you want to generate guides in languages other than English, you can keep them in a separate directory under +source+ (eg. <tt>source/es</tt>) and use the +GUIDES_LANGUAGE+ environment variable: +If you want to generate guides in a language other than English, you can keep them in a separate directory under +source+ (eg. <tt>source/es</tt>) and use the +GUIDES_LANGUAGE+ environment variable: <plain> bundle exec rake generate_guides GUIDES_LANGUAGE=es @@ -78,11 +79,11 @@ Please validate the generated HTML with: bundle exec rake validate_guides </plain> -Particularly, titles get an ID generated from their content and this often leads to duplicates. Please set +WARNINGS=1+ when generating guides to detect them. The warning messages suggest a way to fix them. +Particularly, titles get an ID generated from their content and this often leads to duplicates. Please set +WARNINGS=1+ when generating guides to detect them. The warning messages suggest a solution. h3. Kindle Guides -h4. Generation +h4(#generation-kindle). Generation To generate guides for the Kindle, you need to provide +KINDLE=1+ as an environment variable: diff --git a/railties/guides/source/security.textile b/guides/source/security.textile index b1a09c0c05..747a4d6791 100644 --- a/railties/guides/source/security.textile +++ b/guides/source/security.textile @@ -374,7 +374,7 @@ 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: <pre> -"name":http://www.example.com/user/signup?user[name]=ow3ned&user[admin]=1 +http://www.example.com/user/signup?user[name]=ow3ned&user[admin]=1 </pre> This will set the following parameters in the controller: diff --git a/railties/guides/source/testing.textile b/guides/source/testing.textile index c367f532ae..d35be6a70e 100644 --- a/railties/guides/source/testing.textile +++ b/guides/source/testing.textile @@ -412,7 +412,7 @@ NOTE: +assert_valid(record)+ has been deprecated. Please use +assert(record.vali |+assert_no_difference(expressions, message = nil, &block)+ |Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.| |+assert_recognizes(expected_options, path, extras={}, message=nil)+ |Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.| |+assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)+ |Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.| -|+assert_response(type, message = nil)+ |Asserts that the response comes with a specific status code. You can specify +:success+ to indicate 200, +:redirect+ to indicate 300-399, +:missing+ to indicate 404, or +:error+ to match the 500-599 range| +|+assert_response(type, message = nil)+ |Asserts that the response comes with a specific status code. You can specify +:success+ to indicate 200-299, +:redirect+ to indicate 300-399, +:missing+ to indicate 404, or +:error+ to match the 500-599 range| |+assert_redirected_to(options = {}, message=nil)+ |Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that +assert_redirected_to(:controller => "weblog")+ will also match the redirection of +redirect_to(:controller => "weblog", :action => "show")+ and so on.| |+assert_template(expected = nil, message=nil)+ |Asserts that the request was rendered with the appropriate template file.| @@ -524,6 +524,44 @@ You also have access to three instance variables in your functional tests: * +@request+ - The request * +@response+ - The response +h4. Testing Templates and Layouts + +If you want to make sure that the response rendered the correct template and layout, you can use the +assert_template+ +method: + +<ruby> +test "index should render correct template and layout" do + get :index + assert_template :index + assert_template :layout => "layouts/application" +end +</ruby> + +Note that you cannot test for template and layout at the same time, with one call to +assert_template+ method. +Also, for the +layout+ test, you can give a regular expression instead of a string, but using the string, makes +things clearer. On the other hand, you have to include the "layouts" directory name even if you save your layout +file in this standard layout directory. Hence, + +<ruby> +assert_template :layout => "application" +</ruby> + +will not work. + +If your view renders any partial, when asserting for the layout, you have to assert for the partial at the same time. +Otherwise, assertion will fail. + +Hence: + +<ruby> +test "new should render correct layout" do + get :new + assert_template :layout => "layouts/application", :partial => "_form" +end +</ruby> + +is the correct way to assert for the layout when the view renders a partial with name +_form+. Omitting the +:partial+ key in your +assert_template+ call will complain. + h4. A Fuller Functional Test Example Here's another example that uses +flash+, +assert_redirected_to+, and +assert_difference+: diff --git a/guides/source/upgrading_ruby_on_rails.textile b/guides/source/upgrading_ruby_on_rails.textile new file mode 100644 index 0000000000..e63548abc9 --- /dev/null +++ b/guides/source/upgrading_ruby_on_rails.textile @@ -0,0 +1,194 @@ +h2. 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. + +endprologue. + +h3. General Advice + +Before attempting to upgrade an existing application, you should be sure you have a good reason to upgrade. You need to balance out several factors: the need for new features, the increasing difficulty of finding support for old code, and your available time and skills, to name a few. + +h4(#general_testing). Test Coverage + +The best way to be sure that your application still works after upgrading is to have good test coverage before you start the process. If you don't have automated tests that exercise the bulk of your application, you'll need to spend time manually exercising all the parts that have changed. In the case of a Rails upgrade, that will mean every single piece of functionality in the application. Do yourself a favor and make sure your test coverage is good _before_ you start an upgrade. + +h4(#general_ruby). Ruby Versions + +Rails generally stays close to the latest released Ruby version when it's released: + +* Rails 3 and above requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. +* Rails 3.2.x will be the last branch to support Ruby 1.8.7. +* Rails 4 will support only Ruby 1.9.3. + +TIP: Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump on to 1.9.2 or 1.9.3 for smooth sailing. + +h3. 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. + +The following changes are meant for upgrading your application to Rails 4.0. + +h4(#plugins4_0). vendor/plugins + +Rails 4.0 no longer supports loading plugins from <tt>vendor/plugins</tt>. 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, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>. + +h4(#identity_map4_0). IdentityMap + +Rails 4.0 has removed <tt>IdentityMap</tt> from <tt>ActiveRecord</tt>, 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: <tt>config.active_record.identity_map</tt>. + +h3. Upgrading from Rails 3.1 to Rails 3.2 + +If your application is currently on any version of Rails older than 3.1.x, you should upgrade to Rails 3.1 before attempting an update to Rails 3.2. + +The following changes are meant for upgrading your application to Rails 3.2.2, the latest 3.2.x version of Rails. + +h4(#gemfile3_2). Gemfile + +Make the following changes to your +Gemfile+. + +<ruby> +gem 'rails', '= 3.2.2' + +group :assets do + gem 'sass-rails', '~> 3.2.3' + gem 'coffee-rails', '~> 3.2.1' + gem 'uglifier', '>= 1.0.3' +end +</ruby> + +h4(#config_dev3_2). config/environments/development.rb + +There are a couple of new configuration settings that you should add to your development environment: + +<ruby> +# Raise exception on mass assignment protection for Active Record models +config.active_record.mass_assignment_sanitizer = :strict + +# Log the query plan for queries taking more than this (works +# with SQLite, MySQL, and PostgreSQL) +config.active_record.auto_explain_threshold_in_seconds = 0.5 +</ruby> + +h4(#config_test3_2). config/environments/test.rb + +The <tt>mass_assignment_sanitizer</tt> configuration setting should also be be added to <tt>config/environments/test.rb</tt>: + +<ruby> +# Raise exception on mass assignment protection for Active Record models +config.active_record.mass_assignment_sanitizer = :strict +</ruby> + +h4(#plugins3_2). vendor/plugins + +Rails 3.2 deprecates <tt>vendor/plugins</tt> and Rails 4.0 will remove them completely. While it's not strictly necessary as part of a Rails 3.2 upgrade, you can start replacing 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, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>. + +h3. Upgrading from Rails 3.0 to Rails 3.1 + +If your application is currently on any version of Rails older than 3.0.x, you should upgrade to Rails 3.0 before attempting an update to Rails 3.1. + +The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails. + +h4(#gemfile3_1). Gemfile + +Make the following changes to your +Gemfile+. + +<ruby> +gem 'rails', '= 3.1.3' +gem 'mysql2' + +# Needed for the new asset pipeline +group :assets do + gem 'sass-rails', "~> 3.1.5" + gem 'coffee-rails', "~> 3.1.1" + gem 'uglifier', ">= 1.0.3" +end + +# jQuery is the default JavaScript library in Rails 3.1 +gem 'jquery-rails' +</ruby> + +h4(#config_app3_1). config/application.rb + +The asset pipeline requires the following additions: + +<ruby> +config.assets.enabled = true +config.assets.version = '1.0' +</ruby> + +If your application is using an "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts: + +<ruby> +# Defaults to '/assets' +config.assets.prefix = '/asset-files' +</ruby> + +h4(#config_dev3_1). config/environments/development.rb + +Remove the RJS setting <tt>config.action_view.debug_rjs = true</tt>. + +Add these settings if you enable the asset pipeline: + +<ruby> +# Do not compress assets +config.assets.compress = false + +# Expands the lines which load the assets +config.assets.debug = true +</ruby> + +h4(#config_prod3_1). config/environments/production.rb + +Again, most of the changes below are for the asset pipeline. You can read more about these in the "Asset Pipeline":asset_pipeline.html guide. + +<ruby> +# Compress JavaScripts and CSS +config.assets.compress = true + +# Don't fallback to assets pipeline if a precompiled asset is missed +config.assets.compile = false + +# Generate digests for assets URLs +config.assets.digest = true + +# Defaults to Rails.root.join("public/assets") +# config.assets.manifest = YOUR_PATH + +# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) +# config.assets.precompile += %w( search.js ) + +# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. +# config.force_ssl = true +</ruby> + +h4(#config_test3_1). config/environments/test.rb + +You can help test performance with these additions to your test environment: + +<ruby> +# Configure static asset server for tests with Cache-Control for performance +config.serve_static_assets = true +config.static_cache_control = "public, max-age=3600" +</ruby> + +h4(#config_wp3_1). config/initializers/wrap_parameters.rb + +Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications. + +<ruby> +# Be sure to restart your server when you modify this file. +# This file contains settings for ActionController::ParamsWrapper which +# is enabled by default. + +# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. +ActiveSupport.on_load(:action_controller) do + wrap_parameters :format => [:json] +end + +# Disable root element in JSON by default. +ActiveSupport.on_load(:active_record) do + self.include_root_in_json = false +end +</ruby> diff --git a/railties/guides/w3c_validator.rb b/guides/w3c_validator.rb index f1fe1e0f33..f1fe1e0f33 100644 --- a/railties/guides/w3c_validator.rb +++ b/guides/w3c_validator.rb diff --git a/install.rb b/install.rb index abc02249c2..b87b008c2e 100644 --- a/install.rb +++ b/install.rb @@ -1,6 +1,6 @@ version = ARGV.pop -%w( activesupport activemodel activerecord activeresource actionpack actionmailer railties ).each do |framework| +%w( activesupport activemodel activerecord actionpack actionmailer railties ).each do |framework| puts "Installing #{framework}..." `cd #{framework} && gem build #{framework}.gemspec && gem install #{framework}-#{version}.gem --no-ri --no-rdoc && rm #{framework}-#{version}.gem` end diff --git a/rails.gemspec b/rails.gemspec index cd7c5d1ee9..8314036ad1 100644 --- a/rails.gemspec +++ b/rails.gemspec @@ -10,18 +10,19 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 1.9.3' s.required_rubygems_version = ">= 1.8.11" - s.author = 'David Heinemeier Hansson' - s.email = 'david@loudthinking.com' - s.homepage = 'http://www.rubyonrails.org' + s.author = 'David Heinemeier Hansson' + s.email = 'david@loudthinking.com' + s.homepage = 'http://www.rubyonrails.org' - s.bindir = 'bin' - s.executables = [] + s.bindir = 'bin' + s.executables = [] + s.files = Dir['guides/**/*'] - s.add_dependency('activesupport', version) - s.add_dependency('actionpack', version) - s.add_dependency('activerecord', version) - s.add_dependency('activeresource', version) - s.add_dependency('actionmailer', version) - s.add_dependency('railties', version) - s.add_dependency('bundler', '~> 1.0') + s.add_dependency('activesupport', version) + s.add_dependency('actionpack', version) + s.add_dependency('activerecord', version) + s.add_dependency('actionmailer', version) + s.add_dependency('railties', version) + s.add_dependency('bundler', '~> 1.1') + s.add_dependency('sprockets-rails', '~> 1.0') end diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index f556ee210d..bc34ced283 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Remove Rack::SSL in favour of ActionDispatch::SSL. *Rafael Mendonça França* + +* Remove Active Resource from Rails framework. *Prem Sichangrist* + * Allow to set class that will be used to run as a console, other than IRB, with `Rails.application.config.console=`. It's best to add it to `console` block. *Piotr Sarnacki* Example: @@ -20,6 +24,11 @@ * Rails::Plugin has gone. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies. *Santiago Pastorino* +## Rails 3.2.2 (March 1, 2012) ## + +* No changes. + + ## Rails 3.2.1 (January 26, 2012) ## * Documentation fixes. @@ -71,7 +80,7 @@ * Remove old 'config.paths.app.controller' API in favor of 'config.paths["app/controller"]' API *Guillermo Iguaran* -## Rails 3.1.4 (unreleased) ## +## Rails 3.1.4 (March 1, 2012) ## * Setting config.force_ssl also marks the session cookie as secure. @@ -203,7 +212,7 @@ * Include all helpers from plugins and shared engines in application *Piotr Sarnacki* -## Rails 3.0.12 (unreleased) ## +## Rails 3.0.12 (March 1, 2012) ## * No changes. diff --git a/railties/Rakefile b/railties/Rakefile index 25e515e016..c4a91a1d36 100755 --- a/railties/Rakefile +++ b/railties/Rakefile @@ -44,18 +44,6 @@ task :update_readme do cp "./README.rdoc", readme end -desc 'Generate guides (for authors), use ONLY=foo to process just "foo.textile"' -task :generate_guides do - ENV["WARN_BROKEN_LINKS"] = "1" # authors can't disable this - ruby "guides/rails_guides.rb" -end - -# Validate guides ------------------------------------------------------------------------- -desc 'Validate guides, use ONLY=foo to process just "foo.html"' -task :validate_guides do - ruby "guides/w3c_validator.rb" -end - # Generate GEM ---------------------------------------------------------------------------- spec = eval(File.read('railties.gemspec')) diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile deleted file mode 100644 index 37abb8a640..0000000000 --- a/railties/guides/source/active_resource_basics.textile +++ /dev/null @@ -1,120 +0,0 @@ -h2. Active Resource Basics - -This guide should provide you with all you need to get started managing the connection between business objects and RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics. - -endprologue. - -WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails. - -h3. Introduction - -Active Resource allows you to connect with RESTful web services. So, in Rails, Resource classes inherited from +ActiveResource::Base+ and live in +app/models+. - -h3. Configuration and Usage - -Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class -that inherits from ActiveResource::Base and providing a <tt>site</tt> class variable to it: - -<ruby> -class Person < ActiveResource::Base - self.site = "http://api.people.com:3000/" -end -</ruby> - -Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes -life cycle methods that operate against a persistent store. - -h3. Reading and Writing Data - -Active Resource make request over HTTP using a standard JSON format. It mirrors the RESTful routing built into Action Controller but will also work with any other REST service that properly implements the protocol. - -h4. Read - -Read requests use the GET method and expect the JSON form of whatever resource/resources is/are being requested. - -<ruby> -# Find a person with id = 1 -person = Person.find(1) -# Check if a person exists with id = 1 -Person.exists?(1) # => true -# Get all resources of Person class -Person.all -</ruby> - -h4. Create - -Creating a new resource submits the JSON form of the resource as the body of the request with HTTP POST method and parse the response into Active Resource object. - -<ruby> -person = Person.create(:name => 'Vishnu') -person.id # => 1 -</ruby> - -h4. Update - -To update an existing resource, 'save' method is used. This method make a HTTP PUT request in JSON format. - -<ruby> -person = Person.find(1) -person.name = 'Atrai' -person.save -</ruby> - -h4. Delete - -'destroy' method makes a HTTP DELETE request for an existing resource in JSON format to delete that resource. - -<ruby> -person = Person.find(1) -person.destroy -</ruby> - -h3. Validations - -Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveModel::Errors. - -h4. Validating client side resources by overriding validation methods in base class - -<ruby> -class Person < ActiveResource::Base - self.site = "http://api.people.com:3000/" - - protected - - def validate - errors.add("last", "has invalid characters") unless last =~ /[a-zA-Z]*/ - end -end -</ruby> - -h4. Validating client side resources - -Consider a Person resource on the server requiring both a first_name and a last_name with a validates_presence_of :first_name, :last_name declaration in the model: - -<ruby> -person = Person.new(:first_name => "Jim", :last_name => "") -person.save # => false (server returns an HTTP 422 status code and errors) -person.valid? # => false -person.errors.empty? # => false -person.errors.count # => 1 -person.errors.full_messages # => ["Last name can't be empty"] -person.errors[:last_name] # => ["can't be empty"] -person.last_name = "Halpert" -person.save # => true (and person is now saved to the remote service) -</ruby> - -h4. Public instance methods - -ActiveResource::Validations have three public instance methods - -h5. errors() - -This will return errors object that holds all information about attribute error messages - -h5. save_with_validation(options=nil) - -This validates the resource with any local validations written in base class and then it will try to POST if there are no errors. - -h5. valid? - -Runs all the local validations and will return true if no errors. diff --git a/railties/guides/source/upgrading_ruby_on_rails.textile b/railties/guides/source/upgrading_ruby_on_rails.textile deleted file mode 100644 index 3588a67196..0000000000 --- a/railties/guides/source/upgrading_ruby_on_rails.textile +++ /dev/null @@ -1,164 +0,0 @@ -h2. 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. - -endprologue. - -h3. Rails Upgrades - -When you're upgrading an existing application, it's always a great idea to have good test coverage before going in. Rails 3 and above requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.2.x will be the last branch to support 1.8.7 and Rails 4 (current edge) will support only Ruby 1.9.3. - -TIP: Note that Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump on to 1.9.2 or 1.9.3 for smooth sailing. - -h3. Upgrading from Rails 3.1 to Rails 3.2 - -We recommend that you first upgrade to Rails 3.1 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 3.2. - -The following changes are meant for upgrading your application to Rails 3.2.1, the latest 3.2.x version of Rails. - -h4(#gemfile3_2). Gemfile - -Make the following changes to your +Gemfile+. - -<ruby> -gem 'rails', '= 3.2.1' - -group :assets do - gem 'sass-rails', '~> 3.2.3' - gem 'coffee-rails', '~> 3.2.1' - gem 'uglifier', '>= 1.0.3' -end -</ruby> - -h4(#config_dev3_2). config/environments/development.rb - -* There are a couple of new configuration changes you'd want to add: - -<ruby> -# Raise exception on mass assignment protection for Active Record models -config.active_record.mass_assignment_sanitizer = :strict - -# Log the query plan for queries taking more than this (works -# with SQLite, MySQL, and PostgreSQL) -config.active_record.auto_explain_threshold_in_seconds = 0.5 -</ruby> - -h4(#config_test3_2). config/environments/test.rb - -The <tt>mass_assignment_sanitizer</tt> config also needs to be added in <tt>config/environments/test.rb</tt>: - -<ruby> -# Raise exception on mass assignment protection for Active Record models -config.active_record.mass_assignment_sanitizer = :strict -</ruby> - -h4(#plugins3_2). vendor/plugins - -* Rails 3.2 deprecates <tt>vendor/plugins</tt> and Rails 4.0 will remove them completely. You can start replacing these plugins by extracting them as gems and adding them in your Gemfile. If you choose not to make them gems, you can move them into, say, <tt>lib/my_plugin/*</tt> and add an appropriate initializer in <tt>config/initializers/my_plugin.rb</tt>. - -h3. Upgrading from Rails 3.0 to Rails 3.1 - -We recommend that you first upgrade to Rails 3.0 in case you haven't and make sure your application still runs as expected before attempting an update to Rails 3.1. - -The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails. - -h4(#gemfile3_1). Gemfile - -Make the following changes to your +Gemfile+. - -<ruby> -gem 'rails', '= 3.1.3' -gem 'mysql2' - -# Needed for the new asset pipeline -group :assets do - gem 'sass-rails', "~> 3.1.5" - gem 'coffee-rails', "~> 3.1.1" - gem 'uglifier', ">= 1.0.3" -end - -# jQuery is the default JavaScript library in Rails 3.1 -gem 'jquery-rails' -</ruby> - -h4(#config_app3_1). config/application.rb - -* The asset pipeline requires the following additions: - -<ruby> -config.assets.enabled = true -config.assets.version = '1.0' -</ruby> - -* If your application is using the "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts: - -<ruby> -# Defaults to '/assets' -config.assets.prefix = '/asset-files' -</ruby> - -h4(#config_dev3_1). config/environments/development.rb - -* Remove the RJS setting <tt>config.action_view.debug_rjs = true</tt>. - -* Add the following, if you enable the asset pipeline. - -<ruby> -# Do not compress assets -config.assets.compress = false - -# Expands the lines which load the assets -config.assets.debug = true -</ruby> - -h4(#config_prod3_1). config/environments/production.rb - -* Again, most of the changes below are for the asset pipeline. You can read more about these in the "Asset Pipeline":asset_pipeline.html guide. - -<ruby> -# Compress JavaScripts and CSS -config.assets.compress = true - -# Don't fallback to assets pipeline if a precompiled asset is missed -config.assets.compile = false - -# Generate digests for assets URLs -config.assets.digest = true - -# Defaults to Rails.root.join("public/assets") -# config.assets.manifest = YOUR_PATH - -# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) -# config.assets.precompile += %w( search.js ) - -# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. -# config.force_ssl = true -</ruby> - -h4(#config_test3_1). config/environments/test.rb - -<ruby> -# Configure static asset server for tests with Cache-Control for performance -config.serve_static_assets = true -config.static_cache_control = "public, max-age=3600" -</ruby> - -h4(#config_wp3_1). config/initializers/wrap_parameters.rb - -* Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications. - -<ruby> -# Be sure to restart your server when you modify this file. -# This file contains settings for ActionController::ParamsWrapper which -# is enabled by default. - -# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. -ActiveSupport.on_load(:action_controller) do - wrap_parameters :format => [:json] -end - -# Disable root element in JSON by default. -ActiveSupport.on_load(:active_record) do - self.include_root_in_json = false -end -</ruby> diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 77a09507a8..6b431d3ee3 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -42,11 +42,7 @@ module Rails end def initialized? - @@initialized || false - end - - def initialized=(initialized) - @@initialized ||= initialized + application.initialized? end def logger diff --git a/railties/lib/rails/all.rb b/railties/lib/rails/all.rb index 01ceb80972..eabe566829 100644 --- a/railties/lib/rails/all.rb +++ b/railties/lib/rails/all.rb @@ -4,9 +4,8 @@ require "rails" active_record action_controller action_mailer - active_resource rails/test_unit - sprockets + sprockets/rails ).each do |framework| begin require "#{framework}/railtie" diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index c163081bfc..b063e7681d 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,4 +1,3 @@ -require 'active_support/core_ext/hash/reverse_merge' require 'fileutils' require 'rails/engine' @@ -134,6 +133,10 @@ module Rails self end + def initialized? + @initialized + end + # Load the application and its railties tasks and invoke the registered hooks. # Check <tt>Rails::Railtie.rake_tasks</tt> for more info. def load_tasks(app=self) @@ -225,8 +228,11 @@ module Rails end if config.force_ssl - require "rack/ssl" - middleware.use ::Rack::SSL, config.ssl_options + middleware.use ::ActionDispatch::SSL, config.ssl_options + end + + if config.action_dispatch.x_sendfile_header.present? + middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header end if config.serve_static_assets @@ -242,10 +248,6 @@ module Rails middleware.use ::ActionDispatch::DebugExceptions middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies - if config.action_dispatch.x_sendfile_header.present? - middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header - end - unless config.cache_classes app = self middleware.use ::ActionDispatch::Reloader, lambda { app.reload_dependencies? } diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index 93a0fba10b..e567df7162 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -32,9 +32,9 @@ module Rails f.binmode f.sync = config.autoflush_log # if true make sure every write flushes - logger = ActiveSupport::TaggedLogging.new( - ActiveSupport::Logger.new(f) - ) + logger = ActiveSupport::Logger.new f + logger.formatter = config.log_formatter + logger = ActiveSupport::TaggedLogging.new(logger) logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase) logger rescue StandardError diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 1e424d9b4a..37297f0496 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -8,10 +8,10 @@ module Rails attr_accessor :allow_concurrency, :asset_host, :asset_path, :assets, :autoflush_log, :cache_classes, :cache_store, :consider_all_requests_local, :console, :dependency_loading, :exceptions_app, :file_watcher, :filter_parameters, - :force_ssl, :helpers_paths, :logger, :log_tags, :preload_frameworks, + :force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags, :preload_frameworks, :railties_order, :relative_url_root, :secret_token, :serve_static_assets, :ssl_options, :static_cache_control, :session_options, - :time_zone, :reload_classes_only_on_change + :time_zone, :reload_classes_only_on_change, :use_schema_cache_dump attr_writer :log_level attr_reader :encoding @@ -41,6 +41,8 @@ module Rails @file_watcher = ActiveSupport::FileUpdateChecker @exceptions_app = nil @autoflush_log = true + @log_formatter = ActiveSupport::Logger::SimpleFormatter.new + @use_schema_cache_dump = true @assets = ActiveSupport::OrderedOptions.new @assets.enabled = false @@ -104,7 +106,7 @@ module Rails # YAML::load. def database_configuration require 'erb' - YAML::load(ERB.new(IO.read(paths["config/database"].first)).result) + YAML.load ERB.new(IO.read(paths["config/database"].first)).result end def log_level diff --git a/railties/lib/rails/application/route_inspector.rb b/railties/lib/rails/application/route_inspector.rb index 2ca0c68243..1e5ce67a58 100644 --- a/railties/lib/rails/application/route_inspector.rb +++ b/railties/lib/rails/application/route_inspector.rb @@ -64,7 +64,7 @@ module Rails # executes `rake routes`. People should not use this class. class RouteInspector # :nodoc: def initialize - @engines = ActiveSupport::OrderedHash.new + @engines = Hash.new end def format all_routes, filter = nil diff --git a/railties/lib/rails/backtrace_cleaner.rb b/railties/lib/rails/backtrace_cleaner.rb index cc26db849d..539f68a3be 100644 --- a/railties/lib/rails/backtrace_cleaner.rb +++ b/railties/lib/rails/backtrace_cleaner.rb @@ -19,7 +19,7 @@ module Rails def add_gem_filters return unless defined?(Gem) - gems_paths = (Gem.path + [Gem.default_dir]).uniq.map!{ |p| Regexp.escape(p) } + gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) } return if gems_paths.empty? gems_regexp = %r{(#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)} diff --git a/railties/lib/rails/code_statistics.rb b/railties/lib/rails/code_statistics.rb index 03538b2422..bcac0751d6 100644 --- a/railties/lib/rails/code_statistics.rb +++ b/railties/lib/rails/code_statistics.rb @@ -95,13 +95,7 @@ class CodeStatistics #:nodoc: m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0 loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0 - start = if TEST_TYPES.include? name - "| #{name.ljust(20)} " - else - "| #{name.ljust(20)} " - end - - puts start + + puts "| #{name.ljust(20)} " + "| #{statistics["lines"].to_s.rjust(5)} " + "| #{statistics["codelines"].to_s.rjust(5)} " + "| #{statistics["classes"].to_s.rjust(7)} " + diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index 86376ac7e6..d7c9e820dc 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -24,6 +24,9 @@ module Rails OptionParser.new do |opt| opt.banner = "Usage: console [environment] [options]" opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v } + opt.on("-e", "--environment=name", String, + "Specifies the environment to run this console under (test/development/production).", + "Default: development") { |v| options[:environment] = v.strip } opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v } opt.parse!(arguments) end @@ -36,6 +39,14 @@ module Rails options[:sandbox] end + def environment? + options[:environment] + end + + def set_environment! + Rails.env = options[:environment] + end + def debugger? options[:debugger] end @@ -45,6 +56,8 @@ module Rails require_debugger if debugger? + set_environment! if environment? + if sandbox? puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})" puts "Any modifications you make will be rolled back on exit" diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index 0b757cbe28..721a47a974 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -32,6 +32,11 @@ module Rails opt_parser.parse! args + # Handle's environment like RAILS_ENV=production passed in directly + if index = args.index {|arg| arg.include?("RAILS_ENV")} + options[:environment] ||= args.delete_at(index).split('=').last + end + options[:server] = args.shift options end @@ -71,6 +76,8 @@ module Rails wrapped_app # touch the app so the logger is set up console = ActiveSupport::Logger.new($stdout) + console.formatter = Rails.logger.formatter + Rails.logger.extend(ActiveSupport::Logger.broadcast(console)) end diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index f8ad17773a..d8ca6cbd21 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -6,7 +6,34 @@ require 'rails/rack' module Rails module Configuration - class MiddlewareStackProxy #:nodoc: + # MiddlewareStackProxy is a proxy for the Rails middleware stack that allows + # you to configure middlewares in your application. It works basically as a + # command recorder, saving each command to be applied after initialization + # over the default middleware stack, so you can add, swap, or remove any + # middleware in Rails. + # + # You can add your own middlewares by using the +config.middleware.use+ method: + # + # config.middleware.use Magical::Unicorns + # + # This will put the +Magical::Unicorns+ middleware on the end of the stack. + # You can use +insert_before+ if you wish to add a middleware before another: + # + # config.middleware.insert_before ActionDispatch::Head, Magical::Unicorns + # + # There's also +insert_after+ which will insert a middleware after another: + # + # config.middleware.insert_after ActionDispatch::Head, Magical::Unicorns + # + # Middlewares can also be completely swapped out and replaced with others: + # + # config.middleware.swap ActionDispatch::BestStandardsSupport, Magical::Unicorns + # + # And finally they can also be removed from the stack completely: + # + # config.middleware.delete ActionDispatch::BestStandardsSupport + # + class MiddlewareStackProxy def initialize @operations = [] end @@ -33,7 +60,7 @@ module Rails @operations << [:delete, args, block] end - def merge_into(other) + def merge_into(other) #:nodoc: @operations.each do |operation, args, block| other.send(operation, *args, &block) end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index af2bde5a6e..131d6e5711 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -245,7 +245,7 @@ module Rails # # Additionally, an isolated engine will set its name according to namespace, so # MyEngine::Engine.engine_name will be "my_engine". It will also set MyEngine.table_name_prefix - # to "my_engine_", changing the MyEngine::Article model to use the my_engine_article table. + # to "my_engine_", changing the MyEngine::Article model to use the my_engine_articles table. # # == Using Engine's routes outside Engine # @@ -561,7 +561,7 @@ module Rails initializer :add_view_paths do views = paths["app/views"].existent unless views.empty? - ActiveSupport.on_load(:action_controller){ prepend_view_path(views) } + ActiveSupport.on_load(:action_controller){ prepend_view_path(views) if respond_to?(:prepend_view_path) } ActiveSupport.on_load(:action_mailer){ prepend_view_path(views) } end end diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index cd277c5097..b9c1b01f54 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -70,7 +70,7 @@ module Rails hide_namespaces(*config.hidden_namespaces) end - def self.templates_path + def self.templates_path #:nodoc: @templates_path ||= [] end @@ -235,7 +235,7 @@ module Rails rails.delete("plugin_new") print_list("rails", rails) - hidden_namespaces.each {|n| groups.delete(n.to_s) } + hidden_namespaces.each { |n| groups.delete(n.to_s) } groups.sort.each { |b, n| print_list(b, n) } end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 32793b1a70..8976927578 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -239,7 +239,7 @@ module Rails # # === Example # - # route "root :to => 'welcome'" + # route "root :to => 'welcome#index'" # def route(routing_code) log :route, routing_code diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 7c449657b5..bb2a9fcf22 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -189,6 +189,7 @@ module Rails # Gems used only for assets and not required # in production environments by default. group :assets do + gem 'sprockets-rails', :git => 'https://github.com/rails/sprockets-rails.git' gem 'sass-rails', :git => 'https://github.com/rails/sass-rails.git' gem 'coffee-rails', :git => 'https://github.com/rails/coffee-rails.git' @@ -202,6 +203,7 @@ module Rails # Gems used only for assets and not required # in production environments by default. group :assets do + gem 'sprockets-rails', :git => 'https://github.com/rails/sprockets-rails.git' gem 'sass-rails', '~> 4.0.0.beta' gem 'coffee-rails', '~> 4.0.0.beta' @@ -223,7 +225,7 @@ module Rails if defined?(JRUBY_VERSION) "gem 'therubyrhino'\n" else - "# gem 'therubyracer'\n" + "# gem 'therubyracer', :platform => :ruby\n" end end @@ -255,11 +257,6 @@ module Rails def git_keep(destination) create_file("#{destination}/.gitkeep") unless options[:skip_git] end - - # Returns Ruby 1.9 style key-value pair. - def key_value(key, value) - "#{key}: #{value}" - end end end end diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index 60e94486bb..ffc2d6e081 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -31,10 +31,9 @@ module Rails # root otherwise uses a default description. def self.desc(description=nil) return super if description - usage = source_root && File.expand_path("../USAGE", source_root) - @desc ||= if usage && File.exist?(usage) - ERB.new(File.read(usage)).result(binding) + @desc ||= if usage_path + ERB.new(File.read(usage_path)).result(binding) else "Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator." end @@ -213,7 +212,8 @@ module Rails # root, you should use source_root. def self.default_source_root return unless base_name && generator_name - path = File.expand_path(File.join(base_name, generator_name, 'templates'), base_root) + return unless default_generator_root + path = File.join(default_generator_root, 'templates') path if File.exists?(path) end @@ -373,6 +373,19 @@ module Rails } end + def self.usage_path + paths = [ + source_root && File.expand_path("../USAGE", source_root), + default_generator_root && File.join(default_generator_root, "USAGE") + ] + paths.compact.detect { |path| File.exists? path } + end + + def self.default_generator_root + path = File.expand_path(File.join(base_name, generator_name), base_root) + path if File.exists?(path) + end + end end end diff --git a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb index 85296ca37b..303331a4f0 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -16,7 +16,7 @@ <% end -%> <td><%%= link_to 'Show', <%= singular_table_name %> %></td> <td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td> - <td><%%= link_to 'Destroy', <%= singular_table_name %>, <%= key_value :confirm, "'Are you sure?'" %>, <%= key_value :method, ":delete" %> %></td> + <td><%%= link_to 'Destroy', <%= singular_table_name %>, confirm: 'Are you sure?', method: :delete %></td> <%% end %> </table> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 9cef55e0a6..862fd9e88d 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -180,11 +180,6 @@ module Rails class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}" end end - - # Returns Ruby 1.9 style key-value pair. - def key_value(key, value) - "#{key}: #{value}" - end end end end diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 5fdfe8f04e..676662b9f5 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -15,7 +15,7 @@ source 'https://rubygems.org' # To use Jbuilder templates for JSON # gem 'jbuilder' -# Use unicorn as the web server +# Use unicorn as the app server # gem 'unicorn' # Deploy with Capistrano diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb deleted file mode 100644 index e8065d9505..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ApplicationController < ActionController::Base - protect_from_forgery -end diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt new file mode 100644 index 0000000000..3ddc86ae0a --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt @@ -0,0 +1,5 @@ +class ApplicationController < ActionController::Base + # Prevent CSRF attacks by raising an exception. + # For APIs, you may want to use :reset_session instead. + protect_from_forgery :with => :exception +end
\ No newline at end of file diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb index acf47a03e5..c8a3c13b95 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/application.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -7,8 +7,7 @@ require 'rails/all' <%= comment_if :skip_active_record %>require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" -require "active_resource/railtie" -<%= comment_if :skip_sprockets %>require "sprockets/railtie" +<%= comment_if :skip_sprockets %>require "sprockets/rails/railtie" <%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" <% end -%> @@ -54,14 +53,14 @@ module <%= app_const_base %> # This will create an empty whitelist of attributes available for mass-assignment for all models # in your app. As such, your models will need to explicitly whitelist or blacklist accessible # parameters by using an attr_accessible or attr_protected declaration. - # config.active_record.whitelist_attributes = true + <%= comment_if :skip_active_record %>config.active_record.whitelist_attributes = true # Specifies wether or not has_many or has_one association option :dependent => :restrict raises # an exception. If set to true, then an ActiveRecord::DeleteRestrictionError exception would be # raised. If set to false, then an error will be added on the model instead. <%= comment_if :skip_active_record %>config.active_record.dependent_restrict_raises = false - <% unless options.skip_sprockets? -%> + # Enable the asset pipeline. config.assets.enabled = true diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml index 950016ad92..c3349912aa 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml @@ -12,9 +12,7 @@ development: adapter: mysql2 encoding: utf8 database: <%= app_name %>_development - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 username: root password: <% if mysql_socket -%> @@ -30,9 +28,7 @@ test: adapter: mysql2 encoding: utf8 database: <%= app_name %>_test - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 username: root password: <% if mysql_socket -%> @@ -45,9 +41,7 @@ production: adapter: mysql2 encoding: utf8 database: <%= app_name %>_production - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 username: root password: <% if mysql_socket -%> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml index a8ed15c2dc..f08f86aac3 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml @@ -16,9 +16,7 @@ development: adapter: postgresql encoding: unicode database: <%= app_name %>_development - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 username: <%= app_name %> password: @@ -44,9 +42,7 @@ test: adapter: postgresql encoding: unicode database: <%= app_name %>_test - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 username: <%= app_name %> password: @@ -54,8 +50,6 @@ production: adapter: postgresql encoding: unicode database: <%= app_name %>_production - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 username: <%= app_name %> password: diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml index 32a998ad72..51a4dd459d 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml @@ -6,9 +6,7 @@ development: adapter: sqlite3 database: db/development.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 timeout: 5000 # Warning: The database defined as "test" will be erased and @@ -17,15 +15,11 @@ development: test: adapter: sqlite3 database: db/test.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 - # Maximum number of database connections available per process. Please - # increase this number in multithreaded applications. - pool: 1 + pool: 5 timeout: 5000 diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt index 7041550fd0..7b2c86db24 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -21,7 +21,7 @@ # Generate digests for assets URLs. config.assets.digest = true - # Defaults to Rails.root.join("public/assets"). + # Defaults to nil and saved in location specified by config.assets.prefix # config.assets.manifest = YOUR_PATH <%- end -%> @@ -73,4 +73,7 @@ # Disable automatic flushing of the log to improve performance. # config.autoflush_log = false + + # Use default logging formatter so that PID and timestamp are not suppressed + config.log_formatter = ::Logger::Formatter.new end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt index a3143f1346..e02397aaf9 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt @@ -4,4 +4,6 @@ # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. +# Make sure your secret_token is kept private +# if you're sharing your code publicly. <%= app_const %>.config.secret_token = '<%= app_secret %>' diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt index ddfe4ba1e1..ade0c4f78c 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt @@ -1,6 +1,6 @@ # Be sure to restart your server when you modify this file. -<%= app_const %>.config.session_store :cookie_store, <%= key_value :key, "'_#{app_name}_session'" %> +<%= app_const %>.config.session_store :cookie_store, key: <%= "'_#{app_name}_session'" %> # Use the database for sessions instead of the cookie-based default, # which shouldn't be used to store highly confidential information diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt index d640f578da..19cbf0e4f1 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt @@ -5,7 +5,7 @@ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. ActiveSupport.on_load(:action_controller) do - wrap_parameters <%= key_value :format, "[:json]" %> + wrap_parameters format: [:json] if respond_to?(:wrap_parameters) end <%- unless options.skip_active_record? -%> diff --git a/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt index f75c5dd941..4edb1e857e 100644 --- a/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt @@ -3,5 +3,5 @@ # # Examples: # -# cities = City.create([{ <%= key_value :name, "'Chicago'" %> }, { <%= key_value :name, "'Copenhagen'" %> }]) -# Mayor.create(<%= key_value :name, "'Emanuel'" %>, <%= key_value :city, "cities.first" %>) +# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) +# Mayor.create(name: 'Emanuel', city: cities.first) diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile index 6ed6adcf1b..b7bc69d2e5 100755 --- a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile +++ b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile @@ -4,13 +4,8 @@ begin rescue LoadError puts 'You must `gem install bundler` and `bundle install` to run rake tasks' end -begin - require 'rdoc/task' -rescue LoadError - require 'rdoc/rdoc' - require 'rake/rdoctask' - RDoc::Task = Rake::RDocTask -end + +require 'rdoc/task' RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb b/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb index 996ea79e67..2f9b7fc962 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +++ b/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb @@ -7,8 +7,7 @@ require 'rails/all' <%= comment_if :skip_active_record %>require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" -require "active_resource/railtie" -<%= comment_if :skip_sprockets %>require "sprockets/railtie" +<%= comment_if :skip_sprockets %>require "sprockets/rails/railtie" <%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" <% end -%> diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb index dcd3b276e3..1e26a313cd 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb +++ b/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb @@ -8,3 +8,8 @@ Rails.backtrace_cleaner.remove_silencers! # Load support files Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } + +# Load fixtures from the engine +if ActiveSupport::TestCase.method_defined?(:fixture_path=) + ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) +end diff --git a/railties/lib/rails/generators/rails/resource/resource_generator.rb b/railties/lib/rails/generators/rails/resource/resource_generator.rb index c7345f3cfb..7c7b289d19 100644 --- a/railties/lib/rails/generators/rails/resource/resource_generator.rb +++ b/railties/lib/rails/generators/rails/resource/resource_generator.rb @@ -14,9 +14,12 @@ module Rails class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [], :desc => "Actions for the resource controller" + class_option :http, :type => :boolean, :default => false, + :desc => "Generate resource with HTTP actions only" + def add_resource_route return if options[:actions].present? - route_config = regular_class_path.collect{|namespace| "namespace :#{namespace} do " }.join(" ") + route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ") route_config << "resources :#{file_name.pluralize}" route_config << " end" * regular_class_path.size route route_config diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb index 2271c6f9c1..083eb49d65 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -10,8 +10,11 @@ module Rails class_option :orm, :banner => "NAME", :type => :string, :required => true, :desc => "ORM to generate the controller for" + class_option :http, :type => :boolean, :default => false, + :desc => "Generate controller with HTTP actions only" + def create_controller_files - template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb") + template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb") end hook_for :template_engine, :test_framework, :as => :scaffold 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 ee49534a04..b95aea5f19 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -7,7 +7,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # index.html.erb - format.json { render <%= key_value :json, "@#{plural_table_name}" %> } + format.json { render json: <%= "@#{plural_table_name}" %> } end end @@ -18,7 +18,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # show.html.erb - format.json { render <%= key_value :json, "@#{singular_table_name}" %> } + format.json { render json: <%= "@#{singular_table_name}" %> } end end @@ -29,7 +29,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # new.html.erb - format.json { render <%= key_value :json, "@#{singular_table_name}" %> } + format.json { render json: <%= "@#{singular_table_name}" %> } end end @@ -45,11 +45,11 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| if @<%= orm_instance.save %> - format.html { redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully created.'" %> } - format.json { render <%= key_value :json, "@#{singular_table_name}" %>, <%= key_value :status, ':created' %>, <%= key_value :location, "@#{singular_table_name}" %> } + format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> } + format.json { render json: <%= "@#{singular_table_name}" %>, status: :created, location: <%= "@#{singular_table_name}" %> } else - format.html { render <%= key_value :action, '"new"' %> } - format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> } + format.html { render action: "new" } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end end end @@ -61,11 +61,11 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %> - format.html { redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully updated.'" %> } + format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> } format.json { head :no_content } else - format.html { render <%= key_value :action, '"edit"' %> } - format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> } + format.html { render action: "edit" } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end end end diff --git a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb b/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb index d296b26b16..370750a175 100644 --- a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb +++ b/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb @@ -3,7 +3,7 @@ require 'rails/performance_test_help' class <%= class_name %>Test < ActionDispatch::PerformanceTest # Refer to the documentation for all available options - # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory] + # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory], # :output => 'tmp/performance', :formats => [:flat] } def test_homepage 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 f7e907a017..9e76587a0d 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -8,10 +8,30 @@ module TestUnit check_class_collision :suffix => "ControllerTest" + argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" + + class_option :http, :type => :boolean, :default => false, + :desc => "Generate functional test with HTTP actions only" + def create_test_files - template 'functional_test.rb', - File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb") + template "functional_test.rb", + File.join("test/functional", controller_class_path, "#{controller_file_name}_controller_test.rb") end + + private + + def attributes_hash + return if accessible_attributes.empty? + + accessible_attributes.map do |a| + name = a.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/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb index 9ec2e34545..30e1650555 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -19,30 +19,30 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post :create, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %> + post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %> end assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should show <%= singular_table_name %>" do - get :show, <%= key_value :id, "@#{singular_table_name}" %> + get :show, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should get edit" do - get :edit, <%= key_value :id, "@#{singular_table_name}" %> + get :edit, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should update <%= singular_table_name %>" do - put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %> + put :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should destroy <%= singular_table_name %>" do assert_difference('<%= class_name %>.count', -1) do - delete :destroy, <%= key_value :id, "@#{singular_table_name}" %> + delete :destroy, id: <%= "@#{singular_table_name}" %> end assert_redirected_to <%= index_helper %>_path diff --git a/railties/lib/rails/info.rb b/railties/lib/rails/info.rb index a1e15092b2..aacc1be2fc 100644 --- a/railties/lib/rails/info.rb +++ b/railties/lib/rails/info.rb @@ -23,7 +23,7 @@ module Rails end def frameworks - %w( active_record action_pack active_resource action_mailer active_support ) + %w( active_record action_pack action_mailer active_support ) end def framework_version(framework) @@ -83,7 +83,7 @@ module Rails end # Versions of each Rails framework (Active Record, Action Pack, - # Active Resource, Action Mailer, and Active Support). + # Action Mailer, and Active Support). frameworks.each do |framework| property "#{framework.titlecase} version" do framework_version(framework) diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb index 04d5b55c69..a61673fab8 100644 --- a/railties/lib/rails/initializable.rb +++ b/railties/lib/rails/initializable.rb @@ -51,7 +51,7 @@ module Rails def run_initializers(group=:default, *args) return if instance_variable_defined?(:@ran) - initializers.tsort.each do |initializer| + initializers.tsort_each do |initializer| initializer.run(*args) if initializer.belongs_to?(group) end @ran = true diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index b37421c09c..dd1790299d 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -1,5 +1,3 @@ -require 'set' - module Rails module Paths # This object is an extended hash that behaves as root of the <tt>Rails::Paths</tt> system. @@ -16,13 +14,13 @@ module Rails # path.eager_load? # => true # path.is_a?(Rails::Paths::Path) # => true # - # The +Path+ object is simply an array and allows you to easily add extra paths: + # The +Path+ object is simply an enumerable and allows you to easily add extra paths: # - # path.is_a?(Array) # => true - # path.inspect # => ["app/controllers"] + # path.is_a?(Enumerable) # => true + # path.to_ary.inspect # => ["app/controllers"] # # path << "lib/controllers" - # path.inspect # => ["app/controllers", "lib/controllers"] + # path.to_ary.inspect # => ["app/controllers", "lib/controllers"] # # Notice that when you add a path using +add+, the path object created already # contains the path with the same path value given to +add+. In some situations, @@ -43,25 +41,38 @@ module Rails # root["app/controllers"].existent # => ["/rails/app/controllers"] # # Check the <tt>Rails::Paths::Path</tt> documentation for more information. - class Root < ::Hash + class Root attr_accessor :path def initialize(path) - raise "Argument should be a String of the physical root path" if path.is_a?(Array) @current = nil @path = path - @root = self - super() + @root = {} end def []=(path, value) - value = Path.new(self, path, value) unless value.is_a?(Path) - super(path, value) + add(path, :with => value) end def add(path, options={}) with = options[:with] || path - self[path] = Path.new(self, path, with, options) + @root[path] = Path.new(self, path, [with].flatten, options) + end + + def [](path) + @root[path] + end + + def values + @root.values + end + + def keys + @root.keys + end + + def values_at(*list) + @root.values_at(*list) end def all_paths @@ -100,14 +111,14 @@ module Rails end end - class Path < Array + class Path + include Enumerable + attr_reader :path attr_accessor :glob - def initialize(root, current, *paths) - options = paths.last.is_a?(::Hash) ? paths.pop : {} - super(paths.flatten) - + def initialize(root, current, paths, options = {}) + @paths = paths @current = current @root = root @glob = options[:glob] @@ -148,6 +159,27 @@ module Rails RUBY end + def each(&block) + @paths.each(&block) + end + + def <<(path) + @paths << path + end + alias :push :<< + + def concat(paths) + @paths.concat paths + end + + def unshift(path) + @paths.unshift path + end + + def to_ary + @paths + end + # Expands all paths against the root and return all unique values. def expanded raise "You need to set a path root" unless @root.path @@ -156,8 +188,10 @@ module Rails each do |p| path = File.expand_path(p, @root.path) - if @glob - result.concat Dir[File.join(path, @glob)].sort + if @glob && File.directory?(path) + result.concat Dir.chdir(path) { + Dir.glob(@glob).map { |file| File.join path, file }.sort + } else result << path end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 7fed7c8631..e8563f4daf 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -9,7 +9,7 @@ module Rails # Rails and/or modify the initialization process. # # Every major component of Rails (Action Mailer, Action Controller, - # Action View, Active Record and Active Resource) is a Railtie. Each of + # Action View and Active Record) is a Railtie. Each of # them is responsible for their own initialization. This makes Rails itself # absent of any component hooks, allowing other components to be used in # place of any of the Rails defaults. diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index 9bfc2b16ab..4cd60fdc39 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -22,7 +22,7 @@ class SourceAnnotationExtractor # If +options+ has a flag <tt>:tag</tt> the tag is shown as in the example above. # Otherwise the string contains just line and text. def to_s(options={}) - s = "[#{line.to_s.rjust(options[:indent])}]" + s = "[#{line.to_s.rjust(options[:indent])}] " s << "[#{tag}] " if options[:tag] s << text end diff --git a/railties/lib/rails/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake index e09379c8c2..2851ca4189 100644 --- a/railties/lib/rails/tasks/documentation.rake +++ b/railties/lib/rails/tasks/documentation.rake @@ -1,10 +1,4 @@ -begin - require 'rdoc/task' -rescue LoadError - require 'rdoc/rdoc' - require 'rake/rdoctask' - RDoc::Task = Rake::RDocTask -end +require 'rdoc/task' # Monkey-patch to remove redoc'ing and clobber descriptions to cut down on rake -T noise class RDocTaskWithoutDescriptions < RDoc::Task @@ -89,12 +83,6 @@ namespace :doc do end end - gem_path('activeresource') do |activeresource| - %w(README.rdoc CHANGELOG.md lib/active_resource.rb lib/active_resource/*).each do |file| - rdoc.rdoc_files.include("#{activeresource}/#{file}") - end - end - gem_path('activesupport') do |activesupport| %w(README.rdoc CHANGELOG.md lib/active_support/**/*.rb).each do |file| rdoc.rdoc_files.include("#{activesupport}/#{file}") diff --git a/railties/lib/rails/test_help.rb b/railties/lib/rails/test_help.rb index 11e4353c87..46bf3bbe48 100644 --- a/railties/lib/rails/test_help.rb +++ b/railties/lib/rails/test_help.rb @@ -18,10 +18,6 @@ if defined?(ActiveRecord::Base) class ActiveSupport::TestCase include ActiveRecord::TestFixtures self.fixture_path = "#{Rails.root}/test/fixtures/" - - setup do - ActiveRecord::IdentityMap.clear - end end ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path diff --git a/railties/railties.gemspec b/railties/railties.gemspec index 82655ad394..e84b1d0644 100644 --- a/railties/railties.gemspec +++ b/railties/railties.gemspec @@ -12,7 +12,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/**/*', 'guides/**/*', 'lib/**/{*,.[a-z]*}'] + s.files = Dir['CHANGELOG.md', 'README.rdoc', 'bin/**/*', 'lib/**/{*,.[a-z]*}'] s.require_path = 'lib' s.bindir = 'bin' @@ -22,7 +22,6 @@ Gem::Specification.new do |s| s.add_dependency('rake', '>= 0.8.7') s.add_dependency('thor', '~> 0.14.6') - s.add_dependency('rack-ssl', '~> 1.3.2') s.add_dependency('rdoc', '~> 3.4') s.add_dependency('activesupport', version) s.add_dependency('actionpack', version) diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index c9310aff87..ac5ac2b93e 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -275,19 +275,23 @@ module ApplicationTests require "#{app_path}/config/environment" + token = "cf50faa3fe97702ca1ae" + PostsController.any_instance.stubs(:form_authenticity_token).returns(token) + params = {:authenticity_token => token} + get "/posts/1" assert_match /patch/, last_response.body - patch "/posts/1" + patch "/posts/1", params assert_match /update/, last_response.body - patch "/posts/1" + patch "/posts/1", params assert_equal 200, last_response.status - put "/posts/1" + put "/posts/1", params assert_match /update/, last_response.body - put "/posts/1" + put "/posts/1", params assert_equal 200, last_response.status end @@ -528,6 +532,12 @@ module ApplicationTests end RUBY + app_file 'app/controllers/application_controller.rb', <<-RUBY + class ApplicationController < ActionController::Base + protect_from_forgery :with => :reset_session # as we are testing API here + end + RUBY + app_file 'app/controllers/posts_controller.rb', <<-RUBY class PostsController < ApplicationController def create diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index 8812685620..a08e5b2374 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -1,4 +1,5 @@ require "isolation/abstract_unit" +require 'set' module ApplicationTests class FrameworksTest < ActiveSupport::TestCase @@ -66,7 +67,7 @@ module ApplicationTests require "#{app_path}/config/environment" assert Foo.method_defined?(:foo_path) assert Foo.method_defined?(:main_app) - assert_equal ["notify"], Foo.action_methods + assert_equal Set.new(["notify"]), Foo.action_methods end test "allows to not load all helpers for controllers" do @@ -193,5 +194,26 @@ module ApplicationTests require "#{app_path}/config/environment" assert_nil defined?(ActiveRecord::Base) end + + test "use schema cache dump" do + Dir.chdir(app_path) do + `rails generate model post title:string; + bundle exec rake db:migrate db:schema:cache:dump` + end + require "#{app_path}/config/environment" + ActiveRecord::Base.connection.drop_table("posts") # force drop posts table for test. + assert ActiveRecord::Base.connection.schema_cache.tables["posts"] + end + + test "expire schema cache dump" do + Dir.chdir(app_path) do + `rails generate model post title:string; + bundle exec rake db:migrate db:schema:cache:dump db:rollback` + end + silence_warnings { + require "#{app_path}/config/environment" + assert !ActiveRecord::Base.connection.schema_cache.tables["posts"] + } + end end end diff --git a/railties/test/application/loading_test.rb b/railties/test/application/loading_test.rb index 5ad51f8476..92951e1676 100644 --- a/railties/test/application/loading_test.rb +++ b/railties/test/application/loading_test.rb @@ -20,6 +20,7 @@ class LoadingTest < ActiveSupport::TestCase app_file "app/models/post.rb", <<-MODEL class Post < ActiveRecord::Base validates_acceptance_of :title, :accept => "omg" + attr_accessible :title end MODEL @@ -94,7 +95,7 @@ class LoadingTest < ActiveSupport::TestCase assert_equal [ActiveRecord::SchemaMigration], ActiveRecord::Base.descendants end - test "initialize_cant_be_called_twice" do + test "initialize cant be called twice" do require "#{app_path}/config/environment" assert_raise(RuntimeError) { ::AppTemplate::Application.initialize! } end @@ -255,6 +256,45 @@ class LoadingTest < ActiveSupport::TestCase assert_equal "BODY", last_response.body end + test "AC load hooks can be used with metal" do + app_file "app/controllers/omg_controller.rb", <<-RUBY + begin + class OmgController < ActionController::Metal + ActiveSupport.run_load_hooks(:action_controller, self) + def show + self.response_body = ["OK"] + end + end + rescue => e + puts "Error loading metal: \#{e.class} \#{e.message}" + end + RUBY + + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match "/:controller(/:action)" + end + RUBY + + require "#{rails_root}/config/environment" + + require 'rack/test' + extend Rack::Test::Methods + + get '/omg/show' + assert_equal 'OK', last_response.body + end + + def test_initialize_can_be_called_at_any_time + require "#{app_path}/config/application" + + assert !Rails.initialized? + assert !AppTemplate::Application.initialized? + Rails.initialize! + assert Rails.initialized? + assert AppTemplate::Application.initialized? + end + protected def setup_ar! diff --git a/railties/test/application/middleware/sendfile_test.rb b/railties/test/application/middleware/sendfile_test.rb index 0591386a87..eb791f5687 100644 --- a/railties/test/application/middleware/sendfile_test.rb +++ b/railties/test/application/middleware/sendfile_test.rb @@ -57,5 +57,18 @@ module ApplicationTests get "/" assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"] end + + test "files handled by ActionDispatch::Static are handled by Rack::Sendfile" do + make_basic_app do |app| + app.config.action_dispatch.x_sendfile_header = 'X-Sendfile' + app.config.serve_static_assets = true + app.paths["public"] = File.join(rails_root, "public") + end + + app_file "public/foo.txt", "foo" + + get "/foo.txt", "HTTP_X_SENDFILE_TYPE" => "X-Sendfile" + assert_equal File.join(rails_root, "public/foo.txt"), last_response.headers["X-Sendfile"] + end end end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index d0a550d2f0..fc5fb60174 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -26,6 +26,7 @@ module ApplicationTests boot! assert_equal [ + "Rack::Sendfile", "ActionDispatch::Static", "Rack::Lock", "ActiveSupport::Cache::Strategy::LocalCache", @@ -36,7 +37,6 @@ module ApplicationTests "ActionDispatch::ShowExceptions", "ActionDispatch::DebugExceptions", "ActionDispatch::RemoteIp", - "Rack::Sendfile", "ActionDispatch::Reloader", "ActionDispatch::Callbacks", "ActiveRecord::ConnectionAdapters::ConnectionManagement", @@ -66,13 +66,13 @@ module ApplicationTests assert_equal "Rack::Cache", middleware.first end - test "Rack::SSL is present when force_ssl is set" do + test "ActionDispatch::SSL is present when force_ssl is set" do add_to_config "config.force_ssl = true" boot! - assert middleware.include?("Rack::SSL") + assert middleware.include?("ActionDispatch::SSL") end - test "Rack::SSL is configured with options when given" do + test "ActionDispatch::SSL is configured with options when given" do add_to_config "config.force_ssl = true" add_to_config "config.ssl_options = { :host => 'example.com' }" boot! @@ -85,7 +85,6 @@ module ApplicationTests boot! assert !middleware.include?("ActiveRecord::ConnectionAdapters::ConnectionManagement") assert !middleware.include?("ActiveRecord::QueryCache") - assert !middleware.include?("ActiveRecord::IdentityMap::Middleware") end test "removes lock if allow concurrency is set" do @@ -143,12 +142,6 @@ module ApplicationTests assert_equal "Rack::Runtime", middleware.fourth end - test "identity map is inserted" do - add_to_config "config.active_record.identity_map = true" - boot! - assert middleware.include?("ActiveRecord::IdentityMap::Middleware") - end - test "insert middleware before" do add_to_config "config.middleware.insert_before ActionDispatch::Static, Rack::Config" boot! @@ -186,7 +179,6 @@ module ApplicationTests assert_equal etag, last_response.headers["Etag"] get "/?nothing=true" - puts last_response.body assert_equal 200, last_response.status assert_equal "", last_response.body assert_equal "text/html; charset=utf-8", last_response.headers["Content-Type"] diff --git a/railties/test/application/rake/migrations_test.rb b/railties/test/application/rake/migrations_test.rb index c94334f189..0a47fd014c 100644 --- a/railties/test/application/rake/migrations_test.rb +++ b/railties/test/application/rake/migrations_test.rb @@ -16,44 +16,45 @@ module ApplicationTests test 'running migrations with given scope' do Dir.chdir(app_path) do `rails generate model user username:string password:string` - end - app_file "db/migrate/01_a_migration.bukkits.rb", <<-MIGRATION - class AMigration < ActiveRecord::Migration - end - MIGRATION - output = Dir.chdir(app_path) { `rake db:migrate SCOPE=bukkits` } - assert_no_match(/create_table\(:users\)/, output) - assert_no_match(/CreateUsers/, output) - assert_no_match(/add_column\(:users, :email, :string\)/, output) + app_file "db/migrate/01_a_migration.bukkits.rb", <<-MIGRATION + class AMigration < ActiveRecord::Migration + end + MIGRATION + + output = `rake db:migrate SCOPE=bukkits` + assert_no_match(/create_table\(:users\)/, output) + assert_no_match(/CreateUsers/, output) + assert_no_match(/add_column\(:users, :email, :string\)/, output) - assert_match(/AMigration: migrated/, output) + assert_match(/AMigration: migrated/, output) - output = Dir.chdir(app_path) { `rake db:migrate SCOPE=bukkits VERSION=0` } - assert_no_match(/drop_table\(:users\)/, output) - assert_no_match(/CreateUsers/, output) - assert_no_match(/remove_column\(:users, :email\)/, output) + output = `rake db:migrate SCOPE=bukkits VERSION=0` + assert_no_match(/drop_table\(:users\)/, output) + assert_no_match(/CreateUsers/, output) + assert_no_match(/remove_column\(:users, :email\)/, output) - assert_match(/AMigration: reverted/, output) + assert_match(/AMigration: reverted/, output) + end end test 'model and migration generator with change syntax' do Dir.chdir(app_path) do - `rails generate model user username:string password:string` - `rails generate migration add_email_to_users email:string` + `rails generate model user username:string password:string; + rails generate migration add_email_to_users email:string` + + output = `rake db:migrate` + assert_match(/create_table\(:users\)/, output) + assert_match(/CreateUsers: migrated/, output) + assert_match(/add_column\(:users, :email, :string\)/, output) + assert_match(/AddEmailToUsers: migrated/, output) + + output = `rake db:rollback STEP=2` + assert_match(/drop_table\("users"\)/, output) + assert_match(/CreateUsers: reverted/, output) + assert_match(/remove_column\("users", :email\)/, output) + assert_match(/AddEmailToUsers: reverted/, output) end - - output = Dir.chdir(app_path){ `rake db:migrate` } - assert_match(/create_table\(:users\)/, output) - assert_match(/CreateUsers: migrated/, output) - assert_match(/add_column\(:users, :email, :string\)/, output) - assert_match(/AddEmailToUsers: migrated/, output) - - output = Dir.chdir(app_path){ `rake db:rollback STEP=2` } - assert_match(/drop_table\("users"\)/, output) - assert_match(/CreateUsers: reverted/, output) - assert_match(/remove_column\("users", :email\)/, output) - assert_match(/AddEmailToUsers: reverted/, output) end test 'migration status when schema migrations table is not present' do @@ -63,94 +64,94 @@ module ApplicationTests test 'test migration status' do Dir.chdir(app_path) do - `rails generate model user username:string password:string` - `rails generate migration add_email_to_users email:string` - end + `rails generate model user username:string password:string; + rails generate migration add_email_to_users email:string; + rake db:migrate` - Dir.chdir(app_path) { `rake db:migrate` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + output = `rake db:migrate:status` - assert_match(/up\s+\d{14}\s+Create users/, output) - assert_match(/up\s+\d{14}\s+Add email to users/, output) + assert_match(/up\s+\d{14}\s+Create users/, output) + assert_match(/up\s+\d{14}\s+Add email to users/, output) - Dir.chdir(app_path) { `rake db:rollback STEP=1` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + `rake db:rollback STEP=1` + output = `rake db:migrate:status` - assert_match(/up\s+\d{14}\s+Create users/, output) - assert_match(/down\s+\d{14}\s+Add email to users/, output) + assert_match(/up\s+\d{14}\s+Create users/, output) + assert_match(/down\s+\d{14}\s+Add email to users/, output) + end end test 'migration status without timestamps' do add_to_config('config.active_record.timestamped_migrations = false') Dir.chdir(app_path) do - `rails generate model user username:string password:string` - `rails generate migration add_email_to_users email:string` - end + `rails generate model user username:string password:string; + rails generate migration add_email_to_users email:string; + rake db:migrate` - Dir.chdir(app_path) { `rake db:migrate` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + output = `rake db:migrate:status` - assert_match(/up\s+\d{3,}\s+Create users/, output) - assert_match(/up\s+\d{3,}\s+Add email to users/, output) + assert_match(/up\s+\d{3,}\s+Create users/, output) + assert_match(/up\s+\d{3,}\s+Add email to users/, output) - Dir.chdir(app_path) { `rake db:rollback STEP=1` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + `rake db:rollback STEP=1` + output = `rake db:migrate:status` - assert_match(/up\s+\d{3,}\s+Create users/, output) - assert_match(/down\s+\d{3,}\s+Add email to users/, output) + assert_match(/up\s+\d{3,}\s+Create users/, output) + assert_match(/down\s+\d{3,}\s+Add email to users/, output) + end end test 'test migration status after rollback and redo' do Dir.chdir(app_path) do - `rails generate model user username:string password:string` - `rails generate migration add_email_to_users email:string` - end + `rails generate model user username:string password:string; + rails generate migration add_email_to_users email:string; + rake db:migrate` - Dir.chdir(app_path) { `rake db:migrate` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + output = `rake db:migrate:status` - assert_match(/up\s+\d{14}\s+Create users/, output) - assert_match(/up\s+\d{14}\s+Add email to users/, output) + assert_match(/up\s+\d{14}\s+Create users/, output) + assert_match(/up\s+\d{14}\s+Add email to users/, output) - Dir.chdir(app_path) { `rake db:rollback STEP=2` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + `rake db:rollback STEP=2` + output = `rake db:migrate:status` - assert_match(/down\s+\d{14}\s+Create users/, output) - assert_match(/down\s+\d{14}\s+Add email to users/, output) + assert_match(/down\s+\d{14}\s+Create users/, output) + assert_match(/down\s+\d{14}\s+Add email to users/, output) - Dir.chdir(app_path) { `rake db:migrate:redo` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + `rake db:migrate:redo` + output = `rake db:migrate:status` - assert_match(/up\s+\d{14}\s+Create users/, output) - assert_match(/up\s+\d{14}\s+Add email to users/, output) + assert_match(/up\s+\d{14}\s+Create users/, output) + assert_match(/up\s+\d{14}\s+Add email to users/, output) + end end test 'migration status after rollback and redo without timestamps' do add_to_config('config.active_record.timestamped_migrations = false') Dir.chdir(app_path) do - `rails generate model user username:string password:string` - `rails generate migration add_email_to_users email:string` - end + `rails generate model user username:string password:string; + rails generate migration add_email_to_users email:string; + rake db:migrate` - Dir.chdir(app_path) { `rake db:migrate` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + output = `rake db:migrate:status` - assert_match(/up\s+\d{3,}\s+Create users/, output) - assert_match(/up\s+\d{3,}\s+Add email to users/, output) + assert_match(/up\s+\d{3,}\s+Create users/, output) + assert_match(/up\s+\d{3,}\s+Add email to users/, output) - Dir.chdir(app_path) { `rake db:rollback STEP=2` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + `rake db:rollback STEP=2` + output = `rake db:migrate:status` - assert_match(/down\s+\d{3,}\s+Create users/, output) - assert_match(/down\s+\d{3,}\s+Add email to users/, output) + assert_match(/down\s+\d{3,}\s+Create users/, output) + assert_match(/down\s+\d{3,}\s+Add email to users/, output) - Dir.chdir(app_path) { `rake db:migrate:redo` } - output = Dir.chdir(app_path) { `rake db:migrate:status` } + `rake db:migrate:redo` + output = `rake db:migrate:status` - assert_match(/up\s+\d{3,}\s+Create users/, output) - assert_match(/up\s+\d{3,}\s+Add email to users/, output) + assert_match(/up\s+\d{3,}\s+Create users/, output) + assert_match(/up\s+\d{3,}\s+Add email to users/, output) + end end end end diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 4ab20afc47..04abf9e3a1 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -3,7 +3,7 @@ require "isolation/abstract_unit" module ApplicationTests module RakeTests class RakeNotesTest < ActiveSupport::TestCase - def setup + def setup build_app require "rails/all" end @@ -13,7 +13,6 @@ module ApplicationTests end test 'notes' do - app_file "app/views/home/index.html.erb", "<% # TODO: note in erb %>" app_file "app/views/home/index.html.haml", "-# TODO: note in haml" app_file "app/views/home/index.html.slim", "/ TODO: note in slim" @@ -29,7 +28,7 @@ module ApplicationTests Dir.chdir(app_path) do output = `bundle exec rake notes` - lines = output.scan(/\[([0-9\s]+)\]/).flatten + lines = output.scan(/\[([0-9\s]+)\](\s)/) assert_match /note in erb/, output assert_match /note in haml/, output @@ -39,8 +38,9 @@ module ApplicationTests assert_equal 5, lines.size - lines.each do |line_number| - assert_equal 4, line_number.size + lines.each do |line| + assert_equal 4, line[0].size + assert_equal ' ', line[1] end end diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index ff12b3e9fc..27d521485c 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -107,9 +107,9 @@ module ApplicationTests def test_loading_specific_fixtures Dir.chdir(app_path) do - `rails generate model user username:string password:string` - `rails generate model product name:string` - `rake db:migrate` + `rails generate model user username:string password:string; + rails generate model product name:string; + rake db:migrate` end require "#{rails_root}/config/environment" @@ -123,20 +123,49 @@ module ApplicationTests end def test_scaffold_tests_pass_by_default - content = Dir.chdir(app_path) do - `rails generate scaffold user username:string password:string` - `bundle exec rake db:migrate db:test:clone test` + output = Dir.chdir(app_path) do + `rails generate scaffold user username:string password:string; + bundle exec rake db:migrate db:test:clone test` end - assert_match(/\d+ tests, \d+ assertions, 0 failures, 0 errors/, content) + assert_match(/7 tests, 13 assertions, 0 failures, 0 errors/, output) + assert_no_match(/Errors running/, output) end def test_rake_dump_structure_should_respect_db_structure_env_variable Dir.chdir(app_path) do - `bundle exec rake db:migrate` # ensure we have a schema_migrations table to dump - `bundle exec rake db:structure:dump DB_STRUCTURE=db/my_structure.sql` + # ensure we have a schema_migrations table to dump + `bundle exec rake db:migrate db:structure:dump DB_STRUCTURE=db/my_structure.sql` end assert File.exists?(File.join(app_path, 'db', 'my_structure.sql')) end + + def test_rake_dump_structure_should_be_called_twice_when_migrate_redo + add_to_config "config.active_record.schema_format = :sql" + + output = Dir.chdir(app_path) do + `rails g model post title:string; + bundle exec rake db:migrate:redo 2>&1 --trace;` + end + + # expect only Invoke db:structure:dump (first_time) + assert_no_match(/^\*\* Invoke db:structure:dump\s+$/, output) + end + + def test_rake_dump_schema_cache + Dir.chdir(app_path) do + `rails generate model post title:string; + rails generate model product name:string; + bundle exec rake db:migrate db:schema:cache:dump` + end + assert File.exists?(File.join(app_path, 'db', 'schema_cache.dump')) + end + + def test_rake_clear_schema_cache + Dir.chdir(app_path) do + `bundle exec rake db:schema:cache:dump db:schema:cache:clear` + end + assert !File.exists?(File.join(app_path, 'db', 'schema_cache.dump')) + end end end diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 01847ae58c..9aa1d68675 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -55,6 +55,25 @@ class Rails::ConsoleTest < ActiveSupport::TestCase assert_match /Loading \w+ environment in sandbox \(Rails/, output end + def test_console_with_environment + app.expects(:sandbox=).with(nil) + FakeConsole.expects(:start) + + start ["-e production"] + + assert_match /production/, output + end + + def test_console_with_rails_environment + app.expects(:sandbox=).with(nil) + FakeConsole.expects(:start) + + start ["RAILS_ENV=production"] + + assert_match /production/, output + end + + def test_console_defaults_to_IRB config = mock("config", :console => nil) app = mock("app", :config => config) diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb new file mode 100644 index 0000000000..8039aec873 --- /dev/null +++ b/railties/test/commands/server_test.rb @@ -0,0 +1,26 @@ +require 'abstract_unit' +require 'rails/commands/server' + +class Rails::ServerTest < ActiveSupport::TestCase + + def test_environment_with_server_option + args = ["thin", "RAILS_ENV=production"] + options = Rails::Server::Options.new.parse!(args) + assert_equal 'production', options[:environment] + assert_equal 'thin', options[:server] + end + + def test_environment_without_server_option + args = ["RAILS_ENV=production"] + options = Rails::Server::Options.new.parse!(args) + assert_equal 'production', options[:environment] + assert_nil options[:server] + end + + def test_server_option_without_environment + args = ["thin"] + options = Rails::Server::Options.new.parse!(args) + assert_nil options[:environment] + assert_equal 'thin', options[:server] + end +end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index a3c24c392b..128854e4ad 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -202,6 +202,7 @@ class AppGeneratorTest < Rails::Generators::TestCase run_generator [destination_root, "--skip-active-record"] assert_no_file "config/database.yml" assert_file "config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ + assert_file "config/application.rb", /#\s+config\.active_record\.whitelist_attributes = true/ assert_file "config/application.rb", /#\s+config\.active_record\.dependent_restrict_raises = false/ assert_file "test/test_helper.rb" do |helper_content| assert_no_match(/fixtures :all/, helper_content) @@ -212,7 +213,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_generator_if_skip_sprockets_is_given run_generator [destination_root, "--skip-sprockets"] assert_file "config/application.rb" do |content| - assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content) + assert_match(/#\s+require\s+["']sprockets\/rails\/railtie["']/, content) assert_no_match(/config\.assets\.enabled = true/, content) end assert_file "Gemfile" do |content| @@ -235,7 +236,7 @@ class AppGeneratorTest < Rails::Generators::TestCase if defined?(JRUBY_VERSION) assert_file "Gemfile", /gem\s+["']therubyrhino["']$/ else - assert_file "Gemfile", /# gem\s+["']therubyracer["']$/ + assert_file "Gemfile", /# gem\s+["']therubyracer["']+, :platform => :ruby$/ end end @@ -301,7 +302,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_default_usage - File.expects(:exist?).returns(false) + Rails::Generators::AppGenerator.expects(:usage_path).returns(nil) assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc) end @@ -350,6 +351,11 @@ class AppGeneratorTest < Rails::Generators::TestCase end end + def test_active_record_whitelist_attributes_is_present_application_config + run_generator + assert_file "config/application.rb", /config\.active_record\.whitelist_attributes = true/ + end + def test_active_record_dependent_restrict_raises_is_present_application_config run_generator assert_file "config/application.rb", /config\.active_record\.dependent_restrict_raises = false/ @@ -365,7 +371,6 @@ protected def action(*args, &block) silence(:stdout) { generator.send(*args, &block) } end - end class CustomAppGeneratorTest < Rails::Generators::TestCase diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 4e08e5dae1..b320e40654 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -41,6 +41,24 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end + def test_remove_migration_with_indexed_attribute + migration = "remove_title_body_from_posts" + 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) + end + end + end + def test_remove_migration_with_attributes migration = "remove_title_body_from_posts" run_generator [migration, "title:string", "body:text"] @@ -134,4 +152,8 @@ class MigrationGeneratorTest < Rails::Generators::TestCase end end end + + def test_properly_identifies_usage_file + assert generator_class.send(:usage_path) + end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 156fa86eee..e8d933935d 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -317,4 +317,14 @@ class ModelGeneratorTest < Rails::Generators::TestCase end end end + + def test_attr_accessible_added_with_non_reference_attributes + run_generator + assert_file 'app/models/account.rb', /attr_accessible :age, :name/ + end + + def test_attr_accessible_added_with_comments_when_no_attributes_present + run_generator ["Account"] + assert_file 'app/models/account.rb', /# attr_accessible :title, :body/ + end end diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 1382133d7b..1eea50b0d9 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -75,6 +75,19 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_file "test/functional/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) + end + end + + def test_functional_tests_without_attributes + run_generator ["User"] + + assert_file "test/functional/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: \{ \}/, content) + assert_match(/put :update, id: @user, user: \{ \}/, content) end end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 2db8090621..7123950add 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -62,8 +62,11 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end end - assert_file "test/functional/product_lines_controller_test.rb", - /class ProductLinesControllerTest < ActionController::TestCase/ + assert_file "test/functional/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) + end # Views %w( @@ -85,6 +88,17 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_file "app/assets/stylesheets/product_lines.css" end + def test_functional_tests_without_attributes + run_generator ["product_line"] + + assert_file "test/functional/product_lines_controller_test.rb" do |content| + assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, content) + assert_match(/test "should get index"/, content) + assert_match(/post :create, product_line: \{ \}/, content) + assert_match(/put :update, id: @product_line, product_line: \{ \}/, content) + end + end + def test_scaffold_on_revoke run_generator run_generator ["product_line"], :behavior => :revoke diff --git a/railties/test/generators/shared_generator_tests.rb b/railties/test/generators/shared_generator_tests.rb index 14a20eddb8..92117855b7 100644 --- a/railties/test/generators/shared_generator_tests.rb +++ b/railties/test/generators/shared_generator_tests.rb @@ -91,21 +91,6 @@ module SharedGeneratorTests assert_match(/It works!/, capture(:stdout) { generator.invoke_all }) end - def test_template_raises_an_error_with_invalid_path - content = capture(:stderr){ run_generator([destination_root, "-m", "non/existant/path"]) } - assert_match(/The template \[.*\] could not be loaded/, content) - assert_match(/non\/existant\/path/, content) - end - - def test_template_is_executed_when_supplied - path = "http://gist.github.com/103208.txt" - template = %{ say "It works!" } - template.instance_eval "def read; self; end" # Make the string respond to read - - generator([destination_root], :template => path).expects(:open).with(path, 'Accept' => 'application/x-thor-template').returns(template) - assert_match(/It works!/, capture(:stdout) { generator.invoke_all }) - end - def test_template_is_executed_when_supplied_an_https_path path = "https://gist.github.com/103208.txt" template = %{ say "It works!" } diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 4fb5e6a4eb..ac4c2abfc8 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -116,7 +116,12 @@ module TestHelpers end end - add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"; config.active_support.deprecation = :log' + add_to_config <<-RUBY + config.secret_token = "3b7cd727ee24e8444053437c36cc66c4" + config.session_store :cookie_store, :key => "_myapp_session" + config.active_support.deprecation = :log + config.action_controller.allow_forgery_protection = false + RUBY end def teardown_app @@ -245,9 +250,11 @@ module TestHelpers def use_frameworks(arr) to_remove = [:actionmailer, :activemodel, - :activerecord, - :activeresource] - arr - remove_from_config "config.active_record.dependent_restrict_raises = false" if to_remove.include? :activerecord + :activerecord] - arr + if to_remove.include? :activerecord + remove_from_config "config.active_record.whitelist_attributes = true" + remove_from_config "config.active_record.dependent_restrict_raises = false" + end $:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' } end diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index c0f3887263..d334034e7d 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -22,7 +22,7 @@ class PathsTest < ActiveSupport::TestCase root = Rails::Paths::Root.new(nil) root.add "app" root.path = "/root" - assert_equal ["app"], root["app"] + assert_equal ["app"], root["app"].to_ary assert_equal ["/root/app"], root["app"].to_a end @@ -91,10 +91,6 @@ class PathsTest < ActiveSupport::TestCase assert_equal ["/foo/bar/app2", "/foo/bar/app"], @root["app"].to_a end - test "the root can only have one physical path" do - assert_raise(RuntimeError) { Rails::Paths::Root.new(["/fiz", "/biz"]) } - end - test "it is possible to add a path that should be autoloaded only once" do @root.add "app", :with => "/app" @root["app"].autoload_once! diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 5ed923a484..5e93a8e783 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1,5 +1,4 @@ require "isolation/abstract_unit" -require "railties/shared_tests" require "stringio" require "rack/test" @@ -7,7 +6,6 @@ module RailtiesTest class EngineTest < ActiveSupport::TestCase include ActiveSupport::Testing::Isolation - include SharedTests include Rack::Test::Methods def setup @@ -29,6 +27,365 @@ module RailtiesTest teardown_app end + def boot_rails + super + require "#{app_path}/config/environment" + end + + test "serving sprocket's assets" do + @plugin.write "app/assets/javascripts/engine.js.erb", "<%= :alert %>();" + + boot_rails + require 'rack/test' + extend Rack::Test::Methods + + get "/assets/engine.js" + assert_match "alert()", last_response.body + end + + test "rake environment can be called in the engine" do + boot_rails + + @plugin.write "Rakefile", <<-RUBY + APP_RAKEFILE = '#{app_path}/Rakefile' + load 'rails/tasks/engine.rake' + task :foo => :environment do + puts "Task ran" + end + RUBY + + Dir.chdir(@plugin.path) do + output = `bundle exec rake foo` + assert_match "Task ran", output + end + end + + test "copying migrations" do + @plugin.write "db/migrate/1_create_users.rb", <<-RUBY + class CreateUsers < ActiveRecord::Migration + end + RUBY + + @plugin.write "db/migrate/2_add_last_name_to_users.rb", <<-RUBY + class AddLastNameToUsers < ActiveRecord::Migration + end + RUBY + + @plugin.write "db/migrate/3_create_sessions.rb", <<-RUBY + class CreateSessions < ActiveRecord::Migration + end + RUBY + + app_file "db/migrate/1_create_sessions.rb", <<-RUBY + class CreateSessions < ActiveRecord::Migration + def up + end + end + RUBY + + add_to_config "ActiveRecord::Base.timestamped_migrations = false" + + boot_rails + railties = Rails.application.railties.all.map(&:railtie_name) + + Dir.chdir(app_path) do + output = `bundle exec rake bukkits:install:migrations` + + assert File.exists?("#{app_path}/db/migrate/2_create_users.bukkits.rb") + assert File.exists?("#{app_path}/db/migrate/3_add_last_name_to_users.bukkits.rb") + assert_match(/Copied migration 2_create_users.bukkits.rb from bukkits/, output) + assert_match(/Copied migration 3_add_last_name_to_users.bukkits.rb from bukkits/, output) + assert_match(/NOTE: Migration 3_create_sessions.rb from bukkits has been skipped/, output) + assert_equal 3, Dir["#{app_path}/db/migrate/*.rb"].length + + output = `bundle exec rake railties:install:migrations`.split("\n") + + assert_no_match(/2_create_users/, output.join("\n")) + + bukkits_migration_order = output.index(output.detect{|o| /NOTE: Migration 3_create_sessions.rb from bukkits has been skipped/ =~ o }) + assert_not_nil bukkits_migration_order, "Expected migration to be skipped" + + migrations_count = Dir["#{app_path}/db/migrate/*.rb"].length + output = `bundle exec rake railties:install:migrations` + + assert_equal migrations_count, Dir["#{app_path}/db/migrate/*.rb"].length + end + end + + test "no rake task without migrations" do + boot_rails + require 'rake' + require 'rdoc/task' + require 'rake/testtask' + Rails.application.load_tasks + assert !Rake::Task.task_defined?('bukkits:install:migrations') + end + + test "puts its lib directory on load path" do + boot_rails + require "another" + assert_equal "Another", Another.name + end + + test "puts its models directory on autoload path" do + @plugin.write "app/models/my_bukkit.rb", "class MyBukkit ; end" + boot_rails + assert_nothing_raised { MyBukkit } + end + + test "puts its controllers directory on autoload path" do + @plugin.write "app/controllers/bukkit_controller.rb", "class BukkitController ; end" + boot_rails + assert_nothing_raised { BukkitController } + end + + test "adds its views to view paths" do + @plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY + class BukkitController < ActionController::Base + def index + end + end + RUBY + + @plugin.write "app/views/bukkit/index.html.erb", "Hello bukkits" + + boot_rails + + require "action_controller" + require "rack/mock" + response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/")) + assert_equal "Hello bukkits\n", response[2].body + end + + test "adds its views to view paths with lower proriority than app ones" do + @plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY + class BukkitController < ActionController::Base + def index + end + end + RUBY + + @plugin.write "app/views/bukkit/index.html.erb", "Hello bukkits" + app_file "app/views/bukkit/index.html.erb", "Hi bukkits" + + boot_rails + + require "action_controller" + require "rack/mock" + response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/")) + assert_equal "Hi bukkits\n", response[2].body + end + + test "adds helpers to controller views" do + @plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY + class BukkitController < ActionController::Base + def index + end + end + RUBY + + @plugin.write "app/helpers/bukkit_helper.rb", <<-RUBY + module BukkitHelper + def bukkits + "bukkits" + end + end + RUBY + + @plugin.write "app/views/bukkit/index.html.erb", "Hello <%= bukkits %>" + + boot_rails + + require "rack/mock" + response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/")) + assert_equal "Hello bukkits\n", response[2].body + end + + test "autoload any path under app" do + @plugin.write "app/anything/foo.rb", <<-RUBY + module Foo; end + RUBY + boot_rails + assert Foo + end + + test "routes are added to router" do + @plugin.write "config/routes.rb", <<-RUBY + class Sprokkit + def self.call(env) + [200, {'Content-Type' => 'text/html'}, ["I am a Sprokkit"]] + end + end + + Rails.application.routes.draw do + match "/sprokkit", :to => Sprokkit + end + RUBY + + boot_rails + require 'rack/test' + extend Rack::Test::Methods + + get "/sprokkit" + assert_equal "I am a Sprokkit", last_response.body + end + + test "routes in engines have lower priority than application ones" do + controller "foo", <<-RUBY + class FooController < ActionController::Base + def index + render :text => "foo" + end + end + RUBY + + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + match 'foo', :to => 'foo#index' + end + RUBY + + @plugin.write "app/controllers/bar_controller.rb", <<-RUBY + class BarController < ActionController::Base + def index + render :text => "bar" + end + end + RUBY + + @plugin.write "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + match 'foo', :to => 'bar#index' + match 'bar', :to => 'bar#index' + end + RUBY + + boot_rails + require 'rack/test' + extend Rack::Test::Methods + + get '/foo' + assert_equal 'foo', last_response.body + + get '/bar' + assert_equal 'bar', last_response.body + end + + test "rake tasks lib tasks are loaded" do + $executed = false + @plugin.write "lib/tasks/foo.rake", <<-RUBY + task :foo do + $executed = true + end + RUBY + + boot_rails + require 'rake' + require 'rdoc/task' + require 'rake/testtask' + Rails.application.load_tasks + Rake::Task[:foo].invoke + assert $executed + end + + test "i18n files have lower priority than application ones" do + add_to_config <<-RUBY + config.i18n.load_path << "#{app_path}/app/locales/en.yml" + RUBY + + app_file 'app/locales/en.yml', <<-YAML +en: + bar: "1" +YAML + + app_file 'config/locales/en.yml', <<-YAML +en: + foo: "2" + bar: "2" +YAML + + @plugin.write 'config/locales/en.yml', <<-YAML +en: + foo: "3" +YAML + + boot_rails + + expected_locales = %W( + #{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/locale/en.yml + #{RAILS_FRAMEWORK_ROOT}/activemodel/lib/active_model/locale/en.yml + #{RAILS_FRAMEWORK_ROOT}/activerecord/lib/active_record/locale/en.yml + #{RAILS_FRAMEWORK_ROOT}/actionpack/lib/action_view/locale/en.yml + #{@plugin.path}/config/locales/en.yml + #{app_path}/config/locales/en.yml + #{app_path}/app/locales/en.yml + ).map { |path| File.expand_path(path) } + + actual_locales = I18n.load_path.map { |path| + File.expand_path(path) + } & expected_locales # remove locales external to Rails + + assert_equal expected_locales, actual_locales + + assert_equal "2", I18n.t(:foo) + assert_equal "1", I18n.t(:bar) + end + + test "namespaced controllers with namespaced routes" do + @plugin.write "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + namespace :admin do + namespace :foo do + match "bar", :to => "bar#index" + end + end + end + RUBY + + @plugin.write "app/controllers/admin/foo/bar_controller.rb", <<-RUBY + class Admin::Foo::BarController < ApplicationController + def index + render :text => "Rendered from namespace" + end + end + RUBY + + boot_rails + require 'rack/test' + extend Rack::Test::Methods + + get "/admin/foo/bar" + assert_equal 200, last_response.status + assert_equal "Rendered from namespace", last_response.body + end + + test "initializers" do + $plugin_initializer = false + @plugin.write "config/initializers/foo.rb", <<-RUBY + $plugin_initializer = true + RUBY + + boot_rails + assert $plugin_initializer + end + + test "midleware referenced in configuration" do + @plugin.write "lib/bukkits.rb", <<-RUBY + class Bukkits + def initialize(app) + @app = app + end + + def call(env) + @app.call(env) + end + end + RUBY + + add_to_config "config.middleware.use \"Bukkits\"" + boot_rails + end + test "Rails::Engine itself does not respond to config" do boot_rails assert !Rails::Engine.respond_to?(:config) @@ -60,7 +417,6 @@ module RailtiesTest assert index < initializers.index { |i| i.name == :build_middleware_stack } end - class Upcaser def initialize(app) @app = app @@ -476,8 +832,6 @@ module RailtiesTest boot_rails - require "#{rails_root}/config/environment" - get("/foo") assert_equal "foo", last_response.body @@ -511,7 +865,6 @@ module RailtiesTest RUBY boot_rails - require "#{rails_root}/config/environment" app_generators = Rails.application.config.generators.options[:rails] assert_equal :mongoid , app_generators[:orm] @@ -534,7 +887,6 @@ module RailtiesTest RUBY boot_rails - require "#{rails_root}/config/environment" generators = Bukkits::Engine.config.generators.options[:rails] assert_equal :active_record, generators[:orm] @@ -558,7 +910,6 @@ module RailtiesTest RUBY boot_rails - require "#{rails_root}/config/environment" assert_equal "foo", Bukkits.table_name_prefix end @@ -572,7 +923,6 @@ module RailtiesTest RUBY boot_rails - require "#{rails_root}/config/environment" assert_equal Bukkits::Engine.instance, Rails::Engine.find(@plugin.path) @@ -620,7 +970,6 @@ module RailtiesTest add_to_config("config.action_dispatch.show_exceptions = false") boot_rails - require "#{rails_root}/config/environment" methods = Bukkits::Engine.helpers.public_instance_methods.collect(&:to_s).sort expected = ["bar", "baz"] @@ -699,7 +1048,6 @@ module RailtiesTest add_to_config("config.railties_order = [:all, :main_app, Blog::Engine]") boot_rails - require "#{rails_root}/config/environment" get("/foo") assert_equal "Bukkit's foo partial", last_response.body.strip @@ -747,12 +1095,64 @@ module RailtiesTest add_to_config("config.railties_order = [Bukkits::Engine]") boot_rails - require "#{rails_root}/config/environment" get("/foo") assert_equal "Bukkit's foo partial", last_response.body.strip end + test "engine can be properly mounted at root" do + add_to_config("config.action_dispatch.show_exceptions = false") + add_to_config("config.serve_static_assets = false") + + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + isolate_namespace ::Bukkits + end + end + RUBY + + @plugin.write "config/routes.rb", <<-RUBY + Bukkits::Engine.routes.draw do + root "foo#index" + end + RUBY + + @plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY + module Bukkits + class FooController < ActionController::Base + def index + text = <<-TEXT + script_name: \#{request.script_name} + fullpath: \#{request.fullpath} + path: \#{request.path} + TEXT + render :text => text + end + end + end + RUBY + + + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + mount Bukkits::Engine => "/" + end + RUBY + + boot_rails + + expected = <<-TEXT + script_name: + fullpath: / + path: / + TEXT + + get("/") + assert_equal expected.split("\n").map(&:strip), + last_response.body.split("\n").map(&:strip) + end + private def app Rails.application diff --git a/railties/test/railties/shared_tests.rb b/railties/test/railties/shared_tests.rb deleted file mode 100644 index 3630a0937c..0000000000 --- a/railties/test/railties/shared_tests.rb +++ /dev/null @@ -1,367 +0,0 @@ -module RailtiesTest - # Holds tests shared between plugin and engines - module SharedTests - def boot_rails - super - require "#{app_path}/config/environment" - end - - def app - @app ||= Rails.application - end - - def test_serving_sprockets_assets - @plugin.write "app/assets/javascripts/engine.js.erb", "<%= :alert %>();" - - boot_rails - require 'rack/test' - extend Rack::Test::Methods - - get "/assets/engine.js" - assert_match "alert()", last_response.body - end - - def test_rake_environment_can_be_called_in_the_engine_or_plugin - boot_rails - - @plugin.write "Rakefile", <<-RUBY - APP_RAKEFILE = '#{app_path}/Rakefile' - load 'rails/tasks/engine.rake' - task :foo => :environment do - puts "Task ran" - end - RUBY - - Dir.chdir(@plugin.path) do - output = `bundle exec rake foo` - assert_match "Task ran", output - end - end - - def test_copying_migrations - @plugin.write "db/migrate/1_create_users.rb", <<-RUBY - class CreateUsers < ActiveRecord::Migration - end - RUBY - - @plugin.write "db/migrate/2_add_last_name_to_users.rb", <<-RUBY - class AddLastNameToUsers < ActiveRecord::Migration - end - RUBY - - @plugin.write "db/migrate/3_create_sessions.rb", <<-RUBY - class CreateSessions < ActiveRecord::Migration - end - RUBY - - app_file "db/migrate/1_create_sessions.rb", <<-RUBY - class CreateSessions < ActiveRecord::Migration - def up - end - end - RUBY - - add_to_config "ActiveRecord::Base.timestamped_migrations = false" - - boot_rails - railties = Rails.application.railties.all.map(&:railtie_name) - - Dir.chdir(app_path) do - output = `bundle exec rake bukkits:install:migrations` - - assert File.exists?("#{app_path}/db/migrate/2_create_users.bukkits.rb") - assert File.exists?("#{app_path}/db/migrate/3_add_last_name_to_users.bukkits.rb") - assert_match(/Copied migration 2_create_users.bukkits.rb from bukkits/, output) - assert_match(/Copied migration 3_add_last_name_to_users.bukkits.rb from bukkits/, output) - assert_match(/NOTE: Migration 3_create_sessions.rb from bukkits has been skipped/, output) - assert_equal 3, Dir["#{app_path}/db/migrate/*.rb"].length - - output = `bundle exec rake railties:install:migrations`.split("\n") - - assert_no_match(/2_create_users/, output.join("\n")) - - bukkits_migration_order = output.index(output.detect{|o| /NOTE: Migration 3_create_sessions.rb from bukkits has been skipped/ =~ o }) - assert_not_nil bukkits_migration_order, "Expected migration to be skipped" - - migrations_count = Dir["#{app_path}/db/migrate/*.rb"].length - output = `bundle exec rake railties:install:migrations` - - assert_equal migrations_count, Dir["#{app_path}/db/migrate/*.rb"].length - end - end - - def test_no_rake_task_without_migrations - boot_rails - require 'rake' - require 'rdoc/task' - require 'rake/testtask' - Rails.application.load_tasks - assert !Rake::Task.task_defined?('bukkits:install:migrations') - end - - def test_puts_its_lib_directory_on_load_path - boot_rails - require "another" - assert_equal "Another", Another.name - end - - def test_puts_its_models_directory_on_autoload_path - @plugin.write "app/models/my_bukkit.rb", "class MyBukkit ; end" - boot_rails - assert_nothing_raised { MyBukkit } - end - - def test_puts_its_controllers_directory_on_autoload_path - @plugin.write "app/controllers/bukkit_controller.rb", "class BukkitController ; end" - boot_rails - assert_nothing_raised { BukkitController } - end - - def test_adds_its_views_to_view_paths - @plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY - class BukkitController < ActionController::Base - def index - end - end - RUBY - - @plugin.write "app/views/bukkit/index.html.erb", "Hello bukkits" - - boot_rails - - require "action_controller" - require "rack/mock" - response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/")) - assert_equal "Hello bukkits\n", response[2].body - end - - def test_adds_its_views_to_view_paths_with_lower_proriority_than_app_ones - @plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY - class BukkitController < ActionController::Base - def index - end - end - RUBY - - @plugin.write "app/views/bukkit/index.html.erb", "Hello bukkits" - app_file "app/views/bukkit/index.html.erb", "Hi bukkits" - - boot_rails - - require "action_controller" - require "rack/mock" - response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/")) - assert_equal "Hi bukkits\n", response[2].body - end - - def test_adds_helpers_to_controller_views - @plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY - class BukkitController < ActionController::Base - def index - end - end - RUBY - - @plugin.write "app/helpers/bukkit_helper.rb", <<-RUBY - module BukkitHelper - def bukkits - "bukkits" - end - end - RUBY - - @plugin.write "app/views/bukkit/index.html.erb", "Hello <%= bukkits %>" - - boot_rails - - require "rack/mock" - response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/")) - assert_equal "Hello bukkits\n", response[2].body - end - - def test_autoload_any_path_under_app - @plugin.write "app/anything/foo.rb", <<-RUBY - module Foo; end - RUBY - boot_rails - assert Foo - end - - def test_routes_are_added_to_router - @plugin.write "config/routes.rb", <<-RUBY - class Sprokkit - def self.call(env) - [200, {'Content-Type' => 'text/html'}, ["I am a Sprokkit"]] - end - end - - Rails.application.routes.draw do - match "/sprokkit", :to => Sprokkit - end - RUBY - - boot_rails - require 'rack/test' - extend Rack::Test::Methods - - get "/sprokkit" - assert_equal "I am a Sprokkit", last_response.body - end - - def test_routes_in_plugins_have_lower_priority_than_application_ones - controller "foo", <<-RUBY - class FooController < ActionController::Base - def index - render :text => "foo" - end - end - RUBY - - app_file "config/routes.rb", <<-RUBY - AppTemplate::Application.routes.draw do - match 'foo', :to => 'foo#index' - end - RUBY - - @plugin.write "app/controllers/bar_controller.rb", <<-RUBY - class BarController < ActionController::Base - def index - render :text => "bar" - end - end - RUBY - - @plugin.write "config/routes.rb", <<-RUBY - Rails.application.routes.draw do - match 'foo', :to => 'bar#index' - match 'bar', :to => 'bar#index' - end - RUBY - - boot_rails - require 'rack/test' - extend Rack::Test::Methods - - get '/foo' - assert_equal 'foo', last_response.body - - get '/bar' - assert_equal 'bar', last_response.body - end - - def test_rake_tasks_lib_tasks_are_loaded - $executed = false - @plugin.write "lib/tasks/foo.rake", <<-RUBY - task :foo do - $executed = true - end - RUBY - - boot_rails - require 'rake' - require 'rdoc/task' - require 'rake/testtask' - Rails.application.load_tasks - Rake::Task[:foo].invoke - assert $executed - end - - def test_i18n_files_have_lower_priority_than_application_ones - add_to_config <<-RUBY - config.i18n.load_path << "#{app_path}/app/locales/en.yml" - RUBY - - app_file 'app/locales/en.yml', <<-YAML -en: - bar: "1" -YAML - - app_file 'config/locales/en.yml', <<-YAML -en: - foo: "2" - bar: "2" -YAML - - @plugin.write 'config/locales/en.yml', <<-YAML -en: - foo: "3" -YAML - - boot_rails - - expected_locales = %W( - #{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/locale/en.yml - #{RAILS_FRAMEWORK_ROOT}/activemodel/lib/active_model/locale/en.yml - #{RAILS_FRAMEWORK_ROOT}/activerecord/lib/active_record/locale/en.yml - #{RAILS_FRAMEWORK_ROOT}/actionpack/lib/action_view/locale/en.yml - #{@plugin.path}/config/locales/en.yml - #{app_path}/config/locales/en.yml - #{app_path}/app/locales/en.yml - ).map { |path| File.expand_path(path) } - - actual_locales = I18n.load_path.map { |path| - File.expand_path(path) - } & expected_locales # remove locales external to Rails - - assert_equal expected_locales, actual_locales - - assert_equal "2", I18n.t(:foo) - assert_equal "1", I18n.t(:bar) - end - - def test_namespaced_controllers_with_namespaced_routes - @plugin.write "config/routes.rb", <<-RUBY - Rails.application.routes.draw do - namespace :admin do - namespace :foo do - match "bar", :to => "bar#index" - end - end - end - RUBY - - @plugin.write "app/controllers/admin/foo/bar_controller.rb", <<-RUBY - class Admin::Foo::BarController < ApplicationController - def index - render :text => "Rendered from namespace" - end - end - RUBY - - boot_rails - require 'rack/test' - extend Rack::Test::Methods - - get "/admin/foo/bar" - assert_equal 200, last_response.status - assert_equal "Rendered from namespace", last_response.body - end - - def test_initializers - $plugin_initializer = false - @plugin.write "config/initializers/foo.rb", <<-RUBY - $plugin_initializer = true - RUBY - - boot_rails - assert $plugin_initializer - end - - def test_midleware_referenced_in_configuration - @plugin.write "lib/bukkits.rb", <<-RUBY - class Bukkits - def initialize(app) - @app = app - end - - def call(env) - @app.call(env) - end - end - RUBY - - add_to_config "config.middleware.use \"Bukkits\"" - boot_rails - end - end -end diff --git a/tasks/release.rb b/tasks/release.rb index 191c014f9f..650b381e0f 100644 --- a/tasks/release.rb +++ b/tasks/release.rb @@ -1,4 +1,4 @@ -FRAMEWORKS = %w( activesupport activemodel activerecord activeresource actionpack actionmailer railties ) +FRAMEWORKS = %w( activesupport activemodel activerecord actionpack actionmailer railties ) root = File.expand_path('../../', __FILE__) version = File.read("#{root}/RAILS_VERSION").strip diff --git a/tools/profile b/tools/profile index a6e3b41900..51cb7f33e8 100755 --- a/tools/profile +++ b/tools/profile @@ -1,7 +1,6 @@ #!/usr/bin/env ruby # Example: # tools/profile_requires activesupport/lib/active_support.rb -# tools/profile_requires activeresource/examples/simple.rb abort 'Use REE so you can profile memory and object allocation' unless GC.respond_to?(:enable_stats) ENV['NO_RELOAD'] ||= '1' |