text_helpers 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/lib/text_helpers/railtie.rb +23 -15
- data/lib/text_helpers/version.rb +1 -1
- data/lib/text_helpers.rb +26 -1
- data/test/lib/text_helpers/railtie_test.rb +117 -0
- data/test/test_helper.rb +55 -0
- data/text_helpers.gemspec +3 -0
- metadata +42 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5996591783903a77aa42bf6661456c457408b437
|
4
|
+
data.tar.gz: 290c4f04de2c35b257dfeabcf7a3fa3044b106ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33242fb49efb10f6c229d2acc44e361e371ae548d61a017030c40a7085822b1613e1693cabadadedc01813cdbf2a116456acbde9287728056b7b179920d2a785
|
7
|
+
data.tar.gz: 6b6f8d31bf1b5b04cee1ddb5b0498d8c62c26caade7ac35f0e5e3e5992eb67be6fa8d54751807308585e68a7b6da39efcea22c40d76fefcc3ef26dff780face1
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -90,3 +90,40 @@ specs by adding `view: true` to the spec metadata.
|
|
90
90
|
|
91
91
|
The controller text helpers described above can be accessed in controller
|
92
92
|
specs by adding `controller: true` to your spec metadata.
|
93
|
+
|
94
|
+
## Configuration & Initialization
|
95
|
+
|
96
|
+
### Initialization
|
97
|
+
|
98
|
+
`TextHelpers` performs some setup during your application's initialization. Four
|
99
|
+
initializers are installed:
|
100
|
+
|
101
|
+
#### `text_helpers.action_view.extend_base`
|
102
|
+
|
103
|
+
This initializer includes the `TextHelpers::Translation` module into
|
104
|
+
`ActionView::Base` and adds an appropriate `#translation_scope` method.
|
105
|
+
|
106
|
+
#### `text_helpers.action_mailer.extend_base`
|
107
|
+
|
108
|
+
This initializer includes the `TextHelpers::Translation` module into
|
109
|
+
`ActionMailer::Base` and adds an appropriate `#translation_scope` method.
|
110
|
+
|
111
|
+
#### `text_helpers.action_controller.extend_base`
|
112
|
+
|
113
|
+
This initializer includes the `TextHelpers::Translation` module into
|
114
|
+
`ActionController::Base` and adds an appropriate `#translation_scope` method.
|
115
|
+
|
116
|
+
#### `text_helpers.setup_exception_handling`
|
117
|
+
|
118
|
+
This initializer configures exception handling so that exceptions are raised
|
119
|
+
if `config.text_helpers.raise_on_missing_translations` is set to `true`, which
|
120
|
+
it is by default in the `test` or `development` environments.
|
121
|
+
|
122
|
+
### Configuration
|
123
|
+
|
124
|
+
#### `config.text_helpers.raise_on_missing_translations`
|
125
|
+
|
126
|
+
This configuration value defaults to `true` in `test` or `development`
|
127
|
+
environments. If set to `false`, your own exception handling can be configured
|
128
|
+
by setting `config.action_view.raise_on_missing_translations` and
|
129
|
+
`I18n.exception_handler` as appropriate.
|
data/Rakefile
CHANGED
data/lib/text_helpers/railtie.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
module TextHelpers
|
2
|
-
|
3
2
|
class Railtie < Rails::Railtie
|
3
|
+
config.text_helpers = ActiveSupport::OrderedOptions.new
|
4
|
+
config.text_helpers.raise_on_missing_translations = Rails.env.test? || Rails.env.development?
|
4
5
|
|
5
|
-
initializer "text_helpers.
|
6
|
-
|
7
|
-
class ActionView::Base
|
6
|
+
initializer "text_helpers.action_view.extend_base" do
|
7
|
+
ActionView::Base.class_eval do
|
8
8
|
include TextHelpers::Translation
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
# Protected: Derive a translation scope from the current view template.
|
10
|
+
# Public: Derive a translation scope from the current view template.
|
13
11
|
#
|
14
12
|
# Determines an I18n-friendly scope for the current view file when possible,
|
15
13
|
# or falls back to "views.<controller>.<action>"
|
@@ -27,13 +25,13 @@ module TextHelpers
|
|
27
25
|
end
|
28
26
|
end
|
29
27
|
end
|
28
|
+
end
|
30
29
|
|
31
|
-
|
30
|
+
initializer "text_helpers.action_mailer.extend_base" do
|
31
|
+
ActionMailer::Base.class_eval do
|
32
32
|
include TextHelpers::Translation
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
# Protected: Provides a scope for I18n lookups.
|
34
|
+
# Public: Provides a scope for I18n lookups.
|
37
35
|
#
|
38
36
|
# Should look like `mailers.<mailer>.<action>`
|
39
37
|
#
|
@@ -42,13 +40,13 @@ module TextHelpers
|
|
42
40
|
"mailers.#{mailer_name.tr("/", ".").sub("_mailer", "")}.#{action_name}"
|
43
41
|
end
|
44
42
|
end
|
43
|
+
end
|
45
44
|
|
46
|
-
|
45
|
+
initializer "text_helpers.action_controller.extend_base" do
|
46
|
+
ActionController::Base.class_eval do
|
47
47
|
include TextHelpers::Translation
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
# Protected: Provides a scope for I18n lookups.
|
49
|
+
# Public: Provides a scope for I18n lookups.
|
52
50
|
#
|
53
51
|
# Should look like `controllers.<controller_name>`.
|
54
52
|
#
|
@@ -58,5 +56,15 @@ module TextHelpers
|
|
58
56
|
end
|
59
57
|
end
|
60
58
|
end
|
59
|
+
|
60
|
+
initializer "text_helpers.setup_exception_handling", after: 'after_initialize' do
|
61
|
+
next unless config.text_helpers.raise_on_missing_translations
|
62
|
+
|
63
|
+
if config.respond_to?(:action_view)
|
64
|
+
config.action_view.raise_on_missing_translations = true
|
65
|
+
end
|
66
|
+
|
67
|
+
TextHelpers.install_i18n_exception_handler
|
68
|
+
end
|
61
69
|
end
|
62
70
|
end
|
data/lib/text_helpers/version.rb
CHANGED
data/lib/text_helpers.rb
CHANGED
@@ -2,4 +2,29 @@ require "text_helpers/version"
|
|
2
2
|
require "text_helpers/translation"
|
3
3
|
require "text_helpers/railtie" if defined?(Rails)
|
4
4
|
|
5
|
-
module TextHelpers
|
5
|
+
module TextHelpers
|
6
|
+
# RaiseExceptionHandler just raises all exceptions, rather than swallowing
|
7
|
+
# MissingTranslation ones. It's cribbed almost verbatim from
|
8
|
+
# http://edgeguides.rubyonrails.org/i18n.html#customize-your-i18n-setup.
|
9
|
+
class RaiseExceptionHandler < I18n::ExceptionHandler
|
10
|
+
def call(exception, locale, key, options)
|
11
|
+
if exception.is_a?(I18n::MissingTranslation)
|
12
|
+
raise exception.to_exception
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Public: Install an instance of TextHelpers::RaiseExceptionHandler as the
|
20
|
+
# default I18n exception handler.
|
21
|
+
#
|
22
|
+
# Returns the handler instance.
|
23
|
+
def self.install_i18n_exception_handler
|
24
|
+
if I18n.exception_handler
|
25
|
+
Rails.logger.warn("Overwriting existing I18n exception handler")
|
26
|
+
end
|
27
|
+
|
28
|
+
I18n.exception_handler = RaiseExceptionHandler.new
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'rails'
|
5
|
+
require 'rails/railtie'
|
6
|
+
require 'rails/application'
|
7
|
+
require 'text_helpers/railtie'
|
8
|
+
|
9
|
+
describe TextHelpers::Railtie do
|
10
|
+
include StubHelpers
|
11
|
+
|
12
|
+
let(:railtie) { TextHelpers::Railtie }
|
13
|
+
let(:action_view_base) { Class.new }
|
14
|
+
let(:action_mailer_base) { Class.new }
|
15
|
+
let(:action_controller_base) { Class.new }
|
16
|
+
|
17
|
+
let(:app) do
|
18
|
+
Class.new(Rails::Application) do
|
19
|
+
config.eager_load = false
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:stub_resets) do
|
26
|
+
[]
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
stub_resets << stub_nested_const(TextHelpers::Railtie, "ActionView::Base", action_view_base)
|
31
|
+
stub_resets << stub_nested_const(TextHelpers::Railtie, "ActionMailer::Base", action_mailer_base)
|
32
|
+
stub_resets << stub_nested_const(TextHelpers::Railtie, "ActionController::Base", action_controller_base)
|
33
|
+
|
34
|
+
app.initialize!
|
35
|
+
end
|
36
|
+
|
37
|
+
after do
|
38
|
+
stub_resets.reverse.map(&:call)
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "ActionView::Base extensions" do
|
42
|
+
it "includes TextHelpers::Translation" do
|
43
|
+
assert_includes(action_view_base.included_modules, TextHelpers::Translation)
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#translation_scope" do
|
47
|
+
let(:action_view_base) do
|
48
|
+
Class.new do
|
49
|
+
attr_accessor :virtual_path, :params
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
let(:view_instance) { action_view_base.new }
|
54
|
+
|
55
|
+
it "is based on the view virtual path if a matching path is present" do
|
56
|
+
view_instance.virtual_path = 'foo/bar/_some_view.html.haml'
|
57
|
+
assert_equal('views.foo.bar.some_view', view_instance.translation_scope)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "is based on the controller and action name with a non-matching virtual path" do
|
61
|
+
view_instance.virtual_path = 'this-wont-match-at-all'
|
62
|
+
view_instance.params = {controller: "a_controller", action: "an_action"}
|
63
|
+
assert_equal('views.a_controller.an_action', view_instance.translation_scope)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "is based on the controller and action name with no virtual path" do
|
67
|
+
view_instance.virtual_path = nil
|
68
|
+
view_instance.params = {controller: "a_controller", action: "an_action"}
|
69
|
+
assert_equal('views.a_controller.an_action', view_instance.translation_scope)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "ActionMailer::Base extensions" do
|
75
|
+
it "includes TextHelpers::Translation" do
|
76
|
+
assert_includes(action_mailer_base.included_modules, TextHelpers::Translation)
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#translation_scope" do
|
80
|
+
let(:action_mailer_base) do
|
81
|
+
Class.new do
|
82
|
+
attr_accessor :mailer_name, :action_name
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
let(:mailer_instance) { action_mailer_base.new }
|
87
|
+
|
88
|
+
it "is based on the mailer name and action" do
|
89
|
+
mailer_instance.mailer_name = "foo/bar/baz_mailer"
|
90
|
+
mailer_instance.action_name = "notification_message"
|
91
|
+
assert_equal('mailers.foo.bar.baz.notification_message', mailer_instance.translation_scope)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "ActionController::Base extensions" do
|
97
|
+
it "includes TextHelpers::Translation" do
|
98
|
+
assert_includes(action_mailer_base.included_modules, TextHelpers::Translation)
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "#translation_scope" do
|
102
|
+
let(:action_mailer_base) do
|
103
|
+
Class.new do
|
104
|
+
attr_accessor :mailer_name, :action_name
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
let(:mailer_instance) { action_mailer_base.new }
|
109
|
+
|
110
|
+
it "is based on the mailer name and action" do
|
111
|
+
mailer_instance.mailer_name = "foo/bar/baz_mailer"
|
112
|
+
mailer_instance.action_name = "notification_message"
|
113
|
+
assert_equal('mailers.foo.bar.baz.notification_message', mailer_instance.translation_scope)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,59 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
2
|
require File.expand_path('../../lib/text_helpers.rb', __FILE__)
|
3
3
|
|
4
|
+
module StubHelpers
|
5
|
+
|
6
|
+
# Public: Stub a constant down a nested module path.
|
7
|
+
#
|
8
|
+
# target - A target Class or Module to stub a constant on.
|
9
|
+
# module_path - A path to a constant, like "Foo::Bar::Baz".
|
10
|
+
# stub - The stub value to return.
|
11
|
+
#
|
12
|
+
# Yields a block during which the stub is in place.
|
13
|
+
# Returns a Proc that can be called to reset the stub, if no block was given.
|
14
|
+
def stub_nested_const(target, module_path, stub)
|
15
|
+
module_names = module_path.split("::")[0..-1]
|
16
|
+
|
17
|
+
# Save original values
|
18
|
+
_, *original_values = module_names.inject([target]) do |collected, module_name|
|
19
|
+
last_module = collected.last
|
20
|
+
break(collected) unless last_module.const_defined?(module_name, false)
|
21
|
+
|
22
|
+
collected.push(last_module.const_get(module_name, false))
|
23
|
+
end
|
24
|
+
|
25
|
+
# Stub new values
|
26
|
+
*chain, last = module_names
|
27
|
+
|
28
|
+
last_stubbed = chain.inject(target) do |t, module_name|
|
29
|
+
module_value = if t.const_defined?(module_name, false)
|
30
|
+
t.const_get(module_name, false)
|
31
|
+
else
|
32
|
+
Module.new
|
33
|
+
end
|
34
|
+
|
35
|
+
t.const_set(module_name, module_value)
|
36
|
+
end
|
37
|
+
|
38
|
+
last_stubbed.const_set(last, stub)
|
39
|
+
|
40
|
+
# Reset values
|
41
|
+
reset_block = lambda do
|
42
|
+
unset_last = original_values.length != module_names.length
|
43
|
+
|
44
|
+
last_target = original_values.zip(module_names).inject(target) do |t, (value, name)|
|
45
|
+
t.const_set(name, value)
|
46
|
+
end
|
47
|
+
|
48
|
+
if unset_last
|
49
|
+
last_target.send(:remove_const, module_names[original_values.length])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
block_given? ? yield : reset_block
|
54
|
+
ensure
|
55
|
+
reset_block.call if block_given?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
4
59
|
I18n.enforce_available_locales = false
|
data/text_helpers.gemspec
CHANGED
metadata
CHANGED
@@ -1,55 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Horner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: i18n
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 0.6.8
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.6.8
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: redcarpet
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: railties
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
53
81
|
- !ruby/object:Gem::Version
|
54
82
|
version: '0'
|
55
83
|
description: Easily fetch text and static content from your locales
|
@@ -59,7 +87,7 @@ executables: []
|
|
59
87
|
extensions: []
|
60
88
|
extra_rdoc_files: []
|
61
89
|
files:
|
62
|
-
- .gitignore
|
90
|
+
- ".gitignore"
|
63
91
|
- Gemfile
|
64
92
|
- LICENSE
|
65
93
|
- README.md
|
@@ -69,6 +97,7 @@ files:
|
|
69
97
|
- lib/text_helpers/railtie.rb
|
70
98
|
- lib/text_helpers/translation.rb
|
71
99
|
- lib/text_helpers/version.rb
|
100
|
+
- test/lib/text_helpers/railtie_test.rb
|
72
101
|
- test/lib/text_helpers/translation_test.rb
|
73
102
|
- test/lib/text_helpers/version_test.rb
|
74
103
|
- test/test_helper.rb
|
@@ -83,23 +112,24 @@ require_paths:
|
|
83
112
|
- lib
|
84
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
85
114
|
requirements:
|
86
|
-
- -
|
115
|
+
- - ">="
|
87
116
|
- !ruby/object:Gem::Version
|
88
117
|
version: '0'
|
89
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
119
|
requirements:
|
91
|
-
- -
|
120
|
+
- - ">="
|
92
121
|
- !ruby/object:Gem::Version
|
93
122
|
version: '0'
|
94
123
|
requirements: []
|
95
124
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.4.2
|
97
126
|
signing_key:
|
98
127
|
specification_version: 4
|
99
128
|
summary: TextHelpers is a gem which supplies some basic utilities for text localization
|
100
129
|
in Rails projects. The library is intended to make it simple to keep your application's
|
101
130
|
static and semi-static text content independent of the view structure.
|
102
131
|
test_files:
|
132
|
+
- test/lib/text_helpers/railtie_test.rb
|
103
133
|
- test/lib/text_helpers/translation_test.rb
|
104
134
|
- test/lib/text_helpers/version_test.rb
|
105
135
|
- test/test_helper.rb
|