Permalink
Browse files

Merge commit 'mainstream/master'

Conflicts:
	actionpack/lib/action_controller/base/mime_responds.rb
  • Loading branch information...
2 parents 1fee2fb + 6e039e8 commit 7324e46a3fe7be282deeda14783f4170ccf03a3c @lifo lifo committed May 24, 2009
Showing with 3,460 additions and 1,744 deletions.
  1. +3 −1 Rakefile
  2. +2 −4 {actionpack → actionmailer}/test/adv_attr_test.rb
  3. +7 −0 actionpack/CHANGELOG
  4. +29 −6 actionpack/Rakefile
  5. +24 −16 actionpack/examples/minimal.rb
  6. +1 −0 actionpack/lib/action_controller.rb
  7. +1 −0 actionpack/lib/action_controller/abstract.rb
  8. +3 −3 actionpack/lib/action_controller/abstract/base.rb
  9. +28 −0 actionpack/lib/action_controller/abstract/benchmarker.rb
  10. +11 −0 actionpack/lib/action_controller/abstract/callbacks.rb
  11. +23 −4 actionpack/lib/action_controller/abstract/helpers.rb
  12. +36 −16 actionpack/lib/action_controller/abstract/layouts.rb
  13. +1 −0 actionpack/lib/action_controller/abstract/logger.rb
  14. +15 −9 actionpack/lib/action_controller/abstract/renderer.rb
  15. +3 −61 actionpack/lib/action_controller/base/base.rb
  16. +1 −1 actionpack/lib/action_controller/base/chained/benchmarking.rb
  17. +1 −6 actionpack/lib/action_controller/base/chained/filters.rb
  18. +52 −22 actionpack/lib/action_controller/base/chained/flash.rb
  19. +97 −0 actionpack/lib/action_controller/base/filter_parameter_logging.rb
  20. +9 −13 actionpack/lib/action_controller/base/helpers.rb
  21. +2 −1 actionpack/lib/action_controller/base/http_authentication.rb
  22. +104 −118 actionpack/lib/action_controller/base/mime_responds.rb
  23. +19 −5 actionpack/lib/action_controller/base/request_forgery_protection.rb
  24. +8 −0 actionpack/lib/action_controller/base/streaming.rb
  25. +6 −3 actionpack/lib/action_controller/base/verification.rb
  26. +15 −15 actionpack/lib/action_controller/caching.rb
  27. +19 −3 actionpack/lib/action_controller/caching/actions.rb
  28. +39 −61 actionpack/lib/action_controller/dispatch/dispatcher.rb
  29. +1 −1 actionpack/lib/action_controller/dispatch/middlewares.rb
  30. +18 −3 actionpack/lib/action_controller/new_base.rb
  31. +40 −11 actionpack/lib/action_controller/new_base/base.rb
  32. +28 −8 actionpack/lib/action_controller/new_base/compatibility.rb
  33. +1 −1 actionpack/lib/action_controller/new_base/conditional_get.rb
  34. +130 −0 actionpack/lib/action_controller/new_base/helpers.rb
  35. +1 −2 actionpack/lib/action_controller/new_base/http.rb
  36. +9 −12 actionpack/lib/action_controller/new_base/layouts.rb
  37. +107 −0 actionpack/lib/action_controller/new_base/render_options.rb
  38. +37 −29 actionpack/lib/action_controller/new_base/renderer.rb
  39. +11 −0 actionpack/lib/action_controller/new_base/session.rb
  40. +10 −5 actionpack/lib/action_controller/new_base/testing.rb
  41. +5 −0 actionpack/lib/action_controller/new_base/url_for.rb
  42. +27 −31 actionpack/lib/action_controller/testing/integration.rb
  43. +1 −3 actionpack/lib/action_controller/testing/process.rb
  44. +4 −1 actionpack/lib/action_controller/testing/process2.rb
  45. +1 −1 actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb
  46. +5 −2 actionpack/lib/action_dispatch.rb
  47. +1 −1 actionpack/lib/action_dispatch/http/mime_type.rb
  48. +1 −1 actionpack/lib/action_dispatch/http/mime_types.rb
  49. +16 −2 actionpack/lib/action_dispatch/http/request.rb
  50. +40 −0 actionpack/lib/action_dispatch/middleware/callbacks.rb
  51. +0 −52 actionpack/lib/action_dispatch/middleware/failsafe.rb
  52. +4 −6 actionpack/lib/action_dispatch/middleware/params_parser.rb
  53. +40 −47 actionpack/lib/action_dispatch/middleware/show_exceptions.rb
  54. +50 −0 actionpack/lib/action_dispatch/vendor/rack-test/rack/mock_session.rb
  55. +239 −0 actionpack/lib/action_dispatch/vendor/rack-test/rack/test.rb
  56. +169 −0 actionpack/lib/action_dispatch/vendor/rack-test/rack/test/cookie_jar.rb
  57. +45 −0 actionpack/lib/action_dispatch/vendor/rack-test/rack/test/methods.rb
  58. +27 −0 actionpack/lib/action_dispatch/vendor/rack-test/rack/test/mock_digest_request.rb
  59. +36 −0 actionpack/lib/action_dispatch/vendor/rack-test/rack/test/uploaded_file.rb
  60. +75 −0 actionpack/lib/action_dispatch/vendor/rack-test/rack/test/utils.rb
  61. +14 −10 actionpack/lib/action_view/base.rb
  62. +1 −0 actionpack/lib/action_view/helpers/active_record_helper.rb
  63. +6 −1 actionpack/lib/action_view/helpers/number_helper.rb
  64. +2 −0 actionpack/lib/action_view/helpers/prototype_helper.rb
  65. +13 −7 actionpack/lib/action_view/helpers/text_helper.rb
  66. +6 −0 actionpack/lib/action_view/template/handler.rb
  67. +1 −1 actionpack/lib/action_view/template/handlers.rb
  68. +2 −0 actionpack/lib/action_view/template/handlers/builder.rb
  69. +2 −0 actionpack/lib/action_view/template/handlers/erb.rb
  70. +6 −0 actionpack/lib/action_view/template/handlers/rjs.rb
  71. +9 −7 actionpack/lib/action_view/template/template.rb
  72. +10 −1 actionpack/lib/action_view/template/text.rb
  73. +1 −1 actionpack/lib/action_view/test_case.rb
  74. +1 −0 actionpack/test/abstract_controller/layouts_test.rb
  75. +6 −4 actionpack/test/abstract_unit.rb
  76. +3 −3 actionpack/test/activerecord/active_record_store_test.rb
  77. +26 −3 actionpack/test/controller/action_pack_assertions_test.rb
  78. +2 −0 actionpack/test/controller/caching_test.rb
  79. +2 −1 actionpack/test/controller/content_type_test.rb
  80. +27 −13 actionpack/test/controller/cookie_test.rb
  81. +20 −14 actionpack/test/controller/dispatcher_test.rb
  82. +42 −3 actionpack/test/controller/filter_params_test.rb
  83. +8 −26 actionpack/test/controller/filters_test.rb
  84. +25 −24 actionpack/test/controller/flash_test.rb
  85. +3 −6 actionpack/test/controller/helper_test.rb
  86. +28 −6 actionpack/test/controller/http_digest_authentication_test.rb
  87. +16 −15 actionpack/test/controller/integration_test.rb
  88. +27 −16 actionpack/test/controller/layout_test.rb
  89. +7 −2 actionpack/test/controller/logging_test.rb
  90. +29 −19 actionpack/test/controller/mime_responds_test.rb
  91. +2 −0 actionpack/test/controller/render_js_test.rb
  92. +19 −3 actionpack/test/controller/render_other_test.rb
  93. +1 −3 actionpack/test/controller/render_test.rb
  94. +14 −0 actionpack/test/controller/rescue_test.rb
  95. +1 −0 actionpack/test/controller/routing_test.rb
  96. +1 −0 actionpack/test/controller/selector_test.rb
  97. +22 −15 actionpack/test/controller/send_file_test.rb
  98. +7 −9 actionpack/test/controller/verification_test.rb
  99. +2 −2 actionpack/test/controller/webservice_test.rb
  100. +5 −0 actionpack/test/dispatch/show_exceptions_test.rb
  101. +1 −1 actionpack/test/fixtures/layout_tests/layouts/third_party_template_library.mab
  102. 0 actionpack/test/fixtures/test/{greeting.erb → greeting.html.erb}
  103. 0 actionpack/test/{controller → }/html-scanner/cdata_node_test.rb
  104. 0 actionpack/test/{controller → }/html-scanner/document_test.rb
  105. 0 actionpack/test/{controller → }/html-scanner/node_test.rb
  106. 0 actionpack/test/{controller → }/html-scanner/sanitizer_test.rb
  107. 0 actionpack/test/{controller → }/html-scanner/tag_node_test.rb
  108. 0 actionpack/test/{controller → }/html-scanner/text_node_test.rb
  109. 0 actionpack/test/{controller → }/html-scanner/tokenizer_test.rb
  110. +3 −3 actionpack/test/{ → lib}/active_record_unit.rb
  111. 0 actionpack/test/{ → lib}/controller/fake_controllers.rb
  112. 0 actionpack/test/{ → lib}/controller/fake_models.rb
  113. +1 −1 actionpack/test/lib/fixture_template.rb
  114. 0 actionpack/test/{ → lib}/testing_sandbox.rb
  115. +31 −4 actionpack/test/{abstract_unit2.rb → new_base/abstract_unit.rb}
  116. +31 −49 actionpack/test/new_base/base_test.rb
  117. +60 −60 actionpack/test/new_base/content_type_test.rb
  118. +12 −13 actionpack/test/new_base/etag_test.rb
  119. +187 −207 actionpack/test/new_base/render_action_test.rb
  120. +14 −14 actionpack/test/new_base/render_implicit_action_test.rb
  121. +62 −34 actionpack/test/new_base/render_layout_test.rb
  122. +43 −44 actionpack/test/new_base/render_template_test.rb
  123. +24 −27 actionpack/test/new_base/render_test.rb
  124. +73 −83 actionpack/test/new_base/render_text_test.rb
  125. +20 −53 actionpack/test/new_base/test_helper.rb
  126. +4 −0 actionpack/test/template/javascript_helper_test.rb
  127. +4 −0 actionpack/test/template/number_helper_test.rb
  128. +4 −0 actionpack/test/template/prototype_helper_test.rb
  129. +10 −0 actionpack/test/template/text_helper_test.rb
  130. +2 −0 activerecord/CHANGELOG
  131. +1 −1 activerecord/lib/active_record/associations.rb
  132. +9 −0 activerecord/lib/active_record/associations/association_collection.rb
  133. +7 −0 activerecord/lib/active_record/associations/has_many_through_association.rb
  134. +8 −8 activerecord/lib/active_record/associations/has_one_through_association.rb
  135. +5 −4 activerecord/lib/active_record/base.rb
  136. +5 −4 activerecord/lib/active_record/fixtures.rb
  137. +10 −1 activerecord/lib/active_record/named_scope.rb
  138. +9 −1 activerecord/lib/active_record/session_store.rb
  139. +2 −0 activerecord/lib/active_record/validations.rb
  140. +0 −24 activerecord/test/cases/aaa_create_tables_test.rb
  141. +4 −0 activerecord/test/cases/associations/eager_test.rb
  142. +39 −0 activerecord/test/cases/associations/has_many_associations_test.rb
  143. +31 −25 activerecord/test/cases/associations/has_many_through_associations_test.rb
  144. +8 −1 activerecord/test/cases/associations/has_one_through_associations_test.rb
  145. +6 −1 activerecord/test/cases/autosave_association_test.rb
  146. +2 −2 activerecord/test/cases/finder_test.rb
  147. +5 −0 activerecord/test/cases/fixtures_test.rb
  148. +18 −0 activerecord/test/cases/helper.rb
  149. +10 −0 activerecord/test/cases/method_scoping_test.rb
  150. +37 −1 activerecord/test/cases/named_scope_test.rb
  151. +4 −1 activerecord/test/fixtures/people.yml
  152. +10 −0 activerecord/test/models/developer.rb
  153. +1 −0 activerecord/test/models/person.rb
  154. +5 −1 activerecord/test/schema/schema.rb
  155. +0 −6 activerecord/test/schema/schema2.rb
  156. +2 −0 activesupport/CHANGELOG
  157. +9 −25 activesupport/lib/active_support.rb
  158. +3 −0 activesupport/lib/active_support/all.rb
  159. +25 −0 activesupport/lib/active_support/autoload.rb
  160. +16 −6 activesupport/lib/active_support/cache/mem_cache_store.rb
  161. +0 −7 activesupport/lib/active_support/core.rb
  162. +0 −5 activesupport/lib/active_support/core/all.rb
  163. +0 −4 activesupport/lib/active_support/core/time.rb
  164. +1 −2 activesupport/lib/active_support/core_ext/hash/conversions.rb
  165. +3 −80 activesupport/lib/active_support/core_ext/object/misc.rb
  166. +42 −0 activesupport/lib/active_support/core_ext/object/returning.rb
  167. +16 −0 activesupport/lib/active_support/core_ext/object/tap.rb
  168. +24 −0 activesupport/lib/active_support/core_ext/object/with_options.rb
  169. +1 −14 activesupport/lib/active_support/core_ext/symbol.rb
  170. +14 −0 activesupport/lib/active_support/core_ext/symbol/to_proc.rb
  171. +6 −4 activesupport/lib/active_support/dependency_module.rb
  172. +8 −3 activesupport/lib/active_support/json.rb
  173. +7 −2 activesupport/lib/active_support/json/backends/jsongem.rb
  174. +7 −2 activesupport/lib/active_support/json/backends/yaml.rb
  175. +1 −0 activesupport/lib/active_support/multibyte/chars.rb
  176. +7 −3 activesupport/lib/active_support/new_callbacks.rb
  177. +24 −0 activesupport/lib/active_support/ruby/shim.rb
  178. +5 −0 activesupport/lib/active_support/test_case.rb
  179. +5 −0 activesupport/lib/active_support/testing/pending.rb
  180. +14 −0 activesupport/lib/active_support/time.rb
  181. 0 activesupport/lib/active_support/{core → }/time/autoload.rb
  182. +10 −6 activesupport/lib/active_support/xml_mini/jdom.rb
  183. +10 −6 activesupport/lib/active_support/xml_mini/libxml.rb
  184. +10 −6 activesupport/lib/active_support/xml_mini/nokogiri.rb
  185. +9 −5 activesupport/lib/active_support/xml_mini/rexml.rb
  186. +25 −4 activesupport/test/caching_test.rb
  187. +1 −1 activesupport/test/core_ext/duration_test.rb
  188. +1 −1 activesupport/test/core_ext/numeric_ext_test.rb
  189. +1 −1 activesupport/test/core_ext/object_and_class_ext_test.rb
  190. +1 −1 activesupport/test/core_ext/time_ext_test.rb
  191. +11 −0 activesupport/test/dependency_module_test.rb
  192. +1 −1 activesupport/test/i18n_test.rb
  193. +3 −0 activesupport/test/json/decoding_test.rb
  194. +62 −0 activesupport/test/new_callbacks_test.rb
  195. +1 −1 activesupport/test/time_zone_test.rb
  196. +13 −0 activesupport/test/xml_mini/nokogiri_engine_test.rb
  197. +14 −0 activesupport/test/xml_mini/rexml_engine_test.rb
  198. +1 −1 ci/ci_setup_notes.txt
  199. +1 −1 ci/cruise_config.rb
  200. +2 −0 ci/geminstaller.yml
  201. +7 −3 railties/builtin/rails_info/rails/info.rb
  202. +1 −1 railties/configs/routes.rb
  203. +1 −1 railties/lib/commands/performance/profiler.rb
  204. +3 −5 railties/lib/console_app.rb
  205. +3 −5 railties/lib/initializer.rb
  206. +1 −2 railties/lib/rails/plugin.rb
  207. +6 −6 railties/lib/rails/plugin/loader.rb
  208. +1 −1 railties/lib/rails/plugin/locator.rb
  209. +3 −10 railties/lib/rails_generator.rb
  210. +1 −1 railties/lib/rails_generator/generators/applications/app/template_runner.rb
  211. +1 −1 railties/lib/rails_generator/generators/components/model/templates/model.rb
  212. +1 −1 railties/lib/rails_generator/scripts.rb
  213. +4 −4 railties/lib/tasks/gems.rake
  214. +2 −2 railties/lib/tasks/misc.rake
  215. +2 −0 railties/test/rails_info_test.rb
  216. +27 −33 tools/profile_requires
View
@@ -15,9 +15,11 @@ task :default => :test
%w(test isolated_test rdoc pgem package release).each do |task_name|
desc "Run #{task_name} task for all projects"
task task_name do
+ errors = []
PROJECTS.each do |project|
- system %(cd #{project} && #{env} #{$0} #{task_name})
+ system(%(cd #{project} && #{env} #{$0} #{task_name})) || errors << project
end
+ fail("Errors in #{errors.join(', ')}") unless errors.empty?
end
end
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/abstract_unit'
+require 'abstract_unit'
require 'action_mailer/adv_attr_accessor'
class AdvAttrTest < Test::Unit::TestCase
@@ -15,6 +15,4 @@ def test_adv_attr
assert_raise(ArgumentError) {bob.name 'x', 'y'}
end
-
-
-end
+end
View
@@ -1,5 +1,12 @@
*Edge*
+* Change integration test helpers to accept Rack environment instead of just HTTP Headers [Pratik Naik]
+
+ Before : get '/path', {}, 'Accept' => 'text/javascript'
+ After : get '/path', {}, 'HTTP_ACCEPT' => 'text/javascript'
+
+* Instead of checking Rails.env.test? in Failsafe middleware, check env["rails.raise_exceptions"] [Bryan Helmkamp]
+
* Fixed that TestResponse.cookies was returning cookies unescaped #1867 [Doug McInnes]
View
@@ -22,39 +22,62 @@ task :default => [ :test ]
# Run the unit tests
desc "Run all unit tests"
-task :test => [:test_action_pack, :test_active_record_integration, :test_new_base]
+task :test => [:test_action_pack, :test_active_record_integration, :test_new_base, :test_new_base_on_old_tests]
+test_lib_dirs = [ENV["NEW"] ? "test/new_base" : "test", "test/lib"]
Rake::TestTask.new(:test_action_pack) do |t|
- t.libs << "test"
+ t.libs.concat test_lib_dirs
# make sure we include the tests in alphabetical order as on some systems
# this will not happen automatically and the tests (as a whole) will error
- t.test_files = Dir.glob( "test/{controller,dispatch,template}/**/*_test.rb" ).sort
+ t.test_files = Dir.glob( "test/{controller,dispatch,template,html-scanner}/**/*_test.rb" ).sort
t.verbose = true
#t.warning = true
end
task :isolated_test do
ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
Dir.glob("test/{controller,dispatch,template}/**/*_test.rb").all? do |file|
- system(ruby, '-Ilib:test', file)
+ system(ruby, "-Ilib:#{test_lib_dirs * ':'}", file)
end or raise "Failures"
end
desc 'ActiveRecord Integration Tests'
Rake::TestTask.new(:test_active_record_integration) do |t|
- t.libs << "test"
+ t.libs.concat test_lib_dirs
t.test_files = Dir.glob("test/activerecord/*_test.rb")
t.verbose = true
end
desc 'New Controller Tests'
Rake::TestTask.new(:test_new_base) do |t|
- t.libs << "test"
+ t.libs << "test/new_base" << "test/lib"
t.test_files = Dir.glob("test/{abstract_controller,new_base}/*_test.rb")
t.verbose = true
end
+desc 'Old Controller Tests on New Base'
+Rake::TestTask.new(:test_new_base_on_old_tests) do |t|
+ t.libs << "test/new_base" << "test/lib"
+ # layout
+ # Dir.glob( "test/{dispatch,template}/**/*_test.rb" ).sort +
+
+ # ==== Not ported
+ # * filters
+ # * integration
+ # * test
+ # * view_paths
+ t.test_files = %w(
+ action_pack_assertions addresses_render assert_select
+ base benchmark caching capture content_type cookie dispatcher
+ filter_params flash helper http_basic_authentication
+ http_digest_authentication layout logging mime_responds
+ record_identifier redirect render render_js render_json
+ render_other render_xml request_forgery_protection rescue
+ resources routing selector send_file url_rewriter verification webservice
+ ).map { |name| "test/controller/#{name}_test.rb" }
+ t.verbose = true
+end
# Genereate the RDoc documentation
@@ -1,3 +1,7 @@
+# Pass NEW=1 to run with the new Base
+ENV['RAILS_ENV'] ||= 'production'
+ENV['NO_RELOAD'] ||= '1'
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'action_controller'
require 'action_controller/new_base' if ENV['NEW']
@@ -9,26 +13,30 @@ def index
end
end
-n = (ENV['N'] || 10000).to_i
-input = StringIO.new('')
-
-def call_index(controller, input, n)
- n.times do
- controller.action(:index).call({ 'rack.input' => input })
+class Runner
+ def initialize(app)
+ @app = app
end
- puts controller.name
- status, headers, body = controller.action(:index).call({ 'rack.input' => input })
+ def call(env)
+ env['n'].to_i.times { @app.call(env) }
+ @app.call(env).tap { |response| report(env, response) }
+ end
- puts status
- puts headers.to_yaml
- puts '---'
- body.each do |part|
- puts part
+ def report(env, response)
+ out = env['rack.errors']
+ out.puts response[0], response[1].to_yaml, '---'
+ response[2].each { |part| out.puts part }
+ out.puts '---'
end
- puts '---'
end
-elapsed = Benchmark.realtime { call_index BaseController, input, n }
+n = (ENV['N'] || 1000).to_i
+input = StringIO.new('')
-puts "%dms elapsed, %d requests/sec" % [1000 * elapsed, n / elapsed]
+elapsed = Benchmark.realtime do
+ Runner.new(BaseController.action(:index)).
+ call('n' => n, 'rack.input' => input, 'rack.errors' => $stdout)
+end
+puts "%dms elapsed, %d req/sec, %.2f msec/req" %
+ [1000 * elapsed, n / elapsed, 1000 * elapsed / n]
@@ -66,6 +66,7 @@ def self.load_all!
autoload :UrlRewriter, 'action_controller/routing/generation/url_rewriter'
autoload :UrlWriter, 'action_controller/routing/generation/url_rewriter'
autoload :Verification, 'action_controller/base/verification'
+ autoload :FilterParameterLogging, 'action_controller/base/filter_parameter_logging'
module Assertions
autoload :DomAssertions, 'action_controller/testing/assertions/dom'
@@ -3,6 +3,7 @@
module AbstractController
autoload :Base, "action_controller/abstract/base"
+ autoload :Benchmarker, "action_controller/abstract/benchmarker"
autoload :Callbacks, "action_controller/abstract/callbacks"
autoload :Helpers, "action_controller/abstract/helpers"
autoload :Layouts, "action_controller/abstract/layouts"
@@ -68,11 +68,11 @@ def initialize
self.response_obj = {}
end
- def process(action_name)
- @_action_name = action_name = action_name.to_s
+ def process(action)
+ @_action_name = action_name = action.to_s
unless action_name = method_for_action(action_name)
- raise ActionNotFound, "The action '#{action_name}' could not be found"
+ raise ActionNotFound, "The action '#{action}' could not be found"
end
process_action(action_name)
@@ -0,0 +1,28 @@
+module AbstractController
+ module Benchmarker
+ extend ActiveSupport::DependencyModule
+
+ depends_on Logger
+
+ module ClassMethods
+ def benchmark(title, log_level = ::Logger::DEBUG, use_silence = true)
+ if logger && logger.level >= log_level
+ result = nil
+ ms = Benchmark.ms { result = use_silence ? silence { yield } : yield }
+ logger.add(log_level, "#{title} (#{('%.1f' % ms)}ms)")
+ result
+ else
+ yield
+ end
+ end
+
+ # Silences the logger for the duration of the block.
+ def silence
+ old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger
+ yield
+ ensure
+ logger.level = old_logger_level if logger
+ end
+ end
+ end
+end
@@ -36,6 +36,17 @@ def #{filter}_filter(*names, &blk)
process_action_callback(:#{filter}, name, options)
end
end
+
+ def skip_#{filter}_filter(*names, &blk)
+ options = names.last.is_a?(Hash) ? names.pop : {}
+ _normalize_callback_options(options)
+ names.push(blk) if block_given?
+ names.each do |name|
+ skip_process_action_callback(:#{filter}, name, options)
+ end
+ end
+
+ alias_method :append_#{filter}_filter, :#{filter}_filter
RUBY_EVAL
end
end
@@ -24,11 +24,30 @@ def inherited(klass)
super
end
-
+
+ # Makes all the (instance) methods in the helper module available to templates rendered through this controller.
+ # See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules
+ # available to the templates.
def add_template_helper(mod)
master_helper_module.module_eval { include mod }
end
-
+
+ # Declare a controller method as a helper. For example, the following
+ # makes the +current_user+ controller method available to the view:
+ # class ApplicationController < ActionController::Base
+ # helper_method :current_user, :logged_in?
+ #
+ # def current_user
+ # @current_user ||= User.find_by_id(session[:user])
+ # end
+ #
+ # def logged_in?
+ # current_user != nil
+ # end
+ # end
+ #
+ # In a view:
+ # <% if logged_in? -%>Welcome, <%= current_user.name %><% end -%>
def helper_method(*meths)
meths.flatten.each do |meth|
master_helper_module.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
@@ -39,14 +58,14 @@ def #{meth}(*args, &blk)
end
end
- def helper(*args, &blk)
+ def helper(*args, &block)
args.flatten.each do |arg|
case arg
when Module
add_template_helper(arg)
end
end
- master_helper_module.module_eval(&blk) if block_given?
+ master_helper_module.module_eval(&block) if block_given?
end
end
@@ -4,11 +4,19 @@ module Layouts
depends_on Renderer
+ included do
+ extlib_inheritable_accessor :_layout_conditions
+ self._layout_conditions = {}
+ end
+
module ClassMethods
- def layout(layout)
+ def layout(layout, conditions = {})
unless [String, Symbol, FalseClass, NilClass].include?(layout.class)
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
end
+
+ conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} }
+ self._layout_conditions = conditions
@_layout = layout || false # Converts nil to false
_write_layout_method
@@ -31,15 +39,15 @@ def _implied_layout_name
def _write_layout_method
case @_layout
when String
- self.class_eval %{def _layout() #{@_layout.inspect} end}
+ self.class_eval %{def _layout(details) #{@_layout.inspect} end}
when Symbol
- self.class_eval %{def _layout() #{@_layout} end}
+ self.class_eval %{def _layout(details) #{@_layout} end}
when false
- self.class_eval %{def _layout() end}
+ self.class_eval %{def _layout(details) end}
else
self.class_eval %{
- def _layout
- if view_paths.find_by_parts?("#{_implied_layout_name}", {:formats => formats}, "layouts")
+ def _layout(details)
+ if view_paths.find_by_parts?("#{_implied_layout_name}", details, "layouts")
"#{_implied_layout_name}"
else
super
@@ -49,38 +57,50 @@ def _layout
end
end
end
-
- def _render_template(template, options)
- _action_view._render_template_from_controller(template, options[:_layout], options, options[:_partial])
- end
private
- def _layout() end # This will be overwritten
+ def _layout(details) end # This will be overwritten
# :api: plugin
# ====
# Override this to mutate the inbound layout name
- def _layout_for_name(name)
+ def _layout_for_name(name, details = {:formats => formats})
unless [String, FalseClass, NilClass].include?(name.class)
raise ArgumentError, "String, false, or nil expected; you passed #{name.inspect}"
end
- name && view_paths.find_by_parts(name, {:formats => formats}, "layouts")
+ name && view_paths.find_by_parts(name, details, _layout_prefix(name))
+ end
+
+ # TODO: Decide if this is the best hook point for the feature
+ def _layout_prefix(name)
+ "layouts"
end
- def _default_layout(require_layout = false)
- if require_layout && !_layout
+ def _default_layout(require_layout = false, details = {:formats => formats})
+ if require_layout && _action_has_layout? && !_layout(details)
raise ArgumentError,
"There was no default layout for #{self.class} in #{view_paths.inspect}"
end
begin
- layout = _layout_for_name(_layout)
+ _layout_for_name(_layout(details), details) if _action_has_layout?
rescue NameError => e
raise NoMethodError,
"You specified #{@_layout.inspect} as the layout, but no such method was found"
end
end
+
+ def _action_has_layout?
+ conditions = _layout_conditions
+ if only = conditions[:only]
+ only.include?(action_name)
+ elsif except = conditions[:except]
+ !except.include?(action_name)
+ else
+ true
+ end
+ end
end
end
Oops, something went wrong.

0 comments on commit 7324e46

Please sign in to comment.