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,186 @@
|
|
|
1
|
+
module Mime
|
|
2
|
+
SET = []
|
|
3
|
+
EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
|
|
4
|
+
LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k.blank? }
|
|
5
|
+
|
|
6
|
+
# Encapsulates the notion of a mime type. Can be used at render time, for example, with:
|
|
7
|
+
#
|
|
8
|
+
# class PostsController < ActionController::Base
|
|
9
|
+
# def show
|
|
10
|
+
# @post = Post.find(params[:id])
|
|
11
|
+
#
|
|
12
|
+
# respond_to do |format|
|
|
13
|
+
# format.html
|
|
14
|
+
# format.ics { render :text => post.to_ics, :mime_type => Mime::Type["text/calendar"] }
|
|
15
|
+
# format.xml { render :xml => @people.to_xml }
|
|
16
|
+
# end
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
class Type
|
|
20
|
+
@@html_types = Set.new [:html, :all]
|
|
21
|
+
@@unverifiable_types = Set.new [:text, :json, :csv, :xml, :rss, :atom, :yaml]
|
|
22
|
+
cattr_reader :html_types, :unverifiable_types
|
|
23
|
+
|
|
24
|
+
# A simple helper class used in parsing the accept header
|
|
25
|
+
class AcceptItem #:nodoc:
|
|
26
|
+
attr_accessor :order, :name, :q
|
|
27
|
+
|
|
28
|
+
def initialize(order, name, q=nil)
|
|
29
|
+
@order = order
|
|
30
|
+
@name = name.strip
|
|
31
|
+
q ||= 0.0 if @name == Mime::ALL # default wilcard match to end of list
|
|
32
|
+
@q = ((q || 1.0).to_f * 100).to_i
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def to_s
|
|
36
|
+
@name
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def <=>(item)
|
|
40
|
+
result = item.q <=> q
|
|
41
|
+
result = order <=> item.order if result == 0
|
|
42
|
+
result
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def ==(item)
|
|
46
|
+
name == (item.respond_to?(:name) ? item.name : item)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class << self
|
|
51
|
+
def lookup(string)
|
|
52
|
+
LOOKUP[string]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def lookup_by_extension(extension)
|
|
56
|
+
EXTENSION_LOOKUP[extension]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Registers an alias that's not used on mime type lookup, but can be referenced directly. Especially useful for
|
|
60
|
+
# rendering different HTML versions depending on the user agent, like an iPhone.
|
|
61
|
+
def register_alias(string, symbol, extension_synonyms = [])
|
|
62
|
+
register(string, symbol, [], extension_synonyms, true)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
|
|
66
|
+
Mime.instance_eval { const_set symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) }
|
|
67
|
+
|
|
68
|
+
SET << Mime.const_get(symbol.to_s.upcase)
|
|
69
|
+
|
|
70
|
+
([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup
|
|
71
|
+
([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def parse(accept_header)
|
|
75
|
+
# keep track of creation order to keep the subsequent sort stable
|
|
76
|
+
list = []
|
|
77
|
+
accept_header.split(/,/).each_with_index do |header, index|
|
|
78
|
+
params, q = header.split(/;\s*q=/)
|
|
79
|
+
if params
|
|
80
|
+
params.strip!
|
|
81
|
+
list << AcceptItem.new(index, params, q) unless params.empty?
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
list.sort!
|
|
85
|
+
|
|
86
|
+
# Take care of the broken text/xml entry by renaming or deleting it
|
|
87
|
+
text_xml = list.index("text/xml")
|
|
88
|
+
app_xml = list.index(Mime::XML.to_s)
|
|
89
|
+
|
|
90
|
+
if text_xml && app_xml
|
|
91
|
+
# set the q value to the max of the two
|
|
92
|
+
list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
|
|
93
|
+
|
|
94
|
+
# make sure app_xml is ahead of text_xml in the list
|
|
95
|
+
if app_xml > text_xml
|
|
96
|
+
list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
|
|
97
|
+
app_xml, text_xml = text_xml, app_xml
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# delete text_xml from the list
|
|
101
|
+
list.delete_at(text_xml)
|
|
102
|
+
|
|
103
|
+
elsif text_xml
|
|
104
|
+
list[text_xml].name = Mime::XML.to_s
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Look for more specific XML-based types and sort them ahead of app/xml
|
|
108
|
+
|
|
109
|
+
if app_xml
|
|
110
|
+
idx = app_xml
|
|
111
|
+
app_xml_type = list[app_xml]
|
|
112
|
+
|
|
113
|
+
while(idx < list.length)
|
|
114
|
+
type = list[idx]
|
|
115
|
+
break if type.q < app_xml_type.q
|
|
116
|
+
if type.name =~ /\+xml$/
|
|
117
|
+
list[app_xml], list[idx] = list[idx], list[app_xml]
|
|
118
|
+
app_xml = idx
|
|
119
|
+
end
|
|
120
|
+
idx += 1
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
|
|
125
|
+
list
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def initialize(string, symbol = nil, synonyms = [])
|
|
130
|
+
@symbol, @synonyms = symbol, synonyms
|
|
131
|
+
@string = string
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def to_s
|
|
135
|
+
@string
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def to_str
|
|
139
|
+
to_s
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def to_sym
|
|
143
|
+
@symbol || @string.to_sym
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def has_synonyms?
|
|
147
|
+
!@synonyms.blank?
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def ===(list)
|
|
151
|
+
if list.is_a?(Array)
|
|
152
|
+
(@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
|
|
153
|
+
else
|
|
154
|
+
super
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def ==(mime_type)
|
|
159
|
+
return false if mime_type.blank?
|
|
160
|
+
(@synonyms + [ self ]).any? do |synonym|
|
|
161
|
+
synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See
|
|
166
|
+
# ActionController::RequestForgerProtection.
|
|
167
|
+
def verify_request?
|
|
168
|
+
!@@unverifiable_types.include?(to_sym)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def html?
|
|
172
|
+
@@html_types.include?(to_sym) || @string =~ /html/
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
private
|
|
176
|
+
def method_missing(method, *args)
|
|
177
|
+
if method.to_s =~ /(\w+)\?$/
|
|
178
|
+
$1.downcase.to_sym == to_sym
|
|
179
|
+
else
|
|
180
|
+
super
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
require 'action_controller/mime_types'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Build list of Mime types for HTTP responses
|
|
2
|
+
# http://www.iana.org/assignments/media-types/
|
|
3
|
+
|
|
4
|
+
Mime::Type.register "*/*", :all
|
|
5
|
+
Mime::Type.register "text/plain", :text, [], %w(txt)
|
|
6
|
+
Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml )
|
|
7
|
+
Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript )
|
|
8
|
+
Mime::Type.register "text/css", :css
|
|
9
|
+
Mime::Type.register "text/calendar", :ics
|
|
10
|
+
Mime::Type.register "text/csv", :csv
|
|
11
|
+
Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml )
|
|
12
|
+
Mime::Type.register "application/rss+xml", :rss
|
|
13
|
+
Mime::Type.register "application/atom+xml", :atom
|
|
14
|
+
Mime::Type.register "application/x-yaml", :yaml, %w( text/yaml )
|
|
15
|
+
|
|
16
|
+
Mime::Type.register "multipart/form-data", :multipart_form
|
|
17
|
+
Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form
|
|
18
|
+
|
|
19
|
+
# http://www.ietf.org/rfc/rfc4627.txt
|
|
20
|
+
Mime::Type.register "application/json", :json, %w( text/x-json )
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
module ActionController
|
|
2
|
+
# Polymorphic URL helpers are methods for smart resolution to a named route call when
|
|
3
|
+
# given an Active Record model instance. They are to be used in combination with
|
|
4
|
+
# ActionController::Resources.
|
|
5
|
+
#
|
|
6
|
+
# These methods are useful when you want to generate correct URL or path to a RESTful
|
|
7
|
+
# resource without having to know the exact type of the record in question.
|
|
8
|
+
#
|
|
9
|
+
# Nested resources and/or namespaces are also supported, as illustrated in the example:
|
|
10
|
+
#
|
|
11
|
+
# polymorphic_url([:admin, @article, @comment])
|
|
12
|
+
#
|
|
13
|
+
# results in:
|
|
14
|
+
#
|
|
15
|
+
# admin_article_comment_url(@article, @comment)
|
|
16
|
+
#
|
|
17
|
+
# == Usage within the framework
|
|
18
|
+
#
|
|
19
|
+
# Polymorphic URL helpers are used in a number of places throughout the Rails framework:
|
|
20
|
+
#
|
|
21
|
+
# * <tt>url_for</tt>, so you can use it with a record as the argument, e.g.
|
|
22
|
+
# <tt>url_for(@article)</tt>;
|
|
23
|
+
# * ActionView::Helpers::FormHelper uses <tt>polymorphic_path</tt>, so you can write
|
|
24
|
+
# <tt>form_for(@article)</tt> without having to specify <tt>:url</tt> parameter for the form
|
|
25
|
+
# action;
|
|
26
|
+
# * <tt>redirect_to</tt> (which, in fact, uses <tt>url_for</tt>) so you can write
|
|
27
|
+
# <tt>redirect_to(post)</tt> in your controllers;
|
|
28
|
+
# * ActionView::Helpers::AtomFeedHelper, so you don't have to explicitly specify URLs
|
|
29
|
+
# for feed entries.
|
|
30
|
+
#
|
|
31
|
+
# == Prefixed polymorphic helpers
|
|
32
|
+
#
|
|
33
|
+
# In addition to <tt>polymorphic_url</tt> and <tt>polymorphic_path</tt> methods, a
|
|
34
|
+
# number of prefixed helpers are available as a shorthand to <tt>:action => "..."</tt>
|
|
35
|
+
# in options. Those are:
|
|
36
|
+
#
|
|
37
|
+
# * <tt>edit_polymorphic_url</tt>, <tt>edit_polymorphic_path</tt>
|
|
38
|
+
# * <tt>new_polymorphic_url</tt>, <tt>new_polymorphic_path</tt>
|
|
39
|
+
# * <tt>formatted_polymorphic_url</tt>, <tt>formatted_polymorphic_path</tt>
|
|
40
|
+
#
|
|
41
|
+
# Example usage:
|
|
42
|
+
#
|
|
43
|
+
# edit_polymorphic_path(@post) # => "/posts/1/edit"
|
|
44
|
+
# formatted_polymorphic_path([@post, :pdf]) # => "/posts/1.pdf"
|
|
45
|
+
module PolymorphicRoutes
|
|
46
|
+
# Constructs a call to a named RESTful route for the given record and returns the
|
|
47
|
+
# resulting URL string. For example:
|
|
48
|
+
#
|
|
49
|
+
# # calls post_url(post)
|
|
50
|
+
# polymorphic_url(post) # => "http://example.com/posts/1"
|
|
51
|
+
# polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
|
|
52
|
+
# polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
|
|
53
|
+
# polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
|
|
54
|
+
#
|
|
55
|
+
# ==== Options
|
|
56
|
+
#
|
|
57
|
+
# * <tt>:action</tt> - Specifies the action prefix for the named route:
|
|
58
|
+
# <tt>:new</tt>, <tt>:edit</tt>, or <tt>:formatted</tt>. Default is no prefix.
|
|
59
|
+
# * <tt>:routing_type</tt> - Allowed values are <tt>:path</tt> or <tt>:url</tt>.
|
|
60
|
+
# Default is <tt>:url</tt>.
|
|
61
|
+
#
|
|
62
|
+
# ==== Examples
|
|
63
|
+
#
|
|
64
|
+
# # an Article record
|
|
65
|
+
# polymorphic_url(record) # same as article_url(record)
|
|
66
|
+
#
|
|
67
|
+
# # a Comment record
|
|
68
|
+
# polymorphic_url(record) # same as comment_url(record)
|
|
69
|
+
#
|
|
70
|
+
# # it recognizes new records and maps to the collection
|
|
71
|
+
# record = Comment.new
|
|
72
|
+
# polymorphic_url(record) # same as comments_url()
|
|
73
|
+
#
|
|
74
|
+
def polymorphic_url(record_or_hash_or_array, options = {})
|
|
75
|
+
if record_or_hash_or_array.kind_of?(Array)
|
|
76
|
+
record_or_hash_or_array = record_or_hash_or_array.dup
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
record = extract_record(record_or_hash_or_array)
|
|
80
|
+
format = extract_format(record_or_hash_or_array, options)
|
|
81
|
+
namespace = extract_namespace(record_or_hash_or_array)
|
|
82
|
+
|
|
83
|
+
args = case record_or_hash_or_array
|
|
84
|
+
when Hash; [ record_or_hash_or_array ]
|
|
85
|
+
when Array; record_or_hash_or_array.dup
|
|
86
|
+
else [ record_or_hash_or_array ]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
inflection =
|
|
90
|
+
case
|
|
91
|
+
when options[:action].to_s == "new"
|
|
92
|
+
args.pop
|
|
93
|
+
:singular
|
|
94
|
+
when record.respond_to?(:new_record?) && record.new_record?
|
|
95
|
+
args.pop
|
|
96
|
+
:plural
|
|
97
|
+
else
|
|
98
|
+
:singular
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
|
|
102
|
+
args << format if format
|
|
103
|
+
|
|
104
|
+
named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options)
|
|
105
|
+
send!(named_route, *args)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Returns the path component of a URL for the given record. It uses
|
|
109
|
+
# <tt>polymorphic_url</tt> with <tt>:routing_type => :path</tt>.
|
|
110
|
+
def polymorphic_path(record_or_hash_or_array, options = {})
|
|
111
|
+
options[:routing_type] = :path
|
|
112
|
+
polymorphic_url(record_or_hash_or_array, options)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
%w(edit new formatted).each do |action|
|
|
116
|
+
module_eval <<-EOT, __FILE__, __LINE__
|
|
117
|
+
def #{action}_polymorphic_url(record_or_hash)
|
|
118
|
+
polymorphic_url(record_or_hash, :action => "#{action}")
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def #{action}_polymorphic_path(record_or_hash)
|
|
122
|
+
polymorphic_url(record_or_hash, :action => "#{action}", :routing_type => :path)
|
|
123
|
+
end
|
|
124
|
+
EOT
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
def action_prefix(options)
|
|
129
|
+
options[:action] ? "#{options[:action]}_" : ""
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def routing_type(options)
|
|
133
|
+
options[:routing_type] || :url
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def build_named_route_call(records, namespace, inflection, options = {})
|
|
137
|
+
unless records.is_a?(Array)
|
|
138
|
+
record = extract_record(records)
|
|
139
|
+
route = ''
|
|
140
|
+
else
|
|
141
|
+
record = records.pop
|
|
142
|
+
route = records.inject("") do |string, parent|
|
|
143
|
+
if parent.is_a?(Symbol) || parent.is_a?(String)
|
|
144
|
+
string << "#{parent}_"
|
|
145
|
+
else
|
|
146
|
+
string << "#{RecordIdentifier.send!("singular_class_name", parent)}_"
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
if record.is_a?(Symbol) || record.is_a?(String)
|
|
152
|
+
route << "#{record}_"
|
|
153
|
+
else
|
|
154
|
+
route << "#{RecordIdentifier.send!("#{inflection}_class_name", record)}_"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
action_prefix(options) + namespace + route + routing_type(options).to_s
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def extract_record(record_or_hash_or_array)
|
|
161
|
+
case record_or_hash_or_array
|
|
162
|
+
when Array; record_or_hash_or_array.last
|
|
163
|
+
when Hash; record_or_hash_or_array[:id]
|
|
164
|
+
else record_or_hash_or_array
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def extract_format(record_or_hash_or_array, options)
|
|
169
|
+
if options[:action].to_s == "formatted" && record_or_hash_or_array.is_a?(Array)
|
|
170
|
+
record_or_hash_or_array.pop
|
|
171
|
+
elsif options[:format]
|
|
172
|
+
options[:format]
|
|
173
|
+
else
|
|
174
|
+
nil
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Remove the first symbols from the array and return the url prefix
|
|
179
|
+
# implied by those symbols.
|
|
180
|
+
def extract_namespace(record_or_hash_or_array)
|
|
181
|
+
return "" unless record_or_hash_or_array.is_a?(Array)
|
|
182
|
+
|
|
183
|
+
namespace_keys = []
|
|
184
|
+
while (key = record_or_hash_or_array.first) && key.is_a?(String) || key.is_a?(Symbol)
|
|
185
|
+
namespace_keys << record_or_hash_or_array.shift
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
namespace_keys.map {|k| "#{k}_"}.join
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
module ActionController
|
|
2
|
+
# The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or
|
|
3
|
+
# Active Resources or pretty much any other model type that has an id. These patterns are then used to try elevate
|
|
4
|
+
# the view actions to a higher logical level. Example:
|
|
5
|
+
#
|
|
6
|
+
# # routes
|
|
7
|
+
# map.resources :posts
|
|
8
|
+
#
|
|
9
|
+
# # view
|
|
10
|
+
# <% div_for(post) do %> <div id="post_45" class="post">
|
|
11
|
+
# <%= post.body %> What a wonderful world!
|
|
12
|
+
# <% end %> </div>
|
|
13
|
+
#
|
|
14
|
+
# # controller
|
|
15
|
+
# def destroy
|
|
16
|
+
# post = Post.find(params[:id])
|
|
17
|
+
# post.destroy
|
|
18
|
+
#
|
|
19
|
+
# respond_to do |format|
|
|
20
|
+
# format.html { redirect_to(post) } # Calls polymorphic_url(post) which in turn calls post_url(post)
|
|
21
|
+
# format.js { render :js => "new Effect.fade('#{dom_id(post)}');" }
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
# end
|
|
25
|
+
#
|
|
26
|
+
# As the example above shows, you can stop caring to a large extent what the actual id of the post is. You just know
|
|
27
|
+
# that one is being assigned and that the subsequent calls in redirect_to and the JS expect that same naming
|
|
28
|
+
# convention and allows you to write less code if you follow it.
|
|
29
|
+
module RecordIdentifier
|
|
30
|
+
extend self
|
|
31
|
+
|
|
32
|
+
JOIN = '_'.freeze
|
|
33
|
+
NEW = 'new'.freeze
|
|
34
|
+
|
|
35
|
+
# Returns plural/singular for a record or class. Example:
|
|
36
|
+
#
|
|
37
|
+
# partial_path(post) # => "posts/post"
|
|
38
|
+
# partial_path(Person) # => "people/person"
|
|
39
|
+
# partial_path(Person, "admin/games") # => "admin/people/person"
|
|
40
|
+
def partial_path(record_or_class, controller_path = nil)
|
|
41
|
+
name = model_name_from_record_or_class(record_or_class)
|
|
42
|
+
|
|
43
|
+
if controller_path && controller_path.include?("/")
|
|
44
|
+
"#{File.dirname(controller_path)}/#{name.partial_path}"
|
|
45
|
+
else
|
|
46
|
+
name.partial_path
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The DOM class convention is to use the singular form of an object or class. Examples:
|
|
51
|
+
#
|
|
52
|
+
# dom_class(post) # => "post"
|
|
53
|
+
# dom_class(Person) # => "person"
|
|
54
|
+
#
|
|
55
|
+
# If you need to address multiple instances of the same class in the same view, you can prefix the dom_class:
|
|
56
|
+
#
|
|
57
|
+
# dom_class(post, :edit) # => "edit_post"
|
|
58
|
+
# dom_class(Person, :edit) # => "edit_person"
|
|
59
|
+
def dom_class(record_or_class, prefix = nil)
|
|
60
|
+
singular = singular_class_name(record_or_class)
|
|
61
|
+
prefix ? "#{prefix}#{JOIN}#{singular}" : singular
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The DOM id convention is to use the singular form of an object or class with the id following an underscore.
|
|
65
|
+
# If no id is found, prefix with "new_" instead. Examples:
|
|
66
|
+
#
|
|
67
|
+
# dom_id(Post.find(45)) # => "post_45"
|
|
68
|
+
# dom_id(Post.new) # => "new_post"
|
|
69
|
+
#
|
|
70
|
+
# If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:
|
|
71
|
+
#
|
|
72
|
+
# dom_id(Post.find(45), :edit) # => "edit_post_45"
|
|
73
|
+
def dom_id(record, prefix = nil)
|
|
74
|
+
if record_id = record.id
|
|
75
|
+
"#{dom_class(record, prefix)}#{JOIN}#{record_id}"
|
|
76
|
+
else
|
|
77
|
+
dom_class(record, prefix || NEW)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Returns the plural class name of a record or class. Examples:
|
|
82
|
+
#
|
|
83
|
+
# plural_class_name(post) # => "posts"
|
|
84
|
+
# plural_class_name(Highrise::Person) # => "highrise_people"
|
|
85
|
+
def plural_class_name(record_or_class)
|
|
86
|
+
model_name_from_record_or_class(record_or_class).plural
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Returns the singular class name of a record or class. Examples:
|
|
90
|
+
#
|
|
91
|
+
# singular_class_name(post) # => "post"
|
|
92
|
+
# singular_class_name(Highrise::Person) # => "highrise_person"
|
|
93
|
+
def singular_class_name(record_or_class)
|
|
94
|
+
model_name_from_record_or_class(record_or_class).singular
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
def model_name_from_record_or_class(record_or_class)
|
|
99
|
+
(record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|