actionpack 4.0.1 → 4.2.11.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionpack might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +402 -1173
- data/MIT-LICENSE +1 -1
- data/README.rdoc +7 -7
- data/lib/abstract_controller/base.rb +39 -7
- data/lib/abstract_controller/callbacks.rb +32 -53
- data/lib/abstract_controller/collector.rb +11 -1
- data/lib/abstract_controller/helpers.rb +26 -16
- data/lib/abstract_controller/railties/routes_helpers.rb +3 -3
- data/lib/abstract_controller/rendering.rb +57 -127
- data/lib/abstract_controller/url_for.rb +1 -1
- data/lib/abstract_controller.rb +1 -2
- data/lib/action_controller/base.rb +19 -10
- data/lib/action_controller/caching/fragments.rb +7 -1
- data/lib/action_controller/caching.rb +2 -12
- data/lib/action_controller/log_subscriber.rb +29 -20
- data/lib/action_controller/metal/conditional_get.rb +37 -12
- data/lib/action_controller/metal/data_streaming.rb +1 -1
- data/lib/action_controller/metal/etag_with_template_digest.rb +50 -0
- data/lib/action_controller/metal/exceptions.rb +1 -1
- data/lib/action_controller/metal/flash.rb +17 -0
- data/lib/action_controller/metal/force_ssl.rb +2 -2
- data/lib/action_controller/metal/head.rb +8 -6
- data/lib/action_controller/metal/helpers.rb +6 -2
- data/lib/action_controller/metal/http_authentication.rb +45 -23
- data/lib/action_controller/metal/instrumentation.rb +9 -6
- data/lib/action_controller/metal/live.rb +173 -20
- data/lib/action_controller/metal/mime_responds.rb +127 -232
- data/lib/action_controller/metal/params_wrapper.rb +16 -9
- data/lib/action_controller/metal/rack_delegation.rb +1 -1
- data/lib/action_controller/metal/redirecting.rb +34 -26
- data/lib/action_controller/metal/renderers.rb +39 -12
- data/lib/action_controller/metal/rendering.rb +41 -14
- data/lib/action_controller/metal/request_forgery_protection.rb +147 -19
- data/lib/action_controller/metal/streaming.rb +19 -21
- data/lib/action_controller/metal/strong_parameters.rb +166 -22
- data/lib/action_controller/metal/testing.rb +0 -1
- data/lib/action_controller/metal/url_for.rb +11 -12
- data/lib/action_controller/metal.rb +14 -8
- data/lib/action_controller/model_naming.rb +1 -1
- data/lib/action_controller/railtie.rb +5 -1
- data/lib/action_controller/test_case.rb +160 -94
- data/lib/action_controller.rb +2 -18
- data/lib/action_dispatch/http/cache.rb +5 -4
- data/lib/action_dispatch/http/filter_parameters.rb +2 -2
- data/lib/action_dispatch/http/filter_redirect.rb +5 -4
- data/lib/action_dispatch/http/headers.rb +46 -10
- data/lib/action_dispatch/http/mime_negotiation.rb +31 -4
- data/lib/action_dispatch/http/mime_type.rb +25 -26
- data/lib/action_dispatch/http/mime_types.rb +1 -0
- data/lib/action_dispatch/http/parameter_filter.rb +1 -1
- data/lib/action_dispatch/http/parameters.rb +25 -41
- data/lib/action_dispatch/http/request.rb +49 -32
- data/lib/action_dispatch/http/response.rb +127 -25
- data/lib/action_dispatch/http/upload.rb +9 -21
- data/lib/action_dispatch/http/url.rb +97 -70
- data/lib/action_dispatch/journey/formatter.rb +35 -19
- data/lib/action_dispatch/journey/gtg/builder.rb +3 -3
- data/lib/action_dispatch/journey/gtg/simulator.rb +10 -7
- data/lib/action_dispatch/journey/gtg/transition_table.rb +23 -33
- data/lib/action_dispatch/journey/nfa/dot.rb +2 -2
- data/lib/action_dispatch/journey/nfa/simulator.rb +1 -1
- data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -5
- data/lib/action_dispatch/journey/nodes/node.rb +4 -0
- data/lib/action_dispatch/journey/parser.rb +51 -59
- data/lib/action_dispatch/journey/parser.y +12 -10
- data/lib/action_dispatch/journey/path/pattern.rb +16 -19
- data/lib/action_dispatch/journey/route.rb +8 -19
- data/lib/action_dispatch/journey/router/strexp.rb +9 -6
- data/lib/action_dispatch/journey/router/utils.rb +54 -18
- data/lib/action_dispatch/journey/router.rb +53 -75
- data/lib/action_dispatch/journey/routes.rb +4 -0
- data/lib/action_dispatch/journey/scanner.rb +5 -5
- data/lib/action_dispatch/journey/visitors.rb +81 -60
- data/lib/action_dispatch/journey/visualizer/fsm.css +0 -4
- data/lib/action_dispatch/journey/visualizer/index.html.erb +2 -2
- data/lib/action_dispatch/middleware/callbacks.rb +7 -7
- data/lib/action_dispatch/middleware/cookies.rb +119 -43
- data/lib/action_dispatch/middleware/debug_exceptions.rb +32 -13
- data/lib/action_dispatch/middleware/exception_wrapper.rb +60 -20
- data/lib/action_dispatch/middleware/flash.rb +37 -24
- data/lib/action_dispatch/middleware/params_parser.rb +2 -2
- data/lib/action_dispatch/middleware/public_exceptions.rb +12 -3
- data/lib/action_dispatch/middleware/reloader.rb +11 -2
- data/lib/action_dispatch/middleware/remote_ip.rb +40 -54
- data/lib/action_dispatch/middleware/request_id.rb +1 -1
- data/lib/action_dispatch/middleware/session/cache_store.rb +3 -3
- data/lib/action_dispatch/middleware/session/cookie_store.rb +8 -7
- data/lib/action_dispatch/middleware/show_exceptions.rb +6 -2
- data/lib/action_dispatch/middleware/ssl.rb +10 -7
- data/lib/action_dispatch/middleware/static.rb +79 -23
- data/lib/action_dispatch/middleware/templates/rescues/{_request_and_response.erb → _request_and_response.html.erb} +0 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.erb +21 -19
- data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +52 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/{diagnostics.erb → diagnostics.html.erb} +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +6 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/rescues/{routing_error.erb → routing_error.html.erb} +3 -1
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
- data/lib/action_dispatch/middleware/templates/rescues/{unknown_action.erb → unknown_action.html.erb} +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +120 -64
- data/lib/action_dispatch/railtie.rb +5 -2
- data/lib/action_dispatch/request/session.rb +12 -0
- data/lib/action_dispatch/request/utils.rb +35 -0
- data/lib/action_dispatch/routing/endpoint.rb +10 -0
- data/lib/action_dispatch/routing/inspector.rb +11 -17
- data/lib/action_dispatch/routing/mapper.rb +519 -312
- data/lib/action_dispatch/routing/polymorphic_routes.rb +204 -79
- data/lib/action_dispatch/routing/redirection.rb +51 -26
- data/lib/action_dispatch/routing/route_set.rb +331 -206
- data/lib/action_dispatch/routing/routes_proxy.rb +5 -4
- data/lib/action_dispatch/routing/url_for.rb +19 -5
- data/lib/action_dispatch/routing.rb +9 -6
- data/lib/action_dispatch/testing/assertions/dom.rb +2 -26
- data/lib/action_dispatch/testing/assertions/response.rb +9 -15
- data/lib/action_dispatch/testing/assertions/routing.rb +22 -22
- data/lib/action_dispatch/testing/assertions/selector.rb +2 -429
- data/lib/action_dispatch/testing/assertions/tag.rb +2 -134
- data/lib/action_dispatch/testing/assertions.rb +11 -7
- data/lib/action_dispatch/testing/integration.rb +31 -29
- data/lib/action_dispatch/testing/test_request.rb +1 -1
- data/lib/action_dispatch/testing/test_response.rb +1 -5
- data/lib/action_dispatch.rb +5 -8
- data/lib/action_pack/gem_version.rb +15 -0
- data/lib/action_pack/version.rb +4 -7
- data/lib/action_pack.rb +1 -1
- metadata +77 -159
- data/lib/abstract_controller/layouts.rb +0 -423
- data/lib/abstract_controller/view_paths.rb +0 -96
- data/lib/action_controller/deprecated/integration_test.rb +0 -5
- data/lib/action_controller/deprecated.rb +0 -7
- data/lib/action_controller/metal/responder.rb +0 -287
- data/lib/action_controller/record_identifier.rb +0 -31
- data/lib/action_controller/vendor/html-scanner.rb +0 -5
- data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +0 -24
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +0 -7
- data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +0 -43
- data/lib/action_view/base.rb +0 -201
- data/lib/action_view/buffers.rb +0 -49
- data/lib/action_view/context.rb +0 -36
- data/lib/action_view/dependency_tracker.rb +0 -93
- data/lib/action_view/digestor.rb +0 -113
- data/lib/action_view/flows.rb +0 -76
- data/lib/action_view/helpers/active_model_helper.rb +0 -49
- data/lib/action_view/helpers/asset_tag_helper.rb +0 -320
- data/lib/action_view/helpers/asset_url_helper.rb +0 -355
- data/lib/action_view/helpers/atom_feed_helper.rb +0 -203
- data/lib/action_view/helpers/cache_helper.rb +0 -196
- data/lib/action_view/helpers/capture_helper.rb +0 -216
- data/lib/action_view/helpers/controller_helper.rb +0 -25
- data/lib/action_view/helpers/csrf_helper.rb +0 -30
- data/lib/action_view/helpers/date_helper.rb +0 -1083
- data/lib/action_view/helpers/debug_helper.rb +0 -39
- data/lib/action_view/helpers/form_helper.rb +0 -1880
- data/lib/action_view/helpers/form_options_helper.rb +0 -838
- data/lib/action_view/helpers/form_tag_helper.rb +0 -785
- data/lib/action_view/helpers/javascript_helper.rb +0 -117
- data/lib/action_view/helpers/number_helper.rb +0 -441
- data/lib/action_view/helpers/output_safety_helper.rb +0 -38
- data/lib/action_view/helpers/record_tag_helper.rb +0 -106
- data/lib/action_view/helpers/rendering_helper.rb +0 -90
- data/lib/action_view/helpers/sanitize_helper.rb +0 -256
- data/lib/action_view/helpers/tag_helper.rb +0 -173
- data/lib/action_view/helpers/tags/base.rb +0 -148
- data/lib/action_view/helpers/tags/check_box.rb +0 -64
- data/lib/action_view/helpers/tags/checkable.rb +0 -16
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +0 -44
- data/lib/action_view/helpers/tags/collection_helpers.rb +0 -84
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +0 -36
- data/lib/action_view/helpers/tags/collection_select.rb +0 -28
- data/lib/action_view/helpers/tags/color_field.rb +0 -25
- data/lib/action_view/helpers/tags/date_field.rb +0 -13
- data/lib/action_view/helpers/tags/date_select.rb +0 -72
- data/lib/action_view/helpers/tags/datetime_field.rb +0 -22
- data/lib/action_view/helpers/tags/datetime_local_field.rb +0 -19
- data/lib/action_view/helpers/tags/datetime_select.rb +0 -8
- data/lib/action_view/helpers/tags/email_field.rb +0 -8
- data/lib/action_view/helpers/tags/file_field.rb +0 -8
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +0 -29
- data/lib/action_view/helpers/tags/hidden_field.rb +0 -8
- data/lib/action_view/helpers/tags/label.rb +0 -66
- data/lib/action_view/helpers/tags/month_field.rb +0 -13
- data/lib/action_view/helpers/tags/number_field.rb +0 -18
- data/lib/action_view/helpers/tags/password_field.rb +0 -12
- data/lib/action_view/helpers/tags/radio_button.rb +0 -31
- data/lib/action_view/helpers/tags/range_field.rb +0 -8
- data/lib/action_view/helpers/tags/search_field.rb +0 -24
- data/lib/action_view/helpers/tags/select.rb +0 -40
- data/lib/action_view/helpers/tags/tel_field.rb +0 -8
- data/lib/action_view/helpers/tags/text_area.rb +0 -18
- data/lib/action_view/helpers/tags/text_field.rb +0 -29
- data/lib/action_view/helpers/tags/time_field.rb +0 -13
- data/lib/action_view/helpers/tags/time_select.rb +0 -8
- data/lib/action_view/helpers/tags/time_zone_select.rb +0 -20
- data/lib/action_view/helpers/tags/url_field.rb +0 -8
- data/lib/action_view/helpers/tags/week_field.rb +0 -13
- data/lib/action_view/helpers/tags.rb +0 -39
- data/lib/action_view/helpers/text_helper.rb +0 -443
- data/lib/action_view/helpers/translation_helper.rb +0 -107
- data/lib/action_view/helpers/url_helper.rb +0 -635
- data/lib/action_view/helpers.rb +0 -58
- data/lib/action_view/locale/en.yml +0 -56
- data/lib/action_view/log_subscriber.rb +0 -30
- data/lib/action_view/lookup_context.rb +0 -241
- data/lib/action_view/model_naming.rb +0 -12
- data/lib/action_view/path_set.rb +0 -77
- data/lib/action_view/railtie.rb +0 -43
- data/lib/action_view/record_identifier.rb +0 -84
- data/lib/action_view/renderer/abstract_renderer.rb +0 -47
- data/lib/action_view/renderer/partial_renderer.rb +0 -492
- data/lib/action_view/renderer/renderer.rb +0 -50
- data/lib/action_view/renderer/streaming_template_renderer.rb +0 -103
- data/lib/action_view/renderer/template_renderer.rb +0 -96
- data/lib/action_view/routing_url_for.rb +0 -107
- data/lib/action_view/tasks/dependencies.rake +0 -17
- data/lib/action_view/template/error.rb +0 -138
- data/lib/action_view/template/handlers/builder.rb +0 -26
- data/lib/action_view/template/handlers/erb.rb +0 -146
- data/lib/action_view/template/handlers/raw.rb +0 -11
- data/lib/action_view/template/handlers.rb +0 -53
- data/lib/action_view/template/resolver.rb +0 -326
- data/lib/action_view/template/text.rb +0 -34
- data/lib/action_view/template/types.rb +0 -57
- data/lib/action_view/template.rb +0 -339
- data/lib/action_view/test_case.rb +0 -270
- data/lib/action_view/testing/resolvers.rb +0 -50
- data/lib/action_view/vendor/html-scanner/html/document.rb +0 -68
- data/lib/action_view/vendor/html-scanner/html/node.rb +0 -532
- data/lib/action_view/vendor/html-scanner/html/sanitizer.rb +0 -188
- data/lib/action_view/vendor/html-scanner/html/selector.rb +0 -830
- data/lib/action_view/vendor/html-scanner/html/tokenizer.rb +0 -107
- data/lib/action_view/vendor/html-scanner/html/version.rb +0 -11
- data/lib/action_view/vendor/html-scanner.rb +0 -20
- data/lib/action_view.rb +0 -93
@@ -1,443 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/string/filters'
|
2
|
-
require 'active_support/core_ext/array/extract_options'
|
3
|
-
|
4
|
-
module ActionView
|
5
|
-
# = Action View Text Helpers
|
6
|
-
module Helpers #:nodoc:
|
7
|
-
# The TextHelper module provides a set of methods for filtering, formatting
|
8
|
-
# and transforming strings, which can reduce the amount of inline Ruby code in
|
9
|
-
# your views. These helper methods extend Action View making them callable
|
10
|
-
# within your template files.
|
11
|
-
#
|
12
|
-
# ==== Sanitization
|
13
|
-
#
|
14
|
-
# Most text helpers by default sanitize the given content, but do not escape it.
|
15
|
-
# This means HTML tags will appear in the page but all malicious code will be removed.
|
16
|
-
# Let's look at some examples using the +simple_format+ method:
|
17
|
-
#
|
18
|
-
# simple_format('<a href="http://example.com/">Example</a>')
|
19
|
-
# # => "<p><a href=\"http://example.com/\">Example</a></p>"
|
20
|
-
#
|
21
|
-
# simple_format('<a href="javascript:alert(\'no!\')">Example</a>')
|
22
|
-
# # => "<p><a>Example</a></p>"
|
23
|
-
#
|
24
|
-
# If you want to escape all content, you should invoke the +h+ method before
|
25
|
-
# calling the text helper.
|
26
|
-
#
|
27
|
-
# simple_format h('<a href="http://example.com/">Example</a>')
|
28
|
-
# # => "<p><a href=\"http://example.com/\">Example</a></p>"
|
29
|
-
module TextHelper
|
30
|
-
extend ActiveSupport::Concern
|
31
|
-
|
32
|
-
include SanitizeHelper
|
33
|
-
include TagHelper
|
34
|
-
# The preferred method of outputting text in your views is to use the
|
35
|
-
# <%= "text" %> eRuby syntax. The regular _puts_ and _print_ methods
|
36
|
-
# do not operate as expected in an eRuby code block. If you absolutely must
|
37
|
-
# output text within a non-output code block (i.e., <% %>), you can use the concat method.
|
38
|
-
#
|
39
|
-
# <%
|
40
|
-
# concat "hello"
|
41
|
-
# # is the equivalent of <%= "hello" %>
|
42
|
-
#
|
43
|
-
# if logged_in
|
44
|
-
# concat "Logged in!"
|
45
|
-
# else
|
46
|
-
# concat link_to('login', action: :login)
|
47
|
-
# end
|
48
|
-
# # will either display "Logged in!" or a login link
|
49
|
-
# %>
|
50
|
-
def concat(string)
|
51
|
-
output_buffer << string
|
52
|
-
end
|
53
|
-
|
54
|
-
def safe_concat(string)
|
55
|
-
output_buffer.respond_to?(:safe_concat) ? output_buffer.safe_concat(string) : concat(string)
|
56
|
-
end
|
57
|
-
|
58
|
-
# Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt>
|
59
|
-
# (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "...")
|
60
|
-
# for a total length not exceeding <tt>:length</tt>.
|
61
|
-
#
|
62
|
-
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break.
|
63
|
-
#
|
64
|
-
# Pass a block if you want to show extra content when the text is truncated.
|
65
|
-
#
|
66
|
-
# The result is marked as HTML-safe, but it is escaped by default, unless <tt>:escape</tt> is
|
67
|
-
# +false+. Care should be taken if +text+ contains HTML tags or entities, because truncation
|
68
|
-
# may produce invalid HTML (such as unbalanced or incomplete tags).
|
69
|
-
#
|
70
|
-
# truncate("Once upon a time in a world far far away")
|
71
|
-
# # => "Once upon a time in a world..."
|
72
|
-
#
|
73
|
-
# truncate("Once upon a time in a world far far away", length: 17)
|
74
|
-
# # => "Once upon a ti..."
|
75
|
-
#
|
76
|
-
# truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
|
77
|
-
# # => "Once upon a..."
|
78
|
-
#
|
79
|
-
# truncate("And they found that many people were sleeping better.", length: 25, omission: '... (continued)')
|
80
|
-
# # => "And they f... (continued)"
|
81
|
-
#
|
82
|
-
# truncate("<p>Once upon a time in a world far far away</p>")
|
83
|
-
# # => "<p>Once upon a time in a wo..."
|
84
|
-
#
|
85
|
-
# truncate("Once upon a time in a world far far away") { link_to "Continue", "#" }
|
86
|
-
# # => "Once upon a time in a wo...<a href="#">Continue</a>"
|
87
|
-
def truncate(text, options = {}, &block)
|
88
|
-
if text
|
89
|
-
length = options.fetch(:length, 30)
|
90
|
-
|
91
|
-
content = text.truncate(length, options)
|
92
|
-
content = options[:escape] == false ? content.html_safe : ERB::Util.html_escape(content)
|
93
|
-
content << capture(&block) if block_given? && text.length > length
|
94
|
-
content
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
|
99
|
-
# a <tt>:highlighter</tt> string. The highlighter can be specialized by passing <tt>:highlighter</tt>
|
100
|
-
# as a single-quoted string with <tt>\1</tt> where the phrase is to be inserted (defaults to
|
101
|
-
# '<mark>\1</mark>')
|
102
|
-
#
|
103
|
-
# highlight('You searched for: rails', 'rails')
|
104
|
-
# # => You searched for: <mark>rails</mark>
|
105
|
-
#
|
106
|
-
# highlight('You searched for: ruby, rails, dhh', 'actionpack')
|
107
|
-
# # => You searched for: ruby, rails, dhh
|
108
|
-
#
|
109
|
-
# highlight('You searched for: rails', ['for', 'rails'], highlighter: '<em>\1</em>')
|
110
|
-
# # => You searched <em>for</em>: <em>rails</em>
|
111
|
-
#
|
112
|
-
# highlight('You searched for: rails', 'rails', highlighter: '<a href="search?q=\1">\1</a>')
|
113
|
-
# # => You searched for: <a href="search?q=rails">rails</a>
|
114
|
-
def highlight(text, phrases, options = {})
|
115
|
-
text = sanitize(text) if options.fetch(:sanitize, true)
|
116
|
-
|
117
|
-
if text.blank? || phrases.blank?
|
118
|
-
text
|
119
|
-
else
|
120
|
-
highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
|
121
|
-
match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
|
122
|
-
text.gsub(/(#{match})(?![^<]*?>)/i, highlighter)
|
123
|
-
end.html_safe
|
124
|
-
end
|
125
|
-
|
126
|
-
# Extracts an excerpt from +text+ that matches the first instance of +phrase+.
|
127
|
-
# The <tt>:radius</tt> option expands the excerpt on each side of the first occurrence of +phrase+ by the number of characters
|
128
|
-
# defined in <tt>:radius</tt> (which defaults to 100). If the excerpt radius overflows the beginning or end of the +text+,
|
129
|
-
# then the <tt>:omission</tt> option (which defaults to "...") will be prepended/appended accordingly. Use the
|
130
|
-
# <tt>:separator</tt> option to choose the delimitation. The resulting string will be stripped in any case. If the +phrase+
|
131
|
-
# isn't found, nil is returned.
|
132
|
-
#
|
133
|
-
# excerpt('This is an example', 'an', radius: 5)
|
134
|
-
# # => ...s is an exam...
|
135
|
-
#
|
136
|
-
# excerpt('This is an example', 'is', radius: 5)
|
137
|
-
# # => This is a...
|
138
|
-
#
|
139
|
-
# excerpt('This is an example', 'is')
|
140
|
-
# # => This is an example
|
141
|
-
#
|
142
|
-
# excerpt('This next thing is an example', 'ex', radius: 2)
|
143
|
-
# # => ...next...
|
144
|
-
#
|
145
|
-
# excerpt('This is also an example', 'an', radius: 8, omission: '<chop> ')
|
146
|
-
# # => <chop> is also an example
|
147
|
-
#
|
148
|
-
# excerpt('This is a very beautiful morning', 'very', separator: ' ', radius: 1)
|
149
|
-
# # => ...a very beautiful...
|
150
|
-
def excerpt(text, phrase, options = {})
|
151
|
-
return unless text && phrase
|
152
|
-
|
153
|
-
separator = options[:separator] || ''
|
154
|
-
phrase = Regexp.escape(phrase)
|
155
|
-
regex = /#{phrase}/i
|
156
|
-
|
157
|
-
return unless matches = text.match(regex)
|
158
|
-
phrase = matches[0]
|
159
|
-
|
160
|
-
text.split(separator).each do |value|
|
161
|
-
if value.match(regex)
|
162
|
-
regex = phrase = value
|
163
|
-
break
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
first_part, second_part = text.split(regex, 2)
|
168
|
-
|
169
|
-
prefix, first_part = cut_excerpt_part(:first, first_part, separator, options)
|
170
|
-
postfix, second_part = cut_excerpt_part(:second, second_part, separator, options)
|
171
|
-
|
172
|
-
affix = [first_part, separator, phrase, separator, second_part].join.strip
|
173
|
-
[prefix, affix, postfix].join
|
174
|
-
end
|
175
|
-
|
176
|
-
# Attempts to pluralize the +singular+ word unless +count+ is 1. If
|
177
|
-
# +plural+ is supplied, it will use that when count is > 1, otherwise
|
178
|
-
# it will use the Inflector to determine the plural form.
|
179
|
-
#
|
180
|
-
# pluralize(1, 'person')
|
181
|
-
# # => 1 person
|
182
|
-
#
|
183
|
-
# pluralize(2, 'person')
|
184
|
-
# # => 2 people
|
185
|
-
#
|
186
|
-
# pluralize(3, 'person', 'users')
|
187
|
-
# # => 3 users
|
188
|
-
#
|
189
|
-
# pluralize(0, 'person')
|
190
|
-
# # => 0 people
|
191
|
-
def pluralize(count, singular, plural = nil)
|
192
|
-
word = if (count == 1 || count =~ /^1(\.0+)?$/)
|
193
|
-
singular
|
194
|
-
else
|
195
|
-
plural || singular.pluralize
|
196
|
-
end
|
197
|
-
|
198
|
-
"#{count || 0} #{word}"
|
199
|
-
end
|
200
|
-
|
201
|
-
# Wraps the +text+ into lines no longer than +line_width+ width. This method
|
202
|
-
# breaks on the first whitespace character that does not exceed +line_width+
|
203
|
-
# (which is 80 by default).
|
204
|
-
#
|
205
|
-
# word_wrap('Once upon a time')
|
206
|
-
# # => Once upon a time
|
207
|
-
#
|
208
|
-
# word_wrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...')
|
209
|
-
# # => Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\na successor to the throne turned out to be more trouble than anyone could have\nimagined...
|
210
|
-
#
|
211
|
-
# word_wrap('Once upon a time', line_width: 8)
|
212
|
-
# # => Once\nupon a\ntime
|
213
|
-
#
|
214
|
-
# word_wrap('Once upon a time', line_width: 1)
|
215
|
-
# # => Once\nupon\na\ntime
|
216
|
-
def word_wrap(text, options = {})
|
217
|
-
line_width = options.fetch(:line_width, 80)
|
218
|
-
|
219
|
-
text.split("\n").collect do |line|
|
220
|
-
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
|
221
|
-
end * "\n"
|
222
|
-
end
|
223
|
-
|
224
|
-
# Returns +text+ transformed into HTML using simple formatting rules.
|
225
|
-
# Two or more consecutive newlines(<tt>\n\n</tt>) are considered as a
|
226
|
-
# paragraph and wrapped in <tt><p></tt> tags. One newline (<tt>\n</tt>) is
|
227
|
-
# considered as a linebreak and a <tt><br /></tt> tag is appended. This
|
228
|
-
# method does not remove the newlines from the +text+.
|
229
|
-
#
|
230
|
-
# You can pass any HTML attributes into <tt>html_options</tt>. These
|
231
|
-
# will be added to all created paragraphs.
|
232
|
-
#
|
233
|
-
# ==== Options
|
234
|
-
# * <tt>:sanitize</tt> - If +false+, does not sanitize +text+.
|
235
|
-
# * <tt>:wrapper_tag</tt> - String representing the wrapper tag, defaults to <tt>"p"</tt>
|
236
|
-
#
|
237
|
-
# ==== Examples
|
238
|
-
# my_text = "Here is some basic text...\n...with a line break."
|
239
|
-
#
|
240
|
-
# simple_format(my_text)
|
241
|
-
# # => "<p>Here is some basic text...\n<br />...with a line break.</p>"
|
242
|
-
#
|
243
|
-
# simple_format(my_text, {}, wrapper_tag: "div")
|
244
|
-
# # => "<div>Here is some basic text...\n<br />...with a line break.</div>"
|
245
|
-
#
|
246
|
-
# more_text = "We want to put a paragraph...\n\n...right there."
|
247
|
-
#
|
248
|
-
# simple_format(more_text)
|
249
|
-
# # => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"
|
250
|
-
#
|
251
|
-
# simple_format("Look ma! A class!", class: 'description')
|
252
|
-
# # => "<p class='description'>Look ma! A class!</p>"
|
253
|
-
#
|
254
|
-
# simple_format("<blink>Unblinkable.</blink>")
|
255
|
-
# # => "<p>Unblinkable.</p>"
|
256
|
-
#
|
257
|
-
# simple_format("<blink>Blinkable!</blink> It's true.", {}, sanitize: false)
|
258
|
-
# # => "<p><blink>Blinkable!</span> It's true.</p>"
|
259
|
-
def simple_format(text, html_options = {}, options = {})
|
260
|
-
wrapper_tag = options.fetch(:wrapper_tag, :p)
|
261
|
-
|
262
|
-
text = sanitize(text) if options.fetch(:sanitize, true)
|
263
|
-
paragraphs = split_paragraphs(text)
|
264
|
-
|
265
|
-
if paragraphs.empty?
|
266
|
-
content_tag(wrapper_tag, nil, html_options)
|
267
|
-
else
|
268
|
-
paragraphs.map { |paragraph|
|
269
|
-
content_tag(wrapper_tag, paragraph, html_options, options[:sanitize])
|
270
|
-
}.join("\n\n").html_safe
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
# Creates a Cycle object whose _to_s_ method cycles through elements of an
|
275
|
-
# array every time it is called. This can be used for example, to alternate
|
276
|
-
# classes for table rows. You can use named cycles to allow nesting in loops.
|
277
|
-
# Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
|
278
|
-
# named cycle. The default name for a cycle without a +:name+ key is
|
279
|
-
# <tt>"default"</tt>. You can manually reset a cycle by calling reset_cycle
|
280
|
-
# and passing the name of the cycle. The current cycle string can be obtained
|
281
|
-
# anytime using the current_cycle method.
|
282
|
-
#
|
283
|
-
# # Alternate CSS classes for even and odd numbers...
|
284
|
-
# @items = [1,2,3,4]
|
285
|
-
# <table>
|
286
|
-
# <% @items.each do |item| %>
|
287
|
-
# <tr class="<%= cycle("odd", "even") -%>">
|
288
|
-
# <td>item</td>
|
289
|
-
# </tr>
|
290
|
-
# <% end %>
|
291
|
-
# </table>
|
292
|
-
#
|
293
|
-
#
|
294
|
-
# # Cycle CSS classes for rows, and text colors for values within each row
|
295
|
-
# @items = x = [{first: 'Robert', middle: 'Daniel', last: 'James'},
|
296
|
-
# {first: 'Emily', middle: 'Shannon', maiden: 'Pike', last: 'Hicks'},
|
297
|
-
# {first: 'June', middle: 'Dae', last: 'Jones'}]
|
298
|
-
# <% @items.each do |item| %>
|
299
|
-
# <tr class="<%= cycle("odd", "even", name: "row_class") -%>">
|
300
|
-
# <td>
|
301
|
-
# <% item.values.each do |value| %>
|
302
|
-
# <%# Create a named cycle "colors" %>
|
303
|
-
# <span style="color:<%= cycle("red", "green", "blue", name: "colors") -%>">
|
304
|
-
# <%= value %>
|
305
|
-
# </span>
|
306
|
-
# <% end %>
|
307
|
-
# <% reset_cycle("colors") %>
|
308
|
-
# </td>
|
309
|
-
# </tr>
|
310
|
-
# <% end %>
|
311
|
-
def cycle(first_value, *values)
|
312
|
-
options = values.extract_options!
|
313
|
-
name = options.fetch(:name, 'default')
|
314
|
-
|
315
|
-
values.unshift(first_value)
|
316
|
-
|
317
|
-
cycle = get_cycle(name)
|
318
|
-
unless cycle && cycle.values == values
|
319
|
-
cycle = set_cycle(name, Cycle.new(*values))
|
320
|
-
end
|
321
|
-
cycle.to_s
|
322
|
-
end
|
323
|
-
|
324
|
-
# Returns the current cycle string after a cycle has been started. Useful
|
325
|
-
# for complex table highlighting or any other design need which requires
|
326
|
-
# the current cycle string in more than one place.
|
327
|
-
#
|
328
|
-
# # Alternate background colors
|
329
|
-
# @items = [1,2,3,4]
|
330
|
-
# <% @items.each do |item| %>
|
331
|
-
# <div style="background-color:<%= cycle("red","white","blue") %>">
|
332
|
-
# <span style="background-color:<%= current_cycle %>"><%= item %></span>
|
333
|
-
# </div>
|
334
|
-
# <% end %>
|
335
|
-
def current_cycle(name = "default")
|
336
|
-
cycle = get_cycle(name)
|
337
|
-
cycle.current_value if cycle
|
338
|
-
end
|
339
|
-
|
340
|
-
# Resets a cycle so that it starts from the first element the next time
|
341
|
-
# it is called. Pass in +name+ to reset a named cycle.
|
342
|
-
#
|
343
|
-
# # Alternate CSS classes for even and odd numbers...
|
344
|
-
# @items = [[1,2,3,4], [5,6,3], [3,4,5,6,7,4]]
|
345
|
-
# <table>
|
346
|
-
# <% @items.each do |item| %>
|
347
|
-
# <tr class="<%= cycle("even", "odd") -%>">
|
348
|
-
# <% item.each do |value| %>
|
349
|
-
# <span style="color:<%= cycle("#333", "#666", "#999", name: "colors") -%>">
|
350
|
-
# <%= value %>
|
351
|
-
# </span>
|
352
|
-
# <% end %>
|
353
|
-
#
|
354
|
-
# <% reset_cycle("colors") %>
|
355
|
-
# </tr>
|
356
|
-
# <% end %>
|
357
|
-
# </table>
|
358
|
-
def reset_cycle(name = "default")
|
359
|
-
cycle = get_cycle(name)
|
360
|
-
cycle.reset if cycle
|
361
|
-
end
|
362
|
-
|
363
|
-
class Cycle #:nodoc:
|
364
|
-
attr_reader :values
|
365
|
-
|
366
|
-
def initialize(first_value, *values)
|
367
|
-
@values = values.unshift(first_value)
|
368
|
-
reset
|
369
|
-
end
|
370
|
-
|
371
|
-
def reset
|
372
|
-
@index = 0
|
373
|
-
end
|
374
|
-
|
375
|
-
def current_value
|
376
|
-
@values[previous_index].to_s
|
377
|
-
end
|
378
|
-
|
379
|
-
def to_s
|
380
|
-
value = @values[@index].to_s
|
381
|
-
@index = next_index
|
382
|
-
return value
|
383
|
-
end
|
384
|
-
|
385
|
-
private
|
386
|
-
|
387
|
-
def next_index
|
388
|
-
step_index(1)
|
389
|
-
end
|
390
|
-
|
391
|
-
def previous_index
|
392
|
-
step_index(-1)
|
393
|
-
end
|
394
|
-
|
395
|
-
def step_index(n)
|
396
|
-
(@index + n) % @values.size
|
397
|
-
end
|
398
|
-
end
|
399
|
-
|
400
|
-
private
|
401
|
-
# The cycle helpers need to store the cycles in a place that is
|
402
|
-
# guaranteed to be reset every time a page is rendered, so it
|
403
|
-
# uses an instance variable of ActionView::Base.
|
404
|
-
def get_cycle(name)
|
405
|
-
@_cycles = Hash.new unless defined?(@_cycles)
|
406
|
-
return @_cycles[name]
|
407
|
-
end
|
408
|
-
|
409
|
-
def set_cycle(name, cycle_object)
|
410
|
-
@_cycles = Hash.new unless defined?(@_cycles)
|
411
|
-
@_cycles[name] = cycle_object
|
412
|
-
end
|
413
|
-
|
414
|
-
def split_paragraphs(text)
|
415
|
-
return [] if text.blank?
|
416
|
-
|
417
|
-
text.to_str.gsub(/\r\n?/, "\n").split(/\n\n+/).map! do |t|
|
418
|
-
t.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') || t
|
419
|
-
end
|
420
|
-
end
|
421
|
-
|
422
|
-
def cut_excerpt_part(part_position, part, separator, options)
|
423
|
-
return "", "" unless part
|
424
|
-
|
425
|
-
radius = options.fetch(:radius, 100)
|
426
|
-
omission = options.fetch(:omission, "...")
|
427
|
-
|
428
|
-
part = part.split(separator)
|
429
|
-
part.delete("")
|
430
|
-
affix = part.size > radius ? omission : ""
|
431
|
-
|
432
|
-
part = if part_position == :first
|
433
|
-
drop_index = [part.length - radius, 0].max
|
434
|
-
part.drop(drop_index)
|
435
|
-
else
|
436
|
-
part.first(radius)
|
437
|
-
end
|
438
|
-
|
439
|
-
return affix, part.join(separator)
|
440
|
-
end
|
441
|
-
end
|
442
|
-
end
|
443
|
-
end
|
@@ -1,107 +0,0 @@
|
|
1
|
-
require 'action_view/helpers/tag_helper'
|
2
|
-
require 'i18n/exceptions'
|
3
|
-
|
4
|
-
module I18n
|
5
|
-
class ExceptionHandler
|
6
|
-
include Module.new {
|
7
|
-
def call(exception, locale, key, options)
|
8
|
-
exception.is_a?(MissingTranslation) && options[:rescue_format] == :html ? super.html_safe : super
|
9
|
-
end
|
10
|
-
}
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
module ActionView
|
15
|
-
# = Action View Translation Helpers
|
16
|
-
module Helpers
|
17
|
-
module TranslationHelper
|
18
|
-
# Delegates to <tt>I18n#translate</tt> but also performs three additional functions.
|
19
|
-
#
|
20
|
-
# First, it'll pass the <tt>rescue_format: :html</tt> option to I18n so that any
|
21
|
-
# thrown +MissingTranslation+ messages will be turned into inline spans that
|
22
|
-
#
|
23
|
-
# * have a "translation-missing" class set,
|
24
|
-
# * contain the missing key as a title attribute and
|
25
|
-
# * a titleized version of the last key segment as a text.
|
26
|
-
#
|
27
|
-
# E.g. the value returned for a missing translation key :"blog.post.title" will be
|
28
|
-
# <span class="translation_missing" title="translation missing: en.blog.post.title">Title</span>.
|
29
|
-
# This way your views will display rather reasonable strings but it will still
|
30
|
-
# be easy to spot missing translations.
|
31
|
-
#
|
32
|
-
# Second, it'll scope the key by the current partial if the key starts
|
33
|
-
# with a period. So if you call <tt>translate(".foo")</tt> from the
|
34
|
-
# <tt>people/index.html.erb</tt> template, you'll actually be calling
|
35
|
-
# <tt>I18n.translate("people.index.foo")</tt>. This makes it less repetitive
|
36
|
-
# to translate many keys within the same partials and gives you a simple framework
|
37
|
-
# for scoping them consistently. If you don't prepend the key with a period,
|
38
|
-
# nothing is converted.
|
39
|
-
#
|
40
|
-
# Third, it'll mark the translation as safe HTML if the key has the suffix
|
41
|
-
# "_html" or the last element of the key is the word "html". For example,
|
42
|
-
# calling translate("footer_html") or translate("footer.html") will return
|
43
|
-
# a safe HTML string that won't be escaped by other HTML helper methods. This
|
44
|
-
# naming convention helps to identify translations that include HTML tags so that
|
45
|
-
# you know what kind of output to expect when you call translate in a template.
|
46
|
-
def translate(key, options = {})
|
47
|
-
options.merge!(:rescue_format => :html) unless options.key?(:rescue_format)
|
48
|
-
options[:default] = wrap_translate_defaults(options[:default]) if options[:default]
|
49
|
-
if html_safe_translation_key?(key)
|
50
|
-
html_safe_options = options.dup
|
51
|
-
options.except(*I18n::RESERVED_KEYS).each do |name, value|
|
52
|
-
unless name == :count && value.is_a?(Numeric)
|
53
|
-
html_safe_options[name] = ERB::Util.html_escape(value.to_s)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
translation = I18n.translate(scope_key_by_partial(key), html_safe_options)
|
57
|
-
|
58
|
-
translation.respond_to?(:html_safe) ? translation.html_safe : translation
|
59
|
-
else
|
60
|
-
I18n.translate(scope_key_by_partial(key), options)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
alias :t :translate
|
64
|
-
|
65
|
-
# Delegates to <tt>I18n.localize</tt> with no additional functionality.
|
66
|
-
#
|
67
|
-
# See http://rubydoc.info/github/svenfuchs/i18n/master/I18n/Backend/Base:localize
|
68
|
-
# for more information.
|
69
|
-
def localize(*args)
|
70
|
-
I18n.localize(*args)
|
71
|
-
end
|
72
|
-
alias :l :localize
|
73
|
-
|
74
|
-
private
|
75
|
-
def scope_key_by_partial(key)
|
76
|
-
if key.to_s.first == "."
|
77
|
-
if @virtual_path
|
78
|
-
@virtual_path.gsub(%r{/_?}, ".") + key.to_s
|
79
|
-
else
|
80
|
-
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
|
81
|
-
end
|
82
|
-
else
|
83
|
-
key
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def html_safe_translation_key?(key)
|
88
|
-
key.to_s =~ /(\b|_|\.)html$/
|
89
|
-
end
|
90
|
-
|
91
|
-
def wrap_translate_defaults(defaults)
|
92
|
-
new_defaults = []
|
93
|
-
defaults = Array(defaults)
|
94
|
-
while key = defaults.shift
|
95
|
-
if key.is_a?(Symbol)
|
96
|
-
new_defaults << lambda { |_, options| translate key, options.merge(:default => defaults) }
|
97
|
-
break
|
98
|
-
else
|
99
|
-
new_defaults << key
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
new_defaults
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|