seiten 1.1.0 → 1.3.0

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.
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::LocalizedRoutesTest < ActionDispatch::IntegrationTest
4
+ def route_set(&)
5
+ ActionDispatch::Routing::RouteSet.new.tap do |routes|
6
+ routes.default_url_options[:host] = 'example.com'
7
+ routes.draw(&)
8
+ end
9
+ end
10
+
11
+ def draw(&)
12
+ route_set(&).routes.map { |route| [route.name, route.path.spec.to_s, route.defaults.except(:action, :slug)] }
13
+ end
14
+
15
+ test 'routes a navigation of one file under each locale and its slug, to its pages controller' do
16
+ get '/de/anleitung/dein-account/account-verwalten'
17
+ assert_equal 'guide.de account.manage', response.body
18
+
19
+ get '/en/guide'
20
+ assert_equal 'guide.en start', response.body
21
+ end
22
+
23
+ test 'names the routes after the navigation and the locale' do
24
+ assert_equal '/de/anleitung/rechnungen', guide_page_de_path(slug: 'rechnungen')
25
+ end
26
+
27
+ test 'to, as and path replace the defaults' do
28
+ assert_equal [['docs_de', '/de/doku(/*slug)', { controller: 'docs', navigation_id: 'guide', locale: 'de' }],
29
+ ['docs_en', '/en/doku(/*slug)', { controller: 'docs', navigation_id: 'guide', locale: 'en' }]],
30
+ draw { seiten :guide, to: 'docs#show', as: :docs, path: 'doku' }.sort
31
+ end
32
+
33
+ test 'localized: false draws one catch-all route' do
34
+ assert_equal [['seiten_guide_page', '/(*slug)(.:format)', { controller: 'seiten/pages', navigation_id: 'guide' }]],
35
+ draw { seiten :guide, localized: false }
36
+ end
37
+
38
+ test 'navigations of one file per locale get one catch-all route unless localized' do
39
+ assert_equal 1, draw { seiten :application }.size
40
+ assert_equal ['/de(/*slug)', '/en(/*slug)'], draw { seiten :application, localized: true }.map(&:second).sort
41
+ end
42
+
43
+ test 'links a page by its key in the current or the given locale' do
44
+ I18n.with_locale(:de) { assert_equal '/de/anleitung/dein-account/account-verwalten', guide_page_path('account.manage') }
45
+ assert_equal '/en/guide/your-account/account-security', guide_page_path('account.security', locale: :en)
46
+ assert_equal 'http://example.com/de/anleitung/rechnungen?page=2#top', guide_page_url('billing', locale: 'de', host: 'example.com', page: 2, anchor: 'top')
47
+ end
48
+
49
+ test 'links the navigation root without a key' do
50
+ assert_equal '/en/guide', guide_page_path
51
+ assert_equal '/de/anleitung', guide_page_path(locale: :de)
52
+ end
53
+
54
+ test 'page helpers are named after the navigation and the key' do
55
+ assert_equal '/en/guide/your-account/manage-your-account', guide_account_manage_path
56
+ assert_equal 'http://example.com/de/anleitung/rechnungen#top', guide_billing_url(locale: :de, host: 'example.com', anchor: 'top')
57
+ assert_equal '/en/guide', guide_start_path
58
+ end
59
+
60
+ test 'prefix: false names the page helpers after the key only' do
61
+ helpers = route_set { seiten :guide, prefix: false }.url_helpers
62
+
63
+ assert_equal '/en/guide/your-account/manage-your-account', helpers.account_manage_path
64
+ assert_respond_to helpers, :guide_page_path
65
+ end
66
+
67
+ test 'raises for an unknown page or locale' do
68
+ assert_raises(ArgumentError) { guide_page_path('account.missing') }
69
+ assert_raises(ArgumentError) { guide_page_path('billing', locale: :fr) }
70
+ end
71
+ end
@@ -2,12 +2,13 @@ require 'test_helper'
2
2
 
3
3
  class Seiten::HTML::HelpersTest < ActiveSupport::TestCase
4
4
 
5
+ # Its own navigation: pages added to a shared one would show up in other tests.
5
6
  def navigation
6
- @navigation ||= Seiten::Navigation.find_by(id: 'application.en')
7
+ @navigation ||= Seiten::Navigation.new(name: :helpers_test, locale: :en).tap { |navigation| Seiten.navigations << navigation }
7
8
  end
8
9
 
9
- def current_page
10
- @current_page ||= navigation.pages.find(1)
10
+ teardown do
11
+ Seiten.navigations -= [@navigation] if @navigation
11
12
  end
12
13
 
13
14
  def helpers
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::I18nNavigationTest < ActiveSupport::TestCase
4
+ def navigation(locale)
5
+ Seiten::Navigation.find_by(name: 'guide', locale: locale)
6
+ end
7
+
8
+ def page(locale, key)
9
+ navigation(locale).pages.find_by(key: key)
10
+ end
11
+
12
+ test 'one structure file gives a navigation in every available locale' do
13
+ assert_equal ['de', 'en'], Seiten::Navigation.where(name: 'guide').map(&:locale).sort
14
+ end
15
+
16
+ test 'titles and slugs come from I18n, nested like the pages' do
17
+ assert_equal 'Account verwalten', page('de', 'account.manage').title
18
+ assert_equal 'dein-account/account-verwalten', page('de', 'account.manage').slug
19
+ assert_equal 'your-account/account-security', page('en', 'account.security').slug, 'a translated slug replaces the title'
20
+ assert_equal '', page('en', 'start').slug, 'url in the structure applies to every locale'
21
+ assert_equal page('en', 'account'), page('en', 'account.manage').parent
22
+ end
23
+
24
+ test 'pages with the same key translate each other' do
25
+ assert_equal page('en', 'account.manage'), page('de', 'account.manage').translation(:en)
26
+ assert_equal({ 'de' => page('de', 'billing'), 'en' => page('en', 'billing') }, page('en', 'billing').translations)
27
+ end
28
+
29
+ test 'navigations are built again with reloaded translations' do
30
+ I18n.backend.store_translations(:en, navigations: { guide: { billing: { title: 'Invoices' } } })
31
+ Seiten.reset_i18n_navigations!
32
+
33
+ assert_equal 'invoices', page('en', 'billing').slug
34
+ ensure
35
+ I18n.backend.store_translations(:en, navigations: { guide: { billing: { title: 'Billing' } } })
36
+ Seiten.reset_i18n_navigations!
37
+ end
38
+
39
+ test 'the navigation has a title and a slug from I18n, like its pages' do
40
+ assert_equal 'Anleitung', navigation('de').title
41
+ assert_equal 'anleitung', navigation('de').slug
42
+
43
+ I18n.backend.store_translations(:en, navigations: { guide: { slug: 'docs' } })
44
+ assert_equal 'docs', navigation('en').slug
45
+ ensure
46
+ I18n.backend.store_translations(:en, navigations: { guide: { slug: nil } })
47
+ end
48
+
49
+ test 'a page needs a key and a title or slug' do
50
+ assert_raises(Seiten::Errors::PageError) { Seiten.send(:i18n_pages, [{ 'title' => 'Untitled' }], :en, ['navigations', 'guide']) }
51
+ assert_raises(Seiten::Errors::PageError) { Seiten.send(:i18n_pages, [{ 'key' => 'missing' }], :en, ['navigations', 'guide']) }
52
+ end
53
+
54
+ test 'navigations of one locale per file keep working' do
55
+ assert_equal 'Produkte', Seiten::Navigation.find_by(name: 'application', locale: 'de').pages.find_by(slug: 'produkte').title
56
+ end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seiten
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Puglisi
@@ -104,6 +104,7 @@ files:
104
104
  - test/dummy/README.rdoc
105
105
  - test/dummy/Rakefile
106
106
  - test/dummy/app/controllers/application_controller.rb
107
+ - test/dummy/app/controllers/guide/pages_controller.rb
107
108
  - test/dummy/app/controllers/pages_controller.rb
108
109
  - test/dummy/app/controllers/posts_controller.rb
109
110
  - test/dummy/app/helpers/application_helper.rb
@@ -149,11 +150,15 @@ files:
149
150
  - test/dummy/config/initializers/session_store.rb
150
151
  - test/dummy/config/initializers/wrap_parameters.rb
151
152
  - test/dummy/config/locales/en.yml
153
+ - test/dummy/config/locales/guide.de.yml
154
+ - test/dummy/config/locales/guide.en.yml
152
155
  - test/dummy/config/navigations/application.de.yml
153
156
  - test/dummy/config/navigations/application.en.yml
157
+ - test/dummy/config/navigations/guide.yml
154
158
  - test/dummy/config/navigations/help.en.yml
155
159
  - test/dummy/config/routes.rb
156
160
  - test/dummy/config/secrets.yml
161
+ - test/dummy/log/development.log
157
162
  - test/dummy/log/test.log
158
163
  - test/dummy/public/404.html
159
164
  - test/dummy/public/422.html
@@ -164,11 +169,13 @@ files:
164
169
  - test/fixtures/breadcrumb.html
165
170
  - test/fixtures/navigation.html
166
171
  - test/integration/layout_test.rb
172
+ - test/integration/localized_routes_test.rb
167
173
  - test/integration/navigation_test.rb
168
174
  - test/lib/seiten/helpers/frontend_test.rb
169
175
  - test/lib/seiten/html/breadcrumb_test.rb
170
176
  - test/lib/seiten/html/helpers_test.rb
171
177
  - test/lib/seiten/html/navigation_test.rb
178
+ - test/lib/seiten/i18n_navigation_test.rb
172
179
  - test/lib/seiten/navigation_test.rb
173
180
  - test/lib/seiten/page_collection_builder_test.rb
174
181
  - test/lib/seiten/page_collection_test.rb
@@ -206,6 +213,7 @@ test_files:
206
213
  - test/dummy/README.rdoc
207
214
  - test/dummy/Rakefile
208
215
  - test/dummy/app/controllers/application_controller.rb
216
+ - test/dummy/app/controllers/guide/pages_controller.rb
209
217
  - test/dummy/app/controllers/pages_controller.rb
210
218
  - test/dummy/app/controllers/posts_controller.rb
211
219
  - test/dummy/app/helpers/application_helper.rb
@@ -251,11 +259,15 @@ test_files:
251
259
  - test/dummy/config/initializers/session_store.rb
252
260
  - test/dummy/config/initializers/wrap_parameters.rb
253
261
  - test/dummy/config/locales/en.yml
262
+ - test/dummy/config/locales/guide.de.yml
263
+ - test/dummy/config/locales/guide.en.yml
254
264
  - test/dummy/config/navigations/application.de.yml
255
265
  - test/dummy/config/navigations/application.en.yml
266
+ - test/dummy/config/navigations/guide.yml
256
267
  - test/dummy/config/navigations/help.en.yml
257
268
  - test/dummy/config/routes.rb
258
269
  - test/dummy/config/secrets.yml
270
+ - test/dummy/log/development.log
259
271
  - test/dummy/log/test.log
260
272
  - test/dummy/public/404.html
261
273
  - test/dummy/public/422.html
@@ -266,11 +278,13 @@ test_files:
266
278
  - test/fixtures/breadcrumb.html
267
279
  - test/fixtures/navigation.html
268
280
  - test/integration/layout_test.rb
281
+ - test/integration/localized_routes_test.rb
269
282
  - test/integration/navigation_test.rb
270
283
  - test/lib/seiten/helpers/frontend_test.rb
271
284
  - test/lib/seiten/html/breadcrumb_test.rb
272
285
  - test/lib/seiten/html/helpers_test.rb
273
286
  - test/lib/seiten/html/navigation_test.rb
287
+ - test/lib/seiten/i18n_navigation_test.rb
274
288
  - test/lib/seiten/navigation_test.rb
275
289
  - test/lib/seiten/page_collection_builder_test.rb
276
290
  - test/lib/seiten/page_collection_test.rb