alephant-renderer 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Guardfile +3 -3
- data/README.md +11 -199
- data/Rakefile +6 -6
- data/lib/alephant/renderer.rb +10 -11
- data/lib/alephant/renderer/i18n/json.rb +29 -0
- data/lib/alephant/renderer/i18n/locale_component_yaml.rb +2 -4
- data/lib/alephant/renderer/response.rb +9 -3
- data/lib/alephant/renderer/version.rb +1 -1
- data/lib/alephant/renderer/view_mapper.rb +17 -21
- data/lib/alephant/renderer/views.rb +19 -13
- data/lib/alephant/renderer/views/envelope.rb +48 -0
- data/lib/alephant/renderer/views/html.rb +4 -2
- data/lib/alephant/renderer/views/json.rb +3 -3
- data/spec/fixtures/components/foo-renderer/models/envelope.rb +15 -0
- data/spec/fixtures/components/foo-renderer/models/json.rb +1 -1
- data/spec/fixtures/components/foo-renderer/templates/envelope.mustache +1 -0
- data/spec/integration/fixtures/translations.json +15 -0
- data/spec/integration/i18n_json_spec.rb +23 -0
- data/spec/integration/spec_helper.rb +1 -1
- data/spec/renderer_spec.rb +26 -22
- data/spec/response_spec.rb +5 -5
- data/spec/spec_helper.rb +11 -10
- data/spec/view_mapper_spec.rb +17 -17
- data/spec/views_envelope_spec.rb +24 -0
- data/spec/views_json_spec.rb +16 -16
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac57f02826b45a9bccb89bd778cbc9b5c997c42a
|
4
|
+
data.tar.gz: 2c294492baa6c21d5bd7d11fbd861587d5b34162
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eea72adabbd3c187273ce615e2367a3085b6546a270738f59017b13d94093b151393db3edcc03267537d7221d8fb1905d226cbd3dfbaf4c106ac1a133e39704
|
7
|
+
data.tar.gz: 9e6529da65b64101c121373e5242b272270633b2bdfe843cb9b69427dd2381b1106df274cde63dcc669f8912b94791feb0741252521cabd924420f955a545262
|
data/Gemfile
CHANGED
data/Guardfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
guard
|
1
|
+
guard 'rspec' do
|
2
2
|
watch(%r{^spec/.+_spec\.rb$})
|
3
3
|
watch(%r{^lib/.+\.rb$})
|
4
|
-
watch(%r{^spec/support/(.+)\.rb$}) {
|
5
|
-
watch(
|
4
|
+
watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
|
5
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
6
6
|
end
|
data/README.md
CHANGED
@@ -20,47 +20,7 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Setup
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
```bash
|
26
|
-
src
|
27
|
-
├── components
|
28
|
-
│ ├── weather
|
29
|
-
│ │ ├── models
|
30
|
-
│ │ │ ├── daily_summary.rb
|
31
|
-
│ │ │ ├── weekly_summary.rb
|
32
|
-
│ │ ├── templates
|
33
|
-
│ │ │ ├── daily_summary.mustache
|
34
|
-
│ │ │ ├── weekly_summary.mustache
|
35
|
-
```
|
36
|
-
|
37
|
-
> daily_summary.rb
|
38
|
-
|
39
|
-
```ruby
|
40
|
-
require 'alephant/renderer/views/html'
|
41
|
-
require 'date'
|
42
|
-
|
43
|
-
class DailySummary < Alephant::Renderer::Views::Html
|
44
|
-
def temp
|
45
|
-
@data[:time].round 0
|
46
|
-
end
|
47
|
-
|
48
|
-
def time
|
49
|
-
DateTime.now.strftime "%d/%m/%Y %H:%M"
|
50
|
-
end
|
51
|
-
|
52
|
-
def summary
|
53
|
-
@data[:summary].capitalize
|
54
|
-
end
|
55
|
-
end
|
56
|
-
```
|
57
|
-
|
58
|
-
> daily_summary.mustache
|
59
|
-
|
60
|
-
```mustache
|
61
|
-
Weather for {{ time }}
|
62
|
-
{{ summary }} - {{ temp }}°
|
63
|
-
```
|
23
|
+
Please see the wiki guide on [setting up a basic renderer](https://github.com/BBC-News/alephant-renderer/wiki/Setting-up-a-basic-renderer).
|
64
24
|
|
65
25
|
## Usage
|
66
26
|
|
@@ -85,6 +45,10 @@ Alephant::Renderer.create(
|
|
85
45
|
|
86
46
|
**Note** - Within you application you will be most likely providing the *data* dynamically and thus will not require the JSON library.
|
87
47
|
|
48
|
+
## Documentation
|
49
|
+
|
50
|
+
We have a [wiki](https://github.com/BBC-News/alephant-renderer/wiki)!
|
51
|
+
|
88
52
|
#### Example Application
|
89
53
|
|
90
54
|
The [alephant-publisher-request](https://github.com/BBC-News/alephant-publisher-request) gem is an example of an application which utilises this gem. Overview of process:
|
@@ -94,164 +58,6 @@ The [alephant-publisher-request](https://github.com/BBC-News/alephant-publisher-
|
|
94
58
|
3. Renders specified component using data.
|
95
59
|
4. Returns rendered template.
|
96
60
|
|
97
|
-
## Translations
|
98
|
-
|
99
|
-
Currently there is a simple implementation of the [i18n](https://github.com/svenfuchs/i18n) library that allows templates to be translated.
|
100
|
-
|
101
|
-
### Setup
|
102
|
-
|
103
|
-
You need the following directory structure inside of the folder that relates to the `base_path`:
|
104
|
-
|
105
|
-
```bash
|
106
|
-
components
|
107
|
-
├── locale
|
108
|
-
│ ├── en.yml
|
109
|
-
│ ├── cy.yml
|
110
|
-
```
|
111
|
-
|
112
|
-
The yaml configs must have the extension `.yml`.
|
113
|
-
|
114
|
-
### Yaml structure
|
115
|
-
|
116
|
-
The yaml translations files must follow the following structure:
|
117
|
-
|
118
|
-
```yaml
|
119
|
-
en:
|
120
|
-
template_name:
|
121
|
-
key: 'foo'
|
122
|
-
sub:
|
123
|
-
key: 'bar'
|
124
|
-
|
125
|
-
another_template:
|
126
|
-
key: 'baz'
|
127
|
-
```
|
128
|
-
|
129
|
-
The first node is the language code, then the next set of nodes are the names of the templates files that the translations apply to. This allows you to just reference the translation key in the templates without prefixing the name of the template.
|
130
|
-
|
131
|
-
### Usage
|
132
|
-
|
133
|
-
For each translation, a seperate model and view is needed.
|
134
|
-
|
135
|
-
#### Model
|
136
|
-
|
137
|
-
A model should override the `locale` method:
|
138
|
-
|
139
|
-
```ruby
|
140
|
-
class TestModel < Alephant::Views::HTML
|
141
|
-
def locale
|
142
|
-
:cy
|
143
|
-
end
|
144
|
-
end
|
145
|
-
```
|
146
|
-
|
147
|
-
#### Translations
|
148
|
-
|
149
|
-
The best approach with the templates when translations are needed, is to have a base template then a seperate one for each lanuage that's being translated.
|
150
|
-
|
151
|
-
In each view model, the following method is available for translations:
|
152
|
-
|
153
|
-
```ruby
|
154
|
-
t 'my_key'
|
155
|
-
```
|
156
|
-
|
157
|
-
#### Example
|
158
|
-
|
159
|
-
If we had a template called 'test_template.mustache' we would have the following in the model 'test_template.rb':
|
160
|
-
|
161
|
-
>test_template.rb
|
162
|
-
|
163
|
-
```ruby
|
164
|
-
def my_translation
|
165
|
-
t 'key'
|
166
|
-
end
|
167
|
-
```
|
168
|
-
|
169
|
-
>test_template.mustache
|
170
|
-
|
171
|
-
```mustache
|
172
|
-
{{ my_translation }}
|
173
|
-
```
|
174
|
-
|
175
|
-
>locale/en.yml
|
176
|
-
|
177
|
-
```yaml
|
178
|
-
en:
|
179
|
-
test_template:
|
180
|
-
key: 'A translation!'
|
181
|
-
```
|
182
|
-
|
183
|
-
##### Other options
|
184
|
-
|
185
|
-
The `t()` method takes a second hash argument for accessing other I18n features. Below are a few of the more common uses of this argument.
|
186
|
-
|
187
|
-
By default, if the translation key doesn't exist then the translation key is used as the translation. You can override this behaviour and provide an explicit default:
|
188
|
-
|
189
|
-
```ruby
|
190
|
-
def my_translation
|
191
|
-
t 'missing_key', default: 'Some default'
|
192
|
-
end
|
193
|
-
```
|
194
|
-
|
195
|
-
Plurality can also be handled, including support for languages with multiple pluralities. You would define your translation in the yaml file like this:
|
196
|
-
|
197
|
-
```yaml
|
198
|
-
en:
|
199
|
-
test_template:
|
200
|
-
pluralized_key:
|
201
|
-
one: 'Singular string'
|
202
|
-
other: 'Plural string'
|
203
|
-
```
|
204
|
-
and the code would look like:
|
205
|
-
```ruby
|
206
|
-
t 'pluralized_key', count: @num_of_things
|
207
|
-
```
|
208
|
-
|
209
|
-
You can force it to use a different locale if you need:
|
210
|
-
```ruby
|
211
|
-
t 'some_key', locale: :cy
|
212
|
-
```
|
213
|
-
|
214
|
-
You can use strings from another subsection of the translations. E.g. if your renderer was currently _myRenderer_ and your translations yaml looked like this:
|
215
|
-
```yaml
|
216
|
-
en:
|
217
|
-
myRenderer:
|
218
|
-
'some_key': 'some string'
|
219
|
-
general:
|
220
|
-
'another_key': 'another string'
|
221
|
-
```
|
222
|
-
You can do this in code:
|
223
|
-
```ruby
|
224
|
-
t 'another_key', scope: 'general'
|
225
|
-
```
|
226
|
-
|
227
|
-
You can do simple variable substitution, as often dynamic data can be in a different position in different strings. Your yaml might look like:
|
228
|
-
```yaml
|
229
|
-
en:
|
230
|
-
myRenderer:
|
231
|
-
'some_key': 'some %{insert} string'
|
232
|
-
```
|
233
|
-
and the code looks like:
|
234
|
-
```ruby
|
235
|
-
t 'some_key', insert: 'text inserted into a'
|
236
|
-
```
|
237
|
-
|
238
|
-
## Templates
|
239
|
-
|
240
|
-
The template engine is built into the Alephant::Views::HTML class. Create methods that correspond to variables, and they will become available to the template.
|
241
|
-
|
242
|
-
### Paths
|
243
|
-
|
244
|
-
There are two template paths in Alephant, the template path and the partials path. By default in HTML renderers, the template path is `../templates/` from your model, and the partials path is `../../lib/templates` from your model. You can change either of these pathing strategies by overridding the default renderer_engine instantiation.
|
245
|
-
|
246
|
-
```ruby
|
247
|
-
def renderer_engine
|
248
|
-
partial_path = File.join(base_path, 'templates/')
|
249
|
-
Alephant::Renderer::Engine::Mustache.new(base_path, template_name, partial_path)
|
250
|
-
end
|
251
|
-
```
|
252
|
-
In the above example, base_path is the parent of the current model folder, so this will make the partials path equal to the template path.
|
253
|
-
|
254
|
-
|
255
61
|
## Contributing
|
256
62
|
|
257
63
|
1. [Fork it!](http://github.com/bbc-news/alephant-renderer/fork)
|
@@ -261,3 +67,9 @@ In the above example, base_path is the parent of the current model folder, so th
|
|
261
67
|
5. Create new [Pull Request](https://github.com/BBC-News/alephant-renderer/pulls).
|
262
68
|
|
263
69
|
Feel free to create an [issue](https://github.com/BBC-News/alephant-renderer/issues/new) if you find a bug.
|
70
|
+
|
71
|
+
## Coding standards
|
72
|
+
|
73
|
+
For any submission, please:
|
74
|
+
1. Run rubocop against it, using [these standards](https://github.com/BBC-News/rubocop-config)
|
75
|
+
2. Ensure all unit tests pass (these are also checked on pushing to your branch)
|
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
require 'alephant/renderer'
|
6
|
+
require 'rake/rspec'
|
7
7
|
|
8
|
-
task :
|
8
|
+
task default: :spec
|
data/lib/alephant/renderer.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'alephant/renderer/version'
|
2
|
+
require 'alephant/renderer/view_mapper'
|
3
|
+
require 'alephant/logger'
|
4
4
|
|
5
5
|
module Alephant
|
6
6
|
module Renderer
|
@@ -25,16 +25,15 @@ module Alephant
|
|
25
25
|
private
|
26
26
|
|
27
27
|
def mapper
|
28
|
-
@mapper ||=
|
29
|
-
config[:renderer_id],
|
30
|
-
config[:view_path]
|
31
|
-
).tap do
|
28
|
+
@mapper ||= begin
|
32
29
|
logger.info(
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
30
|
+
event: :ViewMapperCreated,
|
31
|
+
rendererId: config[:renderer_id],
|
32
|
+
viewPath: config[:view_path],
|
33
|
+
method: "#{self.class}#mapper"
|
37
34
|
)
|
35
|
+
|
36
|
+
ViewMapper.new(config[:renderer_id], config[:view_path])
|
38
37
|
end
|
39
38
|
end
|
40
39
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Alephant
|
4
|
+
module Renderer
|
5
|
+
module I18n
|
6
|
+
class Json < LocaleComponentYaml
|
7
|
+
private
|
8
|
+
|
9
|
+
def load_translations
|
10
|
+
return unless i18n_lib.load_path.empty?
|
11
|
+
translations_files.map do |file|
|
12
|
+
store_translation(file)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def store_translation(file)
|
17
|
+
translations = JSON.parse(File.read(file))
|
18
|
+
translations.each do |locale, content|
|
19
|
+
i18n_lib.backend.store_translations(locale, content)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def translation_filename
|
24
|
+
File.join(translations_path, '*.json')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -33,13 +33,11 @@ module Alephant
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def translation_filename
|
36
|
-
File.join(
|
37
|
-
translations_path,
|
38
|
-
'*.yml')
|
36
|
+
File.join(translations_path, '*.yml')
|
39
37
|
end
|
40
38
|
|
41
39
|
def translations_path
|
42
|
-
@translations_path || './components/lib/
|
40
|
+
@translations_path || './components/lib/locale'
|
43
41
|
end
|
44
42
|
|
45
43
|
def i18n_lib
|
@@ -1,10 +1,16 @@
|
|
1
|
-
require
|
1
|
+
require 'json'
|
2
2
|
|
3
3
|
module Alephant
|
4
4
|
module Renderer
|
5
|
-
class Response
|
5
|
+
class Response
|
6
|
+
attr_accessor :content
|
7
|
+
|
8
|
+
def initialize(content)
|
9
|
+
@content = content
|
10
|
+
end
|
11
|
+
|
6
12
|
def to_json
|
7
|
-
::JSON.generate
|
13
|
+
::JSON.generate 'content' => content
|
8
14
|
end
|
9
15
|
end
|
10
16
|
end
|
@@ -1,17 +1,15 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'alephant/renderer/error/invalid_path'
|
2
|
+
require 'alephant/logger'
|
3
3
|
|
4
4
|
module Alephant
|
5
5
|
module Renderer
|
6
6
|
class ViewMapper
|
7
7
|
include Logger
|
8
8
|
|
9
|
-
DEFAULT_LOCATION =
|
9
|
+
DEFAULT_LOCATION = 'components'.freeze
|
10
10
|
|
11
|
-
def initialize(renderer_id, view_base_path=nil)
|
12
|
-
unless view_base_path.nil?
|
13
|
-
self.base_path = "#{view_base_path}/#{renderer_id}"
|
14
|
-
end
|
11
|
+
def initialize(renderer_id, view_base_path = nil)
|
12
|
+
self.base_path = "#{view_base_path}/#{renderer_id}" unless view_base_path.nil?
|
15
13
|
end
|
16
14
|
|
17
15
|
def base_path
|
@@ -24,24 +22,22 @@ module Alephant
|
|
24
22
|
|
25
23
|
def generate(data)
|
26
24
|
model_locations.reduce({}) do |obj, file|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
25
|
+
model_id = model_id_for(file)
|
26
|
+
obj[model_id] = model(model_id, data)
|
27
|
+
obj
|
31
28
|
end
|
32
29
|
end
|
33
30
|
|
34
31
|
private
|
35
32
|
|
36
33
|
def raise_error(path)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
34
|
+
logger.metric('ViewMapperInvalidPath')
|
35
|
+
logger.error(
|
36
|
+
event: :ViewMapperBasePathInvalidFound,
|
37
|
+
path: path,
|
38
|
+
method: "#{self.class}#raise_error"
|
39
|
+
)
|
40
|
+
raise(Error::InvalidBasePath.new(path))
|
45
41
|
end
|
46
42
|
|
47
43
|
def model(view_id, data)
|
@@ -50,7 +46,7 @@ module Alephant
|
|
50
46
|
end
|
51
47
|
|
52
48
|
def model_location_for(view_id)
|
53
|
-
File.join(base_path,
|
49
|
+
File.join(base_path, 'models', "#{view_id}.rb")
|
54
50
|
end
|
55
51
|
|
56
52
|
def model_locations
|
@@ -62,7 +58,7 @@ module Alephant
|
|
62
58
|
end
|
63
59
|
|
64
60
|
def model_id_for(location)
|
65
|
-
location.split(
|
61
|
+
location.split('/').last.sub(/\.rb/, '')
|
66
62
|
end
|
67
63
|
end
|
68
64
|
end
|
@@ -1,22 +1,28 @@
|
|
1
1
|
module Alephant
|
2
2
|
module Renderer
|
3
3
|
module Views
|
4
|
-
|
4
|
+
class << self
|
5
|
+
def register(klass)
|
6
|
+
views[underscorify(klass.name.split('::').last)] = klass
|
7
|
+
end
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
def get_registered_class(id)
|
10
|
+
views[id.downcase]
|
11
|
+
end
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
def underscorify(str)
|
14
|
+
str.gsub(/::/, '/')
|
15
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\\1_\\2')
|
16
|
+
.gsub(/([a-z\d])([A-Z])/, '\\1_\\2')
|
17
|
+
.tr('-', '_')
|
18
|
+
.downcase
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
13
22
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
gsub(/([a-z\d])([A-Z])/,"\\1_\\2").
|
18
|
-
tr("-", "_").
|
19
|
-
downcase
|
23
|
+
def views
|
24
|
+
@views ||= {}
|
25
|
+
end
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require_relative './json.rb'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Alephant
|
5
|
+
module Renderer
|
6
|
+
module Views
|
7
|
+
class Envelope < Json
|
8
|
+
def head
|
9
|
+
[]
|
10
|
+
end
|
11
|
+
|
12
|
+
def body_last
|
13
|
+
[]
|
14
|
+
end
|
15
|
+
|
16
|
+
def locale
|
17
|
+
:en
|
18
|
+
end
|
19
|
+
|
20
|
+
def render
|
21
|
+
{
|
22
|
+
head: head,
|
23
|
+
bodyInline: renderer_engine.render(self),
|
24
|
+
bodyLast: body_last
|
25
|
+
}.to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def renderer_engine
|
31
|
+
Alephant::Renderer::Engine::Mustache.new(base_path, template_name)
|
32
|
+
end
|
33
|
+
|
34
|
+
def template_name
|
35
|
+
Mustache.underscore(self.class.to_s).split('/').last
|
36
|
+
end
|
37
|
+
|
38
|
+
def translator
|
39
|
+
@translator || Alephant::Renderer::I18n::LocaleComponentYaml.new(locale, template_name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def t(key, params = {})
|
43
|
+
translator.t(key, params)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -29,13 +29,15 @@ module Alephant
|
|
29
29
|
Alephant::Renderer::I18n::LocaleComponentYaml.new(
|
30
30
|
locale,
|
31
31
|
template_name,
|
32
|
-
translations_path
|
32
|
+
translations_path
|
33
|
+
)
|
33
34
|
end
|
34
35
|
|
35
36
|
def translations_path
|
36
37
|
File.join(
|
37
38
|
Pathname.new(base_path).parent,
|
38
|
-
'locale'
|
39
|
+
'locale'
|
40
|
+
)
|
39
41
|
end
|
40
42
|
|
41
43
|
def renderer_engine
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'alephant/renderer/views/base'
|
2
|
+
require 'json'
|
3
3
|
|
4
4
|
module Alephant
|
5
5
|
module Renderer
|
@@ -8,7 +8,7 @@ module Alephant
|
|
8
8
|
include ::Alephant::Renderer::Views::Base
|
9
9
|
|
10
10
|
def setup
|
11
|
-
@content_type =
|
11
|
+
@content_type = 'application/json'
|
12
12
|
end
|
13
13
|
|
14
14
|
def render
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module MyApp
|
2
|
+
class Envelope < Alephant::Renderer::Views::Envelope
|
3
|
+
def head
|
4
|
+
['some/css/asset/path','some/js/asset/path']
|
5
|
+
end
|
6
|
+
|
7
|
+
def body_last
|
8
|
+
['some/thing/to/add/after/body', 'some/other/thing/to/add/after/body']
|
9
|
+
end
|
10
|
+
|
11
|
+
def greet
|
12
|
+
@data[:foo]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>Hello, I'm a {{greet}} template</h1>
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require File.dirname(__FILE__) + '/../../lib/alephant/renderer/i18n/json'
|
3
|
+
|
4
|
+
describe Alephant::Renderer::I18n::Json do
|
5
|
+
let(:template_path) { File.join(File.dirname(__FILE__), 'fixtures') }
|
6
|
+
let(:locale) { 'en' }
|
7
|
+
let(:namespace) { 'my_namespace' }
|
8
|
+
|
9
|
+
subject { described_class.new(locale, namespace, template_path) }
|
10
|
+
|
11
|
+
describe 'json translations' do
|
12
|
+
let(:params) do
|
13
|
+
{
|
14
|
+
locale: 'cy',
|
15
|
+
count: 1,
|
16
|
+
scope: 'other_namespace'
|
17
|
+
}
|
18
|
+
end
|
19
|
+
it 'should translate a string held in a json document' do
|
20
|
+
expect(subject.t(:some_string, params)).to eq('another string')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1 +1 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../spec_helper'
|
data/spec/renderer_spec.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Alephant::Renderer do
|
4
|
-
let(:config)
|
4
|
+
let(:config) do
|
5
5
|
{
|
6
|
-
:
|
7
|
-
:
|
6
|
+
renderer_id: 'foo-renderer',
|
7
|
+
view_path: File.join(File.dirname(__FILE__), 'fixtures/components')
|
8
8
|
}
|
9
|
-
|
10
|
-
let(:data) { { :
|
9
|
+
end
|
10
|
+
let(:data) { { content: 'test' } }
|
11
11
|
|
12
|
-
describe
|
13
|
-
context
|
12
|
+
describe '.create' do
|
13
|
+
context 'using valid params' do
|
14
14
|
let(:expected) { Alephant::Renderer::Renderer }
|
15
15
|
|
16
16
|
specify do
|
@@ -22,36 +22,40 @@ describe Alephant::Renderer do
|
|
22
22
|
describe Alephant::Renderer::Renderer do
|
23
23
|
subject { Alephant::Renderer::Renderer.new(config, data) }
|
24
24
|
|
25
|
-
describe
|
25
|
+
describe '#config' do
|
26
26
|
specify { expect(subject.config).to eql config }
|
27
27
|
end
|
28
28
|
|
29
|
-
describe
|
29
|
+
describe '#data' do
|
30
30
|
specify { expect(subject.data).to eql data }
|
31
31
|
end
|
32
32
|
|
33
|
-
describe
|
34
|
-
it
|
33
|
+
describe '#views' do
|
34
|
+
it 'returns a Hash' do
|
35
35
|
expect(subject.views).to be_a Hash
|
36
36
|
end
|
37
37
|
|
38
|
-
context
|
39
|
-
it
|
40
|
-
expect(subject.views.length).to eql
|
38
|
+
context 'using four Models' do
|
39
|
+
it 'returns three Views in Hash' do
|
40
|
+
expect(subject.views.length).to eql 4
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
context
|
45
|
-
it
|
46
|
-
expect(subject.views.key?
|
44
|
+
context 'using `bar`, `foo`, `json` models' do
|
45
|
+
it 'contains a View for `bar` model' do
|
46
|
+
expect(subject.views.key?('BAR_ABC')).to be
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'contains a View for `foo` model' do
|
50
|
+
expect(subject.views.key?('foo_xyz')).to be
|
47
51
|
end
|
48
52
|
|
49
|
-
it
|
50
|
-
expect(subject.views.key?
|
53
|
+
it 'contains a View for `json` model' do
|
54
|
+
expect(subject.views.key?('json')).to be
|
51
55
|
end
|
52
56
|
|
53
|
-
it
|
54
|
-
expect(subject.views.key?
|
57
|
+
it 'contains a View for `envelope` model' do
|
58
|
+
expect(subject.views.key?('envelope')).to be
|
55
59
|
end
|
56
60
|
end
|
57
61
|
end
|
data/spec/response_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Alephant::Renderer::Response do
|
4
4
|
subject { described_class.new content }
|
5
|
-
let(:content) {
|
5
|
+
let(:content) { '<h1>foo</h1>' }
|
6
6
|
|
7
|
-
describe
|
8
|
-
let(:expected_structure) { {
|
7
|
+
describe '#to_json' do
|
8
|
+
let(:expected_structure) { { 'content' => content } }
|
9
9
|
|
10
10
|
specify do
|
11
|
-
expect(::JSON.parse
|
11
|
+
expect(::JSON.parse(subject.to_json)).to eq expected_structure
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
$LOAD_PATH << File.join(File.dirname(__FILE__),
|
1
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'pry'
|
4
|
+
require 'json'
|
5
5
|
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
6
|
+
require 'alephant/renderer'
|
7
|
+
require 'alephant/renderer/error/invalid_path'
|
8
|
+
require 'alephant/renderer/views'
|
9
|
+
require 'alephant/renderer/views/json'
|
10
|
+
require 'alephant/renderer/views/envelope'
|
11
|
+
require 'alephant/renderer/views/html'
|
12
|
+
require 'alephant/renderer/view_mapper'
|
13
|
+
require 'alephant/renderer/response'
|
data/spec/view_mapper_spec.rb
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Alephant::Renderer::ViewMapper do
|
4
|
-
let(:renderer_id) {
|
5
|
-
let(:data) {{ :
|
6
|
-
let(:path) { File.join(File.dirname(__FILE__),
|
4
|
+
let(:renderer_id) { 'foo-renderer' }
|
5
|
+
let(:data) { { foo: :bar } }
|
6
|
+
let(:path) { File.join(File.dirname(__FILE__), 'fixtures/components') }
|
7
7
|
|
8
8
|
subject { Alephant::Renderer::ViewMapper }
|
9
9
|
|
10
|
-
describe
|
11
|
-
context
|
12
|
-
it
|
13
|
-
expect
|
14
|
-
subject.new(renderer_id,
|
15
|
-
|
10
|
+
describe 'initialize(view_base_path)' do
|
11
|
+
context 'view_base_path = invalid_path' do
|
12
|
+
it 'should raise an error' do
|
13
|
+
expect do
|
14
|
+
subject.new(renderer_id, './invalid_path')
|
15
|
+
end.to raise_error ::Alephant::Renderer::Error::InvalidBasePath
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
context
|
20
|
-
it
|
19
|
+
context 'view_base_path = .' do
|
20
|
+
it 'sets base_path' do
|
21
21
|
expect(
|
22
22
|
subject.new(renderer_id, path).base_path
|
23
23
|
).to eq("#{path}/#{renderer_id}")
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
context
|
28
|
-
it
|
27
|
+
context 'view_base_path = nil' do
|
28
|
+
it 'sets base_path' do
|
29
29
|
expect(
|
30
30
|
subject.new(renderer_id).base_path
|
31
31
|
).to eq(Alephant::Renderer::ViewMapper::DEFAULT_LOCATION)
|
@@ -33,11 +33,11 @@ describe Alephant::Renderer::ViewMapper do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
describe
|
37
|
-
it
|
36
|
+
describe 'generate(data)' do
|
37
|
+
it 'calls create_renderer for each template found' do
|
38
38
|
expect(
|
39
39
|
subject.new(renderer_id, path).generate(data).size
|
40
|
-
).to eq(
|
40
|
+
).to eq(4)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fixtures/components/foo-renderer/models/envelope'
|
3
|
+
|
4
|
+
describe Alephant::Renderer::Views::Envelope do
|
5
|
+
let(:data) do
|
6
|
+
{
|
7
|
+
'foo' => 'envelope'
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#content_type' do
|
12
|
+
it 'sets the correct content type' do
|
13
|
+
expect(MyApp::Envelope.new(data).content_type).to eq('application/json')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#render' do
|
18
|
+
it 'returns generated envelope' do
|
19
|
+
expected_result = '{"head":["some/css/asset/path","some/js/asset/path"],"bodyInline":"<h1>Hello, I\'m a envelope template</h1>","bodyLast":["some/thing/to/add/after/body","some/other/thing/to/add/after/body"]}'
|
20
|
+
expect(MyApp::Envelope.new(data).render).to eq(expected_result)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/spec/views_json_spec.rb
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fixtures/components/foo-renderer/models/json'
|
3
3
|
|
4
4
|
describe Alephant::Renderer::Views::Json do
|
5
|
-
let(:data)
|
5
|
+
let(:data) do
|
6
6
|
{
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
'foo' => 'a',
|
8
|
+
'bar' => 'b',
|
9
|
+
'baz' => 'c'
|
10
10
|
}
|
11
|
-
|
11
|
+
end
|
12
12
|
|
13
|
-
describe
|
14
|
-
it
|
15
|
-
expect(MyApp::Json.new(data).content_type).to eq(
|
13
|
+
describe '#content_type' do
|
14
|
+
it 'sets the correct content type' do
|
15
|
+
expect(MyApp::Json.new(data).content_type).to eq('application/json')
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
describe
|
20
|
-
it
|
21
|
-
expected_result = {
|
19
|
+
describe '#to_h' do
|
20
|
+
it 'generates a hash based on the white list' do
|
21
|
+
expected_result = { 'item1' => 'a', 'item2' => 'b' }
|
22
22
|
expect(MyApp::Json.new(data).to_h).to eq(expected_result)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
describe
|
27
|
-
it
|
28
|
-
expected_result =
|
26
|
+
describe '#render' do
|
27
|
+
it 'returns generated json' do
|
28
|
+
expected_result = '{"item1":"a","item2":"b"}'
|
29
29
|
expect(MyApp::Json.new(data).render).to eq(expected_result)
|
30
30
|
end
|
31
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alephant-renderer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- BBC News
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -239,17 +239,21 @@ files:
|
|
239
239
|
- lib/alephant/renderer.rb
|
240
240
|
- lib/alephant/renderer/engine/mustache.rb
|
241
241
|
- lib/alephant/renderer/error/invalid_path.rb
|
242
|
+
- lib/alephant/renderer/i18n/json.rb
|
242
243
|
- lib/alephant/renderer/i18n/locale_component_yaml.rb
|
243
244
|
- lib/alephant/renderer/response.rb
|
244
245
|
- lib/alephant/renderer/version.rb
|
245
246
|
- lib/alephant/renderer/view_mapper.rb
|
246
247
|
- lib/alephant/renderer/views.rb
|
247
248
|
- lib/alephant/renderer/views/base.rb
|
249
|
+
- lib/alephant/renderer/views/envelope.rb
|
248
250
|
- lib/alephant/renderer/views/html.rb
|
249
251
|
- lib/alephant/renderer/views/json.rb
|
250
252
|
- spec/fixtures/components/foo-renderer/models/BAR_ABC.rb
|
253
|
+
- spec/fixtures/components/foo-renderer/models/envelope.rb
|
251
254
|
- spec/fixtures/components/foo-renderer/models/foo_xyz.rb
|
252
255
|
- spec/fixtures/components/foo-renderer/models/json.rb
|
256
|
+
- spec/fixtures/components/foo-renderer/templates/envelope.mustache
|
253
257
|
- spec/integration/engine_mustache_spec.rb
|
254
258
|
- spec/integration/fixtures/components/lib/templates/legacy_shared_template.mustache
|
255
259
|
- spec/integration/fixtures/components/locale/translations.yml
|
@@ -260,7 +264,9 @@ files:
|
|
260
264
|
- spec/integration/fixtures/components/test-renderer/templates/legacy_shared_module.mustache
|
261
265
|
- spec/integration/fixtures/components/test-renderer/templates/translating_module.mustache
|
262
266
|
- spec/integration/fixtures/templates/sample.mustache
|
267
|
+
- spec/integration/fixtures/translations.json
|
263
268
|
- spec/integration/fixtures/translations.yml
|
269
|
+
- spec/integration/i18n_json_spec.rb
|
264
270
|
- spec/integration/i18n_locale_component_yaml_spec.rb
|
265
271
|
- spec/integration/spec_helper.rb
|
266
272
|
- spec/integration/views_html_spec.rb
|
@@ -268,6 +274,7 @@ files:
|
|
268
274
|
- spec/response_spec.rb
|
269
275
|
- spec/spec_helper.rb
|
270
276
|
- spec/view_mapper_spec.rb
|
277
|
+
- spec/views_envelope_spec.rb
|
271
278
|
- spec/views_json_spec.rb
|
272
279
|
homepage: ''
|
273
280
|
licenses:
|
@@ -295,8 +302,10 @@ specification_version: 4
|
|
295
302
|
summary: Render HTML snippets
|
296
303
|
test_files:
|
297
304
|
- spec/fixtures/components/foo-renderer/models/BAR_ABC.rb
|
305
|
+
- spec/fixtures/components/foo-renderer/models/envelope.rb
|
298
306
|
- spec/fixtures/components/foo-renderer/models/foo_xyz.rb
|
299
307
|
- spec/fixtures/components/foo-renderer/models/json.rb
|
308
|
+
- spec/fixtures/components/foo-renderer/templates/envelope.mustache
|
300
309
|
- spec/integration/engine_mustache_spec.rb
|
301
310
|
- spec/integration/fixtures/components/lib/templates/legacy_shared_template.mustache
|
302
311
|
- spec/integration/fixtures/components/locale/translations.yml
|
@@ -307,7 +316,9 @@ test_files:
|
|
307
316
|
- spec/integration/fixtures/components/test-renderer/templates/legacy_shared_module.mustache
|
308
317
|
- spec/integration/fixtures/components/test-renderer/templates/translating_module.mustache
|
309
318
|
- spec/integration/fixtures/templates/sample.mustache
|
319
|
+
- spec/integration/fixtures/translations.json
|
310
320
|
- spec/integration/fixtures/translations.yml
|
321
|
+
- spec/integration/i18n_json_spec.rb
|
311
322
|
- spec/integration/i18n_locale_component_yaml_spec.rb
|
312
323
|
- spec/integration/spec_helper.rb
|
313
324
|
- spec/integration/views_html_spec.rb
|
@@ -315,4 +326,5 @@ test_files:
|
|
315
326
|
- spec/response_spec.rb
|
316
327
|
- spec/spec_helper.rb
|
317
328
|
- spec/view_mapper_spec.rb
|
329
|
+
- spec/views_envelope_spec.rb
|
318
330
|
- spec/views_json_spec.rb
|