thorero-core 0.9.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +21 -0
- data/Rakefile +352 -0
- data/TODO +0 -0
- data/bin/merb +12 -0
- data/bin/merb-specs +5 -0
- data/docs/bootloading.dox +58 -0
- data/docs/documentation_standards +40 -0
- data/docs/merb-core-call-stack-diagram.mmap +0 -0
- data/docs/merb-core-call-stack-diagram.pdf +0 -0
- data/docs/merb-core-call-stack-diagram.png +0 -0
- data/docs/new_render_api +51 -0
- data/lib/merb-core.rb +603 -0
- data/lib/merb-core/autoload.rb +32 -0
- data/lib/merb-core/bootloader.rb +708 -0
- data/lib/merb-core/config.rb +303 -0
- data/lib/merb-core/constants.rb +43 -0
- data/lib/merb-core/controller/abstract_controller.rb +578 -0
- data/lib/merb-core/controller/exceptions.rb +302 -0
- data/lib/merb-core/controller/merb_controller.rb +256 -0
- data/lib/merb-core/controller/mime.rb +106 -0
- data/lib/merb-core/controller/mixins/authentication.rb +87 -0
- data/lib/merb-core/controller/mixins/controller.rb +290 -0
- data/lib/merb-core/controller/mixins/render.rb +481 -0
- data/lib/merb-core/controller/mixins/responder.rb +472 -0
- data/lib/merb-core/controller/template.rb +254 -0
- data/lib/merb-core/core_ext.rb +8 -0
- data/lib/merb-core/core_ext/kernel.rb +319 -0
- data/lib/merb-core/dispatch/cookies.rb +91 -0
- data/lib/merb-core/dispatch/dispatcher.rb +278 -0
- data/lib/merb-core/dispatch/exceptions.html.erb +303 -0
- data/lib/merb-core/dispatch/request.rb +603 -0
- data/lib/merb-core/dispatch/router.rb +179 -0
- data/lib/merb-core/dispatch/router/behavior.rb +867 -0
- data/lib/merb-core/dispatch/router/cached_proc.rb +52 -0
- data/lib/merb-core/dispatch/router/route.rb +321 -0
- data/lib/merb-core/dispatch/session.rb +78 -0
- data/lib/merb-core/dispatch/session/cookie.rb +168 -0
- data/lib/merb-core/dispatch/session/memcached.rb +184 -0
- data/lib/merb-core/dispatch/session/memory.rb +241 -0
- data/lib/merb-core/dispatch/worker.rb +28 -0
- data/lib/merb-core/gem_ext/erubis.rb +77 -0
- data/lib/merb-core/logger.rb +202 -0
- data/lib/merb-core/plugins.rb +59 -0
- data/lib/merb-core/rack.rb +21 -0
- data/lib/merb-core/rack/adapter.rb +44 -0
- data/lib/merb-core/rack/adapter/ebb.rb +25 -0
- data/lib/merb-core/rack/adapter/evented_mongrel.rb +26 -0
- data/lib/merb-core/rack/adapter/fcgi.rb +17 -0
- data/lib/merb-core/rack/adapter/irb.rb +118 -0
- data/lib/merb-core/rack/adapter/mongrel.rb +26 -0
- data/lib/merb-core/rack/adapter/runner.rb +28 -0
- data/lib/merb-core/rack/adapter/swiftiplied_mongrel.rb +26 -0
- data/lib/merb-core/rack/adapter/thin.rb +39 -0
- data/lib/merb-core/rack/adapter/thin_turbo.rb +24 -0
- data/lib/merb-core/rack/adapter/webrick.rb +36 -0
- data/lib/merb-core/rack/application.rb +18 -0
- data/lib/merb-core/rack/handler/mongrel.rb +97 -0
- data/lib/merb-core/rack/middleware.rb +26 -0
- data/lib/merb-core/rack/middleware/path_prefix.rb +31 -0
- data/lib/merb-core/rack/middleware/profiler.rb +19 -0
- data/lib/merb-core/rack/middleware/static.rb +45 -0
- data/lib/merb-core/server.rb +252 -0
- data/lib/merb-core/tasks/audit.rake +68 -0
- data/lib/merb-core/tasks/merb.rb +1 -0
- data/lib/merb-core/tasks/merb_rake_helper.rb +12 -0
- data/lib/merb-core/test.rb +11 -0
- data/lib/merb-core/test/helpers.rb +9 -0
- data/lib/merb-core/test/helpers/controller_helper.rb +8 -0
- data/lib/merb-core/test/helpers/multipart_request_helper.rb +175 -0
- data/lib/merb-core/test/helpers/request_helper.rb +344 -0
- data/lib/merb-core/test/helpers/route_helper.rb +33 -0
- data/lib/merb-core/test/helpers/view_helper.rb +121 -0
- data/lib/merb-core/test/matchers.rb +9 -0
- data/lib/merb-core/test/matchers/controller_matchers.rb +319 -0
- data/lib/merb-core/test/matchers/route_matchers.rb +136 -0
- data/lib/merb-core/test/matchers/view_matchers.rb +335 -0
- data/lib/merb-core/test/run_specs.rb +47 -0
- data/lib/merb-core/test/tasks/spectasks.rb +68 -0
- data/lib/merb-core/test/test_ext/hpricot.rb +32 -0
- data/lib/merb-core/test/test_ext/object.rb +14 -0
- data/lib/merb-core/test/test_ext/string.rb +14 -0
- data/lib/merb-core/vendor/facets.rb +2 -0
- data/lib/merb-core/vendor/facets/dictionary.rb +433 -0
- data/lib/merb-core/vendor/facets/inflect.rb +345 -0
- data/lib/merb-core/version.rb +11 -0
- data/spec/private/config/adapter_spec.rb +32 -0
- data/spec/private/config/config_spec.rb +202 -0
- data/spec/private/config/environment_spec.rb +13 -0
- data/spec/private/config/spec_helper.rb +1 -0
- data/spec/private/core_ext/kernel_spec.rb +169 -0
- data/spec/private/dispatch/bootloader_spec.rb +24 -0
- data/spec/private/dispatch/cookies_spec.rb +107 -0
- data/spec/private/dispatch/dispatch_spec.rb +35 -0
- data/spec/private/dispatch/fixture/app/controllers/application.rb +4 -0
- data/spec/private/dispatch/fixture/app/controllers/exceptions.rb +27 -0
- data/spec/private/dispatch/fixture/app/controllers/foo.rb +21 -0
- data/spec/private/dispatch/fixture/app/helpers/global_helpers.rb +8 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/client_error.html.erb +37 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/internal_server_error.html.erb +216 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/not_acceptable.html.erb +38 -0
- data/spec/private/dispatch/fixture/app/views/exeptions/not_found.html.erb +40 -0
- data/spec/private/dispatch/fixture/app/views/foo/bar.html.erb +0 -0
- data/spec/private/dispatch/fixture/app/views/layout/application.html.erb +11 -0
- data/spec/private/dispatch/fixture/config/black_hole.rb +12 -0
- data/spec/private/dispatch/fixture/config/environments/development.rb +6 -0
- data/spec/private/dispatch/fixture/config/environments/production.rb +5 -0
- data/spec/private/dispatch/fixture/config/environments/test.rb +6 -0
- data/spec/private/dispatch/fixture/config/init.rb +45 -0
- data/spec/private/dispatch/fixture/config/rack.rb +11 -0
- data/spec/private/dispatch/fixture/config/router.rb +35 -0
- data/spec/private/dispatch/fixture/log/merb_test.log +1874 -0
- data/spec/private/dispatch/fixture/public/images/merb.jpg +0 -0
- data/spec/private/dispatch/fixture/public/merb.fcgi +4 -0
- data/spec/private/dispatch/fixture/public/stylesheets/master.css +119 -0
- data/spec/private/dispatch/route_params_spec.rb +24 -0
- data/spec/private/dispatch/session_mixin_spec.rb +47 -0
- data/spec/private/dispatch/spec_helper.rb +1 -0
- data/spec/private/plugins/plugin_spec.rb +166 -0
- data/spec/private/rack/application_spec.rb +49 -0
- data/spec/private/router/behavior_spec.rb +60 -0
- data/spec/private/router/fixture/log/merb_test.log +139 -0
- data/spec/private/router/route_spec.rb +414 -0
- data/spec/private/router/router_spec.rb +175 -0
- data/spec/private/vendor/facets/plural_spec.rb +564 -0
- data/spec/private/vendor/facets/singular_spec.rb +489 -0
- data/spec/public/DEFINITIONS +11 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/application.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_string_controller_layout.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/layout/merb/test/fixtures/abstract/render_template_controller_layout.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/display_object_with_multiple_roots/show.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/partial/basic_partial_with_multiple_roots/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_and_custom_location/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/alt_views/render_template_multiple_roots_inherited/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/cousins.rb +41 -0
- data/spec/public/abstract_controller/controllers/display.rb +54 -0
- data/spec/public/abstract_controller/controllers/filters.rb +193 -0
- data/spec/public/abstract_controller/controllers/helpers.rb +41 -0
- data/spec/public/abstract_controller/controllers/partial.rb +121 -0
- data/spec/public/abstract_controller/controllers/render.rb +113 -0
- data/spec/public/abstract_controller/controllers/views/helpers/capture/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/helpers/capture_eq/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/helpers/capture_with_args/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/helpers/concat/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/layout/alt.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/layout/custom.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/display_object_with_action/new.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_app_layout/index.erb +0 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_custom_layout/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_template_multiple_roots/show.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/merb/test/fixtures/abstract/render_two_throw_contents/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/another_directory/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/basic_partial_with_multiple_roots/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_first.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/_second.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/nested_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_in_another_directory/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_both/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_as/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/_collection.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_collections_and_counter/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/_variables.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_locals/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/_both.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/partial_with_with_and_locals/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_absolute_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_as_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_nil_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_partial/_with_partial.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/partial/with_partial/index.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/test_display/foo.html.erb +1 -0
- data/spec/public/abstract_controller/controllers/views/test_render/foo.html.erb +0 -0
- data/spec/public/abstract_controller/controllers/views/wonderful/index.erb +1 -0
- data/spec/public/abstract_controller/display_spec.rb +33 -0
- data/spec/public/abstract_controller/filter_spec.rb +106 -0
- data/spec/public/abstract_controller/helper_spec.rb +21 -0
- data/spec/public/abstract_controller/partial_spec.rb +61 -0
- data/spec/public/abstract_controller/render_spec.rb +90 -0
- data/spec/public/abstract_controller/spec_helper.rb +31 -0
- data/spec/public/boot_loader/boot_loader_spec.rb +33 -0
- data/spec/public/boot_loader/spec_helper.rb +1 -0
- data/spec/public/controller/authentication_spec.rb +103 -0
- data/spec/public/controller/base_spec.rb +36 -0
- data/spec/public/controller/controllers/authentication.rb +45 -0
- data/spec/public/controller/controllers/base.rb +36 -0
- data/spec/public/controller/controllers/display.rb +118 -0
- data/spec/public/controller/controllers/redirect.rb +30 -0
- data/spec/public/controller/controllers/responder.rb +93 -0
- data/spec/public/controller/controllers/url.rb +7 -0
- data/spec/public/controller/controllers/views/layout/custom.html.erb +1 -0
- data/spec/public/controller/controllers/views/layout/custom_arg.html.erb +1 -0
- data/spec/public/controller/controllers/views/layout/custom_arg.json.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_and_local_provides/index.xml.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/class_provides/index.xml.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template/no_layout.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/display_with_template_argument/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/html_default/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/layout/custom.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/local_provides/index.xml.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.html.erb +1 -0
- data/spec/public/controller/controllers/views/merb/test/fixtures/controllers/multi_provides/index.js.erb +1 -0
- data/spec/public/controller/display_spec.rb +84 -0
- data/spec/public/controller/redirect_spec.rb +27 -0
- data/spec/public/controller/responder_spec.rb +163 -0
- data/spec/public/controller/spec_helper.rb +11 -0
- data/spec/public/controller/url_spec.rb +180 -0
- data/spec/public/core/merb_core_spec.rb +45 -0
- data/spec/public/core_ext/class_spec.rb +91 -0
- data/spec/public/core_ext/fixtures/core_ext_dependency.rb +2 -0
- data/spec/public/core_ext/kernel_spec.rb +9 -0
- data/spec/public/core_ext/spec_helper.rb +1 -0
- data/spec/public/directory_structure/directory/app/controllers/application.rb +3 -0
- data/spec/public/directory_structure/directory/app/controllers/base.rb +13 -0
- data/spec/public/directory_structure/directory/app/controllers/custom.rb +19 -0
- data/spec/public/directory_structure/directory/app/views/base/template.html.erb +1 -0
- data/spec/public/directory_structure/directory/app/views/wonderful/template.erb +1 -0
- data/spec/public/directory_structure/directory/config/router.rb +3 -0
- data/spec/public/directory_structure/directory/log/merb_test.log +562 -0
- data/spec/public/directory_structure/directory_spec.rb +44 -0
- data/spec/public/logger/logger_spec.rb +181 -0
- data/spec/public/logger/spec_helper.rb +1 -0
- data/spec/public/reloading/directory/app/controllers/application.rb +3 -0
- data/spec/public/reloading/directory/app/controllers/reload.rb +6 -0
- data/spec/public/reloading/directory/config/init.rb +2 -0
- data/spec/public/reloading/directory/log/merb_test.log +138 -0
- data/spec/public/reloading/reload_spec.rb +103 -0
- data/spec/public/request/multipart_spec.rb +41 -0
- data/spec/public/request/request_spec.rb +228 -0
- data/spec/public/router/default_spec.rb +21 -0
- data/spec/public/router/deferred_spec.rb +22 -0
- data/spec/public/router/fixation_spec.rb +27 -0
- data/spec/public/router/fixture/log/merb_test.log +1556 -0
- data/spec/public/router/namespace_spec.rb +113 -0
- data/spec/public/router/nested_matches_spec.rb +97 -0
- data/spec/public/router/nested_resources_spec.rb +41 -0
- data/spec/public/router/resource_spec.rb +37 -0
- data/spec/public/router/resources_spec.rb +82 -0
- data/spec/public/router/spec_helper.rb +90 -0
- data/spec/public/router/special_spec.rb +61 -0
- data/spec/public/router/string_spec.rb +61 -0
- data/spec/public/template/template_spec.rb +104 -0
- data/spec/public/template/templates/error.html.erb +2 -0
- data/spec/public/template/templates/template.html.erb +1 -0
- data/spec/public/template/templates/template.html.myt +1 -0
- data/spec/public/test/controller_matchers_spec.rb +402 -0
- data/spec/public/test/controllers/controller_assertion_mock.rb +7 -0
- data/spec/public/test/controllers/dispatch_controller.rb +11 -0
- data/spec/public/test/controllers/spec_helper_controller.rb +38 -0
- data/spec/public/test/multipart_request_helper_spec.rb +159 -0
- data/spec/public/test/multipart_upload_text_file.txt +1 -0
- data/spec/public/test/request_helper_spec.rb +221 -0
- data/spec/public/test/route_helper_spec.rb +71 -0
- data/spec/public/test/route_matchers_spec.rb +162 -0
- data/spec/public/test/view_helper_spec.rb +96 -0
- data/spec/public/test/view_matchers_spec.rb +183 -0
- data/spec/spec_helper.rb +68 -0
- metadata +542 -0
@@ -0,0 +1,481 @@
|
|
1
|
+
module Merb::RenderMixin
|
2
|
+
# So we can do raise TemplateNotFound
|
3
|
+
include Merb::ControllerExceptions
|
4
|
+
|
5
|
+
# ==== Parameters
|
6
|
+
# base<Module>:: Module that is including RenderMixin (probably a controller)
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
base.class_eval do
|
10
|
+
class_inheritable_accessor :_default_render_options
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
|
16
|
+
# Return the default render options.
|
17
|
+
#
|
18
|
+
# ==== Returns
|
19
|
+
# Hash:: An options hash
|
20
|
+
def default_render_options
|
21
|
+
self._default_render_options ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set default render options at the class level.
|
25
|
+
#
|
26
|
+
# ==== Parameters
|
27
|
+
# opts<Hash>:: An options hash
|
28
|
+
def render_options(opts)
|
29
|
+
self._default_render_options = opts
|
30
|
+
end
|
31
|
+
|
32
|
+
# Set the default layout to use or nil/false to disable layout rendering.
|
33
|
+
# This is a shortcut for render_options :layout => false.
|
34
|
+
#
|
35
|
+
# ==== Parameters
|
36
|
+
# layout<~to_s>:: The layout that should be used for this class.
|
37
|
+
#
|
38
|
+
# ==== Notes
|
39
|
+
# You can override by passing :layout => true to render method.
|
40
|
+
#
|
41
|
+
# ==== Returns
|
42
|
+
# Hash:: The default render options.
|
43
|
+
def layout(layout)
|
44
|
+
self.default_render_options.update(:layout => (layout ? layout : false))
|
45
|
+
end
|
46
|
+
|
47
|
+
# Enable the default layout logic - reset the layout option.
|
48
|
+
def default_layout
|
49
|
+
self.default_render_options.delete(:layout)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
# Render the specified item, with the specified options.
|
55
|
+
#
|
56
|
+
# ==== Parameters
|
57
|
+
# thing<String, Symbol, nil>::
|
58
|
+
# The thing to render. This will default to the current action
|
59
|
+
# opts<Hash>:: An options hash (see below)
|
60
|
+
#
|
61
|
+
# ==== Options (opts)
|
62
|
+
# :format<Symbol>:: A registered mime-type format
|
63
|
+
# :template<String>::
|
64
|
+
# The path to the template relative to the template root
|
65
|
+
# :status<~to_i>::
|
66
|
+
# The status to send to the client. Typically, this would be an integer
|
67
|
+
# (200), or a Merb status code (Accepted)
|
68
|
+
# :layout<~to_s, FalseClass>::
|
69
|
+
# A layout to use instead of the default. This should be relative to the
|
70
|
+
# layout root. By default, the layout will be either the controller_name or
|
71
|
+
# application. If you want to use an alternative content-type than the one
|
72
|
+
# that the base template was rendered as, you will need to do :layout =>
|
73
|
+
# "foo.#{content_type}" (i.e. "foo.json"). If you want to render without
|
74
|
+
# layout, use :layout => false. This overrides layout set by +layout+ method.
|
75
|
+
#
|
76
|
+
# ==== Returns
|
77
|
+
# String:: The rendered template, including layout, if appropriate.
|
78
|
+
#
|
79
|
+
# ==== Raises
|
80
|
+
# TemplateNotFound:: There is no template for the specified location.
|
81
|
+
#
|
82
|
+
# ==== Alternatives
|
83
|
+
# If you pass a Hash as the first parameter, it will be moved to opts and
|
84
|
+
# "thing" will be the current action
|
85
|
+
#---
|
86
|
+
# @public
|
87
|
+
def render(thing = nil, opts = {})
|
88
|
+
# render :format => :xml means render nil, :format => :xml
|
89
|
+
opts, thing = thing, nil if thing.is_a?(Hash)
|
90
|
+
|
91
|
+
# Merge with class level default render options
|
92
|
+
opts = self.class.default_render_options.merge(opts)
|
93
|
+
|
94
|
+
# If you don't specify a thing to render, assume they want to render the current action
|
95
|
+
thing ||= action_name.to_sym
|
96
|
+
|
97
|
+
# Content negotiation
|
98
|
+
opts[:format] ? (self.content_type = opts[:format]) : content_type
|
99
|
+
|
100
|
+
# Handle options (:status)
|
101
|
+
_handle_options!(opts)
|
102
|
+
|
103
|
+
# Do we have a template to try to render?
|
104
|
+
if thing.is_a?(Symbol) || opts[:template]
|
105
|
+
|
106
|
+
template_method, template_location =
|
107
|
+
_template_for(thing, content_type, controller_name, opts[:template])
|
108
|
+
|
109
|
+
# Raise an error if there's no template
|
110
|
+
unless template_method && self.respond_to?(template_method)
|
111
|
+
template_files = Merb::Template.template_extensions.map { |ext| "#{template_location}.#{ext}" }
|
112
|
+
raise TemplateNotFound, "Oops! No template found. Merb was looking for #{template_files.join(', ')}" +
|
113
|
+
"for content type '#{content_type}'. You might have mispelled the template or file name. " +
|
114
|
+
"Registered template extensions: #{Merb::Template.template_extensions.join(', ')}. " +
|
115
|
+
"If you use Haml or some other template plugin, make sure you required Merb plugin dependency " +
|
116
|
+
"in your init file."
|
117
|
+
end
|
118
|
+
|
119
|
+
# Call the method in question and throw the content for later consumption by the layout
|
120
|
+
throw_content(:for_layout, self.send(template_method))
|
121
|
+
|
122
|
+
# Do we have a string to render?
|
123
|
+
elsif thing.is_a?(String)
|
124
|
+
|
125
|
+
# Throw it for later consumption by the layout
|
126
|
+
throw_content(:for_layout, thing)
|
127
|
+
end
|
128
|
+
|
129
|
+
# If we find a layout, use it. Otherwise, just render the content thrown for layout.
|
130
|
+
(layout = _get_layout(opts[:layout])) ? send(layout) : catch_content(:for_layout)
|
131
|
+
end
|
132
|
+
|
133
|
+
# Renders an object using to registered transform method based on the
|
134
|
+
# negotiated content-type, if a template does not exist. For instance, if the
|
135
|
+
# content-type is :json, Merb will first look for current_action.json.*.
|
136
|
+
# Failing that, it will run object.to_json.
|
137
|
+
#
|
138
|
+
# ==== Parameter
|
139
|
+
# object<Object>::
|
140
|
+
# An object that responds_to? the transform method registered for the
|
141
|
+
# negotiated mime-type.
|
142
|
+
# thing<String, Symbol>::
|
143
|
+
# The thing to attempt to render via #render before calling the transform
|
144
|
+
# method on the object. Defaults to nil.
|
145
|
+
# opts<Hash>::
|
146
|
+
# An options hash that will be used for rendering
|
147
|
+
# (passed on to #render or serialization methods like #to_json or #to_xml)
|
148
|
+
#
|
149
|
+
# ==== Returns
|
150
|
+
# String::
|
151
|
+
# The rendered template or if no template is found, the transformed object.
|
152
|
+
#
|
153
|
+
# ==== Raises
|
154
|
+
# NotAcceptable::
|
155
|
+
# If there is no transform method for the specified mime-type or the object
|
156
|
+
# does not respond to the transform method.
|
157
|
+
#
|
158
|
+
# ==== Alternatives
|
159
|
+
# A string in the second parameter will be interpreted as a template:
|
160
|
+
# display @object, "path/to/foo"
|
161
|
+
# #=> display @object, nil, :template => "path/to/foo"
|
162
|
+
#
|
163
|
+
# A hash in the second parameters will be interpreted as opts:
|
164
|
+
# display @object, :layout => "zoo"
|
165
|
+
# #=> display @object, nil, :layout => "zoo"
|
166
|
+
#
|
167
|
+
# If you need to pass extra parameters to serialization method, for instance,
|
168
|
+
# to exclude some of attributes or serialize associations, just pass options
|
169
|
+
# for it.
|
170
|
+
# For instance,
|
171
|
+
#
|
172
|
+
# display @locations, :except => [:locatable_type, :locatable_id], :include => [:locatable]
|
173
|
+
#
|
174
|
+
# serializes object with polymorphic association, not raw locatable_* attributes.
|
175
|
+
#
|
176
|
+
#
|
177
|
+
# ==== Options
|
178
|
+
#
|
179
|
+
# :template a template to use for rendering
|
180
|
+
# :layout a layout to use for rendering
|
181
|
+
# :status the status code to return (defaults to 200)
|
182
|
+
# :location the value of the Location header
|
183
|
+
#
|
184
|
+
# all other options options that will be pass to serialization method
|
185
|
+
# like #to_json or #to_xml
|
186
|
+
#
|
187
|
+
# ==== Notes
|
188
|
+
# The transformed object will not be used in a layout unless a :layout is
|
189
|
+
# explicitly passed in the opts.
|
190
|
+
#
|
191
|
+
def display(object, thing = nil, opts = {})
|
192
|
+
template_opt = opts.delete(:template)
|
193
|
+
|
194
|
+
case thing
|
195
|
+
# display @object, "path/to/foo" means display @object, nil, :template => "path/to/foo"
|
196
|
+
when String
|
197
|
+
template_opt, thing = thing, nil
|
198
|
+
# display @object, :template => "path/to/foo" means display @object, nil, :template => "path/to/foo"
|
199
|
+
when Hash
|
200
|
+
opts, thing = thing, nil
|
201
|
+
end
|
202
|
+
|
203
|
+
# Try to render without the object
|
204
|
+
render(thing || action_name.to_sym, opts.merge(:template => template_opt))
|
205
|
+
|
206
|
+
# If the render fails (i.e. a template was not found)
|
207
|
+
rescue TemplateNotFound => e
|
208
|
+
# Merge with class level default render options
|
209
|
+
# @todo can we find a way to refactor this out so we don't have to do it everywhere?
|
210
|
+
opts = self.class.default_render_options.merge(opts)
|
211
|
+
|
212
|
+
# Figure out what to transform and raise NotAcceptable unless there's a transform method assigned
|
213
|
+
transform = Merb.mime_transform_method(content_type)
|
214
|
+
if !transform
|
215
|
+
raise NotAcceptable, "#{e.message} and there was no transform method registered for #{content_type.inspect}"
|
216
|
+
elsif !object.respond_to?(transform)
|
217
|
+
raise NotAcceptable, "#{e.message} and your object does not respond to ##{transform}"
|
218
|
+
end
|
219
|
+
|
220
|
+
layout_opt = opts.delete(:layout)
|
221
|
+
_handle_options!(opts)
|
222
|
+
throw_content(:for_layout, opts.empty? ? object.send(transform) : object.send(transform, opts))
|
223
|
+
|
224
|
+
meth, _ = _template_for(layout_opt, layout_opt.to_s.index(".") ? nil : content_type, "layout") if layout_opt
|
225
|
+
meth ? send(meth) : catch_content(:for_layout)
|
226
|
+
end
|
227
|
+
|
228
|
+
# Render a partial template.
|
229
|
+
#
|
230
|
+
# ==== Parameters
|
231
|
+
# template<~to_s>::
|
232
|
+
# The path to the template, relative to the current controller or the
|
233
|
+
# template root; absolute path will work too. If the template contains a "/",
|
234
|
+
# Merb will search for it relative to the template root; otherwise,
|
235
|
+
# Merb will search for it relative to the current controller.
|
236
|
+
# opts<Hash>:: A hash of options (see below)
|
237
|
+
#
|
238
|
+
# ==== Options (opts)
|
239
|
+
# :with<Object, Array>::
|
240
|
+
# An object or an array of objects that will be passed into the partial.
|
241
|
+
# :as<~to_sym>:: The local name of the :with Object inside of the partial.
|
242
|
+
# :format<Symbol>:: The mime format that you want the partial to be in (:js, :html, etc.)
|
243
|
+
# others::
|
244
|
+
# A Hash object names and values that will be the local names and values
|
245
|
+
# inside the partial.
|
246
|
+
#
|
247
|
+
# ==== Examples
|
248
|
+
# partial :foo, :hello => @object
|
249
|
+
#
|
250
|
+
# The "_foo" partial will be called, relative to the current controller,
|
251
|
+
# with a local variable of +hello+ inside of it, assigned to @object.
|
252
|
+
#
|
253
|
+
# partial :bar, :with => ['one', 'two', 'three']
|
254
|
+
#
|
255
|
+
# The "_bar" partial will be called once for each element of the array
|
256
|
+
# specified by :with for a total of three iterations. Each element
|
257
|
+
# of the array will be available in the partial via a local variable named
|
258
|
+
# +bar+. Additionally, there will be two extra local variables:
|
259
|
+
# +collection_index+ and +collection_size+. +collection_index+ is the index
|
260
|
+
# of the object currently referenced by +bar+ in the collection passed to
|
261
|
+
# the partial. +collection_size+ is the total size of the collection.
|
262
|
+
#
|
263
|
+
# By default, the object specified by :with will be available through a
|
264
|
+
# local variable with the same name as the partial template. However,
|
265
|
+
# this can be changed using the :as option.
|
266
|
+
#
|
267
|
+
# partial :bar, :with => "one", :as => :number
|
268
|
+
#
|
269
|
+
# In this case, "one" will be available in the partial through the local
|
270
|
+
# variable named +number+.
|
271
|
+
#
|
272
|
+
# ==== Notes
|
273
|
+
# It is important to note that the object being passed to the partial
|
274
|
+
# as well as any extra local variables cannot use names of helper methods
|
275
|
+
# since any helper method of the same name will take precedence over the
|
276
|
+
# passed variable. Example:
|
277
|
+
#
|
278
|
+
# partial :bar, :with => "one", :as => :partial
|
279
|
+
#
|
280
|
+
# In this case, "one" will not be available in the partial because "partial"
|
281
|
+
# is already a helper method.
|
282
|
+
def partial(template, opts={})
|
283
|
+
|
284
|
+
# partial :foo becomes "#{controller_name}/_foo"
|
285
|
+
# partial "foo/bar" becomes "foo/_bar"
|
286
|
+
template = template.to_s
|
287
|
+
if template =~ %r{^/}
|
288
|
+
template_path = File.dirname(template) / "_#{File.basename(template)}"
|
289
|
+
else
|
290
|
+
kontroller = (m = template.match(/.*(?=\/)/)) ? m[0] : controller_name
|
291
|
+
template = "_#{File.basename(template)}"
|
292
|
+
end
|
293
|
+
template_method, template_location =
|
294
|
+
_template_for(template, opts.delete(:format) || content_type, kontroller, template_path)
|
295
|
+
|
296
|
+
(@_old_partial_locals ||= []).push @_merb_partial_locals
|
297
|
+
|
298
|
+
# This handles no :with as well
|
299
|
+
with = [opts.delete(:with)].flatten
|
300
|
+
as = opts.delete(:as) || template_location.match(%r[.*/_([^\.]*)])[1]
|
301
|
+
|
302
|
+
@_merb_partial_locals = opts.merge(:collection_index => -1, :collection_size => with.size)
|
303
|
+
|
304
|
+
# this handles an edge-case where the name of the partial is _foo.* and your opts
|
305
|
+
# have :foo as a key.
|
306
|
+
named_local = @_merb_partial_locals.key?(as.to_sym)
|
307
|
+
|
308
|
+
sent_template = with.map do |temp|
|
309
|
+
@_merb_partial_locals[as.to_sym] = temp unless named_local
|
310
|
+
if template_method && self.respond_to?(template_method)
|
311
|
+
@_merb_partial_locals[:collection_index] += 1
|
312
|
+
send(template_method)
|
313
|
+
else
|
314
|
+
raise TemplateNotFound, "Could not find template at #{template_location}.*"
|
315
|
+
end
|
316
|
+
end.join
|
317
|
+
|
318
|
+
@_merb_partial_locals = @_old_partial_locals.pop
|
319
|
+
sent_template
|
320
|
+
end
|
321
|
+
|
322
|
+
# Take the options hash and handle it as appropriate.
|
323
|
+
#
|
324
|
+
# ==== Parameters
|
325
|
+
# opts<Hash>:: The options hash that was passed into render.
|
326
|
+
#
|
327
|
+
# ==== Options
|
328
|
+
# :status<~to_i>::
|
329
|
+
# The status of the response will be set to opts[:status].to_i
|
330
|
+
#
|
331
|
+
# ==== Returns
|
332
|
+
# Hash:: The options hash that was passed in.
|
333
|
+
def _handle_options!(opts)
|
334
|
+
self.status = opts.delete(:status).to_i if opts[:status]
|
335
|
+
headers["Location"] = opts.delete(:location) if opts[:location]
|
336
|
+
opts
|
337
|
+
end
|
338
|
+
|
339
|
+
# Get the layout that should be used. The content-type will be appended to
|
340
|
+
# the layout unless the layout already contains a "." in it.
|
341
|
+
#
|
342
|
+
# If no layout was passed in, this method will look for one with the same
|
343
|
+
# name as the controller, and finally one in "application.#{content_type}".
|
344
|
+
#
|
345
|
+
# ==== Parameters
|
346
|
+
# layout<~to_s>:: A layout, relative to the layout root. Defaults to nil.
|
347
|
+
#
|
348
|
+
# ==== Returns
|
349
|
+
# String:: The method name that corresponds to the found layout.
|
350
|
+
#
|
351
|
+
# ==== Raises
|
352
|
+
# TemplateNotFound::
|
353
|
+
# If a layout was specified (either via layout in the class or by passing
|
354
|
+
# one in to this method), and not found. No error will be raised if no
|
355
|
+
# layout was specified, and the default layouts were not found.
|
356
|
+
def _get_layout(layout = nil)
|
357
|
+
return false if layout == false
|
358
|
+
|
359
|
+
layout = layout.instance_of?(Symbol) && self.respond_to?(layout, true) ? send(layout) : layout
|
360
|
+
layout = layout.to_s if layout
|
361
|
+
|
362
|
+
# If a layout was provided, throw an error if it's not found
|
363
|
+
if layout
|
364
|
+
template_method, template_location =
|
365
|
+
_template_for(layout, layout.index(".") ? nil : content_type, "layout")
|
366
|
+
|
367
|
+
raise TemplateNotFound, "No layout found at #{template_location}" unless template_method
|
368
|
+
template_method
|
369
|
+
|
370
|
+
# If a layout was not provided, try the default locations
|
371
|
+
else
|
372
|
+
template, location = _template_for(controller_name, content_type, "layout")
|
373
|
+
template, location = _template_for("application", content_type, "layout") unless template
|
374
|
+
template
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
# Iterate over the template roots in reverse order, and return the template
|
379
|
+
# and template location of the first match.
|
380
|
+
#
|
381
|
+
# ==== Parameters
|
382
|
+
# context<Object>:: The controller action or template (basename or absolute path).
|
383
|
+
# content_type<~to_s>:: The content type (like html or json).
|
384
|
+
# controller<~to_s>:: The name of the controller. Defaults to nil.
|
385
|
+
#
|
386
|
+
# ==== Options (opts)
|
387
|
+
# :template<String>::
|
388
|
+
# The location of the template to use. Defaults to whatever matches this
|
389
|
+
# context, content_type and controller.
|
390
|
+
#
|
391
|
+
# ==== Returns
|
392
|
+
# Array[Symbol, String]::
|
393
|
+
# A pair consisting of the template method and location.
|
394
|
+
def _template_for(context, content_type, controller=nil, template=nil)
|
395
|
+
template_method, template_location = nil, nil
|
396
|
+
|
397
|
+
# absolute path to a template (:template => "/foo/bar")
|
398
|
+
if template.is_a?(String) && template =~ %r{^/}
|
399
|
+
template_location = self._absolute_template_location(template, content_type)
|
400
|
+
return [_template_method_for(template_location), template_location]
|
401
|
+
end
|
402
|
+
|
403
|
+
self.class._template_roots.reverse_each do |root, template_meth|
|
404
|
+
# :template => "foo/bar.html" where root / "foo/bar.html.*" exists
|
405
|
+
if template
|
406
|
+
template_location = root / self.send(template_meth, template, content_type, nil)
|
407
|
+
# :layout => "foo" where root / "layouts" / "#{controller}.html.*" exists
|
408
|
+
else
|
409
|
+
template_location = root / self.send(template_meth, context, content_type, controller)
|
410
|
+
end
|
411
|
+
|
412
|
+
break if template_method = _template_method_for(template_location.to_s)
|
413
|
+
end
|
414
|
+
|
415
|
+
# template_location is a Pathname
|
416
|
+
[template_method, template_location.to_s]
|
417
|
+
end
|
418
|
+
|
419
|
+
# Return the template method for a location, and check to make sure the current controller
|
420
|
+
# actually responds to the method.
|
421
|
+
#
|
422
|
+
# ==== Parameters
|
423
|
+
# template_location<String>:: The phyical path of the template
|
424
|
+
#
|
425
|
+
# ==== Returns
|
426
|
+
# String:: The method, if it exists. Otherwise return nil.
|
427
|
+
def _template_method_for(template_location)
|
428
|
+
meth = Merb::Template.template_for(template_location)
|
429
|
+
meth && self.respond_to?(meth) ? meth : nil
|
430
|
+
end
|
431
|
+
|
432
|
+
# Called in templates to get at content thrown in another template. The
|
433
|
+
# results of rendering a template are automatically thrown into :for_layout,
|
434
|
+
# so catch_content or catch_content(:for_layout) can be used inside layouts
|
435
|
+
# to get the content rendered by the action template.
|
436
|
+
#
|
437
|
+
# ==== Parameters
|
438
|
+
# obj<Object>:: The key in the thrown_content hash. Defaults to :for_layout.
|
439
|
+
#---
|
440
|
+
# @public
|
441
|
+
def catch_content(obj = :for_layout)
|
442
|
+
@_caught_content[obj] * '' unless @_caught_content[obj].nil?
|
443
|
+
end
|
444
|
+
|
445
|
+
# Called in templates to test for the existence of previously thrown content.
|
446
|
+
#
|
447
|
+
# ==== Parameters
|
448
|
+
# obj<Object>:: The key in the thrown_content hash. Defaults to :for_layout.
|
449
|
+
#---
|
450
|
+
# @public
|
451
|
+
def thrown_content?(obj = :for_layout)
|
452
|
+
@_caught_content.key?(obj)
|
453
|
+
end
|
454
|
+
|
455
|
+
# Called in templates to store up content for later use. Takes a string
|
456
|
+
# and/or a block. First, the string is evaluated, and then the block is
|
457
|
+
# captured using the capture() helper provided by the template languages. The
|
458
|
+
# two are concatenated together.
|
459
|
+
#
|
460
|
+
# ==== Parameters
|
461
|
+
# obj<Object>:: The key in the thrown_content hash.
|
462
|
+
# string<String>:: Textual content. Defaults to nil.
|
463
|
+
# &block:: A block to be evaluated and concatenated to string.
|
464
|
+
#
|
465
|
+
# ==== Raises
|
466
|
+
# ArgumentError:: Neither string nor block given.
|
467
|
+
#
|
468
|
+
# ==== Example
|
469
|
+
# throw_content(:foo, "Foo")
|
470
|
+
# catch_content(:foo) #=> "Foo"
|
471
|
+
#---
|
472
|
+
# @public
|
473
|
+
def throw_content(obj, string = nil, &block)
|
474
|
+
unless string || block_given?
|
475
|
+
raise ArgumentError, "You must pass a block or a string into throw_content"
|
476
|
+
end
|
477
|
+
@_caught_content[obj] = [] if @_caught_content[obj].nil?
|
478
|
+
@_caught_content[obj] << string.to_s << (block_given? ? capture(&block) : "")
|
479
|
+
end
|
480
|
+
|
481
|
+
end
|