eactionpack 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +7 -0
- data/MIT-LICENSE +21 -0
- data/README +469 -0
- data/RUNNING_UNIT_TESTS +24 -0
- data/Rakefile +146 -0
- data/install.rb +30 -0
- data/lib/action_controller.rb +79 -0
- data/lib/action_controller/assertions.rb +69 -0
- data/lib/action_controller/assertions/dom_assertions.rb +39 -0
- data/lib/action_controller/assertions/model_assertions.rb +20 -0
- data/lib/action_controller/assertions/response_assertions.rb +172 -0
- data/lib/action_controller/assertions/routing_assertions.rb +146 -0
- data/lib/action_controller/assertions/selector_assertions.rb +491 -0
- data/lib/action_controller/assertions/tag_assertions.rb +130 -0
- data/lib/action_controller/base.rb +1288 -0
- data/lib/action_controller/benchmarking.rb +94 -0
- data/lib/action_controller/caching.rb +72 -0
- data/lib/action_controller/caching/actions.rb +144 -0
- data/lib/action_controller/caching/fragments.rb +138 -0
- data/lib/action_controller/caching/pages.rb +154 -0
- data/lib/action_controller/caching/sql_cache.rb +18 -0
- data/lib/action_controller/caching/sweeping.rb +97 -0
- data/lib/action_controller/cgi_ext.rb +16 -0
- data/lib/action_controller/cgi_ext/cookie.rb +110 -0
- data/lib/action_controller/cgi_ext/query_extension.rb +22 -0
- data/lib/action_controller/cgi_ext/session.rb +73 -0
- data/lib/action_controller/cgi_ext/stdinput.rb +24 -0
- data/lib/action_controller/cgi_process.rb +223 -0
- data/lib/action_controller/components.rb +166 -0
- data/lib/action_controller/cookies.rb +96 -0
- data/lib/action_controller/dispatcher.rb +162 -0
- data/lib/action_controller/filters.rb +642 -0
- data/lib/action_controller/flash.rb +172 -0
- data/lib/action_controller/headers.rb +31 -0
- data/lib/action_controller/helpers.rb +221 -0
- data/lib/action_controller/http_authentication.rb +124 -0
- data/lib/action_controller/integration.rb +634 -0
- data/lib/action_controller/layout.rb +309 -0
- data/lib/action_controller/mime_responds.rb +173 -0
- data/lib/action_controller/mime_type.rb +186 -0
- data/lib/action_controller/mime_types.rb +20 -0
- data/lib/action_controller/polymorphic_routes.rb +191 -0
- data/lib/action_controller/record_identifier.rb +102 -0
- data/lib/action_controller/request.rb +764 -0
- data/lib/action_controller/request_forgery_protection.rb +140 -0
- data/lib/action_controller/request_profiler.rb +169 -0
- data/lib/action_controller/rescue.rb +258 -0
- data/lib/action_controller/resources.rb +572 -0
- data/lib/action_controller/response.rb +76 -0
- data/lib/action_controller/routing.rb +387 -0
- data/lib/action_controller/routing/builder.rb +203 -0
- data/lib/action_controller/routing/optimisations.rb +120 -0
- data/lib/action_controller/routing/recognition_optimisation.rb +162 -0
- data/lib/action_controller/routing/route.rb +240 -0
- data/lib/action_controller/routing/route_set.rb +436 -0
- data/lib/action_controller/routing/routing_ext.rb +46 -0
- data/lib/action_controller/routing/segments.rb +283 -0
- data/lib/action_controller/session/active_record_store.rb +340 -0
- data/lib/action_controller/session/cookie_store.rb +166 -0
- data/lib/action_controller/session/drb_server.rb +32 -0
- data/lib/action_controller/session/drb_store.rb +35 -0
- data/lib/action_controller/session/mem_cache_store.rb +98 -0
- data/lib/action_controller/session_management.rb +158 -0
- data/lib/action_controller/status_codes.rb +88 -0
- data/lib/action_controller/streaming.rb +155 -0
- data/lib/action_controller/templates/rescues/_request_and_response.erb +24 -0
- data/lib/action_controller/templates/rescues/_trace.erb +26 -0
- data/lib/action_controller/templates/rescues/diagnostics.erb +11 -0
- data/lib/action_controller/templates/rescues/layout.erb +29 -0
- data/lib/action_controller/templates/rescues/missing_template.erb +2 -0
- data/lib/action_controller/templates/rescues/routing_error.erb +10 -0
- data/lib/action_controller/templates/rescues/template_error.erb +21 -0
- data/lib/action_controller/templates/rescues/unknown_action.erb +2 -0
- data/lib/action_controller/test_case.rb +83 -0
- data/lib/action_controller/test_process.rb +526 -0
- data/lib/action_controller/url_rewriter.rb +142 -0
- data/lib/action_controller/vendor/html-scanner/html/document.rb +68 -0
- data/lib/action_controller/vendor/html-scanner/html/node.rb +537 -0
- data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +173 -0
- data/lib/action_controller/vendor/html-scanner/html/selector.rb +828 -0
- data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +105 -0
- data/lib/action_controller/vendor/html-scanner/html/version.rb +11 -0
- data/lib/action_controller/verification.rb +130 -0
- data/lib/action_pack.rb +24 -0
- data/lib/action_pack/version.rb +9 -0
- data/lib/action_view.rb +44 -0
- data/lib/action_view/base.rb +335 -0
- data/lib/action_view/helpers/active_record_helper.rb +276 -0
- data/lib/action_view/helpers/asset_tag_helper.rb +599 -0
- data/lib/action_view/helpers/atom_feed_helper.rb +143 -0
- data/lib/action_view/helpers/benchmark_helper.rb +33 -0
- data/lib/action_view/helpers/cache_helper.rb +40 -0
- data/lib/action_view/helpers/capture_helper.rb +161 -0
- data/lib/action_view/helpers/date_helper.rb +711 -0
- data/lib/action_view/helpers/debug_helper.rb +31 -0
- data/lib/action_view/helpers/form_helper.rb +767 -0
- data/lib/action_view/helpers/form_options_helper.rb +458 -0
- data/lib/action_view/helpers/form_tag_helper.rb +458 -0
- data/lib/action_view/helpers/javascript_helper.rb +148 -0
- data/lib/action_view/helpers/number_helper.rb +186 -0
- data/lib/action_view/helpers/record_identification_helper.rb +20 -0
- data/lib/action_view/helpers/record_tag_helper.rb +59 -0
- data/lib/action_view/helpers/sanitize_helper.rb +229 -0
- data/lib/action_view/helpers/tag_helper.rb +134 -0
- data/lib/action_view/helpers/text_helper.rb +507 -0
- data/lib/action_view/helpers/url_helper.rb +573 -0
- data/lib/action_view/inline_template.rb +20 -0
- data/lib/action_view/partial_template.rb +70 -0
- data/lib/action_view/partials.rb +158 -0
- data/lib/action_view/template.rb +125 -0
- data/lib/action_view/template_error.rb +110 -0
- data/lib/action_view/template_finder.rb +176 -0
- data/lib/action_view/template_handler.rb +34 -0
- data/lib/action_view/template_handlers/builder.rb +27 -0
- data/lib/action_view/template_handlers/compilable.rb +128 -0
- data/lib/action_view/template_handlers/erb.rb +56 -0
- data/lib/action_view/test_case.rb +58 -0
- data/lib/actionpack.rb +1 -0
- data/test/abstract_unit.rb +36 -0
- data/test/active_record_unit.rb +105 -0
- data/test/activerecord/active_record_store_test.rb +141 -0
- data/test/activerecord/render_partial_with_record_identification_test.rb +191 -0
- data/test/adv_attr_test.rb +20 -0
- data/test/controller/action_pack_assertions_test.rb +543 -0
- data/test/controller/addresses_render_test.rb +43 -0
- data/test/controller/assert_select_test.rb +331 -0
- data/test/controller/base_test.rb +219 -0
- data/test/controller/benchmark_test.rb +32 -0
- data/test/controller/caching_test.rb +581 -0
- data/test/controller/capture_test.rb +89 -0
- data/test/controller/cgi_test.rb +116 -0
- data/test/controller/components_test.rb +140 -0
- data/test/controller/content_type_test.rb +139 -0
- data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
- data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
- data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
- data/test/controller/cookie_test.rb +146 -0
- data/test/controller/custom_handler_test.rb +45 -0
- data/test/controller/deprecation/deprecated_base_methods_test.rb +37 -0
- data/test/controller/dispatcher_test.rb +105 -0
- data/test/controller/fake_controllers.rb +33 -0
- data/test/controller/fake_models.rb +11 -0
- data/test/controller/filter_params_test.rb +49 -0
- data/test/controller/filters_test.rb +881 -0
- data/test/controller/flash_test.rb +146 -0
- data/test/controller/header_test.rb +14 -0
- data/test/controller/helper_test.rb +210 -0
- data/test/controller/html-scanner/cdata_node_test.rb +15 -0
- data/test/controller/html-scanner/document_test.rb +148 -0
- data/test/controller/html-scanner/node_test.rb +89 -0
- data/test/controller/html-scanner/sanitizer_test.rb +269 -0
- data/test/controller/html-scanner/tag_node_test.rb +238 -0
- data/test/controller/html-scanner/text_node_test.rb +50 -0
- data/test/controller/html-scanner/tokenizer_test.rb +131 -0
- data/test/controller/http_authentication_test.rb +54 -0
- data/test/controller/integration_test.rb +252 -0
- data/test/controller/integration_upload_test.rb +43 -0
- data/test/controller/layout_test.rb +255 -0
- data/test/controller/mime_responds_test.rb +514 -0
- data/test/controller/mime_type_test.rb +84 -0
- data/test/controller/new_render_test.rb +843 -0
- data/test/controller/polymorphic_routes_test.rb +174 -0
- data/test/controller/record_identifier_test.rb +139 -0
- data/test/controller/redirect_test.rb +289 -0
- data/test/controller/render_test.rb +484 -0
- data/test/controller/request_forgery_protection_test.rb +305 -0
- data/test/controller/request_test.rb +928 -0
- data/test/controller/rescue_test.rb +517 -0
- data/test/controller/resources_test.rb +873 -0
- data/test/controller/routing_test.rb +2464 -0
- data/test/controller/selector_test.rb +628 -0
- data/test/controller/send_file_test.rb +138 -0
- data/test/controller/session/cookie_store_test.rb +258 -0
- data/test/controller/session/mem_cache_store_test.rb +181 -0
- data/test/controller/session_fixation_test.rb +89 -0
- data/test/controller/session_management_test.rb +178 -0
- data/test/controller/test_test.rb +695 -0
- data/test/controller/url_rewriter_test.rb +310 -0
- data/test/controller/verification_test.rb +270 -0
- data/test/controller/view_paths_test.rb +140 -0
- data/test/controller/webservice_test.rb +229 -0
- data/test/fixtures/addresses/list.erb +1 -0
- data/test/fixtures/bad_customers/_bad_customer.html.erb +1 -0
- data/test/fixtures/companies.yml +24 -0
- data/test/fixtures/company.rb +10 -0
- data/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml +1 -0
- data/test/fixtures/content_type/render_default_for_js.js.erb +1 -0
- data/test/fixtures/content_type/render_default_for_rhtml.rhtml +1 -0
- data/test/fixtures/content_type/render_default_for_rxml.rxml +1 -0
- data/test/fixtures/customers/_customer.html.erb +1 -0
- data/test/fixtures/db_definitions/sqlite.sql +49 -0
- data/test/fixtures/developer.rb +9 -0
- data/test/fixtures/developers.yml +21 -0
- data/test/fixtures/developers_projects.yml +13 -0
- data/test/fixtures/fun/games/hello_world.erb +1 -0
- data/test/fixtures/functional_caching/_partial.erb +3 -0
- data/test/fixtures/functional_caching/fragment_cached.html.erb +2 -0
- data/test/fixtures/functional_caching/html_fragment_cached_with_partial.html.erb +1 -0
- data/test/fixtures/functional_caching/js_fragment_cached_with_partial.js.rjs +1 -0
- data/test/fixtures/good_customers/_good_customer.html.erb +1 -0
- data/test/fixtures/helpers/abc_helper.rb +5 -0
- data/test/fixtures/helpers/fun/games_helper.rb +3 -0
- data/test/fixtures/helpers/fun/pdf_helper.rb +3 -0
- data/test/fixtures/layout_tests/alt/hello.rhtml +1 -0
- data/test/fixtures/layout_tests/layouts/controller_name_space/nested.rhtml +1 -0
- data/test/fixtures/layout_tests/layouts/item.rhtml +1 -0
- data/test/fixtures/layout_tests/layouts/layout_test.rhtml +1 -0
- data/test/fixtures/layout_tests/layouts/multiple_extensions.html.erb +1 -0
- data/test/fixtures/layout_tests/layouts/third_party_template_library.mab +1 -0
- data/test/fixtures/layout_tests/views/hello.rhtml +1 -0
- data/test/fixtures/layouts/block_with_layout.erb +3 -0
- data/test/fixtures/layouts/builder.builder +3 -0
- data/test/fixtures/layouts/partial_with_layout.erb +3 -0
- data/test/fixtures/layouts/standard.erb +1 -0
- data/test/fixtures/layouts/talk_from_action.erb +2 -0
- data/test/fixtures/layouts/yield.erb +2 -0
- data/test/fixtures/mascot.rb +3 -0
- data/test/fixtures/mascots.yml +4 -0
- data/test/fixtures/mascots/_mascot.html.erb +1 -0
- data/test/fixtures/multipart/binary_file +0 -0
- data/test/fixtures/multipart/boundary_problem_file +10 -0
- data/test/fixtures/multipart/bracketed_param +5 -0
- data/test/fixtures/multipart/large_text_file +10 -0
- data/test/fixtures/multipart/mixed_files +0 -0
- data/test/fixtures/multipart/mona_lisa.jpg +0 -0
- data/test/fixtures/multipart/single_parameter +5 -0
- data/test/fixtures/multipart/text_file +10 -0
- data/test/fixtures/override/test/hello_world.erb +1 -0
- data/test/fixtures/override2/layouts/test/sub.erb +1 -0
- data/test/fixtures/post_test/layouts/post.html.erb +1 -0
- data/test/fixtures/post_test/layouts/super_post.iphone.erb +1 -0
- data/test/fixtures/post_test/post/index.html.erb +1 -0
- data/test/fixtures/post_test/post/index.iphone.erb +1 -0
- data/test/fixtures/post_test/super_post/index.html.erb +1 -0
- data/test/fixtures/post_test/super_post/index.iphone.erb +1 -0
- data/test/fixtures/project.rb +3 -0
- data/test/fixtures/projects.yml +7 -0
- data/test/fixtures/public/404.html +1 -0
- data/test/fixtures/public/500.html +1 -0
- data/test/fixtures/public/images/rails.png +0 -0
- data/test/fixtures/public/javascripts/application.js +1 -0
- data/test/fixtures/public/javascripts/bank.js +1 -0
- data/test/fixtures/public/javascripts/robber.js +1 -0
- data/test/fixtures/public/javascripts/version.1.0.js +1 -0
- data/test/fixtures/public/stylesheets/bank.css +1 -0
- data/test/fixtures/public/stylesheets/robber.css +1 -0
- data/test/fixtures/public/stylesheets/version.1.0.css +1 -0
- data/test/fixtures/replies.yml +15 -0
- data/test/fixtures/reply.rb +7 -0
- data/test/fixtures/respond_to/all_types_with_layout.html.erb +1 -0
- data/test/fixtures/respond_to/custom_constant_handling_without_block.mobile.erb +1 -0
- data/test/fixtures/respond_to/iphone_with_html_response_type.html.erb +1 -0
- data/test/fixtures/respond_to/iphone_with_html_response_type.iphone.erb +1 -0
- data/test/fixtures/respond_to/layouts/missing.html.erb +1 -0
- data/test/fixtures/respond_to/layouts/standard.html.erb +1 -0
- data/test/fixtures/respond_to/layouts/standard.iphone.erb +1 -0
- data/test/fixtures/respond_to/using_defaults.html.erb +1 -0
- data/test/fixtures/respond_to/using_defaults.js.rjs +1 -0
- data/test/fixtures/respond_to/using_defaults.xml.builder +1 -0
- data/test/fixtures/respond_to/using_defaults_with_type_list.html.erb +1 -0
- data/test/fixtures/respond_to/using_defaults_with_type_list.js.rjs +1 -0
- data/test/fixtures/respond_to/using_defaults_with_type_list.xml.builder +1 -0
- data/test/fixtures/scope/test/modgreet.erb +1 -0
- data/test/fixtures/shared.html.erb +1 -0
- data/test/fixtures/symlink_parent/symlinked_layout.erb +5 -0
- data/test/fixtures/test/_customer.erb +1 -0
- data/test/fixtures/test/_customer_counter.erb +1 -0
- data/test/fixtures/test/_customer_greeting.erb +1 -0
- data/test/fixtures/test/_form.erb +1 -0
- data/test/fixtures/test/_hash_greeting.erb +1 -0
- data/test/fixtures/test/_hash_object.erb +2 -0
- data/test/fixtures/test/_hello.builder +1 -0
- data/test/fixtures/test/_labelling_form.erb +1 -0
- data/test/fixtures/test/_layout_for_partial.html.erb +3 -0
- data/test/fixtures/test/_partial.erb +1 -0
- data/test/fixtures/test/_partial.html.erb +1 -0
- data/test/fixtures/test/_partial.js.erb +1 -0
- data/test/fixtures/test/_partial_for_use_in_layout.html.erb +1 -0
- data/test/fixtures/test/_partial_only.erb +1 -0
- data/test/fixtures/test/_person.erb +2 -0
- data/test/fixtures/test/_raise.html.erb +1 -0
- data/test/fixtures/test/action_talk_to_layout.erb +2 -0
- data/test/fixtures/test/block_content_for.erb +2 -0
- data/test/fixtures/test/calling_partial_with_layout.html.erb +1 -0
- data/test/fixtures/test/capturing.erb +4 -0
- data/test/fixtures/test/content_for.erb +2 -0
- data/test/fixtures/test/content_for_concatenated.erb +3 -0
- data/test/fixtures/test/content_for_with_parameter.erb +2 -0
- data/test/fixtures/test/delete_with_js.rjs +2 -0
- data/test/fixtures/test/dot.directory/render_file_with_ivar.erb +1 -0
- data/test/fixtures/test/enum_rjs_test.rjs +6 -0
- data/test/fixtures/test/erb_content_for.erb +2 -0
- data/test/fixtures/test/formatted_html_erb.html.erb +1 -0
- data/test/fixtures/test/formatted_xml_erb.builder +1 -0
- data/test/fixtures/test/formatted_xml_erb.html.erb +1 -0
- data/test/fixtures/test/formatted_xml_erb.xml.erb +1 -0
- data/test/fixtures/test/greeting.erb +1 -0
- data/test/fixtures/test/greeting.js.rjs +1 -0
- data/test/fixtures/test/hello.builder +4 -0
- data/test/fixtures/test/hello_world.erb +1 -0
- data/test/fixtures/test/hello_world_container.builder +3 -0
- data/test/fixtures/test/hello_world_from_rxml.builder +4 -0
- data/test/fixtures/test/hello_world_with_layout_false.erb +1 -0
- data/test/fixtures/test/hello_xml_world.builder +11 -0
- data/test/fixtures/test/list.erb +1 -0
- data/test/fixtures/test/non_erb_block_content_for.builder +4 -0
- data/test/fixtures/test/potential_conflicts.erb +4 -0
- data/test/fixtures/test/render_file_from_template.html.erb +1 -0
- data/test/fixtures/test/render_file_with_ivar.erb +1 -0
- data/test/fixtures/test/render_file_with_locals.erb +1 -0
- data/test/fixtures/test/render_to_string_test.erb +1 -0
- data/test/fixtures/test/update_element_with_capture.erb +9 -0
- data/test/fixtures/test/using_layout_around_block.html.erb +1 -0
- data/test/fixtures/topic.rb +3 -0
- data/test/fixtures/topics.yml +22 -0
- data/test/fixtures/topics/_topic.html.erb +1 -0
- data/test/template/active_record_helper_test.rb +268 -0
- data/test/template/asset_tag_helper_test.rb +514 -0
- data/test/template/atom_feed_helper_test.rb +179 -0
- data/test/template/benchmark_helper_test.rb +60 -0
- data/test/template/date_helper_test.rb +1791 -0
- data/test/template/deprecated_erb_variable_test.rb +9 -0
- data/test/template/erb_util_test.rb +24 -0
- data/test/template/form_helper_test.rb +885 -0
- data/test/template/form_options_helper_test.rb +1333 -0
- data/test/template/form_tag_helper_test.rb +272 -0
- data/test/template/javascript_helper_test.rb +73 -0
- data/test/template/number_helper_test.rb +97 -0
- data/test/template/record_tag_helper_test.rb +54 -0
- data/test/template/sanitize_helper_test.rb +48 -0
- data/test/template/tag_helper_test.rb +77 -0
- data/test/template/template_finder_test.rb +73 -0
- data/test/template/template_object_test.rb +95 -0
- data/test/template/test_test.rb +56 -0
- data/test/template/text_helper_test.rb +367 -0
- data/test/template/url_helper_test.rb +544 -0
- data/test/testing_sandbox.rb +15 -0
- metadata +469 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
require 'cgi'
|
|
2
|
+
require 'action_view/helpers/form_helper'
|
|
3
|
+
|
|
4
|
+
module ActionView
|
|
5
|
+
class Base
|
|
6
|
+
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }
|
|
7
|
+
cattr_accessor :field_error_proc
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module Helpers
|
|
11
|
+
# The Active Record Helper makes it easier to create forms for records kept in instance variables. The most far-reaching is the +form+
|
|
12
|
+
# method that creates a complete form for all the basic content types of the record (not associations or aggregations, though). This
|
|
13
|
+
# is a great way of making the record quickly available for editing, but likely to prove lackluster for a complicated real-world form.
|
|
14
|
+
# In that case, it's better to use the +input+ method and the specialized +form+ methods in link:classes/ActionView/Helpers/FormHelper.html
|
|
15
|
+
module ActiveRecordHelper
|
|
16
|
+
# Returns a default input tag for the type of object returned by the method. For example, if <tt>@post</tt>
|
|
17
|
+
# has an attribute +title+ mapped to a +VARCHAR+ column that holds "Hello World":
|
|
18
|
+
#
|
|
19
|
+
# input("post", "title")
|
|
20
|
+
# # => <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
|
|
21
|
+
def input(record_name, method, options = {})
|
|
22
|
+
InstanceTag.new(record_name, method, self).to_tag(options)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Returns an entire form with all needed input tags for a specified Active Record object. For example, if <tt>@post</tt>
|
|
26
|
+
# has attributes named +title+ of type +VARCHAR+ and +body+ of type +TEXT+ then
|
|
27
|
+
#
|
|
28
|
+
# form("post")
|
|
29
|
+
#
|
|
30
|
+
# would yield a form like the following (modulus formatting):
|
|
31
|
+
#
|
|
32
|
+
# <form action='/posts/create' method='post'>
|
|
33
|
+
# <p>
|
|
34
|
+
# <label for="post_title">Title</label><br />
|
|
35
|
+
# <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
|
|
36
|
+
# </p>
|
|
37
|
+
# <p>
|
|
38
|
+
# <label for="post_body">Body</label><br />
|
|
39
|
+
# <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
|
|
40
|
+
# </p>
|
|
41
|
+
# <input name="commit" type="submit" value="Create" />
|
|
42
|
+
# </form>
|
|
43
|
+
#
|
|
44
|
+
# It's possible to specialize the form builder by using a different action name and by supplying another
|
|
45
|
+
# block renderer. For example, if <tt>@entry</tt> has an attribute +message+ of type +VARCHAR+ then
|
|
46
|
+
#
|
|
47
|
+
# form("entry",
|
|
48
|
+
# :action => "sign",
|
|
49
|
+
# :input_block => Proc.new { |record, column|
|
|
50
|
+
# "#{column.human_name}: #{input(record, column.name)}<br />"
|
|
51
|
+
# })
|
|
52
|
+
#
|
|
53
|
+
# would yield a form like the following (modulus formatting):
|
|
54
|
+
#
|
|
55
|
+
# <form action="/entries/sign" method="post">
|
|
56
|
+
# Message:
|
|
57
|
+
# <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
|
|
58
|
+
# <input name="commit" type="submit" value="Sign" />
|
|
59
|
+
# </form>
|
|
60
|
+
#
|
|
61
|
+
# It's also possible to add additional content to the form by giving it a block, such as:
|
|
62
|
+
#
|
|
63
|
+
# form("entry", :action => "sign") do |form|
|
|
64
|
+
# form << content_tag("b", "Department")
|
|
65
|
+
# form << collection_select("department", "id", @departments, "id", "name")
|
|
66
|
+
# end
|
|
67
|
+
#
|
|
68
|
+
# The following options are available:
|
|
69
|
+
#
|
|
70
|
+
# * <tt>:action</tt> - The action used when submitting the form (default: +create+ if a new record, otherwise +update+).
|
|
71
|
+
# * <tt>:input_block</tt> - Specialize the output using a different block, see above.
|
|
72
|
+
# * <tt>:method</tt> - The method used when submitting the form (default: +post+).
|
|
73
|
+
# * <tt>:multipart</tt> - Whether to change the enctype of the form to "multipart/form-data", used when uploading a file (default: +false+).
|
|
74
|
+
# * <tt>:submit_value</tt> - The text of the submit button (default: "Create" if a new record, otherwise "Update").
|
|
75
|
+
def form(record_name, options = {})
|
|
76
|
+
record = instance_variable_get("@#{record_name}")
|
|
77
|
+
|
|
78
|
+
options = options.symbolize_keys
|
|
79
|
+
options[:action] ||= record.new_record? ? "create" : "update"
|
|
80
|
+
action = url_for(:action => options[:action], :id => record)
|
|
81
|
+
|
|
82
|
+
submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize
|
|
83
|
+
|
|
84
|
+
contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil)
|
|
85
|
+
contents << hidden_field(record_name, :id) unless record.new_record?
|
|
86
|
+
contents << all_input_tags(record, record_name, options)
|
|
87
|
+
yield contents if block_given?
|
|
88
|
+
contents << submit_tag(submit_value)
|
|
89
|
+
contents << '</form>'
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
|
|
93
|
+
# This error message is wrapped in a <tt>DIV</tt> tag, which can be extended to include a +prepend_text+ and/or +append_text+
|
|
94
|
+
# (to properly explain the error), and a +css_class+ to style it accordingly. +object+ should either be the name of an instance variable or
|
|
95
|
+
# the actual object. As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
|
|
96
|
+
#
|
|
97
|
+
# <%= error_message_on "post", "title" %>
|
|
98
|
+
# # => <div class="formError">can't be empty</div>
|
|
99
|
+
#
|
|
100
|
+
# <%= error_message_on @post, "title" %>
|
|
101
|
+
# # => <div class="formError">can't be empty</div>
|
|
102
|
+
#
|
|
103
|
+
# <%= error_message_on "post", "title", "Title simply ", " (or it won't work).", "inputError" %>
|
|
104
|
+
# # => <div class="inputError">Title simply can't be empty (or it won't work).</div>
|
|
105
|
+
def error_message_on(object, method, prepend_text = "", append_text = "", css_class = "formError")
|
|
106
|
+
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
|
|
107
|
+
(errors = obj.errors.on(method))
|
|
108
|
+
content_tag("div", "#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}", :class => css_class)
|
|
109
|
+
else
|
|
110
|
+
''
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Returns a string with a <tt>DIV</tt> containing all of the error messages for the objects located as instance variables by the names
|
|
115
|
+
# given. If more than one object is specified, the errors for the objects are displayed in the order that the object names are
|
|
116
|
+
# provided.
|
|
117
|
+
#
|
|
118
|
+
# This <tt>DIV</tt> can be tailored by the following options:
|
|
119
|
+
#
|
|
120
|
+
# * <tt>:header_tag</tt> - Used for the header of the error div (default: "h2").
|
|
121
|
+
# * <tt>:id</tt> - The id of the error div (default: "errorExplanation").
|
|
122
|
+
# * <tt>:class</tt> - The class of the error div (default: "errorExplanation").
|
|
123
|
+
# * <tt>:object</tt> - The object (or array of objects) for which to display errors,
|
|
124
|
+
# if you need to escape the instance variable convention.
|
|
125
|
+
# * <tt>:object_name</tt> - The object name to use in the header, or any text that you prefer.
|
|
126
|
+
# If <tt>:object_name</tt> is not set, the name of the first object will be used.
|
|
127
|
+
# * <tt>:header_message</tt> - The message in the header of the error div. Pass +nil+
|
|
128
|
+
# or an empty string to avoid the header message altogether. (Default: "X errors
|
|
129
|
+
# prohibited this object from being saved").
|
|
130
|
+
# * <tt>:message</tt> - The explanation message after the header message and before
|
|
131
|
+
# the error list. Pass +nil+ or an empty string to avoid the explanation message
|
|
132
|
+
# altogether. (Default: "There were problems with the following fields:").
|
|
133
|
+
#
|
|
134
|
+
# To specify the display for one object, you simply provide its name as a parameter.
|
|
135
|
+
# For example, for the <tt>@user</tt> model:
|
|
136
|
+
#
|
|
137
|
+
# error_messages_for 'user'
|
|
138
|
+
#
|
|
139
|
+
# To specify more than one object, you simply list them; optionally, you can add an extra <tt>:object_name</tt> parameter, which
|
|
140
|
+
# will be the name used in the header message:
|
|
141
|
+
#
|
|
142
|
+
# error_messages_for 'user_common', 'user', :object_name => 'user'
|
|
143
|
+
#
|
|
144
|
+
# If the objects cannot be located as instance variables, you can add an extra <tt>:object</tt> paremeter which gives the actual
|
|
145
|
+
# object (or array of objects to use):
|
|
146
|
+
#
|
|
147
|
+
# error_messages_for 'user', :object => @question.user
|
|
148
|
+
#
|
|
149
|
+
# NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what
|
|
150
|
+
# you need is significantly different from the default presentation, it makes plenty of sense to access the <tt>object.errors</tt>
|
|
151
|
+
# instance yourself and set it up. View the source of this method to see how easy it is.
|
|
152
|
+
def error_messages_for(*params)
|
|
153
|
+
options = params.extract_options!.symbolize_keys
|
|
154
|
+
if object = options.delete(:object)
|
|
155
|
+
objects = [object].flatten
|
|
156
|
+
else
|
|
157
|
+
objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
|
|
158
|
+
end
|
|
159
|
+
count = objects.inject(0) {|sum, object| sum + object.errors.count }
|
|
160
|
+
unless count.zero?
|
|
161
|
+
html = {}
|
|
162
|
+
[:id, :class].each do |key|
|
|
163
|
+
if options.include?(key)
|
|
164
|
+
value = options[key]
|
|
165
|
+
html[key] = value unless value.blank?
|
|
166
|
+
else
|
|
167
|
+
html[key] = 'errorExplanation'
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
options[:object_name] ||= params.first
|
|
171
|
+
options[:header_message] = "#{pluralize(count, 'error')} prohibited this #{options[:object_name].to_s.gsub('_', ' ')} from being saved" unless options.include?(:header_message)
|
|
172
|
+
options[:message] ||= 'There were problems with the following fields:' unless options.include?(:message)
|
|
173
|
+
error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }.join
|
|
174
|
+
|
|
175
|
+
contents = ''
|
|
176
|
+
contents << content_tag(options[:header_tag] || :h2, options[:header_message]) unless options[:header_message].blank?
|
|
177
|
+
contents << content_tag(:p, options[:message]) unless options[:message].blank?
|
|
178
|
+
contents << content_tag(:ul, error_messages)
|
|
179
|
+
|
|
180
|
+
content_tag(:div, contents, html)
|
|
181
|
+
else
|
|
182
|
+
''
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
private
|
|
187
|
+
def all_input_tags(record, record_name, options)
|
|
188
|
+
input_block = options[:input_block] || default_input_block
|
|
189
|
+
record.class.content_columns.collect{ |column| input_block.call(record_name, column) }.join("\n")
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def default_input_block
|
|
193
|
+
Proc.new { |record, column| %(<p><label for="#{record}_#{column.name}">#{column.human_name}</label><br />#{input(record, column.name)}</p>) }
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
class InstanceTag #:nodoc:
|
|
198
|
+
def to_tag(options = {})
|
|
199
|
+
case column_type
|
|
200
|
+
when :string
|
|
201
|
+
field_type = @method_name.include?("password") ? "password" : "text"
|
|
202
|
+
to_input_field_tag(field_type, options)
|
|
203
|
+
when :text
|
|
204
|
+
to_text_area_tag(options)
|
|
205
|
+
when :integer, :float, :decimal
|
|
206
|
+
to_input_field_tag("text", options)
|
|
207
|
+
when :date
|
|
208
|
+
to_date_select_tag(options)
|
|
209
|
+
when :datetime, :timestamp
|
|
210
|
+
to_datetime_select_tag(options)
|
|
211
|
+
when :time
|
|
212
|
+
to_time_select_tag(options)
|
|
213
|
+
when :boolean
|
|
214
|
+
to_boolean_select_tag(options)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
alias_method :tag_without_error_wrapping, :tag
|
|
219
|
+
def tag(name, options)
|
|
220
|
+
if object.respond_to?("errors") && object.errors.respond_to?("on")
|
|
221
|
+
error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name))
|
|
222
|
+
else
|
|
223
|
+
tag_without_error_wrapping(name, options)
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
alias_method :content_tag_without_error_wrapping, :content_tag
|
|
228
|
+
def content_tag(name, value, options)
|
|
229
|
+
if object.respond_to?("errors") && object.errors.respond_to?("on")
|
|
230
|
+
error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name))
|
|
231
|
+
else
|
|
232
|
+
content_tag_without_error_wrapping(name, value, options)
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
alias_method :to_date_select_tag_without_error_wrapping, :to_date_select_tag
|
|
237
|
+
def to_date_select_tag(options = {}, html_options = {})
|
|
238
|
+
if object.respond_to?("errors") && object.errors.respond_to?("on")
|
|
239
|
+
error_wrapping(to_date_select_tag_without_error_wrapping(options, html_options), object.errors.on(@method_name))
|
|
240
|
+
else
|
|
241
|
+
to_date_select_tag_without_error_wrapping(options, html_options)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
alias_method :to_datetime_select_tag_without_error_wrapping, :to_datetime_select_tag
|
|
246
|
+
def to_datetime_select_tag(options = {}, html_options = {})
|
|
247
|
+
if object.respond_to?("errors") && object.errors.respond_to?("on")
|
|
248
|
+
error_wrapping(to_datetime_select_tag_without_error_wrapping(options, html_options), object.errors.on(@method_name))
|
|
249
|
+
else
|
|
250
|
+
to_datetime_select_tag_without_error_wrapping(options, html_options)
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
alias_method :to_time_select_tag_without_error_wrapping, :to_time_select_tag
|
|
255
|
+
def to_time_select_tag(options = {}, html_options = {})
|
|
256
|
+
if object.respond_to?("errors") && object.errors.respond_to?("on")
|
|
257
|
+
error_wrapping(to_time_select_tag_without_error_wrapping(options, html_options), object.errors.on(@method_name))
|
|
258
|
+
else
|
|
259
|
+
to_time_select_tag_without_error_wrapping(options, html_options)
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def error_wrapping(html_tag, has_error)
|
|
264
|
+
has_error ? Base.field_error_proc.call(html_tag, self) : html_tag
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def error_message
|
|
268
|
+
object.errors.on(@method_name)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def column_type
|
|
272
|
+
object.send("column_for_attribute", @method_name).type
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
require 'cgi'
|
|
2
|
+
require 'action_view/helpers/url_helper'
|
|
3
|
+
require 'action_view/helpers/tag_helper'
|
|
4
|
+
|
|
5
|
+
module ActionView
|
|
6
|
+
module Helpers #:nodoc:
|
|
7
|
+
# This module provides methods for generating HTML that links views to assets such
|
|
8
|
+
# as images, javascripts, stylesheets, and feeds. These methods do not verify
|
|
9
|
+
# the assets exist before linking to them.
|
|
10
|
+
#
|
|
11
|
+
# === Using asset hosts
|
|
12
|
+
# By default, Rails links to these assets on the current host in the public
|
|
13
|
+
# folder, but you can direct Rails to link to assets from a dedicated assets server by
|
|
14
|
+
# setting ActionController::Base.asset_host in your <tt>config/environment.rb</tt>. For example,
|
|
15
|
+
# let's say your asset host is <tt>assets.example.com</tt>.
|
|
16
|
+
#
|
|
17
|
+
# ActionController::Base.asset_host = "assets.example.com"
|
|
18
|
+
# image_tag("rails.png")
|
|
19
|
+
# => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
|
|
20
|
+
# stylesheet_link_tag("application")
|
|
21
|
+
# => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
|
|
22
|
+
#
|
|
23
|
+
# This is useful since browsers typically open at most two connections to a single host,
|
|
24
|
+
# which means your assets often wait in single file for their turn to load. You can
|
|
25
|
+
# alleviate this by using a <tt>%d</tt> wildcard in <tt>asset_host</tt> (for example, "assets%d.example.com")
|
|
26
|
+
# to automatically distribute asset requests among four hosts (e.g., "assets0.example.com" through "assets3.example.com")
|
|
27
|
+
# so browsers will open eight connections rather than two.
|
|
28
|
+
#
|
|
29
|
+
# image_tag("rails.png")
|
|
30
|
+
# => <img src="http://assets0.example.com/images/rails.png" alt="Rails" />
|
|
31
|
+
# stylesheet_link_tag("application")
|
|
32
|
+
# => <link href="http://assets3.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
|
|
33
|
+
#
|
|
34
|
+
# To do this, you can either setup 4 actual hosts, or you can use wildcard DNS to CNAME
|
|
35
|
+
# the wildcard to a single asset host. You can read more about setting up your DNS CNAME records from
|
|
36
|
+
# your ISP.
|
|
37
|
+
#
|
|
38
|
+
# Note: This is purely a browser performance optimization and is not meant
|
|
39
|
+
# for server load balancing. See http://www.die.net/musings/page_load_time/
|
|
40
|
+
# for background.
|
|
41
|
+
#
|
|
42
|
+
# Alternatively, you can exert more control over the asset host by setting <tt>asset_host</tt> to a proc
|
|
43
|
+
# that takes a single source argument. This is useful if you are unable to setup 4 actual hosts or have
|
|
44
|
+
# fewer/more than 4 hosts. The example proc below generates http://assets1.example.com and
|
|
45
|
+
# http://assets2.example.com randomly.
|
|
46
|
+
#
|
|
47
|
+
# ActionController::Base.asset_host = Proc.new { |source| "http://assets#{rand(2) + 1}.example.com" }
|
|
48
|
+
# image_tag("rails.png")
|
|
49
|
+
# => <img src="http://assets2.example.com/images/rails.png" alt="Rails" />
|
|
50
|
+
# stylesheet_link_tag("application")
|
|
51
|
+
# => <link href="http://assets1.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
|
|
52
|
+
#
|
|
53
|
+
# The proc takes a <tt>source</tt> parameter (which is the path of the source asset) and an optional
|
|
54
|
+
# <tt>request</tt> parameter (which is an entire instance of an <tt>ActionController::AbstractRequest</tt>
|
|
55
|
+
# subclass). This can be used to generate a particular asset host depending on the asset path and the particular
|
|
56
|
+
# request.
|
|
57
|
+
#
|
|
58
|
+
# ActionController::Base.asset_host = Proc.new { |source|
|
|
59
|
+
# if source.starts_with?('/images')
|
|
60
|
+
# "http://images.example.com"
|
|
61
|
+
# else
|
|
62
|
+
# "http://assets.example.com"
|
|
63
|
+
# end
|
|
64
|
+
# }
|
|
65
|
+
# image_tag("rails.png")
|
|
66
|
+
# => <img src="http://images.example.com/images/rails.png" alt="Rails" />
|
|
67
|
+
# stylesheet_link_tag("application")
|
|
68
|
+
# => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
|
|
69
|
+
#
|
|
70
|
+
# The optional <tt>request</tt> parameter to the proc is useful in particular for serving assets from an
|
|
71
|
+
# SSL-protected page. The example proc below disables asset hosting for HTTPS connections, while still sending
|
|
72
|
+
# assets for plain HTTP requests from asset hosts. This is useful for avoiding mixed media warnings when serving
|
|
73
|
+
# non-HTTP assets from HTTPS web pages when you don't have an SSL certificate for each of the asset hosts.
|
|
74
|
+
#
|
|
75
|
+
# ActionController::Base.asset_host = Proc.new { |source, request|
|
|
76
|
+
# if request.ssl?
|
|
77
|
+
# "#{request.protocol}#{request.host_with_port}"
|
|
78
|
+
# else
|
|
79
|
+
# "#{request.protocol}assets.example.com"
|
|
80
|
+
# end
|
|
81
|
+
# }
|
|
82
|
+
#
|
|
83
|
+
# === Using asset timestamps
|
|
84
|
+
#
|
|
85
|
+
# By default, Rails will append all asset paths with that asset's timestamp. This allows you to set a cache-expiration date for the
|
|
86
|
+
# asset far into the future, but still be able to instantly invalidate it by simply updating the file (and hence updating the timestamp,
|
|
87
|
+
# which then updates the URL as the timestamp is part of that, which in turn busts the cache).
|
|
88
|
+
#
|
|
89
|
+
# It's the responsibility of the web server you use to set the far-future expiration date on cache assets that you need to take
|
|
90
|
+
# advantage of this feature. Here's an example for Apache:
|
|
91
|
+
#
|
|
92
|
+
# # Asset Expiration
|
|
93
|
+
# ExpiresActive On
|
|
94
|
+
# <FilesMatch "\.(ico|gif|jpe?g|png|js|css)$">
|
|
95
|
+
# ExpiresDefault "access plus 1 year"
|
|
96
|
+
# </FilesMatch>
|
|
97
|
+
#
|
|
98
|
+
# Also note that in order for this to work, all your application servers must return the same timestamps. This means that they must
|
|
99
|
+
# have their clocks synchronized. If one of them drift out of sync, you'll see different timestamps at random and the cache won't
|
|
100
|
+
# work. Which means that the browser will request the same assets over and over again even thought they didn't change. You can use
|
|
101
|
+
# something like Live HTTP Headers for Firefox to verify that the cache is indeed working (and that the assets are not being
|
|
102
|
+
# requested over and over).
|
|
103
|
+
module AssetTagHelper
|
|
104
|
+
ASSETS_DIR = defined?(Rails.public_path) ? Rails.public_path : "public"
|
|
105
|
+
JAVASCRIPTS_DIR = "#{ASSETS_DIR}/javascripts"
|
|
106
|
+
STYLESHEETS_DIR = "#{ASSETS_DIR}/stylesheets"
|
|
107
|
+
|
|
108
|
+
# Returns a link tag that browsers and news readers can use to auto-detect
|
|
109
|
+
# an RSS or ATOM feed. The +type+ can either be <tt>:rss</tt> (default) or
|
|
110
|
+
# <tt>:atom</tt>. Control the link options in url_for format using the
|
|
111
|
+
# +url_options+. You can modify the LINK tag itself in +tag_options+.
|
|
112
|
+
#
|
|
113
|
+
# ==== Options:
|
|
114
|
+
# * <tt>:rel</tt> - Specify the relation of this link, defaults to "alternate"
|
|
115
|
+
# * <tt>:type</tt> - Override the auto-generated mime type
|
|
116
|
+
# * <tt>:title</tt> - Specify the title of the link, defaults to the +type+
|
|
117
|
+
#
|
|
118
|
+
# ==== Examples
|
|
119
|
+
# auto_discovery_link_tag # =>
|
|
120
|
+
# <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/action" />
|
|
121
|
+
# auto_discovery_link_tag(:atom) # =>
|
|
122
|
+
# <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.currenthost.com/controller/action" />
|
|
123
|
+
# auto_discovery_link_tag(:rss, {:action => "feed"}) # =>
|
|
124
|
+
# <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/controller/feed" />
|
|
125
|
+
# auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"}) # =>
|
|
126
|
+
# <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.currenthost.com/controller/feed" />
|
|
127
|
+
# auto_discovery_link_tag(:rss, {:controller => "news", :action => "feed"}) # =>
|
|
128
|
+
# <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.currenthost.com/news/feed" />
|
|
129
|
+
# auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "Example RSS"}) # =>
|
|
130
|
+
# <link rel="alternate" type="application/rss+xml" title="Example RSS" href="http://www.example.com/feed" />
|
|
131
|
+
def auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
|
|
132
|
+
tag(
|
|
133
|
+
"link",
|
|
134
|
+
"rel" => tag_options[:rel] || "alternate",
|
|
135
|
+
"type" => tag_options[:type] || Mime::Type.lookup_by_extension(type.to_s).to_s,
|
|
136
|
+
"title" => tag_options[:title] || type.to_s.upcase,
|
|
137
|
+
"href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options
|
|
138
|
+
)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Computes the path to a javascript asset in the public javascripts directory.
|
|
142
|
+
# If the +source+ filename has no extension, .js will be appended.
|
|
143
|
+
# Full paths from the document root will be passed through.
|
|
144
|
+
# Used internally by javascript_include_tag to build the script path.
|
|
145
|
+
#
|
|
146
|
+
# ==== Examples
|
|
147
|
+
# javascript_path "xmlhr" # => /javascripts/xmlhr.js
|
|
148
|
+
# javascript_path "dir/xmlhr.js" # => /javascripts/dir/xmlhr.js
|
|
149
|
+
# javascript_path "/dir/xmlhr" # => /dir/xmlhr.js
|
|
150
|
+
# javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr.js
|
|
151
|
+
# javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js
|
|
152
|
+
def javascript_path(source)
|
|
153
|
+
compute_public_path(source, 'javascripts', 'js')
|
|
154
|
+
end
|
|
155
|
+
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
|
|
156
|
+
|
|
157
|
+
JAVASCRIPT_DEFAULT_SOURCES = [] unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES)
|
|
158
|
+
@@javascript_expansions = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup }
|
|
159
|
+
@@stylesheet_expansions = {}
|
|
160
|
+
|
|
161
|
+
# Returns an html script tag for each of the +sources+ provided. You
|
|
162
|
+
# can pass in the filename (.js extension is optional) of javascript files
|
|
163
|
+
# that exist in your public/javascripts directory for inclusion into the
|
|
164
|
+
# current page or you can pass the full path relative to your document
|
|
165
|
+
# root.
|
|
166
|
+
#
|
|
167
|
+
# ==== Examples
|
|
168
|
+
# javascript_include_tag "xmlhr" # =>
|
|
169
|
+
# <script type="text/javascript" src="/javascripts/xmlhr.js"></script>
|
|
170
|
+
#
|
|
171
|
+
# javascript_include_tag "xmlhr.js" # =>
|
|
172
|
+
# <script type="text/javascript" src="/javascripts/xmlhr.js"></script>
|
|
173
|
+
#
|
|
174
|
+
# javascript_include_tag "common.javascript", "/elsewhere/cools" # =>
|
|
175
|
+
# <script type="text/javascript" src="/javascripts/common.javascript"></script>
|
|
176
|
+
# <script type="text/javascript" src="/elsewhere/cools.js"></script>
|
|
177
|
+
#
|
|
178
|
+
# javascript_include_tag "http://www.railsapplication.com/xmlhr" # =>
|
|
179
|
+
# <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js"></script>
|
|
180
|
+
#
|
|
181
|
+
# javascript_include_tag "http://www.railsapplication.com/xmlhr.js" # =>
|
|
182
|
+
# <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js"></script>
|
|
183
|
+
#
|
|
184
|
+
# * = The application.js file is only referenced if it exists
|
|
185
|
+
#
|
|
186
|
+
# Though it's not really recommended practice, if you need to extend the default JavaScript set for any reason
|
|
187
|
+
# (e.g., you're going to be using a certain .js file in every action), then take a look at the register_javascript_include_default method.
|
|
188
|
+
#
|
|
189
|
+
# You can also include all javascripts in the javascripts directory using <tt>:all</tt> as the source:
|
|
190
|
+
#
|
|
191
|
+
# javascript_include_tag :all # =>
|
|
192
|
+
# <script type="text/javascript" src="/javascripts/application.js"></script>
|
|
193
|
+
# <script type="text/javascript" src="/javascripts/shop.js"></script>
|
|
194
|
+
# <script type="text/javascript" src="/javascripts/checkout.js"></script>
|
|
195
|
+
#
|
|
196
|
+
# Note that the default javascript files will be included first. So Prototype and Scriptaculous are available to
|
|
197
|
+
# all subsequently included files.
|
|
198
|
+
#
|
|
199
|
+
# == Caching multiple javascripts into one
|
|
200
|
+
#
|
|
201
|
+
# You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be
|
|
202
|
+
# compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching
|
|
203
|
+
# is set to <tt>true</tt> (which is the case by default for the Rails production environment, but not for the development
|
|
204
|
+
# environment).
|
|
205
|
+
#
|
|
206
|
+
# ==== Examples
|
|
207
|
+
# javascript_include_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
|
|
208
|
+
# ...
|
|
209
|
+
# <script type="text/javascript" src="/javascripts/application.js"></script>
|
|
210
|
+
# <script type="text/javascript" src="/javascripts/shop.js"></script>
|
|
211
|
+
# <script type="text/javascript" src="/javascripts/checkout.js"></script>
|
|
212
|
+
#
|
|
213
|
+
# javascript_include_tag :all, :cache => true # when ActionController::Base.perform_caching is true =>
|
|
214
|
+
# <script type="text/javascript" src="/javascripts/all.js"></script>
|
|
215
|
+
#
|
|
216
|
+
# javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is false =>
|
|
217
|
+
# <script type="text/javascript" src="/javascripts/prototype.js"></script>
|
|
218
|
+
# <script type="text/javascript" src="/javascripts/cart.js"></script>
|
|
219
|
+
# <script type="text/javascript" src="/javascripts/checkout.js"></script>
|
|
220
|
+
#
|
|
221
|
+
# javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is true =>
|
|
222
|
+
# <script type="text/javascript" src="/javascripts/shop.js"></script>
|
|
223
|
+
def javascript_include_tag(*sources)
|
|
224
|
+
options = sources.extract_options!.stringify_keys
|
|
225
|
+
cache = options.delete("cache")
|
|
226
|
+
|
|
227
|
+
if ActionController::Base.perform_caching && cache
|
|
228
|
+
joined_javascript_name = (cache == true ? "all" : cache) + ".js"
|
|
229
|
+
joined_javascript_path = File.join(JAVASCRIPTS_DIR, joined_javascript_name)
|
|
230
|
+
|
|
231
|
+
write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources))
|
|
232
|
+
javascript_src_tag(joined_javascript_name, options)
|
|
233
|
+
else
|
|
234
|
+
expand_javascript_sources(sources).collect { |source| javascript_src_tag(source, options) }.join("\n")
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Register one or more javascript files to be included when <tt>symbol</tt>
|
|
239
|
+
# is passed to <tt>javascript_include_tag</tt>. This method is typically intended
|
|
240
|
+
# to be called from plugin initialization to register javascript files
|
|
241
|
+
# that the plugin installed in <tt>public/javascripts</tt>.
|
|
242
|
+
#
|
|
243
|
+
# ActionView::Helpers::AssetTagHelper.register_javascript_expansion :monkey => ["head", "body", "tail"]
|
|
244
|
+
#
|
|
245
|
+
# javascript_include_tag :monkey # =>
|
|
246
|
+
# <script type="text/javascript" src="/javascripts/head.js"></script>
|
|
247
|
+
# <script type="text/javascript" src="/javascripts/body.js"></script>
|
|
248
|
+
# <script type="text/javascript" src="/javascripts/tail.js"></script>
|
|
249
|
+
def self.register_javascript_expansion(expansions)
|
|
250
|
+
@@javascript_expansions.merge!(expansions)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# Register one or more stylesheet files to be included when <tt>symbol</tt>
|
|
254
|
+
# is passed to <tt>stylesheet_link_tag</tt>. This method is typically intended
|
|
255
|
+
# to be called from plugin initialization to register stylesheet files
|
|
256
|
+
# that the plugin installed in <tt>public/stylesheets</tt>.
|
|
257
|
+
#
|
|
258
|
+
# ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion :monkey => ["head", "body", "tail"]
|
|
259
|
+
#
|
|
260
|
+
# stylesheet_link_tag :monkey # =>
|
|
261
|
+
# <link href="/stylesheets/head.css" media="screen" rel="stylesheet" type="text/css" />
|
|
262
|
+
# <link href="/stylesheets/body.css" media="screen" rel="stylesheet" type="text/css" />
|
|
263
|
+
# <link href="/stylesheets/tail.css" media="screen" rel="stylesheet" type="text/css" />
|
|
264
|
+
def self.register_stylesheet_expansion(expansions)
|
|
265
|
+
@@stylesheet_expansions.merge!(expansions)
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# Register one or more additional JavaScript files to be included when
|
|
269
|
+
# <tt>javascript_include_tag :defaults</tt> is called. This method is
|
|
270
|
+
# typically intended to be called from plugin initialization to register additional
|
|
271
|
+
# .js files that the plugin installed in <tt>public/javascripts</tt>.
|
|
272
|
+
def self.register_javascript_include_default(*sources)
|
|
273
|
+
@@javascript_expansions[:defaults].concat(sources)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def self.reset_javascript_include_default #:nodoc:
|
|
277
|
+
@@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Computes the path to a stylesheet asset in the public stylesheets directory.
|
|
281
|
+
# If the +source+ filename has no extension, <tt>.css</tt> will be appended.
|
|
282
|
+
# Full paths from the document root will be passed through.
|
|
283
|
+
# Used internally by +stylesheet_link_tag+ to build the stylesheet path.
|
|
284
|
+
#
|
|
285
|
+
# ==== Examples
|
|
286
|
+
# stylesheet_path "style" # => /stylesheets/style.css
|
|
287
|
+
# stylesheet_path "dir/style.css" # => /stylesheets/dir/style.css
|
|
288
|
+
# stylesheet_path "/dir/style.css" # => /dir/style.css
|
|
289
|
+
# stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style.css
|
|
290
|
+
# stylesheet_path "http://www.railsapplication.com/css/style.js" # => http://www.railsapplication.com/css/style.css
|
|
291
|
+
def stylesheet_path(source)
|
|
292
|
+
compute_public_path(source, 'stylesheets', 'css')
|
|
293
|
+
end
|
|
294
|
+
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
|
|
295
|
+
|
|
296
|
+
# Returns a stylesheet link tag for the sources specified as arguments. If
|
|
297
|
+
# you don't specify an extension, <tt>.css</tt> will be appended automatically.
|
|
298
|
+
# You can modify the link attributes by passing a hash as the last argument.
|
|
299
|
+
#
|
|
300
|
+
# ==== Examples
|
|
301
|
+
# stylesheet_link_tag "style" # =>
|
|
302
|
+
# <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
|
|
303
|
+
#
|
|
304
|
+
# stylesheet_link_tag "style.css" # =>
|
|
305
|
+
# <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
|
|
306
|
+
#
|
|
307
|
+
# stylesheet_link_tag "http://www.railsapplication.com/style.css" # =>
|
|
308
|
+
# <link href="http://www.railsapplication.com/style.css" media="screen" rel="stylesheet" type="text/css" />
|
|
309
|
+
#
|
|
310
|
+
# stylesheet_link_tag "style", :media => "all" # =>
|
|
311
|
+
# <link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />
|
|
312
|
+
#
|
|
313
|
+
# stylesheet_link_tag "style", :media => "print" # =>
|
|
314
|
+
# <link href="/stylesheets/style.css" media="print" rel="stylesheet" type="text/css" />
|
|
315
|
+
#
|
|
316
|
+
# stylesheet_link_tag "random.styles", "/css/stylish" # =>
|
|
317
|
+
# <link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" />
|
|
318
|
+
# <link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />
|
|
319
|
+
#
|
|
320
|
+
# You can also include all styles in the stylesheet directory using <tt>:all</tt> as the source:
|
|
321
|
+
#
|
|
322
|
+
# stylesheet_link_tag :all # =>
|
|
323
|
+
# <link href="/stylesheets/style1.css" media="screen" rel="stylesheet" type="text/css" />
|
|
324
|
+
# <link href="/stylesheets/styleB.css" media="screen" rel="stylesheet" type="text/css" />
|
|
325
|
+
# <link href="/stylesheets/styleX2.css" media="screen" rel="stylesheet" type="text/css" />
|
|
326
|
+
#
|
|
327
|
+
# == Caching multiple stylesheets into one
|
|
328
|
+
#
|
|
329
|
+
# You can also cache multiple stylesheets into one file, which requires less HTTP connections and can better be
|
|
330
|
+
# compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching
|
|
331
|
+
# is set to true (which is the case by default for the Rails production environment, but not for the development
|
|
332
|
+
# environment). Examples:
|
|
333
|
+
#
|
|
334
|
+
# ==== Examples
|
|
335
|
+
# stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
|
|
336
|
+
# <link href="/stylesheets/style1.css" media="screen" rel="stylesheet" type="text/css" />
|
|
337
|
+
# <link href="/stylesheets/styleB.css" media="screen" rel="stylesheet" type="text/css" />
|
|
338
|
+
# <link href="/stylesheets/styleX2.css" media="screen" rel="stylesheet" type="text/css" />
|
|
339
|
+
#
|
|
340
|
+
# stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is true =>
|
|
341
|
+
# <link href="/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />
|
|
342
|
+
#
|
|
343
|
+
# stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is false =>
|
|
344
|
+
# <link href="/stylesheets/shop.css" media="screen" rel="stylesheet" type="text/css" />
|
|
345
|
+
# <link href="/stylesheets/cart.css" media="screen" rel="stylesheet" type="text/css" />
|
|
346
|
+
# <link href="/stylesheets/checkout.css" media="screen" rel="stylesheet" type="text/css" />
|
|
347
|
+
#
|
|
348
|
+
# stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is true =>
|
|
349
|
+
# <link href="/stylesheets/payment.css" media="screen" rel="stylesheet" type="text/css" />
|
|
350
|
+
def stylesheet_link_tag(*sources)
|
|
351
|
+
options = sources.extract_options!.stringify_keys
|
|
352
|
+
cache = options.delete("cache")
|
|
353
|
+
|
|
354
|
+
if ActionController::Base.perform_caching && cache
|
|
355
|
+
joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
|
|
356
|
+
joined_stylesheet_path = File.join(STYLESHEETS_DIR, joined_stylesheet_name)
|
|
357
|
+
|
|
358
|
+
write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources))
|
|
359
|
+
stylesheet_tag(joined_stylesheet_name, options)
|
|
360
|
+
else
|
|
361
|
+
expand_stylesheet_sources(sources).collect { |source| stylesheet_tag(source, options) }.join("\n")
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
# Computes the path to an image asset in the public images directory.
|
|
366
|
+
# Full paths from the document root will be passed through.
|
|
367
|
+
# Used internally by +image_tag+ to build the image path.
|
|
368
|
+
#
|
|
369
|
+
# ==== Examples
|
|
370
|
+
# image_path("edit") # => /images/edit
|
|
371
|
+
# image_path("edit.png") # => /images/edit.png
|
|
372
|
+
# image_path("icons/edit.png") # => /images/icons/edit.png
|
|
373
|
+
# image_path("/icons/edit.png") # => /icons/edit.png
|
|
374
|
+
# image_path("http://www.railsapplication.com/img/edit.png") # => http://www.railsapplication.com/img/edit.png
|
|
375
|
+
def image_path(source)
|
|
376
|
+
compute_public_path(source, 'images')
|
|
377
|
+
end
|
|
378
|
+
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
|
|
379
|
+
|
|
380
|
+
# Returns an html image tag for the +source+. The +source+ can be a full
|
|
381
|
+
# path or a file that exists in your public images directory.
|
|
382
|
+
#
|
|
383
|
+
# ==== Options
|
|
384
|
+
# You can add HTML attributes using the +options+. The +options+ supports
|
|
385
|
+
# three additional keys for convenience and conformance:
|
|
386
|
+
#
|
|
387
|
+
# * <tt>:alt</tt> - If no alt text is given, the file name part of the
|
|
388
|
+
# +source+ is used (capitalized and without the extension)
|
|
389
|
+
# * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
|
|
390
|
+
# width="30" and height="45". <tt>:size</tt> will be ignored if the
|
|
391
|
+
# value is not in the correct format.
|
|
392
|
+
# * <tt>:mouseover</tt> - Set an alternate image to be used when the onmouseover
|
|
393
|
+
# event is fired, and sets the original image to be replaced onmouseout.
|
|
394
|
+
# This can be used to implement an easy image toggle that fires on onmouseover.
|
|
395
|
+
#
|
|
396
|
+
# ==== Examples
|
|
397
|
+
# image_tag("icon") # =>
|
|
398
|
+
# <img src="/images/icon" alt="Icon" />
|
|
399
|
+
# image_tag("icon.png") # =>
|
|
400
|
+
# <img src="/images/icon.png" alt="Icon" />
|
|
401
|
+
# image_tag("icon.png", :size => "16x10", :alt => "Edit Entry") # =>
|
|
402
|
+
# <img src="/images/icon.png" width="16" height="10" alt="Edit Entry" />
|
|
403
|
+
# image_tag("/icons/icon.gif", :size => "16x16") # =>
|
|
404
|
+
# <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
|
|
405
|
+
# image_tag("/icons/icon.gif", :height => '32', :width => '32') # =>
|
|
406
|
+
# <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
|
|
407
|
+
# image_tag("/icons/icon.gif", :class => "menu_icon") # =>
|
|
408
|
+
# <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
|
|
409
|
+
# image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # =>
|
|
410
|
+
# <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
|
|
411
|
+
# image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # =>
|
|
412
|
+
# <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
|
|
413
|
+
def image_tag(source, options = {})
|
|
414
|
+
options.symbolize_keys!
|
|
415
|
+
|
|
416
|
+
options[:src] = path_to_image(source)
|
|
417
|
+
options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize
|
|
418
|
+
|
|
419
|
+
if size = options.delete(:size)
|
|
420
|
+
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
if mouseover = options.delete(:mouseover)
|
|
424
|
+
options[:onmouseover] = "this.src='#{image_path(mouseover)}'"
|
|
425
|
+
options[:onmouseout] = "this.src='#{image_path(options[:src])}'"
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
tag("img", options)
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
private
|
|
432
|
+
def file_exist?(path)
|
|
433
|
+
@@file_exist_cache ||= {}
|
|
434
|
+
if !(@@file_exist_cache[path] ||= File.exist?(path))
|
|
435
|
+
@@file_exist_cache[path] = true
|
|
436
|
+
false
|
|
437
|
+
else
|
|
438
|
+
true
|
|
439
|
+
end
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
# Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
|
|
443
|
+
# Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
|
|
444
|
+
# roots. Rewrite the asset path for cache-busting asset ids. Include
|
|
445
|
+
# asset host, if configured, with the correct request protocol.
|
|
446
|
+
def compute_public_path(source, dir, ext = nil, include_host = true)
|
|
447
|
+
has_request = @controller.respond_to?(:request)
|
|
448
|
+
|
|
449
|
+
cache_key =
|
|
450
|
+
if has_request
|
|
451
|
+
[ @controller.request.protocol,
|
|
452
|
+
ActionController::Base.asset_host.to_s,
|
|
453
|
+
@controller.request.relative_url_root,
|
|
454
|
+
dir, source, ext, include_host ].join
|
|
455
|
+
else
|
|
456
|
+
[ ActionController::Base.asset_host.to_s,
|
|
457
|
+
dir, source, ext, include_host ].join
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
ActionView::Base.computed_public_paths[cache_key] ||=
|
|
461
|
+
begin
|
|
462
|
+
source += ".#{ext}" if ext && File.extname(source).blank? || File.exist?(File.join(ASSETS_DIR, dir, "#{source}.#{ext}"))
|
|
463
|
+
|
|
464
|
+
if source =~ %r{^[-a-z]+://}
|
|
465
|
+
source
|
|
466
|
+
else
|
|
467
|
+
source = "/#{dir}/#{source}" unless source[0] == ?/
|
|
468
|
+
if has_request
|
|
469
|
+
unless source =~ %r{^#{@controller.request.relative_url_root}/}
|
|
470
|
+
source = "#{@controller.request.relative_url_root}#{source}"
|
|
471
|
+
end
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
rewrite_asset_path(source)
|
|
475
|
+
end
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
source = ActionView::Base.computed_public_paths[cache_key]
|
|
479
|
+
|
|
480
|
+
if include_host && source !~ %r{^[-a-z]+://}
|
|
481
|
+
host = compute_asset_host(source)
|
|
482
|
+
|
|
483
|
+
if has_request && !host.blank? && host !~ %r{^[-a-z]+://}
|
|
484
|
+
host = "#{@controller.request.protocol}#{host}"
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
"#{host}#{source}"
|
|
488
|
+
else
|
|
489
|
+
source
|
|
490
|
+
end
|
|
491
|
+
end
|
|
492
|
+
|
|
493
|
+
# Pick an asset host for this source. Returns +nil+ if no host is set,
|
|
494
|
+
# the host if no wildcard is set, the host interpolated with the
|
|
495
|
+
# numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
|
|
496
|
+
# or the value returned from invoking the proc if it's a proc.
|
|
497
|
+
def compute_asset_host(source)
|
|
498
|
+
if host = ActionController::Base.asset_host
|
|
499
|
+
if host.is_a?(Proc)
|
|
500
|
+
case host.arity
|
|
501
|
+
when 2
|
|
502
|
+
host.call(source, @controller.request)
|
|
503
|
+
else
|
|
504
|
+
host.call(source)
|
|
505
|
+
end
|
|
506
|
+
else
|
|
507
|
+
(host =~ /%d/) ? host % (source.hash % 4) : host
|
|
508
|
+
end
|
|
509
|
+
end
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
# Use the RAILS_ASSET_ID environment variable or the source's
|
|
513
|
+
# modification time as its cache-busting asset id.
|
|
514
|
+
def rails_asset_id(source)
|
|
515
|
+
if asset_id = ENV["RAILS_ASSET_ID"]
|
|
516
|
+
asset_id
|
|
517
|
+
else
|
|
518
|
+
path = File.join(ASSETS_DIR, source)
|
|
519
|
+
|
|
520
|
+
if File.exist?(path)
|
|
521
|
+
File.mtime(path).to_i.to_s
|
|
522
|
+
else
|
|
523
|
+
''
|
|
524
|
+
end
|
|
525
|
+
end
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
# Break out the asset path rewrite in case plugins wish to put the asset id
|
|
529
|
+
# someplace other than the query string.
|
|
530
|
+
def rewrite_asset_path(source)
|
|
531
|
+
asset_id = rails_asset_id(source)
|
|
532
|
+
if asset_id.blank?
|
|
533
|
+
source
|
|
534
|
+
else
|
|
535
|
+
source + "?#{asset_id}"
|
|
536
|
+
end
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
def javascript_src_tag(source, options)
|
|
540
|
+
content_tag("script", "", { "type" => Mime::JS, "src" => path_to_javascript(source) }.merge(options))
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
def stylesheet_tag(source, options)
|
|
544
|
+
tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => html_escape(path_to_stylesheet(source)) }.merge(options), false, false)
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
def compute_javascript_paths(sources)
|
|
548
|
+
expand_javascript_sources(sources).collect { |source| compute_public_path(source, 'javascripts', 'js', false) }
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
def compute_stylesheet_paths(sources)
|
|
552
|
+
expand_stylesheet_sources(sources).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) }
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
def expand_javascript_sources(sources)
|
|
556
|
+
if sources.include?(:all)
|
|
557
|
+
all_javascript_files = Dir[File.join(JAVASCRIPTS_DIR, '*.js')].collect { |file| File.basename(file).gsub(/\.\w+$/, '') }.sort
|
|
558
|
+
@@all_javascript_sources ||= ((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq
|
|
559
|
+
else
|
|
560
|
+
expanded_sources = sources.collect do |source|
|
|
561
|
+
determine_source(source, @@javascript_expansions)
|
|
562
|
+
end.flatten
|
|
563
|
+
expanded_sources << "application" if sources.include?(:defaults) && file_exist?(File.join(JAVASCRIPTS_DIR, "application.js"))
|
|
564
|
+
expanded_sources
|
|
565
|
+
end
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
def expand_stylesheet_sources(sources)
|
|
569
|
+
if sources.first == :all
|
|
570
|
+
@@all_stylesheet_sources ||= Dir[File.join(STYLESHEETS_DIR, '*.css')].collect { |file| File.basename(file).gsub(/\.\w+$/, '') }.sort
|
|
571
|
+
else
|
|
572
|
+
sources.collect do |source|
|
|
573
|
+
determine_source(source, @@stylesheet_expansions)
|
|
574
|
+
end.flatten
|
|
575
|
+
end
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
def determine_source(source, collection)
|
|
579
|
+
case source
|
|
580
|
+
when Symbol
|
|
581
|
+
collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}")
|
|
582
|
+
else
|
|
583
|
+
source
|
|
584
|
+
end
|
|
585
|
+
end
|
|
586
|
+
|
|
587
|
+
def join_asset_file_contents(paths)
|
|
588
|
+
paths.collect { |path| File.read(File.join(ASSETS_DIR, path.split("?").first)) }.join("\n\n")
|
|
589
|
+
end
|
|
590
|
+
|
|
591
|
+
def write_asset_file_contents(joined_asset_path, asset_paths)
|
|
592
|
+
unless file_exist?(joined_asset_path)
|
|
593
|
+
FileUtils.mkdir_p(File.dirname(joined_asset_path))
|
|
594
|
+
File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) }
|
|
595
|
+
end
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
end
|
|
599
|
+
end
|