text_helpers 0.5.0 → 0.5.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5996591783903a77aa42bf6661456c457408b437
4
- data.tar.gz: 290c4f04de2c35b257dfeabcf7a3fa3044b106ad
3
+ metadata.gz: d68768849991fa2e1aef1064d10c70a0c106e3e1
4
+ data.tar.gz: 010c42fa301e3b113586a30ef7e6e1e2ae014059
5
5
  SHA512:
6
- metadata.gz: 33242fb49efb10f6c229d2acc44e361e371ae548d61a017030c40a7085822b1613e1693cabadadedc01813cdbf2a116456acbde9287728056b7b179920d2a785
7
- data.tar.gz: 6b6f8d31bf1b5b04cee1ddb5b0498d8c62c26caade7ac35f0e5e3e5992eb67be6fa8d54751807308585e68a7b6da39efcea22c40d76fefcc3ef26dff780face1
6
+ metadata.gz: 58bea6441da44a2a256ca9ed53a7bb1116a1c4b9d85250846f6e5e84e55d245d7ee626f5d1258556b3527d7ad1893ff8287da9a96e265a740a8d513ca3293bdd
7
+ data.tar.gz: 5ff92cc80d614a94f78c2852f0e8ef9008cc1e75bdbd2728f89fa35c89eb98d30a0526192fb378b450f88fbcba65af819706482c91ec31b0d2a7d4c73f25cbeb
data/README.md CHANGED
@@ -91,6 +91,35 @@ specs by adding `view: true` to the spec metadata.
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
93
 
94
+ ### Temporary/Stub Localizations
95
+
96
+ `text_helpers/rspec.rb` contains some helpers for setting up a test localization
97
+ environment during your test runs.
98
+
99
+ To configure it, `require "text_helpers/rspec"` and configure the `before` and
100
+ `after` hooks appropriately:
101
+
102
+ ```
103
+ require 'text_helpers/rspec'
104
+
105
+ RSpec.configure do |config|
106
+ config.before(:suite) do
107
+ TextHelpers::RSpec.setup_spec_translations
108
+ end
109
+
110
+ config.after(:each) do
111
+ TextHelpers::RSpec.reset_spec_translations
112
+ end
113
+ end
114
+ ```
115
+
116
+ Temporary localizations can then be defined within your examples via the
117
+ `#set_translation` method, like so:
118
+
119
+ ```
120
+ set_translation('models.user.attributes.name', 'Name')
121
+ ```
122
+
94
123
  ## Configuration & Initialization
95
124
 
96
125
  ### Initialization
@@ -0,0 +1,59 @@
1
+ module TextHelpers
2
+ # TextHelpers::RSpec contains some helpers for making testing around i18n
3
+ # easier, especially with tests that may generate keys that map to
4
+ # translations that aren't/should not be present in your production
5
+ # localization files.
6
+ module RSpec
7
+ class << self
8
+ attr_accessor :translation_backend
9
+ end
10
+
11
+ # Public: Set up the test translation environment.
12
+ #
13
+ # Should ideally be called in an RSpec `before(:suite)` hook.
14
+ #
15
+ # Returns the new I18n backend.
16
+ def self.setup_spec_translations
17
+ self.translation_backend = I18n::Backend::Simple.new
18
+ I18n.backend = I18n::Backend::Chain.new(self.translation_backend, I18n.backend)
19
+ end
20
+
21
+ # Public: Reset the test translations.
22
+ #
23
+ # Clears out any translations added during an example run.
24
+ #
25
+ # Returns nothing.
26
+ def self.reset_spec_translations
27
+ if self.translation_backend.nil?
28
+ raise "translation_backend is nil. Ensure .setup_spec_translations was called"
29
+ end
30
+
31
+ self.translation_backend.reload!
32
+ end
33
+
34
+ # TextHelpers::RSpec::TestHelpers contains helper methods to be used from
35
+ # within your examples.
36
+ module TestHelpers
37
+
38
+ # Public: Set a new translation in the test translations.
39
+ #
40
+ # path - The path to the key, like 'models.user.attributes.title'.
41
+ # value - The localized value.
42
+ # locale - The locale the translation should be defined in.
43
+ # scope - The scope the translation should be defined under.
44
+ #
45
+ # Returns true on success.
46
+ def set_translation(path, value, locale: I18n.locale, scope: nil)
47
+ *hash_keys, last_key = I18n.normalize_keys(nil, path, scope)
48
+
49
+ data = {}
50
+ last_hash = hash_keys.inject(data) { |h,k| h[k] = Hash.new }
51
+ last_hash[last_key] = value
52
+
53
+ TextHelpers::RSpec.translation_backend.store_translations(locale, data)
54
+
55
+ true
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,3 +1,3 @@
1
1
  module TextHelpers
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: text_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Horner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-23 00:00:00.000000000 Z
11
+ date: 2015-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -95,6 +95,7 @@ files:
95
95
  - lib/text_helpers.rb
96
96
  - lib/text_helpers/contexts.rb
97
97
  - lib/text_helpers/railtie.rb
98
+ - lib/text_helpers/rspec.rb
98
99
  - lib/text_helpers/translation.rb
99
100
  - lib/text_helpers/version.rb
100
101
  - test/lib/text_helpers/railtie_test.rb