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,31 @@
|
|
|
1
|
+
module ActionView
|
|
2
|
+
module Helpers
|
|
3
|
+
# Provides a set of methods for making it easier to debug Rails objects.
|
|
4
|
+
module DebugHelper
|
|
5
|
+
# Returns a <pre>-tag that has +object+ dumped by YAML. This creates a very
|
|
6
|
+
# readable way to inspect an object.
|
|
7
|
+
#
|
|
8
|
+
# ==== Example
|
|
9
|
+
# my_hash = {'first' => 1, 'second' => 'two', 'third' => [1,2,3]}
|
|
10
|
+
# debug(my_hash)
|
|
11
|
+
#
|
|
12
|
+
# => <pre class='debug_dump'>---
|
|
13
|
+
# first: 1
|
|
14
|
+
# second: two
|
|
15
|
+
# third:
|
|
16
|
+
# - 1
|
|
17
|
+
# - 2
|
|
18
|
+
# - 3
|
|
19
|
+
# </pre>
|
|
20
|
+
def debug(object)
|
|
21
|
+
begin
|
|
22
|
+
Marshal::dump(object)
|
|
23
|
+
"<pre class='debug_dump'>#{h(object.to_yaml).gsub(" ", " ")}</pre>"
|
|
24
|
+
rescue Exception => e # errors from Marshal or YAML
|
|
25
|
+
# Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback
|
|
26
|
+
"<code class='debug_dump'>#{h(object.inspect)}</code>"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
require 'cgi'
|
|
2
|
+
require 'action_view/helpers/date_helper'
|
|
3
|
+
require 'action_view/helpers/tag_helper'
|
|
4
|
+
require 'action_view/helpers/form_tag_helper'
|
|
5
|
+
|
|
6
|
+
module ActionView
|
|
7
|
+
module Helpers
|
|
8
|
+
# Form helpers are designed to make working with models much easier compared to using just standard HTML
|
|
9
|
+
# elements by providing a set of methods for creating forms based on your models. This helper generates the HTML
|
|
10
|
+
# for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form
|
|
11
|
+
# is submitted (i.e., when the user hits the submit button or <tt>form.submit</tt> is called via JavaScript), the form inputs will be bundled into the <tt>params</tt> object and passed back to the controller.
|
|
12
|
+
#
|
|
13
|
+
# There are two types of form helpers: those that specifically work with model attributes and those that don't.
|
|
14
|
+
# This helper deals with those that work with model attributes; to see an example of form helpers that don't work
|
|
15
|
+
# with model attributes, check the ActionView::Helpers::FormTagHelper documentation.
|
|
16
|
+
#
|
|
17
|
+
# The core method of this helper, form_for, gives you the ability to create a form for a model instance;
|
|
18
|
+
# for example, let's say that you have a model <tt>Person</tt> and want to create a new instance of it:
|
|
19
|
+
#
|
|
20
|
+
# # Note: a @person variable will have been created in the controller.
|
|
21
|
+
# # For example: @person = Person.new
|
|
22
|
+
# <% form_for :person, @person, :url => { :action => "create" } do |f| %>
|
|
23
|
+
# <%= f.text_field :first_name %>
|
|
24
|
+
# <%= f.text_field :last_name %>
|
|
25
|
+
# <%= submit_tag 'Create' %>
|
|
26
|
+
# <% end %>
|
|
27
|
+
#
|
|
28
|
+
# The HTML generated for this would be:
|
|
29
|
+
#
|
|
30
|
+
# <form action="/persons/create" method="post">
|
|
31
|
+
# <input id="person_first_name" name="person[first_name]" size="30" type="text" />
|
|
32
|
+
# <input id="person_last_name" name="person[last_name]" size="30" type="text" />
|
|
33
|
+
# <input name="commit" type="submit" value="Create" />
|
|
34
|
+
# </form>
|
|
35
|
+
#
|
|
36
|
+
# If you are using a partial for your form fields, you can use this shortcut:
|
|
37
|
+
#
|
|
38
|
+
# <% form_for :person, @person, :url => { :action => "create" } do |f| %>
|
|
39
|
+
# <%= render :partial => f %>
|
|
40
|
+
# <%= submit_tag 'Create' %>
|
|
41
|
+
# <% end %>
|
|
42
|
+
#
|
|
43
|
+
# This example will render the <tt>people/_form</tt> partial, setting a local variable called <tt>form</tt> which references the yielded FormBuilder.
|
|
44
|
+
#
|
|
45
|
+
# The <tt>params</tt> object created when this form is submitted would look like:
|
|
46
|
+
#
|
|
47
|
+
# {"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
|
|
48
|
+
#
|
|
49
|
+
# The params hash has a nested <tt>person</tt> value, which can therefore be accessed with <tt>params[:person]</tt> in the controller.
|
|
50
|
+
# If were editing/updating an instance (e.g., <tt>Person.find(1)</tt> rather than <tt>Person.new</tt> in the controller), the objects
|
|
51
|
+
# attribute values are filled into the form (e.g., the <tt>person_first_name</tt> field would have that person's first name in it).
|
|
52
|
+
#
|
|
53
|
+
# If the object name contains square brackets the id for the object will be inserted. For example:
|
|
54
|
+
#
|
|
55
|
+
# <%= text_field "person[]", "name" %>
|
|
56
|
+
#
|
|
57
|
+
# ...will generate the following ERb.
|
|
58
|
+
#
|
|
59
|
+
# <input type="text" id="person_<%= @person.id %>_name" name="person[<%= @person.id %>][name]" value="<%= @person.name %>" />
|
|
60
|
+
#
|
|
61
|
+
# If the helper is being used to generate a repetitive sequence of similar form elements, for example in a partial
|
|
62
|
+
# used by <tt>render_collection_of_partials</tt>, the <tt>index</tt> option may come in handy. Example:
|
|
63
|
+
#
|
|
64
|
+
# <%= text_field "person", "name", "index" => 1 %>
|
|
65
|
+
#
|
|
66
|
+
# ...becomes...
|
|
67
|
+
#
|
|
68
|
+
# <input type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />
|
|
69
|
+
#
|
|
70
|
+
# An <tt>index</tt> option may also be passed to <tt>form_for</tt> and <tt>fields_for</tt>. This automatically applies
|
|
71
|
+
# the <tt>index</tt> to all the nested fields.
|
|
72
|
+
#
|
|
73
|
+
# There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper.html,
|
|
74
|
+
# link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper.html
|
|
75
|
+
module FormHelper
|
|
76
|
+
# Creates a form and a scope around a specific model object that is used as
|
|
77
|
+
# a base for questioning about values for the fields.
|
|
78
|
+
#
|
|
79
|
+
# Rails provides succint resource-oriented form generation with +form_for+
|
|
80
|
+
# like this:
|
|
81
|
+
#
|
|
82
|
+
# <% form_for @offer do |f| %>
|
|
83
|
+
# <%= f.label :version, 'Version' %>:
|
|
84
|
+
# <%= f.text_field :version %><br />
|
|
85
|
+
# <%= f.label :author, 'Author' %>:
|
|
86
|
+
# <%= f.text_field :author %><br />
|
|
87
|
+
# <% end %>
|
|
88
|
+
#
|
|
89
|
+
# There, +form_for+ is able to generate the rest of RESTful form parameters
|
|
90
|
+
# based on introspection on the record, but to understand what it does we
|
|
91
|
+
# need to dig first into the alternative generic usage it is based upon.
|
|
92
|
+
#
|
|
93
|
+
# === Generic form_for
|
|
94
|
+
#
|
|
95
|
+
# The generic way to call +form_for+ yields a form builder around a model:
|
|
96
|
+
#
|
|
97
|
+
# <% form_for :person, :url => { :action => "update" } do |f| %>
|
|
98
|
+
# <%= f.error_messages %>
|
|
99
|
+
# First name: <%= f.text_field :first_name %><br />
|
|
100
|
+
# Last name : <%= f.text_field :last_name %><br />
|
|
101
|
+
# Biography : <%= f.text_area :biography %><br />
|
|
102
|
+
# Admin? : <%= f.check_box :admin %><br />
|
|
103
|
+
# <% end %>
|
|
104
|
+
#
|
|
105
|
+
# There, the first argument is a symbol or string with the name of the
|
|
106
|
+
# object the form is about, and also the name of the instance variable the
|
|
107
|
+
# object is stored in.
|
|
108
|
+
#
|
|
109
|
+
# The form builder acts as a regular form helper that somehow carries the
|
|
110
|
+
# model. Thus, the idea is that
|
|
111
|
+
#
|
|
112
|
+
# <%= f.text_field :first_name %>
|
|
113
|
+
#
|
|
114
|
+
# gets expanded to
|
|
115
|
+
#
|
|
116
|
+
# <%= text_field :person, :first_name %>
|
|
117
|
+
#
|
|
118
|
+
# If the instance variable is not <tt>@person</tt> you can pass the actual
|
|
119
|
+
# record as the second argument:
|
|
120
|
+
#
|
|
121
|
+
# <% form_for :person, person, :url => { :action => "update" } do |f| %>
|
|
122
|
+
# ...
|
|
123
|
+
# <% end %>
|
|
124
|
+
#
|
|
125
|
+
# In that case you can think
|
|
126
|
+
#
|
|
127
|
+
# <%= f.text_field :first_name %>
|
|
128
|
+
#
|
|
129
|
+
# gets expanded to
|
|
130
|
+
#
|
|
131
|
+
# <%= text_field :person, :first_name, :object => person %>
|
|
132
|
+
#
|
|
133
|
+
# You can even display error messages of the wrapped model this way:
|
|
134
|
+
#
|
|
135
|
+
# <%= f.error_messages %>
|
|
136
|
+
#
|
|
137
|
+
# In any of its variants, the rightmost argument to +form_for+ is an
|
|
138
|
+
# optional hash of options:
|
|
139
|
+
#
|
|
140
|
+
# * <tt>:url</tt> - The URL the form is submitted to. It takes the same fields
|
|
141
|
+
# you pass to +url_for+ or +link_to+. In particular you may pass here a
|
|
142
|
+
# named route directly as well. Defaults to the current action.
|
|
143
|
+
# * <tt>:html</tt> - Optional HTML attributes for the form tag.
|
|
144
|
+
#
|
|
145
|
+
# Worth noting is that the +form_for+ tag is called in a ERb evaluation block,
|
|
146
|
+
# not an ERb output block. So that's <tt><% %></tt>, not <tt><%= %></tt>.
|
|
147
|
+
#
|
|
148
|
+
# Also note that +form_for+ doesn't create an exclusive scope. It's still
|
|
149
|
+
# possible to use both the stand-alone FormHelper methods and methods from
|
|
150
|
+
# FormTagHelper. For example:
|
|
151
|
+
#
|
|
152
|
+
# <% form_for :person, @person, :url => { :action => "update" } do |f| %>
|
|
153
|
+
# First name: <%= f.text_field :first_name %>
|
|
154
|
+
# Last name : <%= f.text_field :last_name %>
|
|
155
|
+
# Biography : <%= text_area :person, :biography %>
|
|
156
|
+
# Admin? : <%= check_box_tag "person[admin]", @person.company.admin? %>
|
|
157
|
+
# <% end %>
|
|
158
|
+
#
|
|
159
|
+
# This also works for the methods in FormOptionHelper and DateHelper that are
|
|
160
|
+
# designed to work with an object as base, like FormOptionHelper#collection_select
|
|
161
|
+
# and DateHelper#datetime_select.
|
|
162
|
+
#
|
|
163
|
+
# === Resource-oriented style
|
|
164
|
+
#
|
|
165
|
+
# As we said above, in addition to manually configuring the +form_for+ call,
|
|
166
|
+
# you can rely on automated resource identification, which will use the conventions
|
|
167
|
+
# and named routes of that approach. This is the preferred way to use +form_for+
|
|
168
|
+
# nowadays.
|
|
169
|
+
#
|
|
170
|
+
# For example, if <tt>@post</tt> is an existing record you want to edit
|
|
171
|
+
#
|
|
172
|
+
# <% form_for @post do |f| %>
|
|
173
|
+
# ...
|
|
174
|
+
# <% end %>
|
|
175
|
+
#
|
|
176
|
+
# is equivalent to something like:
|
|
177
|
+
#
|
|
178
|
+
# <% form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>
|
|
179
|
+
# ...
|
|
180
|
+
# <% end %>
|
|
181
|
+
#
|
|
182
|
+
# And for new records
|
|
183
|
+
#
|
|
184
|
+
# <% form_for(Post.new) do |f| %>
|
|
185
|
+
# ...
|
|
186
|
+
# <% end %>
|
|
187
|
+
#
|
|
188
|
+
# expands to
|
|
189
|
+
#
|
|
190
|
+
# <% form_for :post, Post.new, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %>
|
|
191
|
+
# ...
|
|
192
|
+
# <% end %>
|
|
193
|
+
#
|
|
194
|
+
# You can also overwrite the individual conventions, like this:
|
|
195
|
+
#
|
|
196
|
+
# <% form_for(@post, :url => super_post_path(@post)) do |f| %>
|
|
197
|
+
# ...
|
|
198
|
+
# <% end %>
|
|
199
|
+
#
|
|
200
|
+
# And for namespaced routes, like +admin_post_url+:
|
|
201
|
+
#
|
|
202
|
+
# <% form_for([:admin, @post]) do |f| %>
|
|
203
|
+
# ...
|
|
204
|
+
# <% end %>
|
|
205
|
+
#
|
|
206
|
+
# === Customized form builders
|
|
207
|
+
#
|
|
208
|
+
# You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers,
|
|
209
|
+
# then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs.
|
|
210
|
+
#
|
|
211
|
+
# <% form_for :person, @person, :url => { :action => "update" }, :builder => LabellingFormBuilder do |f| %>
|
|
212
|
+
# <%= f.text_field :first_name %>
|
|
213
|
+
# <%= f.text_field :last_name %>
|
|
214
|
+
# <%= text_area :person, :biography %>
|
|
215
|
+
# <%= check_box_tag "person[admin]", @person.company.admin? %>
|
|
216
|
+
# <% end %>
|
|
217
|
+
#
|
|
218
|
+
# In this case, if you use this:
|
|
219
|
+
#
|
|
220
|
+
# <%= render :partial => f %>
|
|
221
|
+
#
|
|
222
|
+
# The rendered template is <tt>people/_labelling_form</tt> and the local variable referencing the form builder is called <tt>labelling_form</tt>.
|
|
223
|
+
#
|
|
224
|
+
# In many cases you will want to wrap the above in another helper, so you could do something like the following:
|
|
225
|
+
#
|
|
226
|
+
# def labelled_form_for(record_or_name_or_array, *args, &proc)
|
|
227
|
+
# options = args.extract_options!
|
|
228
|
+
# form_for(record_or_name_or_array, *(args << options.merge(:builder => LabellingFormBuilder)), &proc)
|
|
229
|
+
# end
|
|
230
|
+
#
|
|
231
|
+
# If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag.
|
|
232
|
+
def form_for(record_or_name_or_array, *args, &proc)
|
|
233
|
+
raise ArgumentError, "Missing block" unless block_given?
|
|
234
|
+
|
|
235
|
+
options = args.extract_options!
|
|
236
|
+
|
|
237
|
+
case record_or_name_or_array
|
|
238
|
+
when String, Symbol
|
|
239
|
+
object_name = record_or_name_or_array
|
|
240
|
+
when Array
|
|
241
|
+
object = record_or_name_or_array.last
|
|
242
|
+
object_name = ActionController::RecordIdentifier.singular_class_name(object)
|
|
243
|
+
apply_form_for_options!(record_or_name_or_array, options)
|
|
244
|
+
args.unshift object
|
|
245
|
+
else
|
|
246
|
+
object = record_or_name_or_array
|
|
247
|
+
object_name = ActionController::RecordIdentifier.singular_class_name(object)
|
|
248
|
+
apply_form_for_options!([object], options)
|
|
249
|
+
args.unshift object
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}), proc.binding)
|
|
253
|
+
fields_for(object_name, *(args << options), &proc)
|
|
254
|
+
concat('</form>', proc.binding)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def apply_form_for_options!(object_or_array, options) #:nodoc:
|
|
258
|
+
object = object_or_array.is_a?(Array) ? object_or_array.last : object_or_array
|
|
259
|
+
|
|
260
|
+
html_options =
|
|
261
|
+
if object.respond_to?(:new_record?) && object.new_record?
|
|
262
|
+
{ :class => dom_class(object, :new), :id => dom_id(object), :method => :post }
|
|
263
|
+
else
|
|
264
|
+
{ :class => dom_class(object, :edit), :id => dom_id(object, :edit), :method => :put }
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
options[:html] ||= {}
|
|
268
|
+
options[:html].reverse_merge!(html_options)
|
|
269
|
+
options[:url] ||= polymorphic_path(object_or_array)
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# Creates a scope around a specific model object like form_for, but doesn't create the form tags themselves. This makes
|
|
273
|
+
# fields_for suitable for specifying additional model objects in the same form:
|
|
274
|
+
#
|
|
275
|
+
# ==== Examples
|
|
276
|
+
# <% form_for @person, :url => { :action => "update" } do |person_form| %>
|
|
277
|
+
# First name: <%= person_form.text_field :first_name %>
|
|
278
|
+
# Last name : <%= person_form.text_field :last_name %>
|
|
279
|
+
#
|
|
280
|
+
# <% fields_for @person.permission do |permission_fields| %>
|
|
281
|
+
# Admin? : <%= permission_fields.check_box :admin %>
|
|
282
|
+
# <% end %>
|
|
283
|
+
# <% end %>
|
|
284
|
+
#
|
|
285
|
+
# ...or if you have an object that needs to be represented as a different parameter, like a Client that acts as a Person:
|
|
286
|
+
#
|
|
287
|
+
# <% fields_for :person, @client do |permission_fields| %>
|
|
288
|
+
# Admin?: <%= permission_fields.check_box :admin %>
|
|
289
|
+
# <% end %>
|
|
290
|
+
#
|
|
291
|
+
# ...or if you don't have an object, just a name of the parameter
|
|
292
|
+
#
|
|
293
|
+
# <% fields_for :person do |permission_fields| %>
|
|
294
|
+
# Admin?: <%= permission_fields.check_box :admin %>
|
|
295
|
+
# <% end %>
|
|
296
|
+
#
|
|
297
|
+
# Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base,
|
|
298
|
+
# like FormOptionHelper#collection_select and DateHelper#datetime_select.
|
|
299
|
+
def fields_for(record_or_name_or_array, *args, &block)
|
|
300
|
+
raise ArgumentError, "Missing block" unless block_given?
|
|
301
|
+
options = args.extract_options!
|
|
302
|
+
|
|
303
|
+
case record_or_name_or_array
|
|
304
|
+
when String, Symbol
|
|
305
|
+
object_name = record_or_name_or_array
|
|
306
|
+
object = args.first
|
|
307
|
+
when Array
|
|
308
|
+
object = record_or_name_or_array.last
|
|
309
|
+
object_name = ActionController::RecordIdentifier.singular_class_name(object)
|
|
310
|
+
apply_form_for_options!(record_or_name_or_array, options)
|
|
311
|
+
else
|
|
312
|
+
object = record_or_name_or_array
|
|
313
|
+
object_name = ActionController::RecordIdentifier.singular_class_name(object)
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
builder = options[:builder] || ActionView::Base.default_form_builder
|
|
317
|
+
yield builder.new(object_name, object, self, options, block)
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
|
|
321
|
+
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless you specify
|
|
322
|
+
# it explicitly. Additional options on the label tag can be passed as a hash with +options+. These options will be tagged
|
|
323
|
+
# onto the HTML as an HTML element attribute as in the example shown.
|
|
324
|
+
#
|
|
325
|
+
# ==== Examples
|
|
326
|
+
# label(:post, :title)
|
|
327
|
+
# # => <label for="post_title">Title</label>
|
|
328
|
+
#
|
|
329
|
+
# label(:post, :title, "A short title")
|
|
330
|
+
# # => <label for="post_title">A short title</label>
|
|
331
|
+
#
|
|
332
|
+
# label(:post, :title, "A short title", :class => "title_label")
|
|
333
|
+
# # => <label for="post_title" class="title_label">A short title</label>
|
|
334
|
+
#
|
|
335
|
+
def label(object_name, method, text = nil, options = {})
|
|
336
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_label_tag(text, options)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object
|
|
340
|
+
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
|
341
|
+
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
|
342
|
+
# shown.
|
|
343
|
+
#
|
|
344
|
+
# ==== Examples
|
|
345
|
+
# text_field(:post, :title, :size => 20)
|
|
346
|
+
# # => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" />
|
|
347
|
+
#
|
|
348
|
+
# text_field(:post, :title, :class => "create_input")
|
|
349
|
+
# # => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" class="create_input" />
|
|
350
|
+
#
|
|
351
|
+
# text_field(:session, :user, :onchange => "if $('session[user]').value == 'admin' { alert('Your login can not be admin!'); }")
|
|
352
|
+
# # => <input type="text" id="session_user" name="session[user]" value="#{@session.user}" onchange = "if $('session[user]').value == 'admin' { alert('Your login can not be admin!'); }"/>
|
|
353
|
+
#
|
|
354
|
+
# text_field(:snippet, :code, :size => 20, :class => 'code_input')
|
|
355
|
+
# # => <input type="text" id="snippet_code" name="snippet[code]" size="20" value="#{@snippet.code}" class="code_input" />
|
|
356
|
+
#
|
|
357
|
+
def text_field(object_name, method, options = {})
|
|
358
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("text", options)
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object
|
|
362
|
+
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
|
363
|
+
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
|
364
|
+
# shown.
|
|
365
|
+
#
|
|
366
|
+
# ==== Examples
|
|
367
|
+
# password_field(:login, :pass, :size => 20)
|
|
368
|
+
# # => <input type="text" id="login_pass" name="login[pass]" size="20" value="#{@login.pass}" />
|
|
369
|
+
#
|
|
370
|
+
# password_field(:account, :secret, :class => "form_input")
|
|
371
|
+
# # => <input type="text" id="account_secret" name="account[secret]" value="#{@account.secret}" class="form_input" />
|
|
372
|
+
#
|
|
373
|
+
# password_field(:user, :password, :onchange => "if $('user[password]').length > 30 { alert('Your password needs to be shorter!'); }")
|
|
374
|
+
# # => <input type="text" id="user_password" name="user[password]" value="#{@user.password}" onchange = "if $('user[password]').length > 30 { alert('Your password needs to be shorter!'); }"/>
|
|
375
|
+
#
|
|
376
|
+
# password_field(:account, :pin, :size => 20, :class => 'form_input')
|
|
377
|
+
# # => <input type="text" id="account_pin" name="account[pin]" size="20" value="#{@account.pin}" class="form_input" />
|
|
378
|
+
#
|
|
379
|
+
def password_field(object_name, method, options = {})
|
|
380
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("password", options)
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object
|
|
384
|
+
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
|
385
|
+
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
|
386
|
+
# shown.
|
|
387
|
+
#
|
|
388
|
+
# ==== Examples
|
|
389
|
+
# hidden_field(:signup, :pass_confirm)
|
|
390
|
+
# # => <input type="hidden" id="signup_pass_confirm" name="signup[pass_confirm]" value="#{@signup.pass_confirm}" />
|
|
391
|
+
#
|
|
392
|
+
# hidden_field(:post, :tag_list)
|
|
393
|
+
# # => <input type="hidden" id="post_tag_list" name="post[tag_list]" value="#{@post.tag_list}" />
|
|
394
|
+
#
|
|
395
|
+
# hidden_field(:user, :token)
|
|
396
|
+
# # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />
|
|
397
|
+
def hidden_field(object_name, method, options = {})
|
|
398
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("hidden", options)
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# Returns an file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object
|
|
402
|
+
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
|
403
|
+
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
|
404
|
+
# shown.
|
|
405
|
+
#
|
|
406
|
+
# ==== Examples
|
|
407
|
+
# file_field(:user, :avatar)
|
|
408
|
+
# # => <input type="file" id="user_avatar" name="user[avatar]" />
|
|
409
|
+
#
|
|
410
|
+
# file_field(:post, :attached, :accept => 'text/html')
|
|
411
|
+
# # => <input type="file" id="post_attached" name="post[attached]" />
|
|
412
|
+
#
|
|
413
|
+
# file_field(:attachment, :file, :class => 'file_input')
|
|
414
|
+
# # => <input type="file" id="attachment_file" name="attachment[file]" class="file_input" />
|
|
415
|
+
#
|
|
416
|
+
def file_field(object_name, method, options = {})
|
|
417
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_input_field_tag("file", options)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+)
|
|
421
|
+
# on an object assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
|
|
422
|
+
# hash with +options+.
|
|
423
|
+
#
|
|
424
|
+
# ==== Examples
|
|
425
|
+
# text_area(:post, :body, :cols => 20, :rows => 40)
|
|
426
|
+
# # => <textarea cols="20" rows="40" id="post_body" name="post[body]">
|
|
427
|
+
# # #{@post.body}
|
|
428
|
+
# # </textarea>
|
|
429
|
+
#
|
|
430
|
+
# text_area(:comment, :text, :size => "20x30")
|
|
431
|
+
# # => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
|
|
432
|
+
# # #{@comment.text}
|
|
433
|
+
# # </textarea>
|
|
434
|
+
#
|
|
435
|
+
# text_area(:application, :notes, :cols => 40, :rows => 15, :class => 'app_input')
|
|
436
|
+
# # => <textarea cols="40" rows="15" id="application_notes" name="application[notes]" class="app_input">
|
|
437
|
+
# # #{@application.notes}
|
|
438
|
+
# # </textarea>
|
|
439
|
+
#
|
|
440
|
+
# text_area(:entry, :body, :size => "20x20", :disabled => 'disabled')
|
|
441
|
+
# # => <textarea cols="20" rows="20" id="entry_body" name="entry[body]" disabled="disabled">
|
|
442
|
+
# # #{@entry.body}
|
|
443
|
+
# # </textarea>
|
|
444
|
+
def text_area(object_name, method, options = {})
|
|
445
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_text_area_tag(options)
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
# Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object
|
|
449
|
+
# assigned to the template (identified by +object+). It's intended that +method+ returns an integer and if that
|
|
450
|
+
# integer is above zero, then the checkbox is checked. Additional options on the input tag can be passed as a
|
|
451
|
+
# hash with +options+. The +checked_value+ defaults to 1 while the default +unchecked_value+
|
|
452
|
+
# is set to 0 which is convenient for boolean values. Since HTTP standards say that unchecked checkboxes don't post anything,
|
|
453
|
+
# we add a hidden value with the same name as the checkbox as a work around.
|
|
454
|
+
#
|
|
455
|
+
# ==== Examples
|
|
456
|
+
# # Let's say that @post.validated? is 1:
|
|
457
|
+
# check_box("post", "validated")
|
|
458
|
+
# # => <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
|
|
459
|
+
# # <input name="post[validated]" type="hidden" value="0" />
|
|
460
|
+
#
|
|
461
|
+
# # Let's say that @puppy.gooddog is "no":
|
|
462
|
+
# check_box("puppy", "gooddog", {}, "yes", "no")
|
|
463
|
+
# # => <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
|
|
464
|
+
# # <input name="puppy[gooddog]" type="hidden" value="no" />
|
|
465
|
+
#
|
|
466
|
+
# check_box("eula", "accepted", { :class => 'eula_check' }, "yes", "no")
|
|
467
|
+
# # => <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
|
|
468
|
+
# # <input name="eula[accepted]" type="hidden" value="no" />
|
|
469
|
+
#
|
|
470
|
+
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
|
|
471
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_check_box_tag(options, checked_value, unchecked_value)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
|
|
475
|
+
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
|
|
476
|
+
# radio button will be checked. Additional options on the input tag can be passed as a
|
|
477
|
+
# hash with +options+.
|
|
478
|
+
#
|
|
479
|
+
# ==== Examples
|
|
480
|
+
# # Let's say that @post.category returns "rails":
|
|
481
|
+
# radio_button("post", "category", "rails")
|
|
482
|
+
# radio_button("post", "category", "java")
|
|
483
|
+
# # => <input type="radio" id="post_category_rails" name="post[category]" value="rails" checked="checked" />
|
|
484
|
+
# # <input type="radio" id="post_category_java" name="post[category]" value="java" />
|
|
485
|
+
#
|
|
486
|
+
# radio_button("user", "receive_newsletter", "yes")
|
|
487
|
+
# radio_button("user", "receive_newsletter", "no")
|
|
488
|
+
# # => <input type="radio" id="user_receive_newsletter_yes" name="user[receive_newsletter]" value="yes" />
|
|
489
|
+
# # <input type="radio" id="user_receive_newsletter_no" name="user[receive_newsletter]" value="no" checked="checked" />
|
|
490
|
+
def radio_button(object_name, method, tag_value, options = {})
|
|
491
|
+
InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_radio_button_tag(tag_value, options)
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
class InstanceTag #:nodoc:
|
|
496
|
+
include Helpers::TagHelper, Helpers::FormTagHelper
|
|
497
|
+
|
|
498
|
+
attr_reader :method_name, :object_name
|
|
499
|
+
|
|
500
|
+
DEFAULT_FIELD_OPTIONS = { "size" => 30 }.freeze unless const_defined?(:DEFAULT_FIELD_OPTIONS)
|
|
501
|
+
DEFAULT_RADIO_OPTIONS = { }.freeze unless const_defined?(:DEFAULT_RADIO_OPTIONS)
|
|
502
|
+
DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 20 }.freeze unless const_defined?(:DEFAULT_TEXT_AREA_OPTIONS)
|
|
503
|
+
|
|
504
|
+
def initialize(object_name, method_name, template_object, local_binding = nil, object = nil)
|
|
505
|
+
@object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
|
|
506
|
+
@template_object, @local_binding = template_object, local_binding
|
|
507
|
+
@object = object
|
|
508
|
+
if @object_name.sub!(/\[\]$/,"")
|
|
509
|
+
if object ||= @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param)
|
|
510
|
+
@auto_index = object.to_param
|
|
511
|
+
else
|
|
512
|
+
raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
|
|
513
|
+
end
|
|
514
|
+
end
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
def to_label_tag(text = nil, options = {})
|
|
518
|
+
options = options.stringify_keys
|
|
519
|
+
name_and_id = options.dup
|
|
520
|
+
add_default_name_and_id(name_and_id)
|
|
521
|
+
options.delete("index")
|
|
522
|
+
options["for"] ||= name_and_id["id"]
|
|
523
|
+
content = (text.blank? ? nil : text.to_s) || method_name.humanize
|
|
524
|
+
label_tag(name_and_id["id"], content, options)
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
def to_input_field_tag(field_type, options = {})
|
|
528
|
+
options = options.stringify_keys
|
|
529
|
+
options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size")
|
|
530
|
+
options = DEFAULT_FIELD_OPTIONS.merge(options)
|
|
531
|
+
if field_type == "hidden"
|
|
532
|
+
options.delete("size")
|
|
533
|
+
end
|
|
534
|
+
options["type"] = field_type
|
|
535
|
+
options["value"] ||= value_before_type_cast(object) unless field_type == "file"
|
|
536
|
+
options["value"] &&= html_escape(options["value"])
|
|
537
|
+
add_default_name_and_id(options)
|
|
538
|
+
tag("input", options)
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
def to_radio_button_tag(tag_value, options = {})
|
|
542
|
+
options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
|
|
543
|
+
options["type"] = "radio"
|
|
544
|
+
options["value"] = tag_value
|
|
545
|
+
if options.has_key?("checked")
|
|
546
|
+
cv = options.delete "checked"
|
|
547
|
+
checked = cv == true || cv == "checked"
|
|
548
|
+
else
|
|
549
|
+
checked = self.class.radio_button_checked?(value(object), tag_value)
|
|
550
|
+
end
|
|
551
|
+
options["checked"] = "checked" if checked
|
|
552
|
+
pretty_tag_value = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
|
|
553
|
+
options["id"] ||= defined?(@auto_index) ?
|
|
554
|
+
"#{tag_id_with_index(@auto_index)}_#{pretty_tag_value}" :
|
|
555
|
+
"#{tag_id}_#{pretty_tag_value}"
|
|
556
|
+
add_default_name_and_id(options)
|
|
557
|
+
tag("input", options)
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
def to_text_area_tag(options = {})
|
|
561
|
+
options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys)
|
|
562
|
+
add_default_name_and_id(options)
|
|
563
|
+
|
|
564
|
+
if size = options.delete("size")
|
|
565
|
+
options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
content_tag("textarea", html_escape(options.delete('value') || value_before_type_cast(object)), options)
|
|
569
|
+
end
|
|
570
|
+
|
|
571
|
+
def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
|
|
572
|
+
options = options.stringify_keys
|
|
573
|
+
options["type"] = "checkbox"
|
|
574
|
+
options["value"] = checked_value
|
|
575
|
+
if options.has_key?("checked")
|
|
576
|
+
cv = options.delete "checked"
|
|
577
|
+
checked = cv == true || cv == "checked"
|
|
578
|
+
else
|
|
579
|
+
checked = self.class.check_box_checked?(value(object), checked_value)
|
|
580
|
+
end
|
|
581
|
+
options["checked"] = "checked" if checked
|
|
582
|
+
add_default_name_and_id(options)
|
|
583
|
+
tag("input", options) << tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
|
|
584
|
+
end
|
|
585
|
+
|
|
586
|
+
def to_boolean_select_tag(options = {})
|
|
587
|
+
options = options.stringify_keys
|
|
588
|
+
add_default_name_and_id(options)
|
|
589
|
+
value = value(object)
|
|
590
|
+
tag_text = "<select"
|
|
591
|
+
tag_text << tag_options(options)
|
|
592
|
+
tag_text << "><option value=\"false\""
|
|
593
|
+
tag_text << " selected" if value == false
|
|
594
|
+
tag_text << ">False</option><option value=\"true\""
|
|
595
|
+
tag_text << " selected" if value
|
|
596
|
+
tag_text << ">True</option></select>"
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
def to_content_tag(tag_name, options = {})
|
|
600
|
+
content_tag(tag_name, value(object), options)
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
def object
|
|
604
|
+
@object || @template_object.instance_variable_get("@#{@object_name}")
|
|
605
|
+
rescue NameError
|
|
606
|
+
# As @object_name may contain the nested syntax (item[subobject]) we
|
|
607
|
+
# need to fallback to nil.
|
|
608
|
+
nil
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
def value(object)
|
|
612
|
+
self.class.value(object, @method_name)
|
|
613
|
+
end
|
|
614
|
+
|
|
615
|
+
def value_before_type_cast(object)
|
|
616
|
+
self.class.value_before_type_cast(object, @method_name)
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
class << self
|
|
620
|
+
def value(object, method_name)
|
|
621
|
+
object.send method_name unless object.nil?
|
|
622
|
+
end
|
|
623
|
+
|
|
624
|
+
def value_before_type_cast(object, method_name)
|
|
625
|
+
unless object.nil?
|
|
626
|
+
object.respond_to?(method_name + "_before_type_cast") ?
|
|
627
|
+
object.send(method_name + "_before_type_cast") :
|
|
628
|
+
object.send(method_name)
|
|
629
|
+
end
|
|
630
|
+
end
|
|
631
|
+
|
|
632
|
+
def check_box_checked?(value, checked_value)
|
|
633
|
+
case value
|
|
634
|
+
when TrueClass, FalseClass
|
|
635
|
+
value
|
|
636
|
+
when NilClass
|
|
637
|
+
false
|
|
638
|
+
when Integer
|
|
639
|
+
value != 0
|
|
640
|
+
when String
|
|
641
|
+
value == checked_value
|
|
642
|
+
when Array
|
|
643
|
+
value.include?(checked_value)
|
|
644
|
+
else
|
|
645
|
+
value.to_i != 0
|
|
646
|
+
end
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
def radio_button_checked?(value, checked_value)
|
|
650
|
+
value.to_s == checked_value.to_s
|
|
651
|
+
end
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
private
|
|
655
|
+
def add_default_name_and_id(options)
|
|
656
|
+
if options.has_key?("index")
|
|
657
|
+
options["name"] ||= tag_name_with_index(options["index"])
|
|
658
|
+
options["id"] ||= tag_id_with_index(options["index"])
|
|
659
|
+
options.delete("index")
|
|
660
|
+
elsif defined?(@auto_index)
|
|
661
|
+
options["name"] ||= tag_name_with_index(@auto_index)
|
|
662
|
+
options["id"] ||= tag_id_with_index(@auto_index)
|
|
663
|
+
else
|
|
664
|
+
options["name"] ||= tag_name + (options.has_key?('multiple') ? '[]' : '')
|
|
665
|
+
options["id"] ||= tag_id
|
|
666
|
+
end
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
def tag_name
|
|
670
|
+
"#{@object_name}[#{sanitized_method_name}]"
|
|
671
|
+
end
|
|
672
|
+
|
|
673
|
+
def tag_name_with_index(index)
|
|
674
|
+
"#{@object_name}[#{index}][#{sanitized_method_name}]"
|
|
675
|
+
end
|
|
676
|
+
|
|
677
|
+
def tag_id
|
|
678
|
+
"#{sanitized_object_name}_#{sanitized_method_name}"
|
|
679
|
+
end
|
|
680
|
+
|
|
681
|
+
def tag_id_with_index(index)
|
|
682
|
+
"#{sanitized_object_name}_#{index}_#{sanitized_method_name}"
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
def sanitized_object_name
|
|
686
|
+
@sanitized_object_name ||= @object_name.gsub(/[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
|
687
|
+
end
|
|
688
|
+
|
|
689
|
+
def sanitized_method_name
|
|
690
|
+
@sanitized_method_name ||= @method_name.sub(/\?$/,"")
|
|
691
|
+
end
|
|
692
|
+
end
|
|
693
|
+
|
|
694
|
+
class FormBuilder #:nodoc:
|
|
695
|
+
# The methods which wrap a form helper call.
|
|
696
|
+
class_inheritable_accessor :field_helpers
|
|
697
|
+
self.field_helpers = (FormHelper.instance_methods - ['form_for'])
|
|
698
|
+
|
|
699
|
+
attr_accessor :object_name, :object, :options
|
|
700
|
+
|
|
701
|
+
def initialize(object_name, object, template, options, proc)
|
|
702
|
+
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
|
|
703
|
+
@default_options = @options ? @options.slice(:index) : {}
|
|
704
|
+
end
|
|
705
|
+
|
|
706
|
+
(field_helpers - %w(label check_box radio_button fields_for)).each do |selector|
|
|
707
|
+
src = <<-end_src
|
|
708
|
+
def #{selector}(method, options = {})
|
|
709
|
+
@template.send(#{selector.inspect}, @object_name, method, objectify_options(options))
|
|
710
|
+
end
|
|
711
|
+
end_src
|
|
712
|
+
class_eval src, __FILE__, __LINE__
|
|
713
|
+
end
|
|
714
|
+
|
|
715
|
+
def fields_for(record_or_name_or_array, *args, &block)
|
|
716
|
+
case record_or_name_or_array
|
|
717
|
+
when String, Symbol
|
|
718
|
+
name = "#{object_name}[#{record_or_name_or_array}]"
|
|
719
|
+
when Array
|
|
720
|
+
object = record_or_name_or_array.last
|
|
721
|
+
name = "#{object_name}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
|
|
722
|
+
args.unshift(object)
|
|
723
|
+
else
|
|
724
|
+
object = record_or_name_or_array
|
|
725
|
+
name = "#{object_name}[#{ActionController::RecordIdentifier.singular_class_name(object)}]"
|
|
726
|
+
args.unshift(object)
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
@template.fields_for(name, *args, &block)
|
|
730
|
+
end
|
|
731
|
+
|
|
732
|
+
def label(method, text = nil, options = {})
|
|
733
|
+
@template.label(@object_name, method, text, objectify_options(options))
|
|
734
|
+
end
|
|
735
|
+
|
|
736
|
+
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
|
|
737
|
+
@template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
def radio_button(method, tag_value, options = {})
|
|
741
|
+
@template.radio_button(@object_name, method, tag_value, objectify_options(options))
|
|
742
|
+
end
|
|
743
|
+
|
|
744
|
+
def error_message_on(method, prepend_text = "", append_text = "", css_class = "formError")
|
|
745
|
+
@template.error_message_on(@object, method, prepend_text, append_text, css_class)
|
|
746
|
+
end
|
|
747
|
+
|
|
748
|
+
def error_messages(options = {})
|
|
749
|
+
@template.error_messages_for(@object_name, objectify_options(options))
|
|
750
|
+
end
|
|
751
|
+
|
|
752
|
+
def submit(value = "Save changes", options = {})
|
|
753
|
+
@template.submit_tag(value, options.reverse_merge(:id => "#{object_name}_submit"))
|
|
754
|
+
end
|
|
755
|
+
|
|
756
|
+
private
|
|
757
|
+
def objectify_options(options)
|
|
758
|
+
@default_options.merge(options.merge(:object => @object))
|
|
759
|
+
end
|
|
760
|
+
end
|
|
761
|
+
end
|
|
762
|
+
|
|
763
|
+
class Base
|
|
764
|
+
cattr_accessor :default_form_builder
|
|
765
|
+
self.default_form_builder = ::ActionView::Helpers::FormBuilder
|
|
766
|
+
end
|
|
767
|
+
end
|