omg-actionview 8.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +25 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +40 -0
- data/app/assets/javascripts/rails-ujs.esm.js +686 -0
- data/app/assets/javascripts/rails-ujs.js +630 -0
- data/lib/action_view/base.rb +316 -0
- data/lib/action_view/buffers.rb +165 -0
- data/lib/action_view/cache_expiry.rb +69 -0
- data/lib/action_view/context.rb +32 -0
- data/lib/action_view/dependency_tracker/erb_tracker.rb +159 -0
- data/lib/action_view/dependency_tracker/ruby_tracker.rb +43 -0
- data/lib/action_view/dependency_tracker/wildcard_resolver.rb +32 -0
- data/lib/action_view/dependency_tracker.rb +41 -0
- data/lib/action_view/deprecator.rb +7 -0
- data/lib/action_view/digestor.rb +130 -0
- data/lib/action_view/flows.rb +75 -0
- data/lib/action_view/gem_version.rb +17 -0
- data/lib/action_view/helpers/active_model_helper.rb +54 -0
- data/lib/action_view/helpers/asset_tag_helper.rb +680 -0
- data/lib/action_view/helpers/asset_url_helper.rb +473 -0
- data/lib/action_view/helpers/atom_feed_helper.rb +205 -0
- data/lib/action_view/helpers/cache_helper.rb +315 -0
- data/lib/action_view/helpers/capture_helper.rb +236 -0
- data/lib/action_view/helpers/content_exfiltration_prevention_helper.rb +70 -0
- data/lib/action_view/helpers/controller_helper.rb +42 -0
- data/lib/action_view/helpers/csp_helper.rb +26 -0
- data/lib/action_view/helpers/csrf_helper.rb +35 -0
- data/lib/action_view/helpers/date_helper.rb +1266 -0
- data/lib/action_view/helpers/debug_helper.rb +38 -0
- data/lib/action_view/helpers/form_helper.rb +2765 -0
- data/lib/action_view/helpers/form_options_helper.rb +927 -0
- data/lib/action_view/helpers/form_tag_helper.rb +1088 -0
- data/lib/action_view/helpers/javascript_helper.rb +96 -0
- data/lib/action_view/helpers/number_helper.rb +165 -0
- data/lib/action_view/helpers/output_safety_helper.rb +70 -0
- data/lib/action_view/helpers/rendering_helper.rb +218 -0
- data/lib/action_view/helpers/sanitize_helper.rb +201 -0
- data/lib/action_view/helpers/tag_helper.rb +621 -0
- data/lib/action_view/helpers/tags/base.rb +138 -0
- data/lib/action_view/helpers/tags/check_box.rb +65 -0
- data/lib/action_view/helpers/tags/checkable.rb +18 -0
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +37 -0
- data/lib/action_view/helpers/tags/collection_helpers.rb +118 -0
- data/lib/action_view/helpers/tags/collection_radio_buttons.rb +31 -0
- data/lib/action_view/helpers/tags/collection_select.rb +33 -0
- data/lib/action_view/helpers/tags/color_field.rb +26 -0
- data/lib/action_view/helpers/tags/date_field.rb +14 -0
- data/lib/action_view/helpers/tags/date_select.rb +75 -0
- data/lib/action_view/helpers/tags/datetime_field.rb +39 -0
- data/lib/action_view/helpers/tags/datetime_local_field.rb +29 -0
- data/lib/action_view/helpers/tags/datetime_select.rb +10 -0
- data/lib/action_view/helpers/tags/email_field.rb +10 -0
- data/lib/action_view/helpers/tags/file_field.rb +26 -0
- data/lib/action_view/helpers/tags/grouped_collection_select.rb +34 -0
- data/lib/action_view/helpers/tags/hidden_field.rb +14 -0
- data/lib/action_view/helpers/tags/label.rb +84 -0
- data/lib/action_view/helpers/tags/month_field.rb +14 -0
- data/lib/action_view/helpers/tags/number_field.rb +20 -0
- data/lib/action_view/helpers/tags/password_field.rb +14 -0
- data/lib/action_view/helpers/tags/placeholderable.rb +24 -0
- data/lib/action_view/helpers/tags/radio_button.rb +32 -0
- data/lib/action_view/helpers/tags/range_field.rb +10 -0
- data/lib/action_view/helpers/tags/search_field.rb +27 -0
- data/lib/action_view/helpers/tags/select.rb +45 -0
- data/lib/action_view/helpers/tags/select_renderer.rb +56 -0
- data/lib/action_view/helpers/tags/tel_field.rb +10 -0
- data/lib/action_view/helpers/tags/text_area.rb +24 -0
- data/lib/action_view/helpers/tags/text_field.rb +33 -0
- data/lib/action_view/helpers/tags/time_field.rb +23 -0
- data/lib/action_view/helpers/tags/time_select.rb +10 -0
- data/lib/action_view/helpers/tags/time_zone_select.rb +25 -0
- data/lib/action_view/helpers/tags/translator.rb +39 -0
- data/lib/action_view/helpers/tags/url_field.rb +10 -0
- data/lib/action_view/helpers/tags/week_field.rb +14 -0
- data/lib/action_view/helpers/tags/weekday_select.rb +31 -0
- data/lib/action_view/helpers/tags.rb +47 -0
- data/lib/action_view/helpers/text_helper.rb +568 -0
- data/lib/action_view/helpers/translation_helper.rb +161 -0
- data/lib/action_view/helpers/url_helper.rb +812 -0
- data/lib/action_view/helpers.rb +68 -0
- data/lib/action_view/layouts.rb +434 -0
- data/lib/action_view/locale/en.yml +56 -0
- data/lib/action_view/log_subscriber.rb +132 -0
- data/lib/action_view/lookup_context.rb +299 -0
- data/lib/action_view/model_naming.rb +14 -0
- data/lib/action_view/path_registry.rb +57 -0
- data/lib/action_view/path_set.rb +84 -0
- data/lib/action_view/railtie.rb +132 -0
- data/lib/action_view/record_identifier.rb +118 -0
- data/lib/action_view/render_parser/prism_render_parser.rb +139 -0
- data/lib/action_view/render_parser/ripper_render_parser.rb +350 -0
- data/lib/action_view/render_parser.rb +40 -0
- data/lib/action_view/renderer/abstract_renderer.rb +186 -0
- data/lib/action_view/renderer/collection_renderer.rb +204 -0
- data/lib/action_view/renderer/object_renderer.rb +34 -0
- data/lib/action_view/renderer/partial_renderer/collection_caching.rb +120 -0
- data/lib/action_view/renderer/partial_renderer.rb +267 -0
- data/lib/action_view/renderer/renderer.rb +107 -0
- data/lib/action_view/renderer/streaming_template_renderer.rb +107 -0
- data/lib/action_view/renderer/template_renderer.rb +115 -0
- data/lib/action_view/rendering.rb +190 -0
- data/lib/action_view/routing_url_for.rb +149 -0
- data/lib/action_view/tasks/cache_digests.rake +25 -0
- data/lib/action_view/template/error.rb +264 -0
- data/lib/action_view/template/handlers/builder.rb +25 -0
- data/lib/action_view/template/handlers/erb/erubi.rb +85 -0
- data/lib/action_view/template/handlers/erb.rb +157 -0
- data/lib/action_view/template/handlers/html.rb +11 -0
- data/lib/action_view/template/handlers/raw.rb +11 -0
- data/lib/action_view/template/handlers.rb +66 -0
- data/lib/action_view/template/html.rb +33 -0
- data/lib/action_view/template/inline.rb +22 -0
- data/lib/action_view/template/raw_file.rb +25 -0
- data/lib/action_view/template/renderable.rb +30 -0
- data/lib/action_view/template/resolver.rb +212 -0
- data/lib/action_view/template/sources/file.rb +17 -0
- data/lib/action_view/template/sources.rb +13 -0
- data/lib/action_view/template/text.rb +32 -0
- data/lib/action_view/template/types.rb +50 -0
- data/lib/action_view/template.rb +580 -0
- data/lib/action_view/template_details.rb +66 -0
- data/lib/action_view/template_path.rb +66 -0
- data/lib/action_view/test_case.rb +449 -0
- data/lib/action_view/testing/resolvers.rb +44 -0
- data/lib/action_view/unbound_template.rb +67 -0
- data/lib/action_view/version.rb +10 -0
- data/lib/action_view/view_paths.rb +117 -0
- data/lib/action_view.rb +104 -0
- metadata +275 -0
@@ -0,0 +1,161 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_view/helpers/tag_helper"
|
4
|
+
require "active_support/html_safe_translation"
|
5
|
+
|
6
|
+
module ActionView
|
7
|
+
module Helpers # :nodoc:
|
8
|
+
# = Action View Translation \Helpers
|
9
|
+
module TranslationHelper
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
include TagHelper
|
13
|
+
|
14
|
+
# Specify whether an error should be raised for missing translations.
|
15
|
+
singleton_class.attr_accessor :raise_on_missing_translations
|
16
|
+
|
17
|
+
included do
|
18
|
+
mattr_accessor :debug_missing_translation, default: true
|
19
|
+
end
|
20
|
+
|
21
|
+
# Delegates to <tt>I18n#translate</tt> but also performs three additional
|
22
|
+
# functions.
|
23
|
+
#
|
24
|
+
# First, it will ensure that any thrown +MissingTranslation+ messages will
|
25
|
+
# be rendered as inline spans that:
|
26
|
+
#
|
27
|
+
# * Have a <tt>translation-missing</tt> class applied
|
28
|
+
# * Contain the missing key as the value of the +title+ attribute
|
29
|
+
# * Have a titleized version of the last key segment as text
|
30
|
+
#
|
31
|
+
# For example, the value returned for the missing translation key
|
32
|
+
# <tt>"blog.post.title"</tt> will be:
|
33
|
+
#
|
34
|
+
# <span
|
35
|
+
# class="translation_missing"
|
36
|
+
# title="translation missing: en.blog.post.title">Title</span>
|
37
|
+
#
|
38
|
+
# This allows for views to display rather reasonable strings while still
|
39
|
+
# giving developers a way to find missing translations.
|
40
|
+
#
|
41
|
+
# If you would prefer missing translations to raise an error, you can
|
42
|
+
# opt out of span-wrapping behavior globally by setting
|
43
|
+
# <tt>config.i18n.raise_on_missing_translations = true</tt> or
|
44
|
+
# individually by passing <tt>raise: true</tt> as an option to
|
45
|
+
# <tt>translate</tt>.
|
46
|
+
#
|
47
|
+
# Second, if the key starts with a period <tt>translate</tt> will scope
|
48
|
+
# the key by the current partial. Calling <tt>translate(".foo")</tt> from
|
49
|
+
# the <tt>people/index.html.erb</tt> template is equivalent to calling
|
50
|
+
# <tt>translate("people.index.foo")</tt>. This makes it less
|
51
|
+
# repetitive to translate many keys within the same partial and provides
|
52
|
+
# a convention to scope keys consistently.
|
53
|
+
#
|
54
|
+
# Third, the translation will be marked as <tt>html_safe</tt> if the key
|
55
|
+
# has the suffix "_html" or the last element of the key is "html". Calling
|
56
|
+
# <tt>translate("footer_html")</tt> or <tt>translate("footer.html")</tt>
|
57
|
+
# will return an HTML safe string that won't be escaped by other HTML
|
58
|
+
# helper methods. This naming convention helps to identify translations
|
59
|
+
# that include HTML tags so that you know what kind of output to expect
|
60
|
+
# when you call translate in a template and translators know which keys
|
61
|
+
# they can provide HTML values for.
|
62
|
+
#
|
63
|
+
# To access the translated text along with the fully resolved
|
64
|
+
# translation key, <tt>translate</tt> accepts a block:
|
65
|
+
#
|
66
|
+
# <%= translate(".relative_key") do |translation, resolved_key| %>
|
67
|
+
# <span title="<%= resolved_key %>"><%= translation %></span>
|
68
|
+
# <% end %>
|
69
|
+
#
|
70
|
+
# This enables annotate translated text to be aware of the scope it was
|
71
|
+
# resolved against.
|
72
|
+
#
|
73
|
+
def translate(key, **options)
|
74
|
+
return key.map { |k| translate(k, **options) } if key.is_a?(Array)
|
75
|
+
key = key&.to_s unless key.is_a?(Symbol)
|
76
|
+
|
77
|
+
alternatives = if options.key?(:default)
|
78
|
+
options[:default].is_a?(Array) ? options.delete(:default).compact : [options.delete(:default)]
|
79
|
+
end
|
80
|
+
|
81
|
+
options[:raise] = true if options[:raise].nil? && TranslationHelper.raise_on_missing_translations
|
82
|
+
default = MISSING_TRANSLATION
|
83
|
+
|
84
|
+
translation = while key || alternatives.present?
|
85
|
+
if alternatives.blank? && !options[:raise].nil?
|
86
|
+
default = NO_DEFAULT # let I18n handle missing translation
|
87
|
+
end
|
88
|
+
|
89
|
+
key = scope_key_by_partial(key)
|
90
|
+
|
91
|
+
translated = ActiveSupport::HtmlSafeTranslation.translate(key, **options, default: default)
|
92
|
+
|
93
|
+
break translated unless translated == MISSING_TRANSLATION
|
94
|
+
|
95
|
+
if alternatives.present? && !alternatives.first.is_a?(Symbol)
|
96
|
+
break alternatives.first && I18n.translate(nil, **options, default: alternatives)
|
97
|
+
end
|
98
|
+
|
99
|
+
first_key ||= key
|
100
|
+
key = alternatives&.shift
|
101
|
+
end
|
102
|
+
|
103
|
+
if key.nil? && !first_key.nil?
|
104
|
+
translation = missing_translation(first_key, options)
|
105
|
+
key = first_key
|
106
|
+
end
|
107
|
+
|
108
|
+
block_given? ? yield(translation, key) : translation
|
109
|
+
end
|
110
|
+
alias :t :translate
|
111
|
+
|
112
|
+
# Delegates to <tt>I18n.localize</tt> with no additional functionality.
|
113
|
+
#
|
114
|
+
# See https://www.rubydoc.info/gems/i18n/I18n/Backend/Base:localize
|
115
|
+
# for more information.
|
116
|
+
def localize(object, **options)
|
117
|
+
I18n.localize(object, **options)
|
118
|
+
end
|
119
|
+
alias :l :localize
|
120
|
+
|
121
|
+
private
|
122
|
+
MISSING_TRANSLATION = -(2**60)
|
123
|
+
private_constant :MISSING_TRANSLATION
|
124
|
+
|
125
|
+
NO_DEFAULT = [].freeze
|
126
|
+
private_constant :NO_DEFAULT
|
127
|
+
|
128
|
+
def scope_key_by_partial(key)
|
129
|
+
if key&.start_with?(".")
|
130
|
+
if @virtual_path
|
131
|
+
@_scope_key_by_partial_cache ||= {}
|
132
|
+
@_scope_key_by_partial_cache[@virtual_path] ||= @virtual_path.gsub(%r{/_?}, ".")
|
133
|
+
"#{@_scope_key_by_partial_cache[@virtual_path]}#{key}"
|
134
|
+
else
|
135
|
+
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
|
136
|
+
end
|
137
|
+
else
|
138
|
+
key
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def missing_translation(key, options)
|
143
|
+
keys = I18n.normalize_keys(options[:locale] || I18n.locale, key, options[:scope])
|
144
|
+
|
145
|
+
title = +"translation missing: #{keys.join(".")}"
|
146
|
+
|
147
|
+
options.each do |name, value|
|
148
|
+
unless name == :scope
|
149
|
+
title << ", " << name.to_s << ": " << ERB::Util.html_escape(value)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
if ActionView::Base.debug_missing_translation
|
154
|
+
content_tag("span", keys.last.to_s.titleize, class: "translation_missing", title: title)
|
155
|
+
else
|
156
|
+
title
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|