locomotivecms_steam 1.1.2 → 1.2.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -2
  3. data/Gemfile.lock +41 -52
  4. data/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb +30 -35
  5. data/lib/locomotive/steam/adapters/filesystem.rb +8 -0
  6. data/lib/locomotive/steam/adapters/mongodb/command.rb +8 -1
  7. data/lib/locomotive/steam/adapters/mongodb.rb +10 -1
  8. data/lib/locomotive/steam/entities/content_entry.rb +13 -6
  9. data/lib/locomotive/steam/entities/page.rb +4 -0
  10. data/lib/locomotive/steam/entities/site.rb +1 -0
  11. data/lib/locomotive/steam/errors.rb +3 -0
  12. data/lib/locomotive/steam/initializers/dragonfly.rb +3 -5
  13. data/lib/locomotive/steam/initializers/sprockets.rb +10 -0
  14. data/lib/locomotive/steam/liquid/drops/content_entry.rb +8 -0
  15. data/lib/locomotive/steam/liquid/drops/content_entry_collection.rb +1 -1
  16. data/lib/locomotive/steam/liquid/tags/action.rb +59 -0
  17. data/lib/locomotive/steam/middlewares/entry_submission.rb +1 -1
  18. data/lib/locomotive/steam/middlewares/helpers.rb +8 -0
  19. data/lib/locomotive/steam/middlewares/locale.rb +1 -5
  20. data/lib/locomotive/steam/middlewares/locale_redirection.rb +1 -7
  21. data/lib/locomotive/steam/middlewares/renderer.rb +6 -3
  22. data/lib/locomotive/steam/middlewares/site.rb +12 -5
  23. data/lib/locomotive/steam/middlewares/sitemap.rb +6 -2
  24. data/lib/locomotive/steam/middlewares/thread_safe.rb +0 -4
  25. data/lib/locomotive/steam/models/associations/many_to_many.rb +1 -1
  26. data/lib/locomotive/steam/models/entity.rb +5 -0
  27. data/lib/locomotive/steam/models/repository.rb +4 -0
  28. data/lib/locomotive/steam/repositories/content_entry_repository.rb +23 -4
  29. data/lib/locomotive/steam/repositories/content_type_field_repository.rb +4 -0
  30. data/lib/locomotive/steam/services/action_service.rb +92 -0
  31. data/lib/locomotive/steam/services/content_entry_service.rb +114 -0
  32. data/lib/locomotive/steam/services/email_service.rb +102 -0
  33. data/lib/locomotive/steam/services/entry_submission_service.rb +6 -58
  34. data/lib/locomotive/steam/services/liquid_parser_service.rb +6 -0
  35. data/lib/locomotive/steam/services/url_builder_service.rb +5 -2
  36. data/lib/locomotive/steam/services.rb +13 -1
  37. data/lib/locomotive/steam/version.rb +1 -1
  38. data/lib/locomotive/steam.rb +5 -3
  39. data/locomotivecms_steam.gemspec +2 -0
  40. data/spec/fixtures/default/data/messages.yml +0 -0
  41. data/spec/integration/services/content_entry_service_spec.rb +110 -0
  42. data/spec/unit/adapters/filesystem_adapter_spec.rb +10 -0
  43. data/spec/unit/adapters/mongodb_adapter_spec.rb +18 -0
  44. data/spec/unit/entities/content_entry_spec.rb +34 -0
  45. data/spec/unit/entities/editable_element_spec.rb +19 -0
  46. data/spec/unit/entities/page_spec.rb +29 -0
  47. data/spec/unit/liquid/drops/content_entry_collection_spec.rb +4 -0
  48. data/spec/unit/liquid/drops/content_entry_spec.rb +5 -2
  49. data/spec/unit/liquid/tags/action_spec.rb +23 -0
  50. data/spec/unit/liquid/tags/link_to_spec.rb +12 -4
  51. data/spec/unit/liquid/tags/locale_switcher_spec.rb +15 -7
  52. data/spec/unit/liquid/tags/nav_spec.rb +19 -11
  53. data/spec/unit/liquid/tags/path_to_spec.rb +12 -4
  54. data/spec/unit/middlewares/helpers_spec.rb +29 -0
  55. data/spec/unit/middlewares/locale_redirection_spec.rb +11 -29
  56. data/spec/unit/middlewares/site_spec.rb +66 -13
  57. data/spec/unit/middlewares/sitemap_spec.rb +44 -0
  58. data/spec/unit/models/i18n_field_spec.rb +23 -0
  59. data/spec/unit/repositories/content_entry_repository_spec.rb +39 -7
  60. data/spec/unit/repositories/content_type_field_repository_spec.rb +10 -0
  61. data/spec/unit/services/action_service_spec.rb +173 -0
  62. data/spec/unit/services/content_entry_service_spec.rb +63 -0
  63. data/spec/unit/services/email_service_spec.rb +198 -0
  64. data/spec/unit/services/entry_submission_service_spec.rb +28 -112
  65. data/spec/unit/services/url_builder_service_spec.rb +14 -5
  66. metadata +50 -6
  67. data/spec/unit/middlewares/locale_spec.rb +0 -52
@@ -14,8 +14,11 @@ module Locomotive
14
14
  locale ||= current_locale
15
15
  same_locale = locale.to_sym == site.default_locale.to_sym
16
16
 
17
- # locale
18
- segments << locale unless same_locale
17
+ # if the prefix_default_locale is enabled, we need to
18
+ # add the locale no matter if the locale is the same as the default one
19
+ if site.prefix_default_locale || !same_locale
20
+ segments << locale
21
+ end
19
22
 
20
23
  # fullpath
21
24
  segments << sanitized_fullpath(page, same_locale)
@@ -62,8 +62,16 @@ module Locomotive
62
62
  Steam::SnippetFinderService.new(repositories.snippet)
63
63
  end
64
64
 
65
+ register :action do
66
+ Steam::ActionService.new(current_site, email, content_entry)
67
+ end
68
+
69
+ register :content_entry do
70
+ Steam::ContentEntryService.new(repositories.content_type, repositories.content_entry, locale)
71
+ end
72
+
65
73
  register :entry_submission do
66
- Steam::EntrySubmissionService.new(repositories.content_type, repositories.content_entry, locale)
74
+ Steam::EntrySubmissionService.new(content_entry)
67
75
  end
68
76
 
69
77
  register :liquid_parser do
@@ -106,6 +114,10 @@ module Locomotive
106
114
  Steam::TextileService.new
107
115
  end
108
116
 
117
+ register :email do
118
+ Steam::EmailService.new(page_finder, liquid_parser, asset_host, configuration.mode == :test)
119
+ end
120
+
109
121
  register :cache do
110
122
  Steam::NoCacheService.new
111
123
  end
@@ -3,6 +3,6 @@
3
3
  # 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0
4
4
  module Locomotive
5
5
  module Steam
6
- VERSION = '1.1.2'
6
+ VERSION = '1.2.0.beta1'
7
7
  end
8
8
  end
@@ -13,13 +13,15 @@ require_relative 'steam/services'
13
13
  module Locomotive
14
14
  module Steam
15
15
 
16
- FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo
16
+ FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo.freeze
17
17
 
18
18
  WILDCARD = 'content_type_template'.freeze
19
19
 
20
- CONTENT_ENTRY_ENGINE_CLASS_NAME = /^Locomotive::ContentEntry(.*)$/o
20
+ CONTENT_ENTRY_ENGINE_CLASS_NAME = /^Locomotive::ContentEntry(.*)$/o.freeze
21
21
 
22
- IsHTTP = /\Ahttps?:\/\//o
22
+ IsHTTP = /\Ahttps?:\/\//o.freeze
23
+
24
+ IsLAYOUT = /\Alayouts(\/|\z)/o.freeze
23
25
 
24
26
  class << self
25
27
  attr_writer :configuration
@@ -44,6 +44,8 @@ Gem::Specification.new do |spec|
44
44
  spec.add_dependency 'haml', '~> 4.0.6'
45
45
  spec.add_dependency 'mimetype-fu', '~> 0.1.2'
46
46
  spec.add_dependency 'mime-types', '~> 2.6.1'
47
+ spec.add_dependency 'duktape', '~> 1.3.0.6'
48
+ spec.add_dependency 'pony', '~> 1.11'
47
49
 
48
50
  spec.add_dependency 'locomotivecms-solid', '~> 4.0.1'
49
51
  spec.add_dependency 'locomotivecms_common', '~> 0.2.0'
File without changes
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb'
4
+ require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb'
5
+
6
+ describe Locomotive::Steam::ContentEntryService do
7
+
8
+ shared_examples_for 'a content entry service' do
9
+
10
+ let(:site) { Locomotive::Steam::Site.new(_id: site_id, locales: %w(en fr nb)) }
11
+ let(:locale) { :en }
12
+ let(:type_repository) { Locomotive::Steam::ContentTypeRepository.new(adapter, site, locale) }
13
+ let(:entry_repository) { Locomotive::Steam::ContentEntryRepository.new(adapter, site, locale, type_repository) }
14
+ let(:service) { described_class.new(type_repository, entry_repository, locale) }
15
+ let(:type) { 'bands' }
16
+
17
+ describe '#all' do
18
+ subject { service.all(type) }
19
+ it { expect(subject.size).to eq 3 }
20
+ context 'with conditions' do
21
+ subject { service.all(type, kind: 'grunge') }
22
+ it { expect(subject.size).to eq 2 }
23
+ end
24
+ context 'as_json enabled' do
25
+ subject { service.all(type, { kind: 'grunge' }, true) }
26
+ it { expect(subject.first.slice('name', 'leader')).to eq('name' => 'Alice in Chains', 'leader' => 'Layne') }
27
+ end
28
+ end
29
+
30
+ describe '#find' do
31
+ let(:id_or_slug) { 'alice-in-chains'}
32
+ subject { service.find(type, id_or_slug) }
33
+ it { expect(subject.name).to eq 'Alice in Chains' }
34
+ context 'with an id' do
35
+ let(:id_or_slug) { entry_id }
36
+ it { expect(subject.name).to eq 'Pearl Jam' }
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ context 'MongoDB' do
43
+
44
+ it_should_behave_like 'a content entry service' do
45
+
46
+ let(:site_id) { mongodb_site_id }
47
+ let(:adapter) { Locomotive::Steam::MongoDBAdapter.new(database: 'steam_test', hosts: ['127.0.0.1:27017']) }
48
+ let(:entry_id) { BSON::ObjectId.from_string('5610310b87f6431588000029') }
49
+
50
+ describe '#create' do
51
+ subject { service.create('messages', { name: 'John', email: 'john@doe.net', message: 'Hello world!' }) }
52
+ it { expect { subject }.to change { service.all('messages').size } }
53
+ it { expect(subject.name).to eq 'John' }
54
+ after { service.delete('messages', subject._id) }
55
+ end
56
+
57
+ describe '#update' do
58
+ let!(:message) { service.create('messages', { name: 'John', email: 'john@doe.net', message: 'Hello world!' }) }
59
+ subject { service.update('messages', message._id, { name: 'Jane' }) }
60
+ it { expect { subject }.not_to change { service.all('messages').size } }
61
+ it { expect(subject.name).to eq 'Jane' }
62
+ after { service.delete('messages', message._id) }
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ context 'Filesystem' do
70
+
71
+ it_should_behave_like 'a content entry service' do
72
+
73
+ let(:site_id) { 1 }
74
+ let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) }
75
+ let(:entry_id) { 'pearl-jam' }
76
+
77
+ after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear }
78
+
79
+ describe '#create' do
80
+
81
+ let(:attributes) { { name: 'John', email: 'john@doe.net', message: 'Hello world!' } }
82
+
83
+ subject { service.create('messages', attributes, true) }
84
+
85
+ it { expect { subject }.to change { service.all('messages').size } }
86
+ it { expect(subject['name']).to eq 'John' }
87
+ it { expect(subject['errors'].blank?).to eq true }
88
+
89
+ context 'missing attributes' do
90
+
91
+ let(:attributes) { {} }
92
+
93
+ it { expect { subject }.not_to change { service.all('messages').size } }
94
+ it { expect(subject['errors']).to eq({ 'name' => ["can't be blank"], 'email' => ["can't be blank"], 'message' => ["can't be blank"] }) }
95
+
96
+ end
97
+ end
98
+
99
+ describe '#update' do
100
+ let!(:message) { service.create('messages', { name: 'John', email: 'john@doe.net', message: 'Hello world!' }) }
101
+ subject { service.update('messages', message._id, { name: 'Jane' }, true) }
102
+ it { expect { subject }.not_to change { service.all('messages').size } }
103
+ it { expect(subject['name']).to eq 'Jane' }
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -68,4 +68,14 @@ describe Locomotive::Steam::FilesystemAdapter do
68
68
 
69
69
  end
70
70
 
71
+ describe '#make_id' do
72
+
73
+ let(:id) { '42' }
74
+
75
+ subject { adapter.make_id(id) }
76
+
77
+ it { is_expected.to eq('42') }
78
+
79
+ end
80
+
71
81
  end
@@ -14,4 +14,22 @@ describe Locomotive::Steam::MongoDBAdapter do
14
14
 
15
15
  end
16
16
 
17
+ describe '#make_id' do
18
+
19
+ let(:id) { '56fd9f48a2f42217744a85d7' }
20
+
21
+ subject { adapter.make_id(id) }
22
+
23
+ it { is_expected.to eq(BSON::ObjectId.from_string('56fd9f48a2f42217744a85d7')) }
24
+
25
+ context 'passing a BSON::ObjectId' do
26
+
27
+ let(:id) { BSON::ObjectId.from_string('56fd9f48a2f42217744a85d7') }
28
+
29
+ it { is_expected.to eq(BSON::ObjectId.from_string('56fd9f48a2f42217744a85d7')) }
30
+
31
+ end
32
+
33
+ end
34
+
17
35
  end
@@ -10,6 +10,21 @@ describe Locomotive::Steam::ContentEntry do
10
10
 
11
11
  before { content_entry.content_type = type }
12
12
 
13
+ describe '#change' do
14
+
15
+ let(:fields) { [instance_double('Field', name: :title, type: :string, required: true)] }
16
+
17
+ before do
18
+ allow(type).to receive(:fields_by_name).and_return({ title: fields.first })
19
+ end
20
+
21
+ subject { content_entry.change('title' => 'Hello world!') }
22
+
23
+ it { expect(subject.title).to eq('Hello world!') }
24
+ it { expect(subject._slug).to eq('hello-world') }
25
+
26
+ end
27
+
13
28
  describe '#valid?' do
14
29
 
15
30
  let(:fields) { [instance_double('Field', name: :title, type: :string, required: true)] }
@@ -84,6 +99,25 @@ describe Locomotive::Steam::ContentEntry do
84
99
 
85
100
  end
86
101
 
102
+ describe '#as_json' do
103
+
104
+ let(:fields) { [instance_double('TitleField', name: :title, type: :string), instance_double('PictureField', name: :picture, type: :file, localized: true)] }
105
+ let(:attributes) { { id: 42, title: 'Hello world', _slug: 'hello-world', picture: Locomotive::Steam::Models::I18nField.new(:picture, fr: 'foo.png', en: 'bar.png'), custom_fields_recipe: ['hello', 'world'], _type: 'Entry' } }
106
+ let(:decorated) { Locomotive::Steam::Decorators::I18nDecorator.new(content_entry, :fr, :en) }
107
+
108
+ before do
109
+ allow(type).to receive(:fields_by_name).and_return({ title: fields.first, picture: fields.last })
110
+ allow(type).to receive(:persisted_field_names).and_return([:title, :picture])
111
+ allow(content_entry).to receive(:localized_attributes).and_return({ picture: true })
112
+ allow(content_entry).to receive(:base_url).and_return('/assets')
113
+ end
114
+
115
+ subject { decorated.as_json }
116
+
117
+ it { expect(subject['picture']['url']).to eq '/assets/foo.png' }
118
+
119
+ end
120
+
87
121
  describe 'dynamic attributes' do
88
122
 
89
123
  let(:field_type) { :string }
@@ -7,4 +7,23 @@ describe Locomotive::Steam::EditableElement do
7
7
 
8
8
  it { expect(page.block).to eq nil }
9
9
 
10
+ describe '#source' do
11
+
12
+ let(:source) { 'Hello world' }
13
+ let(:attributes) { { content: 'Lorem ipsum', source: source } }
14
+
15
+ subject { page.source }
16
+
17
+ it { is_expected.to eq 'Hello world' }
18
+
19
+ context 'no source attribute' do
20
+
21
+ let(:source) { nil }
22
+
23
+ it { is_expected.to eq 'Lorem ipsum' }
24
+
25
+ end
26
+
27
+ end
28
+
10
29
  end
@@ -33,6 +33,25 @@ describe Locomotive::Steam::Page do
33
33
 
34
34
  end
35
35
 
36
+ describe '#layout?' do
37
+
38
+ let(:attributes) { { fullpath: { en: 'foo/layouts' } } }
39
+
40
+ subject { page.layout? }
41
+ it { is_expected.to eq false }
42
+
43
+ context 'true if starting by layouts' do
44
+ let(:attributes) { { fullpath: { en: 'layouts/base' } } }
45
+ it { is_expected.to eq true }
46
+ end
47
+
48
+ context 'true if the root layouts page' do
49
+ let(:attributes) { { fullpath: { en: 'layouts' } } }
50
+ it { is_expected.to eq true }
51
+ end
52
+
53
+ end
54
+
36
55
  describe '#valid?' do
37
56
 
38
57
  subject { page.valid? }
@@ -77,4 +96,14 @@ describe Locomotive::Steam::Page do
77
96
 
78
97
  end
79
98
 
99
+ describe '#source' do
100
+
101
+ let(:attributes) { { 'raw_template' => 'template code here' } }
102
+
103
+ subject { page.source }
104
+
105
+ it { is_expected.to eq 'template code here'}
106
+
107
+ end
108
+
80
109
  end
@@ -32,6 +32,10 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntryCollection do
32
32
  it { expect(drop.last).to eq('b') }
33
33
  end
34
34
 
35
+ describe '#last' do
36
+ it { expect(drop.map(&:to_s)).to eq(['a', 'b']) }
37
+ end
38
+
35
39
  context 'with a scope' do
36
40
 
37
41
  let(:assigns) { { 'with_scope' => { 'visible' => true } } }
@@ -100,7 +100,10 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do
100
100
 
101
101
  describe '#as_json' do
102
102
 
103
- let(:type) { instance_double('Type', fields_by_name: { title: instance_double('StringField', type: :string ), picture: instance_double('FileField', type: :file), category: instance_double('SelectField', type: :select) }) }
103
+ let(:entry) { instance_double('Article', _id: 42, localized_attributes: {}, content_type: type, title: 'Hello world', _label: 'Hello world', _slug: 'hello-world', _translated: false, seo_title: 'seo title', meta_keywords: 'keywords', meta_description: 'description', created_at: 0, updated_at: 1, author: author, authors: authors) }
104
+ let(:type) { instance_double('Type', fields_by_name: { title: instance_double('StringField', type: :string ), author: instance_double('Author', type: :belongs_to), authors: instance_double('Author', type: :many_to_many), picture: instance_double('FileField', type: :file), category: instance_double('SelectField', type: :select) }) }
105
+ let(:author) { instance_double('Author', _slug: 'john-doe', localized_attributes: {}) }
106
+ let(:authors) { instance_double('Authors', all: [author]) }
104
107
  let(:picture_field) { Locomotive::Steam::ContentEntry::FileField.new('foo.png', 'http://assets.dev', 0, 42) }
105
108
 
106
109
  before do
@@ -110,7 +113,7 @@ describe Locomotive::Steam::Liquid::Drops::ContentEntry do
110
113
 
111
114
  subject { drop.as_json }
112
115
 
113
- it { is_expected.to eq('id' => 1, '_id' => 1, 'title' => 'Hello world', 'picture' => 'http://assets.dev/foo.png?42', 'picture_url' => 'http://assets.dev/foo.png?42', 'category_id' => 42, 'category' => 'Test') }
116
+ it { is_expected.to eq('id' => 1, '_id' => 1, 'title' => 'Hello world', 'picture' => 'http://assets.dev/foo.png?42', 'picture_url' => 'http://assets.dev/foo.png?42', 'category_id' => 42, 'category' => 'Test', 'author' => 'john-doe', 'authors' => ['john-doe']) }
114
117
 
115
118
  end
116
119
 
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Locomotive::Steam::Liquid::Tags::Action do
4
+
5
+ let(:site) { instance_double('Site', default_locale: 'en') }
6
+ let(:source) { '{% action "random Javascript action" %}var foo = 42; setProp("foo", foo);{% endaction %}' }
7
+ let(:assigns) { {} }
8
+ let(:services) { Locomotive::Steam::Services.build_instance }
9
+ let(:context) { ::Liquid::Context.new(assigns, {}, { services: services }) }
10
+
11
+ before { allow(services).to receive(:current_site).and_return(site) }
12
+
13
+ subject { render_template(source, context) }
14
+
15
+ describe 'rendering' do
16
+
17
+ it { is_expected.to eq '' }
18
+
19
+ it { subject; expect(context['foo']).to eq 42.0 }
20
+
21
+ end
22
+
23
+ end
@@ -2,10 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Locomotive::Steam::Liquid::Tags::PathTo do
4
4
 
5
- let(:assigns) { {} }
6
- let(:services) { Locomotive::Steam::Services.build_instance }
7
- let(:site) { instance_double('Site', default_locale: 'en') }
8
- let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, site: site, locale: 'en' }) }
5
+ let(:prefix_default) { false }
6
+ let(:assigns) { {} }
7
+ let(:services) { Locomotive::Steam::Services.build_instance }
8
+ let(:site) { instance_double('Site', default_locale: 'en', prefix_default_locale: prefix_default) }
9
+ let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, site: site, locale: 'en' }) }
9
10
 
10
11
  subject { render_template(source, context) }
11
12
 
@@ -67,6 +68,13 @@ describe Locomotive::Steam::Liquid::Tags::PathTo do
67
68
 
68
69
  end
69
70
 
71
+ context 'prefix_default_locale is true' do
72
+
73
+ let(:prefix_default) { true }
74
+ it { is_expected.to eq 'My link: <a href="/en/">Home</a>!' }
75
+
76
+ end
77
+
70
78
  end
71
79
 
72
80
  describe 'used as a block' do
@@ -2,13 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe Locomotive::Steam::Liquid::Tags::LocaleSwitcher do
4
4
 
5
- let(:locale) { 'en' }
6
- let(:assigns) { { 'page' => drop } }
7
- let(:services) { Locomotive::Steam::Services.build_instance }
8
- let(:site) { instance_double('Site', locales: %w(en fr), default_locale: 'en') }
9
- let(:drop) { Locomotive::Steam::Liquid::Drops::Page.new(page) }
10
- let(:page) { liquid_instance_double('Index', localized_attributes: { title: true, fullpath: true }, title: { en: 'Home', fr: 'Accueil' }, fullpath: { en: 'index', fr: 'index' }, templatized?: false) }
11
- let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, site: site, locale: locale }) }
5
+ let(:locale) { 'en' }
6
+ let(:assigns) { { 'page' => drop } }
7
+ let(:prefix_default) { false }
8
+ let(:services) { Locomotive::Steam::Services.build_instance }
9
+ let(:site) { instance_double('Site', locales: %w(en fr), default_locale: 'en', prefix_default_locale: prefix_default) }
10
+ let(:drop) { Locomotive::Steam::Liquid::Drops::Page.new(page) }
11
+ let(:page) { liquid_instance_double('Index', localized_attributes: { title: true, fullpath: true }, title: { en: 'Home', fr: 'Accueil' }, fullpath: { en: 'index', fr: 'index' }, templatized?: false) }
12
+ let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, site: site, locale: locale }) }
12
13
 
13
14
  subject { render_template(source, context) }
14
15
 
@@ -26,6 +27,13 @@ describe Locomotive::Steam::Liquid::Tags::LocaleSwitcher do
26
27
 
27
28
  end
28
29
 
30
+ context 'prefix_default_locale is true' do
31
+
32
+ let(:prefix_default) { true }
33
+ it { is_expected.to eq '<div id="locale-switcher"><a href="/en/" class="en current">en</a> | <a href="/fr" class="fr">fr</a></div>' }
34
+
35
+ end
36
+
29
37
  end
30
38
 
31
39
  describe 'using the locale to display the links' do
@@ -19,17 +19,18 @@ describe 'Locomotive::Steam::Liquid::Tags::Nav' do
19
19
  ]
20
20
  end
21
21
 
22
- let(:source) { '{% nav site %}' }
23
- let(:site) { instance_double('Site', name: 'My portfolio', default_locale: 'en') }
24
- let(:page) { index }
25
- let(:services) { Locomotive::Steam::Services.build_instance }
26
- let(:repository) { services.repositories.page }
27
- let(:assigns) { {} }
28
- let(:registers) { { services: services, site: site, page: page } }
29
- let(:context) { ::Liquid::Context.new(assigns, {}, registers) }
30
- let(:options) { { services: services } }
31
-
32
- let(:output) { render_template(source, context, options) }
22
+ let(:prefix_default) { false }
23
+ let(:source) { '{% nav site %}' }
24
+ let(:site) { instance_double('Site', name: 'My portfolio', default_locale: 'en', prefix_default_locale: prefix_default) }
25
+ let(:page) { index }
26
+ let(:services) { Locomotive::Steam::Services.build_instance }
27
+ let(:repository) { services.repositories.page }
28
+ let(:assigns) { {} }
29
+ let(:registers) { { services: services, site: site, page: page } }
30
+ let(:context) { ::Liquid::Context.new(assigns, {}, registers) }
31
+ let(:options) { { services: services } }
32
+
33
+ let(:output) { render_template(source, context, options) }
33
34
 
34
35
  before { allow(services).to receive(:current_site).and_return(site) }
35
36
 
@@ -46,6 +47,13 @@ describe 'Locomotive::Steam::Liquid::Tags::Nav' do
46
47
 
47
48
  it { is_expected.to eq %{<nav id="nav"><ul><li id="child-1-link" class="link first"><a href="/child-1">Child #1</a></li>\n<li id="child-2-link" class="link last"><a href="/child-2">Child #2</a></li></ul></nav>} }
48
49
 
50
+ context 'prefix_default_locale is true' do
51
+
52
+ let(:prefix_default) { true }
53
+ it { is_expected.to eq %{<nav id="nav"><ul><li id="child-1-link" class="link first"><a href="/en/child-1">Child #1</a></li>\n<li id="child-2-link" class="link last"><a href="/en/child-2">Child #2</a></li></ul></nav>} }
54
+
55
+ end
56
+
49
57
  end
50
58
 
51
59
  describe 'from a page' do
@@ -2,10 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Locomotive::Steam::Liquid::Tags::PathTo do
4
4
 
5
- let(:assigns) { {} }
6
- let(:services) { Locomotive::Steam::Services.build_instance }
7
- let(:site) { instance_double('Site', locales: ['en'], default_locale: 'en') }
8
- let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, site: site, locale: 'en' }) }
5
+ let(:prefix_default) { false }
6
+ let(:assigns) { {} }
7
+ let(:services) { Locomotive::Steam::Services.build_instance }
8
+ let(:site) { instance_double('Site', locales: ['en'], default_locale: 'en', prefix_default_locale: prefix_default) }
9
+ let(:context) { ::Liquid::Context.new(assigns, {}, { services: services, site: site, locale: 'en' }) }
9
10
 
10
11
  subject { render_template(source, context) }
11
12
 
@@ -51,6 +52,13 @@ describe Locomotive::Steam::Liquid::Tags::PathTo do
51
52
 
52
53
  end
53
54
 
55
+ context 'prefix_default_locale is true' do
56
+
57
+ let(:prefix_default) { true }
58
+ it { is_expected.to eq '/en/' }
59
+
60
+ end
61
+
54
62
  end
55
63
 
56
64
  describe 'from a page (drop) itself' do
@@ -7,6 +7,35 @@ describe Locomotive::Steam::Middlewares::Helpers do
7
7
  let(:middleware) { Class.new { include Locomotive::Steam::Middlewares::Helpers } }
8
8
  let(:instance) { middleware.new }
9
9
 
10
+ describe '#make_local_path' do
11
+
12
+ let(:mounted_on) { nil }
13
+ let(:location) { '/foo/bar' }
14
+
15
+ before { allow(instance).to receive(:mounted_on).and_return(mounted_on) }
16
+
17
+ subject { instance.make_local_path(location) }
18
+
19
+ it { is_expected.to eq '/foo/bar' }
20
+
21
+ context 'mounted_on is not blank' do
22
+
23
+ let(:mounted_on) { '/my_app' }
24
+
25
+ it { is_expected.to eq '/foo/bar' }
26
+
27
+ context 'path including mounted_on' do
28
+
29
+ let(:location) { '/my_app/foo/bar' }
30
+
31
+ it { is_expected.to eq '/foo/bar' }
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
10
39
  describe '#redirect_to' do
11
40
 
12
41
  subject { instance.redirect_to(location)[1]['Location'] }
@@ -6,7 +6,6 @@ require_relative '../../../lib/locomotive/steam/middlewares/locale_redirection'
6
6
 
7
7
  describe Locomotive::Steam::Middlewares::LocaleRedirection do
8
8
 
9
- let(:prefixed) { false }
10
9
  let(:site) { instance_double('Site', prefix_default_locale: prefixed, default_locale: :de, locales: %w(de fr)) }
11
10
  let(:url) { 'http://models.example.com' }
12
11
  let(:app) { ->(env) { [200, env, 'app'] } }
@@ -19,49 +18,32 @@ describe Locomotive::Steam::Middlewares::LocaleRedirection do
19
18
  env = env_for(url, 'steam.site' => site, 'steam.locale' => locale, 'steam.locale_in_path' => locale_in_path)
20
19
  env['steam.mounted_on'] = mounted_on
21
20
  env['steam.request'] = Rack::Request.new(env)
22
- env['steam.path'] = env['steam.request'].path_info.gsub(/\A#{mounted_on}/, '')
21
+ env['steam.path'] = env['steam.request'].path_info.gsub(/\A#{mounted_on}/, '').gsub(/\A\/#{locale}/, '')
23
22
  code, env = middleware.call(env)
24
23
  [code, env['Location']]
25
24
  end
26
25
 
27
- describe 'not prefixed by locale' do
26
+ describe 'prefix_default_locale is false' do
28
27
 
29
- describe 'strip default locale from root path' do
30
- let(:url) { 'http://models.example.com/de' }
31
- it { is_expected.to eq [301, '/'] }
32
- end
28
+ let(:prefixed) { false }
33
29
 
34
- describe 'strip default locale' do
35
- let(:url) { 'http://models.example.com/de/hello' }
36
- it { is_expected.to eq [301, '/hello'] }
37
- end
30
+ describe 'locale is not part of the path' do
38
31
 
39
- describe 'strip default locale from root path with query' do
40
- let(:url) { 'http://models.example.com/de?this=is_a_param' }
41
- it { is_expected.to eq [301, '/?this=is_a_param'] }
42
- end
32
+ let(:locale_in_path) { false }
33
+ it { is_expected.to eq [200, nil] }
43
34
 
44
- describe 'strip default locale from path with query' do
45
- let(:url) { 'http://models.example.com/de/hello?this=is_a_param' }
46
- it { is_expected.to eq [301, '/hello?this=is_a_param'] }
47
35
  end
48
36
 
49
- describe 'dont strip a non-default locale' do
50
- let(:locale) { 'fr' }
51
- let(:url) { 'http://models.example.com/fr/hello' }
52
- it { is_expected.to eq [200, nil] }
53
- end
37
+ describe 'for seo purpose redirect to the path without the locale' do
38
+
39
+ let(:url) { 'http://models.example.com/de/hello' }
40
+ it { is_expected.to eq [301, '/hello'] }
54
41
 
55
- describe 'dont redirect URL without locale' do
56
- let(:locale) { :de }
57
- let(:locale_in_path) { false }
58
- let(:url) { 'http://models.example.com/hello' }
59
- it { is_expected.to eq [200, nil] }
60
42
  end
61
43
 
62
44
  end
63
45
 
64
- describe 'prefixed by locale' do
46
+ describe 'prefix_default_locale is true' do
65
47
 
66
48
  let(:prefixed) { true }
67
49