aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.travis.yml2
-rw-r--r--Gemfile4
-rwxr-xr-xRakefile8
-rw-r--r--actionmailer/lib/rails/generators/mailer/templates/mailer.rb4
-rw-r--r--actionpack/CHANGELOG.md7
-rw-r--r--actionpack/actionpack.gemspec1
-rw-r--r--actionpack/lib/abstract_controller/layouts.rb20
-rw-r--r--actionpack/lib/abstract_controller/rendering.rb1
-rw-r--r--actionpack/lib/action_controller/base.rb2
-rw-r--r--actionpack/lib/action_controller/caching/sweeping.rb7
-rw-r--r--actionpack/lib/action_controller/metal/force_ssl.rb2
-rw-r--r--actionpack/lib/action_controller/metal/helpers.rb1
-rw-r--r--actionpack/lib/action_controller/metal/redirecting.rb2
-rw-r--r--actionpack/lib/action_controller/railtie.rb31
-rw-r--r--actionpack/lib/action_controller/railties/helpers.rb22
-rw-r--r--actionpack/lib/action_controller/railties/paths.rb25
-rw-r--r--actionpack/lib/action_controller/record_identifier.rb4
-rw-r--r--actionpack/lib/action_controller/test_case.rb5
-rw-r--r--actionpack/lib/action_dispatch/http/response.rb9
-rw-r--r--actionpack/lib/action_dispatch/middleware/static.rb1
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/response.rb2
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb15
-rw-r--r--actionpack/lib/action_view/helpers/tags/check_box.rb10
-rw-r--r--actionpack/lib/action_view/helpers/url_helper.rb2
-rw-r--r--actionpack/lib/sprockets/assets.rake105
-rw-r--r--actionpack/lib/sprockets/bootstrap.rb37
-rw-r--r--actionpack/lib/sprockets/compressors.rb85
-rw-r--r--actionpack/lib/sprockets/helpers.rb6
-rw-r--r--actionpack/lib/sprockets/helpers/isolated_helper.rb13
-rw-r--r--actionpack/lib/sprockets/helpers/rails_helper.rb167
-rw-r--r--actionpack/lib/sprockets/railtie.rb62
-rw-r--r--actionpack/lib/sprockets/static_compiler.rb62
-rw-r--r--actionpack/test/abstract/abstract_controller_test.rb4
-rw-r--r--actionpack/test/abstract/collector_test.rb4
-rw-r--r--actionpack/test/abstract/helper_test.rb4
-rw-r--r--actionpack/test/abstract/layouts_test.rb33
-rw-r--r--actionpack/test/controller/base_test.rb5
-rw-r--r--actionpack/test/controller/http_digest_authentication_test.rb2
-rw-r--r--actionpack/test/controller/redirect_test.rb20
-rw-r--r--actionpack/test/controller/render_test.rb39
-rw-r--r--actionpack/test/controller/request_forgery_protection_test.rb24
-rw-r--r--actionpack/test/controller/sweeper_test.rb16
-rw-r--r--actionpack/test/dispatch/static_test.rb4
-rw-r--r--actionpack/test/fixtures/layouts/with_html_partial.html.erb1
-rw-r--r--actionpack/test/fixtures/sprockets/alternate/stylesheets/style.css1
-rw-r--r--actionpack/test/fixtures/sprockets/app/fonts/dir/font.ttf0
-rw-r--r--actionpack/test/fixtures/sprockets/app/fonts/font.ttf0
-rw-r--r--actionpack/test/fixtures/sprockets/app/images/logo.pngbin6646 -> 0 bytes
-rw-r--r--actionpack/test/fixtures/sprockets/app/javascripts/application.js1
-rw-r--r--actionpack/test/fixtures/sprockets/app/javascripts/dir/xmlhr.js0
-rw-r--r--actionpack/test/fixtures/sprockets/app/javascripts/extra.js0
-rw-r--r--actionpack/test/fixtures/sprockets/app/javascripts/xmlhr.js0
-rw-r--r--actionpack/test/fixtures/sprockets/app/stylesheets/application.css1
-rw-r--r--actionpack/test/fixtures/sprockets/app/stylesheets/dir/style.css0
-rw-r--r--actionpack/test/fixtures/sprockets/app/stylesheets/extra.css0
-rw-r--r--actionpack/test/fixtures/sprockets/app/stylesheets/style.css0
-rw-r--r--actionpack/test/fixtures/test/_partial_only_html.html1
-rw-r--r--actionpack/test/fixtures/test/with_html_partial.html.erb1
-rw-r--r--actionpack/test/fixtures/test/with_partial.html.erb1
-rw-r--r--actionpack/test/fixtures/test/with_partial.text.erb1
-rw-r--r--actionpack/test/fixtures/test/with_xml_template.html.erb1
-rw-r--r--actionpack/test/template/compressors_test.rb28
-rw-r--r--actionpack/test/template/form_helper_test.rb5
-rw-r--r--actionpack/test/template/sprockets_helper_test.rb349
-rw-r--r--actionpack/test/template/sprockets_helper_with_routes_test.rb57
-rw-r--r--activemodel/activemodel.gemspec2
-rw-r--r--activemodel/lib/active_model/mass_assignment_security/sanitizer.rb12
-rw-r--r--activerecord/CHANGELOG.md11
-rw-r--r--activerecord/RUNNING_UNIT_TESTS7
-rw-r--r--activerecord/lib/active_record.rb1
-rw-r--r--activerecord/lib/active_record/associations/association.rb13
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb5
-rw-r--r--activerecord/lib/active_record/autosave_association.rb6
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_adapter.rb39
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb2
-rw-r--r--activerecord/lib/active_record/core.rb2
-rw-r--r--activerecord/lib/active_record/counter_cache.rb2
-rw-r--r--activerecord/lib/active_record/fixtures.rb4
-rw-r--r--activerecord/lib/active_record/identity_map.rb144
-rw-r--r--activerecord/lib/active_record/inheritance.rb23
-rw-r--r--activerecord/lib/active_record/model.rb1
-rw-r--r--activerecord/lib/active_record/model_schema.rb3
-rw-r--r--activerecord/lib/active_record/persistence.rb16
-rw-r--r--activerecord/lib/active_record/railtie.rb5
-rw-r--r--activerecord/lib/active_record/relation.rb15
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb10
-rw-r--r--activerecord/lib/active_record/test_case.rb10
-rw-r--r--activerecord/lib/active_record/transactions.rb2
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb20
-rw-r--r--activerecord/test/cases/adapters/postgresql/explain_test.rb7
-rw-r--r--activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb1
-rw-r--r--activerecord/test/cases/associations/eager_test.rb20
-rw-r--r--activerecord/test/cases/associations/identity_map_test.rb137
-rw-r--r--activerecord/test/cases/autosave_association_test.rb8
-rw-r--r--activerecord/test/cases/base_test.rb14
-rw-r--r--activerecord/test/cases/helper.rb3
-rw-r--r--activerecord/test/cases/identity_map/middleware_test.rb74
-rw-r--r--activerecord/test/cases/identity_map_test.rb439
-rw-r--r--activerecord/test/cases/log_subscriber_test.rb12
-rw-r--r--activerecord/test/cases/query_cache_test.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb38
-rw-r--r--activerecord/test/cases/schema_dumper_test.rb2
-rw-r--r--activerecord/test/cases/validations/uniqueness_validation_test.rb12
-rw-r--r--activerecord/test/support/connection.rb2
-rw-r--r--activeresource/CHANGELOG.md390
-rw-r--r--activeresource/MIT-LICENSE20
-rw-r--r--activeresource/README.rdoc189
-rwxr-xr-xactiveresource/Rakefile66
-rw-r--r--activeresource/activeresource.gemspec24
-rw-r--r--activeresource/examples/performance.rb70
-rw-r--r--activeresource/lib/active_resource.rb45
-rw-r--r--activeresource/lib/active_resource/base.rb1515
-rw-r--r--activeresource/lib/active_resource/connection.rb284
-rw-r--r--activeresource/lib/active_resource/custom_methods.rb127
-rw-r--r--activeresource/lib/active_resource/exceptions.rb82
-rw-r--r--activeresource/lib/active_resource/formats.rb22
-rw-r--r--activeresource/lib/active_resource/formats/json_format.rb25
-rw-r--r--activeresource/lib/active_resource/formats/xml_format.rb25
-rw-r--r--activeresource/lib/active_resource/http_mock.rb332
-rw-r--r--activeresource/lib/active_resource/log_subscriber.rb15
-rw-r--r--activeresource/lib/active_resource/observing.rb29
-rw-r--r--activeresource/lib/active_resource/railtie.rb14
-rw-r--r--activeresource/lib/active_resource/schema.rb59
-rw-r--r--activeresource/lib/active_resource/validations.rb172
-rw-r--r--activeresource/lib/active_resource/version.rb10
-rw-r--r--activeresource/test/abstract_unit.rb143
-rw-r--r--activeresource/test/cases/authorization_test.rb251
-rw-r--r--activeresource/test/cases/base/custom_methods_test.rb101
-rw-r--r--activeresource/test/cases/base/equality_test.rb52
-rw-r--r--activeresource/test/cases/base/load_test.rb199
-rw-r--r--activeresource/test/cases/base/schema_test.rb411
-rw-r--r--activeresource/test/cases/base_errors_test.rb137
-rw-r--r--activeresource/test/cases/base_test.rb1177
-rw-r--r--activeresource/test/cases/connection_test.rb275
-rw-r--r--activeresource/test/cases/finder_test.rb139
-rw-r--r--activeresource/test/cases/format_test.rb118
-rw-r--r--activeresource/test/cases/http_mock_test.rb202
-rw-r--r--activeresource/test/cases/log_subscriber_test.rb32
-rw-r--r--activeresource/test/cases/observing_test.rb55
-rw-r--r--activeresource/test/cases/validations_test.rb67
-rw-r--r--activeresource/test/fixtures/address.rb19
-rw-r--r--activeresource/test/fixtures/beast.rb14
-rw-r--r--activeresource/test/fixtures/customer.rb3
-rw-r--r--activeresource/test/fixtures/person.rb3
-rw-r--r--activeresource/test/fixtures/project.rb18
-rw-r--r--activeresource/test/fixtures/proxy.rb4
-rw-r--r--activeresource/test/fixtures/sound.rb9
-rw-r--r--activeresource/test/fixtures/street_address.rb4
-rw-r--r--activeresource/test/fixtures/subscription_plan.rb5
-rw-r--r--activeresource/test/setter_trap.rb26
-rw-r--r--activesupport/lib/active_support/basic_object.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb16
-rw-r--r--activesupport/lib/active_support/dependencies.rb29
-rw-r--r--activesupport/lib/active_support/file_update_checker.rb17
-rw-r--r--activesupport/lib/active_support/log_subscriber.rb8
-rw-r--r--activesupport/lib/active_support/notifications/fanout.rb7
-rw-r--r--activesupport/lib/active_support/notifications/instrumenter.rb2
-rw-r--r--activesupport/lib/active_support/railtie.rb27
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb13
-rw-r--r--activesupport/test/file_update_checker_test.rb15
-rw-r--r--activesupport/test/time_zone_test.rb17
-rwxr-xr-xci/travis.rb10
-rw-r--r--install.rb2
-rw-r--r--rails.gemspec14
-rw-r--r--railties/CHANGELOG.md2
-rw-r--r--railties/guides/source/active_resource_basics.textile120
-rw-r--r--railties/guides/source/api_app.textile275
-rw-r--r--railties/guides/source/command_line.textile1
-rw-r--r--railties/guides/source/configuring.textile10
-rw-r--r--railties/guides/source/engines.textile20
-rw-r--r--railties/guides/source/initialization.textile2
-rw-r--r--railties/guides/source/upgrading_ruby_on_rails.textile4
-rw-r--r--railties/lib/rails/all.rb3
-rw-r--r--railties/lib/rails/application.rb8
-rw-r--r--railties/lib/rails/configuration.rb18
-rw-r--r--railties/lib/rails/engine.rb2
-rw-r--r--railties/lib/rails/generators.rb4
-rw-r--r--railties/lib/rails/generators/app_base.rb7
-rw-r--r--railties/lib/rails/generators/erb/scaffold/templates/index.html.erb2
-rw-r--r--railties/lib/rails/generators/named_base.rb5
-rw-r--r--railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb5
-rw-r--r--railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt5
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/application.rb5
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt2
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt2
-rw-r--r--railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt4
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb3
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb4
-rw-r--r--railties/lib/rails/generators/rails/resource/resource_generator.rb5
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb5
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb20
-rw-r--r--railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb11
-rw-r--r--railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb10
-rw-r--r--railties/lib/rails/info.rb4
-rw-r--r--railties/lib/rails/railtie.rb2
-rw-r--r--railties/lib/rails/tasks/documentation.rake6
-rw-r--r--railties/lib/rails/test_help.rb4
-rw-r--r--railties/test/application/loading_test.rb31
-rw-r--r--railties/test/application/middleware_test.rb37
-rw-r--r--railties/test/application/rake_test.rb5
-rw-r--r--railties/test/generators/app_generator_test.rb3
-rw-r--r--railties/test/generators/scaffold_controller_generator_test.rb8
-rw-r--r--railties/test/generators/scaffold_generator_test.rb8
-rw-r--r--railties/test/generators/shared_generator_tests.rb15
-rw-r--r--railties/test/isolation/abstract_unit.rb10
-rw-r--r--railties/test/railties/engine_test.rb424
-rw-r--r--railties/test/railties/shared_tests.rb367
-rw-r--r--tasks/release.rb2
-rwxr-xr-xtools/profile1
209 files changed, 1035 insertions, 10066 deletions
diff --git a/.travis.yml b/.travis.yml
index 674d41c88f..c6f868498d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,7 +5,7 @@ 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"
diff --git a/Gemfile b/Gemfile
index 5bd9d4a215..b19f532b87 100644
--- a/Gemfile
+++ b/Gemfile
@@ -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'
@@ -22,6 +22,8 @@ end
# it being automatically loaded by sprockets
gem 'uglifier', '>= 1.0.3', :require => false
+gem 'sprockets-rails', :git => "git://github.com/rails/sprockets-rails.git"
+
gem 'rake', '>= 0.8.7'
gem 'mocha', '>= 0.9.8'
diff --git a/Rakefile b/Rakefile
index 03b8a952c3..0d218844b2 100755
--- a/Rakefile
+++ b/Rakefile
@@ -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/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/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index ebaf2d407c..6c9f3e0e67 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,6 +1,6 @@
## Rails 4.0.0 (unreleased) ##
-* Add `include_hidden` option to select tag. With `:include_hidden => true` select with `multiple` attribute doesn't generate hidden input with blank value. *Vasiliy Ermolovich*
+* Add `include_hidden` option to select tag. With `:include_hidden => false` select with `multiple` attribute doesn't generate hidden input with blank value. *Vasiliy Ermolovich*
* Removed default `size` option from the `text_field`, `search_field`, `telephone_field`, `url_field`, `email_field` helpers. *Philip Arndt*
@@ -89,6 +89,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*
@@ -123,6 +126,8 @@
## Rails 3.2.3 (unreleased) ##
+* Do not include the authenticity token in forms where remote: true as ajax forms use the meta-tag value *DHH*
+
* Upgrade rack-cache to 1.2. *José Valim*
* ActionController::SessionManagement is removed. *Santiago Pastorino*
diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec
index 089913be3c..589a67dc02 100644
--- a/actionpack/actionpack.gemspec
+++ b/actionpack/actionpack.gemspec
@@ -23,7 +23,6 @@ Gem::Specification.new do |s|
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/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb
index a82e3dc90a..6f13ebe0d0 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:
@@ -298,12 +306,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
@@ -363,7 +371,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/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 6ce78f34b5..71425cd542 100644
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -171,7 +171,7 @@ module ActionController
class Base < Metal
abstract!
- # Shortcut helper that returns all the ActionController modules except the ones passed in the argument:
+ # 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|
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/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/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/railtie.rb b/actionpack/lib/action_controller/railtie.rb
index 3e170d7872..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 #: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 7af30ed690..7ba8319e4c 100644
--- a/actionpack/lib/action_controller/test_case.rb
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -505,11 +505,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_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/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/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb
index 094cfbfc76..a5e7a8c715 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/response.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb
@@ -83,7 +83,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_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index 9fad30a48f..41d895c15e 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -27,7 +27,9 @@ 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>). If this is a remote form, the authenticity_token will by default
+ # not be included as the ajax handler will get it from the meta-tag (but you can force it to be
+ # rendered anyway in that case by passing <tt>true</tt>).
# * 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.
@@ -616,8 +618,17 @@ 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"] && 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
+ elsif html_options["data-remote"]
+ # The authenticity token is taken from the meta tag in this case
+ html_options["authenticity_token"] = false
+ end
end
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/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb
index f4946e65b5..29f556502b 100644
--- a/actionpack/lib/action_view/helpers/url_helper.rb
+++ b/actionpack/lib/action_view/helpers/url_helper.rb
@@ -60,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).
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 30d6a7ae14..62f82a4c7a 100644
--- a/actionpack/test/abstract/abstract_controller_test.rb
+++ b/actionpack/test/abstract/abstract_controller_test.rb
@@ -29,7 +29,7 @@ module AbstractController
# Test Render mixin
# ====
class RenderingController < AbstractController::Base
- include ::AbstractController::Rendering
+ include AbstractController::Rendering
def _prefixes
[]
@@ -153,7 +153,7 @@ module AbstractController
# ====
# self._layout is used when defined
class WithLayouts < PrefixedViews
- include Layouts
+ include AbstractController::Layouts
private
def self.layout(formats)
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 e07a6de4a9..58795aa327 100644
--- a/actionpack/test/abstract/layouts_test.rb
+++ b/actionpack/test/abstract/layouts_test.rb
@@ -72,6 +72,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 +242,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)
diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb
index 03d9873fc7..2d4083252e 100644
--- a/actionpack/test/controller/base_test.rb
+++ b/actionpack/test/controller/base_test.rb
@@ -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
@@ -206,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
@@ -257,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/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/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 8167fc2fd2..09d9e65d38 100644
--- a/actionpack/test/controller/render_test.rb
+++ b/actionpack/test/controller/render_test.rb
@@ -559,6 +559,23 @@ class TestController < ActionController::Base
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
@@ -1268,6 +1285,28 @@ class RenderTest < ActionController::TestCase
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
get :partial_with_counter
assert_equal "5", @response.body
diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb
index ef795dad89..7b722bd3d7 100644
--- a/actionpack/test/controller/request_forgery_protection_test.rb
+++ b/actionpack/test/controller/request_forgery_protection_test.rb
@@ -35,6 +35,16 @@ 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 rescue_action(e) raise e end
end
# sample controllers
@@ -98,6 +108,20 @@ 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_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_allow_get
assert_not_blocked { get :index }
end
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/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/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/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
deleted file mode 100644
index d5edc04e65..0000000000
--- a/actionpack/test/fixtures/sprockets/app/images/logo.png
+++ /dev/null
Binary files differ
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/_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/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/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index bc42e14b8a..2bdb54bd5e 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -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(
diff --git a/actionpack/test/template/sprockets_helper_test.rb b/actionpack/test/template/sprockets_helper_test.rb
deleted file mode 100644
index d4d2dedfb6..0000000000
--- a/actionpack/test/template/sprockets_helper_test.rb
+++ /dev/null
@@ -1,349 +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
- def script_name() nil 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 89b9940eb7..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_equal 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/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/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/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 6fd86b1f32..5a289a5aac 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,16 @@
## Rails 4.0.0 (unreleased) ##
+* 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
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/association.rb b/activerecord/lib/active_record/associations/association.rb
index 512c52338e..fb0ca15c23 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -45,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
@@ -135,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
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 3a737e5b35..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.
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index eb3ae7014e..be0af081d1 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -328,7 +328,6 @@ module ActiveRecord
autosave = reflection.options[:autosave]
if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
- begin
records.each do |record|
next if record.destroyed?
@@ -348,11 +347,6 @@ module ActiveRecord
raise ActiveRecord::Rollback unless saved
end
- rescue
- records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled?
- raise
- end
-
end
# reconstruct the scope now that we know the owner's id
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index 767c99de4c..1d713e472b 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -284,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/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 5b7fa029da..10a178e369 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -1006,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
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 46aababfb6..9a2f859fc7 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -176,7 +176,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
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index 8d5388e1f3..f52979ebd9 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -69,8 +69,6 @@ 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)
end
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index b82d5b5621..5c42e8f719 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -796,9 +796,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..ebe244c6a6 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -63,26 +63,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/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 99847ac161..c85d590ce1 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -160,6 +160,7 @@ module ActiveRecord
# Sets the value of inheritance_column
def inheritance_column=(value)
@inheritance_column = value.to_s
+ @explicit_inheritance_column = true
end
def sequence_name
@@ -303,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/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 09502bb52c..35c922e979 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
@@ -129,7 +126,6 @@ module ActiveRecord
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)
@@ -284,11 +280,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
@@ -363,10 +357,8 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 516c450f85..ee3a6bf8c0 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)
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 7531e1fe6f..ae2dc6872a 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?
@@ -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
@@ -516,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/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index adfacf37ee..52e67ecc6e 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -318,17 +318,7 @@ 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]]
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/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/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/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..b27a93f857 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
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/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
index 1376810adf..67b65c51d5 100644
--- a/activerecord/test/cases/autosave_association_test.rb
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -978,10 +978,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 +1074,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 01f647b261..bddaf6afa8 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -1139,7 +1139,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 +1168,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
@@ -1503,6 +1503,16 @@ class BasicsTest < ActiveRecord::TestCase
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
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/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/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 acec230c72..63d47f5162 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
@@ -685,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
@@ -1236,4 +1234,26 @@ class RelationTest < ActiveRecord::TestCase
scope = Post.order('foo(comments.body)')
assert_equal [], scope.references_values
end
+
+ def test_presence
+ topics = Topic.scoped
+
+ # the fist 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
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/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb
index 79442d68b0..376c0000c7 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))
+ t1 = Topic.create("title" => "I'm a topic", "approved" => true)
+ t2 = 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/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 670cb19f57..0000000000
--- a/activeresource/CHANGELOG.md
+++ /dev/null
@@ -1,390 +0,0 @@
-## Rails 4.0.0 (unreleased) ##
-
-* Adds support for PATCH requests. *dlee*
-
-
-## Rails 3.2.2 (March 1, 2012) ##
-
-* No changes.
-
-
-## 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 (March 1, 2012) ##
-
-* 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 (March 1, 2012) ##
-
-* 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/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/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/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/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb
index 2ede084e95..fe22b9515b 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:
@@ -93,10 +91,10 @@ module ActiveSupport
def updated_at #:nodoc:
@updated_at || begin
- all = []
- all.concat @files.select { |f| File.exists?(f) }
+ all = @files.select { |f| File.exists?(f) }
all.concat Dir[@glob] if @glob
- all.map { |path| File.mtime(path) }.max || Time.at(0)
+ all.map! { |path| File.mtime(path) }
+ all.max || Time.at(0)
end
end
@@ -104,13 +102,16 @@ module ActiveSupport
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 escape(key)
+ key.gsub(',','\,')
+ end
+
def compile_ext(array) #:nodoc:
array = Array(array)
return if array.empty?
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/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb
index a9aa5464e9..4c1b7b2784 100644
--- a/activesupport/lib/active_support/notifications/fanout.rb
+++ b/activesupport/lib/active_support/notifications/fanout.rb
@@ -9,15 +9,14 @@ module ActiveSupport
end
def subscribe(pattern = nil, block = Proc.new)
- subscriber = Subscriber.new(pattern, block).tap do |s|
- @subscribers << s
- end
+ subscriber = Subscriber.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
diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb
index 3941c285a2..547df5c731 100644
--- a/activesupport/lib/active_support/notifications/instrumenter.rb
+++ b/activesupport/lib/active_support/notifications/instrumenter.rb
@@ -1,5 +1,3 @@
-require 'active_support/core_ext/module/delegation'
-
module ActiveSupport
module Notifications
class Instrumenter
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/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index bfbb838ea7..ce46c46092 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -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/test/file_update_checker_test.rb b/activesupport/test/file_update_checker_test.rb
index dd2483287b..066db7c0f9 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__)
@@ -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/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/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 b55d0e6859..2c47a88d7c 100644
--- a/rails.gemspec
+++ b/rails.gemspec
@@ -17,11 +17,11 @@ Gem::Specification.new do |s|
s.bindir = 'bin'
s.executables = []
- 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.1')
+ 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 e7994e45c6..34de7fe2b8 100644
--- a/railties/CHANGELOG.md
+++ b/railties/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* 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:
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/api_app.textile b/railties/guides/source/api_app.textile
deleted file mode 100644
index 9bbf7b36b1..0000000000
--- a/railties/guides/source/api_app.textile
+++ /dev/null
@@ -1,275 +0,0 @@
-h2. Using Rails for API-only Apps
-
-In this guide you will learn:
-
-* What Rails provides for API-only applications
-* How to configure Rails to start without any browser features
-* How to decide which middlewares you will want to include
-* How to decide which modules to use in your controller
-
-NOTE: This guide reflects features that have not yet been fully implemented. Docs first :)
-
-endprologue.
-
-h3. What is an API app?
-
-Traditionally, when people said that they used Rails as an "API", they meant providing a programmatically accessible API alongside their web application.
-For example, GitHub provides "an API":http://developer.github.com that you can use from your own custom clients.
-
-With the advent of client-side frameworks, more developers are using Rails to build a backend that is shared between their web application and other native applications.
-
-For example, Twitter uses its "public API":https://dev.twitter.com in its web application, which is built as a static site that consumes JSON resources.
-
-Instead of using Rails to generate dynamic HTML that will communicate with the server through forms and links, many developers are treating their web application as just another client, delivered as static HTML, CSS and JavaScript, and consuming a simple JSON API
-
-This guide covers building a Rails application that serves JSON resources to an API client *or* client-side framework.
-
-h3. Why use Rails for JSON APIs?
-
-The first question a lot of people have when thinking about building a JSON API using Rails is: "isn't using Rails to spit out some JSON overkill? Shouldn't I just use something like Sinatra?"
-
-For very simple APIs, this may be true. However, even in very HTML-heavy applications, most of an application's logic is actually outside of the view layer.
-
-The reason most people use Rails is that it provides a set of defaults that allows us to get up and running quickly without having to make a lot of trivial decisions.
-
-Let's take a look at some of the things that Rails provides out of the box that are still applicable to API applications.
-
-Handled at the middleware layer:
-
-* Reloading: Rails applications support transparent reloading. This works even if your application gets big and restarting the server for every request becomes non-viable.
-* Development Mode: Rails application come with smart defaults for development, making development pleasant without compromising production-time performance.
-* Test Mode: Ditto test mode.
-* Logging: Rails applications log every request, with a level of verbosity appropriate for the current mode. Rails logs in development include information about the request environment, database queries, and basic performance information.
-* Security: Rails detects and thwarts "IP spoofing attacks":http://en.wikipedia.org/wiki/IP_address_spoofing and handles cryptographic signatures in a "timing attack":http://en.wikipedia.org/wiki/Timing_attack aware way. Don't know what an IP spoofing attack or a timing attack is? Exactly.
-* Parameter Parsing: Want to specify your parameters as JSON instead of as a URL-encoded String? No problem. Rails will decode the JSON for you and make it available in +params+. Want to use nested URL-encoded params? That works too.
-* Conditional GETs: Rails handles conditional +GET+, (+ETag+ and +Last-Modified+), processing request headers and returning the correct response headers and status code. All you need to do is use the "stale?":http://api.rubyonrails.org/classes/ActionController/ConditionalGet.html#method-i-stale-3F check in your controller, and Rails will handle all of the HTTP details for you.
-* Caching: If you use +dirty?+ with public cache control, Rails will automatically cache your responses. You can easily configure the cache store.
-* HEAD requests: Rails will transparently convert +HEAD+ requests into +GET+ requests, and return just the headers on the way out. This makes +HEAD+ work reliably in all Rails APIs.
-
-While you could obviously build these up in terms of existing Rack middlewares, I think this list demonstrates that the default Rails middleware stack provides a lot of value, even if you're "just generating JSON".
-
-Handled at the ActionPack layer:
-
-* Resourceful Routing: If you're building a RESTful JSON API, you want to be using the Rails router. Clean and conventional mapping from HTTP to controllers means not having to spend time thinking about how to model your API in terms of HTTP.
-* URL Generation: The flip side of routing is URL generation. A good API based on HTTP includes URLs (see "the GitHub gist API":http://developer.github.com/v3/gists/ for an example).
-* Header and Redirection Responses: +head :no_content+ and +redirect_to user_url(current_user)+ come in handy. Sure, you could manually add the response headers, but why?
-* Content Negotiation: The Rails +respond_to+ and +respond_with+ features automatically figure out which MIME type to serve, based on the request's +Accept+ header and available types. If you ever need to add support for types other than JSON (XML, CSV, or some proprietary format), this will come in handy.
-* Caching: Rails provides page, action and fragment caching. Fragment caching is especially helpful when building up a nested JSON object.
-* Basic, Digest and Token Authentication: Rails comes with out-of-the-box support for three kinds of HTTP authentication.
-* Instrumentation: Rails 3.0 added an instrumentation API that will trigger registered handlers for a variety of events, such as action processing, sending a file or data, redirection, and database queries. The payload of each event comes with relevant information (for the action processing event, the payload includes the controller, action, params, request format, request method and the request's full path).
-* Generators: This may be passé for advanced Rails users, but it can be nice to generate a resource and get your model, controller, test stubs, and routes created for you in a single command.
-* Plugins: Many third-party libraries come with support for Rails that reduces or eliminates the cost of setting up and gluing together the library and the web framework. This includes things like overriding default generators, adding rake tasks, and honoring Rails choices (like the logger and cache backend).
-
-Of course, the Rails boot process also glues together all registered components. For example, the Rails boot process is what uses your +config/database.yml+ file when configuring ActiveRecord.
-
-**The short version is**: you may not have thought about which parts of Rails are still applicable even if you remove the view layer, but the answer turns out to be "most of it".
-
-h3. The Basic Configuration
-
-If you're building a Rails application that will be an API server first and foremost, you can start with a more limited subset of Rails and add in features as needed.
-
-You can generate a new bare Rails app:
-
-<shell>
-$ rails new my_api --http
-</shell>
-
-This will do three main things for you:
-
-* Configure your application to start with a more limited set of middleware than normal. Specifically, it will not include any middleware primarily useful for browser applications (like cookie support) by default.
-* Make +ApplicationController+ inherit from +ActionController::HTTP+ instead of +ActionController::Base+. As with middleware, this will leave out any +ActionController+ modules that provide functionality primarily used by browser applications.
-* Configure the generators to skip generating views, helpers and assets when you generate a new resource.
-
-If you want to take an existing app and make it an API app, follow the following steps.
-
-In +config/application.rb+ add the following lines at the top of the +Application+ class:
-
-<ruby>
-config.middleware.http_only!
-config.generators.http_only!
-</ruby>
-
-Change +app/controllers/application_controller.rb+:
-
-<ruby>
-# instead of
-class ApplicationController < ActionController::Base
-end
-
-# do
-class ApplicationController < ActionController::HTTP
-end
-</ruby>
-
-h3. Choosing Middlewares
-
-An API application comes with the following middlewares by default.
-
-* +Rack::Cache+: Caches responses with public +Cache-Control+ headers using HTTP caching semantics. See below for more information.
-* +Rack::Sendfile+: Uses a front-end server's file serving support from your Rails application.
-* +Rack::Lock+: If your application is not marked as threadsafe (+config.threadsafe!+), this middleware will add a mutex around your requests.
-* +ActionDispatch::RequestId+:
-* +Rails::Rack::Logger+:
-* +ActionDispatch::ShowExceptions+: Rescue exceptions and re-dispatch them to an exception handling application
-* +ActionDispatch::DebugExceptions+: Log exceptions
-* +ActionDispatch::RemoteIp+: Protect against IP spoofing attacks
-* +ActionDispatch::Reloader+: In development mode, support code reloading.
-* +ActionDispatch::ParamsParser+: Parse XML, YAML and JSON parameters when the request's +Content-Type+ is one of those.
-* +ActionDispatch::Head+: Dispatch +HEAD+ requests as +GET+ requests, and return only the status code and headers.
-* +Rack::ConditionalGet+: Supports the +stale?+ feature in Rails controllers.
-* +Rack::ETag+: Automatically set an +ETag+ on all string responses. This means that if the same response is returned from a controller for the same URL, the server will return a +304 Not Modified+, even if no additional caching steps are taken. This is primarily a client-side optimization; it reduces bandwidth costs but not server processing time.
-
-Other plugins, including +ActiveRecord+, may add additional middlewares. In general, these middlewares are agnostic to the type of app you are building, and make sense in an API-only Rails application.
-
-You can get a list of all middlewares in your application via:
-
-<shell>
-$ rake middleware
-</shell>
-
-h4. Using Rack::Cache
-
-When used with Rails, +Rack::Cache+ uses the Rails cache store for its entity and meta stores. This means that if you use memcache, for your Rails app, for instance, the built-in HTTP cache will use memcache.
-
-To make use of +Rack::Cache+, you will want to use +stale?+ in your controller. Here's an example of +stale?+ in use.
-
-<ruby>
-def show
- @post = Post.find(params[:id])
-
- if stale?(:last_modified => @post.updated_at)
- render json: @post
- end
-end
-</ruby>
-
-The call to +stale?+ will compare the +If-Modified-Since+ header in the request with +@post.updated_at+. If the header is newer than the last modified, this action will return a +304 Not Modified+ response. Otherwise, it will render the response and include a +Last-Modified+ header with the response.
-
-Normally, this mechanism is used on a per-client basis. +Rack::Cache+ allows us to share this caching mechanism across clients. We can enable cross-client caching in the call to +stale?+
-
-<ruby>
-def show
- @post = Post.find(params[:id])
-
- if stale?(:last_modified => @post.updated_at, :public => true)
- render json: @post
- end
-end
-</ruby>
-
-This means that +Rack::Cache+ will store off +Last-Modified+ value for a URL in the Rails cache, and add an +If-Modified-Since+ header to any subsequent inbound requests for the same URL.
-
-Think of it as page caching using HTTP semantics.
-
-NOTE: The +Rack::Cache+ middleware is always outside of the +Rack::Lock+ mutex, even in single-threaded apps.
-
-h4. Using Rack::Sendfile
-
-When you use the +send_file+ method in a Rails controller, it sets the +X-Sendfile+ header. +Rack::Sendfile+ is responsible for actually sending the file.
-
-If your front-end server supports accelerated file sending, +Rack::Sendfile+ will offload the actual file sending work to the front-end server.
-
-You can configure the name of the header that your front-end server uses for this purposes using +config.action_dispatch.x_sendfile_header+ in the appropriate environment config file.
-
-You can learn more about how to use +Rack::Sendfile+ with popular front-ends in "the Rack::Sendfile documentation":http://rubydoc.info/github/rack/rack/master/Rack/Sendfile
-
-The values for popular servers once they are configured to support accelerated file sending:
-
-<ruby>
-# Apache and lighttpd
-config.action_dispatch.x_sendfile_header = "X-Sendfile"
-
-# nginx
-config.action_dispatch.x_sendfile_header = "X-Accel-Redirect"
-</ruby>
-
-Make sure to configure your server to support these options following the instructions in the +Rack::Sendfile+ documentation.
-
-NOTE: The +Rack::Sendfile+ middleware is always outside of the +Rack::Lock+ mutex, even in single-threaded apps.
-
-h4. Using ActionDispatch::ParamsParser
-
-+ActionDispatch::ParamsParser+ will take parameters from the client in JSON and make them available in your controller as +params+.
-
-To use this, your client will need to make a request with JSON-encoded parameters and specify the +Content-Type+ as +application/json+.
-
-Here's an example in jQuery:
-
-<plain>
-jQuery.ajax({
- type: 'POST',
- url: '/people'
- dataType: 'json',
- contentType: 'application/json',
- data: JSON.stringify({ person: { firstName: "Yehuda", lastName: "Katz" } }),
-
- success: function(json) { }
-});
-</plain>
-
-+ActionDispatch::ParamsParser+ will see the +Content-Type+ and your params will be +{ :person => { :firstName => "Yehuda", :lastName => "Katz" } }+.
-
-h4. Other Middlewares
-
-Rails ships with a number of other middlewares that you might want to use in an API app, especially if one of your API clients is the browser:
-
-* +Rack::SSL+: Redirects any HTTP request to HTTPS.
-* +Rack::Runtime+: Adds a header to the response listing the total runtime of the request.
-* +Rack::MethodOverride+: Allows the use of the +_method+ hack to route POST requests to other verbs.
-* +ActionDispatch::Cookies+: Supports the +cookie+ method in +ActionController+, including support for signed and encrypted cookies.
-* +ActionDispatch::Flash+: Supports the +flash+ mechanism in +ActionController+.
-* +ActionDispatch::BestStandards+: Tells Internet Explorer to use the most standards-compliant available renderer. In production mode, if ChromeFrame is available, use ChromeFrame.
-* Session Management: If a +config.session_store+ is supplied, this middleware makes the session available as the +session+ method in +ActionController+.
-
-Any of these middlewares can be adding via:
-
-<ruby>
-config.middleware.use Rack::MethodOverride
-</ruby>
-
-h4. Removing Middlewares
-
-If you don't want to use a middleware that is included by default in the API-only middleware set, you can remove it using +config.middleware.delete+:
-
-<ruby>
-config.middleware.delete ::Rack::Sendfile
-</ruby>
-
-Keep in mind that removing these features may remove support for certain features in +ActionController+.
-
-h3. Choosing Controller Modules
-
-An API application (using +ActionController::HTTP+) comes with the following controller modules by default:
-
-* +AbstractController::Translation+: Support for the +l+ and +t+ localization and translation methods. These delegate to +I18n.translate+ and +I18n.localize+.
-* +ActionController::UrlFor+: Makes +url_for+ and friends available.
-* +ActionController::Redirecting+: Support for +redirect_to+
-* +ActionController::Renderers::JSON+: Support for +render :json+
-* +ActionController::ConditionalGet+: Support for +stale?+
-* +ActionController::RackDelegation+: Support for the +request+ and +response+ methods returning +ActionDispatch::Request+ and +ActionDispatch::Response+ objects.
-* +ActionController::MimeResponds+: Support for content negotiation (+respond_to+, +respond_with+)
-* +ActionController::DataStreaming+: Support for +send_file+ and +send_data+
-* +AbstractController::Callbacks+: Support for +before_filter+ and friends
-* +ActionController::Instrumentation+: Support for the instrumentation hooks defined by +ActionController+ (see "the source":https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/instrumentation.rb for more).
-
-Other plugins may add additional modules. You can get a list of all modules included into +ActionController::HTTP+ in the rails console:
-
-<shell>
-$ irb
->> ActionController::HTTP.ancestors - ActionController::Metal.ancestors
-</shell>
-
-h4. Adding Other Modules
-
-All ActionController modules know about their dependent modules, so you can feel free to include any modules into your controllers, and all dependencies will be included and set up as well.
-
-Some common modules you might want to add:
-
-* +ActionController::HTTPAuthentication::Basic+ (or +Digest+ or +Token): Support for basic, digest or token HTTP authentication.
-* +ActionController::Rendering+: Support for templating and +ActionView+.
-* +AbstractController::Layouts+: Support for layouts when rendering.
-* +ActionController::Renderers::XML+: Support for +render :xml+.
-* +ActionController::Cookies+: Support for +cookies+, which includes support for signed and encrypted cookies. This requires the cookie middleware.
-* +ActionController::Rescue+: Support for +rescue_from+.
-
-The best place to add a module is in your +ApplicationController+. You can also add modules to individual controllers.
diff --git a/railties/guides/source/command_line.textile b/railties/guides/source/command_line.textile
index fe4a84dae9..463c2b172b 100644
--- a/railties/guides/source/command_line.textile
+++ b/railties/guides/source/command_line.textile
@@ -374,7 +374,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
diff --git a/railties/guides/source/configuring.textile b/railties/guides/source/configuring.textile
index 28d198c00b..cfad642e0d 100644
--- a/railties/guides/source/configuring.textile
+++ b/railties/guides/source/configuring.textile
@@ -288,8 +288,6 @@ 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.
@@ -439,12 +437,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:
@@ -713,8 +705,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+.
diff --git a/railties/guides/source/engines.textile b/railties/guides/source/engines.textile
index 5f7eb5290c..6ae9504c23 100644
--- a/railties/guides/source/engines.textile
+++ b/railties/guides/source/engines.textile
@@ -315,7 +315,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:
@@ -361,7 +361,7 @@ This is the final part required to get the new comment form working. Displaying
* "/Users/ryan/Sites/side_projects/blorgh/app/views"
</text>
-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 +440,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 +493,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.
diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile
index 5ae9cf0f2b..69e5c1edcc 100644
--- a/railties/guides/source/initialization.textile
+++ b/railties/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/upgrading_ruby_on_rails.textile b/railties/guides/source/upgrading_ruby_on_rails.textile
index 731d56c850..e63548abc9 100644
--- a/railties/guides/source/upgrading_ruby_on_rails.textile
+++ b/railties/guides/source/upgrading_ruby_on_rails.textile
@@ -34,6 +34,10 @@ 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.
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 3191fe68a7..8d64aff430 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -239,7 +239,7 @@ module Rails
middleware.use ::Rack::Lock unless config.allow_concurrency
middleware.use ::Rack::Runtime
- middleware.use ::Rack::MethodOverride unless config.middleware.http_only?
+ middleware.use ::Rack::MethodOverride
middleware.use ::ActionDispatch::RequestId
middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
middleware.use ::ActionDispatch::ShowExceptions, config.exceptions_app || ActionDispatch::PublicExceptions.new(Rails.public_path)
@@ -252,9 +252,9 @@ module Rails
end
middleware.use ::ActionDispatch::Callbacks
- middleware.use ::ActionDispatch::Cookies unless config.middleware.http_only?
+ middleware.use ::ActionDispatch::Cookies
- if !config.middleware.http_only? && config.session_store
+ if config.session_store
if config.force_ssl && !config.session_options.key?(:secure)
config.session_options[:secure] = true
end
@@ -267,7 +267,7 @@ module Rails
middleware.use ::Rack::ConditionalGet
middleware.use ::Rack::ETag, "no-cache"
- if !config.middleware.http_only? && config.action_dispatch.best_standards_support
+ if config.action_dispatch.best_standards_support
middleware.use ::ActionDispatch::BestStandardsSupport, config.action_dispatch.best_standards_support
end
end
diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb
index d8185bcb34..d8ca6cbd21 100644
--- a/railties/lib/rails/configuration.rb
+++ b/railties/lib/rails/configuration.rb
@@ -33,27 +33,9 @@ module Rails
#
# config.middleware.delete ActionDispatch::BestStandardsSupport
#
- # 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:
- #
- # config.middleware.http_only!
- #
- # 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.
class MiddlewareStackProxy
def initialize
@operations = []
- @http_only = false
- end
-
- attr_reader :http_only
- alias :http_only? :http_only
-
- def http_only!
- @http_only = true
end
def insert_before(*args, &block)
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index 9629ac55c2..131d6e5711 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -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/app_base.rb b/railties/lib/rails/generators/app_base.rb
index 7c449657b5..8e9083e6eb 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'
@@ -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/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/app/controllers/application_controller.rb b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb
deleted file mode 100644
index b3d6adad2a..0000000000
--- a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class ApplicationController < ActionController::Base
- # prevent CSRF attacks by raising an exception,
- # if your application has an API, you'll probably need to use :reset_session
- protect_from_forgery :with => :exception
-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 e47784994a..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 -%>
@@ -60,8 +59,8 @@ module <%= app_const_base %>
# 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/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/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 8d45b2bbd9..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
@@ -10,6 +10,6 @@ Rails.backtrace_cleaner.remove_silencers!
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
# Load fixtures from the engine
-if ActiveSupport::TestCase.method_defined?(:fixture_path)
+if ActiveSupport::TestCase.method_defined?(:fixture_path=)
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
-end \ No newline at end of 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/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
index 4c07baae36..9e76587a0d 100644
--- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
+++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
@@ -10,17 +10,16 @@ module TestUnit
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 resource_attributes
- key_value singular_table_name, "{ #{attributes_hash} }"
- end
-
def attributes_hash
return if accessible_attributes.empty?
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 19894443d5..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, <%= resource_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}" %>, <%= resource_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/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/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake
index cec346d86b..2851ca4189 100644
--- a/railties/lib/rails/tasks/documentation.rake
+++ b/railties/lib/rails/tasks/documentation.rake
@@ -83,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/test/application/loading_test.rb b/railties/test/application/loading_test.rb
index 0354c08067..9302443c98 100644
--- a/railties/test/application/loading_test.rb
+++ b/railties/test/application/loading_test.rb
@@ -95,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
@@ -256,6 +256,35 @@ 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
+
protected
def setup_ar!
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb
index 712a555c4a..2f62d978e3 100644
--- a/railties/test/application/middleware_test.rb
+++ b/railties/test/application/middleware_test.rb
@@ -52,36 +52,6 @@ module ApplicationTests
], middleware
end
- test "api middleware stack" do
- add_to_config "config.middleware.http_only!"
- add_to_config "config.force_ssl = true"
- add_to_config "config.action_dispatch.x_sendfile_header = 'X-Sendfile'"
-
- boot!
-
- assert_equal [
- "Rack::SSL",
- "Rack::Sendfile",
- "ActionDispatch::Static",
- "Rack::Lock",
- "ActiveSupport::Cache::Strategy::LocalCache",
- "Rack::Runtime",
- "ActionDispatch::RequestId",
- "Rails::Rack::Logger",
- "ActionDispatch::ShowExceptions",
- "ActionDispatch::DebugExceptions",
- "ActionDispatch::RemoteIp",
- "ActionDispatch::Reloader",
- "ActionDispatch::Callbacks",
- "ActiveRecord::ConnectionAdapters::ConnectionManagement",
- "ActiveRecord::QueryCache",
- "ActionDispatch::ParamsParser",
- "ActionDispatch::Head",
- "Rack::ConditionalGet",
- "Rack::ETag"
- ], middleware
- end
-
test "Rack::Sendfile is not included by default" do
boot!
@@ -115,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
@@ -173,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!
diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb
index fed9dce8e1..b6cbb10141 100644
--- a/railties/test/application/rake_test.rb
+++ b/railties/test/application/rake_test.rb
@@ -123,12 +123,13 @@ module ApplicationTests
end
def test_scaffold_tests_pass_by_default
- content = Dir.chdir(app_path) do
+ 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
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index cf6f9b90c9..27960c63e9 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -213,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|
@@ -371,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/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb
index 03a753151d..1eea50b0d9 100644
--- a/railties/test/generators/scaffold_controller_generator_test.rb
+++ b/railties/test/generators/scaffold_controller_generator_test.rb
@@ -75,8 +75,8 @@ 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)
+ 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
@@ -86,8 +86,8 @@ 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: { }/, content)
- assert_match(/put :update, id: @user, user: { }/, 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 f4f0147156..7123950add 100644
--- a/railties/test/generators/scaffold_generator_test.rb
+++ b/railties/test/generators/scaffold_generator_test.rb
@@ -64,8 +64,8 @@ class ScaffoldGeneratorTest < Rails::Generators::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)
+ 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
@@ -94,8 +94,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
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)
+ assert_match(/post :create, product_line: \{ \}/, content)
+ assert_match(/put :update, id: @product_line, product_line: \{ \}/, content)
end
end
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 b0be555c4c..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,8 +250,7 @@ module TestHelpers
def use_frameworks(arr)
to_remove = [:actionmailer,
:activemodel,
- :activerecord,
- :activeresource] - arr
+ :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"
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'