locomotivecms_steam 1.4.1 → 1.5.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +31 -25
  4. data/Rakefile +2 -2
  5. data/config/locales/pt-BR.yml +43 -13
  6. data/lib/locomotive/steam.rb +9 -4
  7. data/lib/locomotive/steam/adapters/filesystem.rb +2 -7
  8. data/lib/locomotive/steam/adapters/filesystem/sanitizer.rb +0 -2
  9. data/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb +11 -0
  10. data/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +37 -0
  11. data/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb +18 -0
  12. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb +4 -3
  13. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb +4 -3
  14. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/section.rb +40 -0
  15. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/snippet.rb +0 -1
  16. data/lib/locomotive/steam/adapters/memory.rb +1 -1
  17. data/lib/locomotive/steam/entities/content_entry.rb +15 -3
  18. data/lib/locomotive/steam/entities/content_type.rb +2 -1
  19. data/lib/locomotive/steam/entities/page.rb +21 -19
  20. data/lib/locomotive/steam/entities/section.rb +24 -0
  21. data/lib/locomotive/steam/entities/site.rb +3 -1
  22. data/lib/locomotive/steam/liquid.rb +2 -0
  23. data/lib/locomotive/steam/liquid/drops/page.rb +4 -2
  24. data/lib/locomotive/steam/liquid/drops/params.rb +51 -0
  25. data/lib/locomotive/steam/liquid/drops/section.rb +56 -0
  26. data/lib/locomotive/steam/liquid/drops/section_block.rb +47 -0
  27. data/lib/locomotive/steam/liquid/drops/section_content_proxy.rb +97 -0
  28. data/lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb +65 -0
  29. data/lib/locomotive/steam/liquid/drops/site.rb +2 -1
  30. data/lib/locomotive/steam/liquid/errors.rb +2 -0
  31. data/lib/locomotive/steam/liquid/filters/base.rb +3 -3
  32. data/lib/locomotive/steam/liquid/filters/date.rb +1 -1
  33. data/lib/locomotive/steam/liquid/filters/html.rb +7 -2
  34. data/lib/locomotive/steam/liquid/filters/json.rb +3 -1
  35. data/lib/locomotive/steam/liquid/tags/action.rb +2 -2
  36. data/lib/locomotive/steam/liquid/tags/alt_page_links.rb +63 -0
  37. data/lib/locomotive/steam/liquid/tags/concerns/i18n_page.rb +1 -1
  38. data/lib/locomotive/steam/liquid/tags/concerns/section.rb +117 -0
  39. data/lib/locomotive/steam/liquid/tags/global_section.rb +32 -0
  40. data/lib/locomotive/steam/liquid/tags/model_form.rb +1 -1
  41. data/lib/locomotive/steam/liquid/tags/section.rb +87 -0
  42. data/lib/locomotive/steam/liquid/tags/sections_dropzone.rb +56 -0
  43. data/lib/locomotive/steam/liquid/tags/snippet.rb +3 -2
  44. data/lib/locomotive/steam/liquid/template.rb +0 -5
  45. data/lib/locomotive/steam/middlewares.rb +1 -1
  46. data/lib/locomotive/steam/middlewares/auth.rb +29 -13
  47. data/lib/locomotive/steam/middlewares/concerns/helpers.rb +59 -0
  48. data/lib/locomotive/steam/middlewares/concerns/liquid_context.rb +94 -0
  49. data/lib/locomotive/steam/middlewares/encoded_link_redirection.rb +45 -0
  50. data/lib/locomotive/steam/middlewares/entry_submission.rb +1 -1
  51. data/lib/locomotive/steam/middlewares/favicon.rb +1 -1
  52. data/lib/locomotive/steam/middlewares/locale.rb +63 -15
  53. data/lib/locomotive/steam/middlewares/locale_redirection.rb +18 -6
  54. data/lib/locomotive/steam/middlewares/logging.rb +1 -1
  55. data/lib/locomotive/steam/middlewares/page.rb +35 -6
  56. data/lib/locomotive/steam/middlewares/path.rb +1 -1
  57. data/lib/locomotive/steam/middlewares/private_access.rb +1 -1
  58. data/lib/locomotive/steam/middlewares/redirection.rb +1 -1
  59. data/lib/locomotive/steam/middlewares/renderer.rb +2 -82
  60. data/lib/locomotive/steam/middlewares/robots.rb +1 -1
  61. data/lib/locomotive/steam/middlewares/section.rb +56 -0
  62. data/lib/locomotive/steam/middlewares/site.rb +1 -1
  63. data/lib/locomotive/steam/middlewares/sitemap.rb +58 -45
  64. data/lib/locomotive/steam/middlewares/templatized_page.rb +1 -1
  65. data/lib/locomotive/steam/middlewares/thread_safe.rb +85 -2
  66. data/lib/locomotive/steam/middlewares/timezone.rb +1 -1
  67. data/lib/locomotive/steam/middlewares/url_redirection.rb +1 -1
  68. data/lib/locomotive/steam/models/entity.rb +0 -2
  69. data/lib/locomotive/steam/models/mapper.rb +5 -3
  70. data/lib/locomotive/steam/models/scope.rb +8 -0
  71. data/lib/locomotive/steam/repositories.rb +4 -0
  72. data/lib/locomotive/steam/repositories/content_entry_repository.rb +6 -2
  73. data/lib/locomotive/steam/repositories/page_repository.rb +4 -1
  74. data/lib/locomotive/steam/repositories/section_repository.rb +14 -0
  75. data/lib/locomotive/steam/repositories/site_repository.rb +1 -1
  76. data/lib/locomotive/steam/repositories/snippet_repository.rb +0 -3
  77. data/lib/locomotive/steam/server.rb +3 -1
  78. data/lib/locomotive/steam/services.rb +17 -1
  79. data/lib/locomotive/steam/services/action_service.rb +10 -0
  80. data/lib/locomotive/steam/services/concerns/decorator.rb +0 -2
  81. data/lib/locomotive/steam/services/image_resizer_service.rb +1 -1
  82. data/lib/locomotive/steam/services/page_finder_service.rb +6 -0
  83. data/lib/locomotive/steam/services/section_finder_service.rb +17 -0
  84. data/lib/locomotive/steam/services/url_builder_service.rb +31 -17
  85. data/lib/locomotive/steam/services/url_finder_service.rb +87 -0
  86. data/lib/locomotive/steam/version.rb +2 -2
  87. data/locomotivecms_steam.gemspec +1 -1
  88. data/spec/fixtures/default/app/views/pages/tags/section.liquid.haml +11 -0
  89. data/spec/fixtures/default/app/views/sections/carousel.liquid +20 -0
  90. data/spec/fixtures/default/app/views/sections/footer.liquid +48 -0
  91. data/spec/fixtures/default/app/views/sections/header.liquid +54 -0
  92. data/spec/fixtures/default/config/deploy.yml +1 -1
  93. data/spec/fixtures/errors/section_bad_json_content.liquid +9 -0
  94. data/spec/fixtures/errors/section_bad_json_header.liquid +8 -0
  95. data/spec/fixtures/mongodb/locomotive_accounts.bson +0 -0
  96. data/spec/fixtures/mongodb/locomotive_accounts.metadata.json +1 -1
  97. data/spec/fixtures/mongodb/locomotive_activities.bson +0 -0
  98. data/spec/fixtures/mongodb/locomotive_activities.metadata.json +1 -1
  99. data/spec/fixtures/mongodb/locomotive_content_assets.bson +0 -0
  100. data/spec/fixtures/mongodb/locomotive_content_assets.metadata.json +1 -1
  101. data/spec/fixtures/mongodb/locomotive_content_entries.bson +0 -0
  102. data/spec/fixtures/mongodb/locomotive_content_entries.metadata.json +1 -1
  103. data/spec/fixtures/mongodb/locomotive_content_types.bson +0 -0
  104. data/spec/fixtures/mongodb/locomotive_content_types.metadata.json +1 -1
  105. data/spec/fixtures/mongodb/locomotive_pages.bson +0 -0
  106. data/spec/fixtures/mongodb/locomotive_pages.metadata.json +1 -1
  107. data/spec/fixtures/mongodb/locomotive_sections.bson +0 -0
  108. data/spec/fixtures/mongodb/{sessions.metadata.json → locomotive_sections.metadata.json} +1 -1
  109. data/spec/fixtures/mongodb/locomotive_sites.bson +0 -0
  110. data/spec/fixtures/mongodb/locomotive_sites.metadata.json +1 -1
  111. data/spec/fixtures/mongodb/locomotive_snippets.bson +0 -0
  112. data/spec/fixtures/mongodb/locomotive_snippets.metadata.json +1 -1
  113. data/spec/fixtures/mongodb/locomotive_theme_assets.bson +0 -0
  114. data/spec/fixtures/mongodb/locomotive_theme_assets.metadata.json +1 -1
  115. data/spec/fixtures/mongodb/locomotive_translations.bson +0 -0
  116. data/spec/fixtures/mongodb/locomotive_translations.metadata.json +1 -1
  117. data/spec/integration/repositories/content_entry_repository_spec.rb +1 -1
  118. data/spec/integration/repositories/page_repository_spec.rb +1 -1
  119. data/spec/integration/repositories/theme_asset_repository_spec.rb +1 -1
  120. data/spec/integration/server/auth_spec.rb +0 -4
  121. data/spec/integration/server/basic_spec.rb +8 -0
  122. data/spec/integration/server/contact_form_spec.rb +15 -2
  123. data/spec/integration/server/sitemap_spec.rb +5 -3
  124. data/spec/integration/services/content_entry_service_spec.rb +1 -1
  125. data/spec/support/helpers.rb +3 -3
  126. data/spec/unit/adapters/filesystem/sanitizers/section_spec.rb +65 -0
  127. data/spec/unit/adapters/filesystem/sanitizers/site_spec.rb +28 -2
  128. data/spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb +8 -4
  129. data/spec/unit/adapters/filesystem/yaml_loaders/content_type_spec.rb +2 -2
  130. data/spec/unit/adapters/filesystem/yaml_loaders/page_spec.rb +1 -1
  131. data/spec/unit/adapters/filesystem/yaml_loaders/section_spec.rb +27 -0
  132. data/spec/unit/entities/section_spec.rb +29 -0
  133. data/spec/unit/liquid/drops/params_spec.rb +38 -0
  134. data/spec/unit/liquid/drops/section_content_proxy_spec.rb +88 -0
  135. data/spec/unit/liquid/drops/section_spec.rb +26 -0
  136. data/spec/unit/liquid/filters/html_spec.rb +198 -170
  137. data/spec/unit/liquid/filters/json_spec.rb +7 -0
  138. data/spec/unit/liquid/tags/action_spec.rb +12 -2
  139. data/spec/unit/liquid/tags/alt_page_links_spec.rb +58 -0
  140. data/spec/unit/liquid/tags/global_section_spec.rb +130 -0
  141. data/spec/unit/liquid/tags/link_to_spec.rb +1 -1
  142. data/spec/unit/liquid/tags/locale_switcher_spec.rb +1 -1
  143. data/spec/unit/liquid/tags/model_form_spec.rb +8 -0
  144. data/spec/unit/liquid/tags/path_to_spec.rb +26 -3
  145. data/spec/unit/liquid/tags/section_spec.rb +231 -0
  146. data/spec/unit/liquid/tags/sections_dropzone_spec.rb +107 -0
  147. data/spec/unit/middlewares/auth_spec.rb +9 -2
  148. data/spec/unit/middlewares/encoded_link_redirection_spec.rb +73 -0
  149. data/spec/unit/middlewares/entry_submission_spec.rb +1 -1
  150. data/spec/unit/middlewares/helpers_spec.rb +3 -64
  151. data/spec/unit/middlewares/locale_redirection_spec.rb +13 -2
  152. data/spec/unit/middlewares/locale_spec.rb +52 -10
  153. data/spec/unit/middlewares/page_spec.rb +57 -16
  154. data/spec/unit/middlewares/private_access_spec.rb +1 -1
  155. data/spec/unit/middlewares/redirection_spec.rb +1 -1
  156. data/spec/unit/middlewares/renderer_spec.rb +2 -1
  157. data/spec/unit/middlewares/section_spec.rb +70 -0
  158. data/spec/unit/middlewares/site_spec.rb +1 -1
  159. data/spec/unit/middlewares/sitemap_spec.rb +4 -42
  160. data/spec/unit/middlewares/url_redirection_spec.rb +1 -1
  161. data/spec/unit/repositories/content_entry_repository_spec.rb +13 -13
  162. data/spec/unit/repositories/section_repository_spec.rb +38 -0
  163. data/spec/unit/repositories/snippet_repository_spec.rb +0 -3
  164. data/spec/unit/services/action_service_spec.rb +19 -1
  165. data/spec/unit/services/asset_host_service_spec.rb +8 -0
  166. data/spec/unit/services/parent_finder_service_spec.rb +1 -2
  167. data/spec/unit/services/section_finder_service_spec.rb +28 -0
  168. data/spec/unit/services/url_builder_service_spec.rb +14 -4
  169. data/spec/unit/services/url_finder_service_spec.rb +100 -0
  170. data/spec/unit/services_spec.rb +34 -20
  171. metadata +72 -11
  172. data/lib/locomotive/steam/middlewares/helpers.rb +0 -140
  173. data/spec/fixtures/mongodb/sessions.bson +0 -0
@@ -3,7 +3,7 @@ module Locomotive::Steam
3
3
 
4
4
  class TemplatizedPage < ThreadSafe
5
5
 
6
- include Helpers
6
+ include Concerns::Helpers
7
7
 
8
8
  def _call
9
9
  if page && page.templatized?
@@ -6,10 +6,14 @@ module Locomotive::Steam::Middlewares
6
6
  attr_accessor :env
7
7
 
8
8
  def call(env)
9
- threadsafed = dup
9
+ threadsafed = dup
10
10
  threadsafed.env = env
11
11
 
12
- threadsafed._call
12
+ # time = Benchmark.realtime do
13
+ threadsafed._call # thread-safe purpose
14
+ # end
15
+
16
+ # puts "[Benchmark][#{self.class.name}] Time elapsed #{time*1000} milliseconds"
13
17
 
14
18
  threadsafed.next
15
19
  end
@@ -19,6 +23,85 @@ module Locomotive::Steam::Middlewares
19
23
  @next_response || (@next_response = app.call(env))
20
24
  end
21
25
 
26
+ #= Shortcuts =
27
+
28
+ def services
29
+ @services ||= env.fetch('steam.services')
30
+ end
31
+
32
+ def repositories
33
+ @repositories ||= services.repositories
34
+ end
35
+
36
+ def request
37
+ @request ||= env.fetch('steam.request')
38
+ end
39
+
40
+ def site
41
+ @site ||= env.fetch('steam.site')
42
+ end
43
+
44
+ def page
45
+ @page ||= env.fetch('steam.page')
46
+ end
47
+
48
+ def path
49
+ @path ||= env.fetch('steam.path')
50
+ end
51
+
52
+ def locale
53
+ @locale ||= env.fetch('steam.locale')
54
+ end
55
+
56
+ def liquid_assigns
57
+ @liquid_assigns ||= env.fetch('steam.liquid_assigns')
58
+ end
59
+
60
+ def locales
61
+ site.locales
62
+ end
63
+
64
+ def default_locale
65
+ site.default_locale
66
+ end
67
+
68
+ def params
69
+ @params ||= if request.content_type == 'application/json' && (request.post? || request.put?)
70
+ request.body.rewind
71
+ JSON.parse(request.body.read).with_indifferent_access
72
+ else
73
+ request.params.with_indifferent_access
74
+ end
75
+ end
76
+
77
+ def merge_with_params(values)
78
+ values.each { |name, value| self.request.params[name] = value }
79
+ @params = nil
80
+ end
81
+
82
+ def session
83
+ env['rack.session']
84
+ end
85
+
86
+ def live_editing?
87
+ !!env['steam.live_editing']
88
+ end
89
+
90
+ def decorate_entry(entry)
91
+ return nil if entry.nil?
92
+ Locomotive::Steam::Decorators::I18nDecorator.new(entry, locale, default_locale)
93
+ end
94
+
95
+ def default_liquid_context
96
+ ::Liquid::Context.new({ 'site' => site.to_liquid }, {}, {
97
+ request: request,
98
+ locale: locale,
99
+ site: site,
100
+ services: services,
101
+ repositories: services.repositories
102
+ }, true)
103
+ end
104
+
22
105
  end
23
106
 
24
107
  end
@@ -5,7 +5,7 @@ module Locomotive::Steam
5
5
  #
6
6
  class Timezone < ThreadSafe
7
7
 
8
- include Helpers
8
+ include Concerns::Helpers
9
9
 
10
10
  def _call
11
11
  timezone = site.try(:timezone)
@@ -9,7 +9,7 @@ module Locomotive::Steam
9
9
  #
10
10
  class UrlRedirection < ThreadSafe
11
11
 
12
- include Helpers
12
+ include Concerns::Helpers
13
13
 
14
14
  def _call
15
15
  if url = redirect_url
@@ -1,6 +1,5 @@
1
1
  module Locomotive::Steam
2
2
  module Models
3
-
4
3
  module Entity
5
4
 
6
5
  include Locomotive::Steam::Models::Concerns::Validation
@@ -47,7 +46,6 @@ module Locomotive::Steam
47
46
  def serialize
48
47
  attributes.dup
49
48
  end
50
-
51
49
  end
52
50
  end
53
51
  end
@@ -92,7 +92,7 @@ module Locomotive::Steam
92
92
 
93
93
  def i18n_value_of(entity, name, locale)
94
94
  value = entity.send(name.to_sym)
95
- value.respond_to?(:translations) ? value[locale] : value
95
+ (value.respond_to?(:translations) ? value[locale] : value)
96
96
  end
97
97
 
98
98
  def reset_entity_map
@@ -141,9 +141,11 @@ module Locomotive::Steam
141
141
  end
142
142
 
143
143
  def cache_entity(entity_klass, attributes, &block)
144
- return yield if attributes['_id'].blank?
144
+ entity_id = attributes['_id'] || attributes[:_id] # FIXME: in Wagon, we deal with symbols
145
145
 
146
- key = "#{entity_klass.to_s}-#{attributes['_id']}"
146
+ return yield if entity_id.blank?
147
+
148
+ key = "#{entity_klass.to_s}-#{entity_id}"
147
149
 
148
150
  if (entity = @entity_map[key]).nil?
149
151
  entity = @entity_map[key] = yield
@@ -17,6 +17,14 @@ module Locomotive::Steam
17
17
  site.try(:locales)
18
18
  end
19
19
 
20
+ def with_locale(locale, &block)
21
+ @previous_locale = @locale
22
+ @locale = locale
23
+ yield.tap do
24
+ @locale = @previous_locale
25
+ end
26
+ end
27
+
20
28
  def apply(attributes)
21
29
  attributes['site_id'] = @site._id
22
30
  end
@@ -25,6 +25,10 @@ module Locomotive
25
25
  SnippetRepository.new(adapter, current_site, locale)
26
26
  end
27
27
 
28
+ register :section do
29
+ SectionRepository.new(adapter, current_site, locale)
30
+ end
31
+
28
32
  register :content_type do
29
33
  ContentTypeRepository.new(adapter, current_site, locale)
30
34
  end
@@ -104,7 +104,7 @@ module Locomotive
104
104
  _groups = all.group_by { |entry| i18n_value_of(entry, name) }
105
105
 
106
106
  groups_to_array(name, _groups).tap do |groups|
107
- # entries with a not existing select_option value?
107
+ # entries with a non existing select_option value?
108
108
  unless _groups.blank?
109
109
  groups << { name: nil, entries: _groups.values.flatten }.with_indifferent_access
110
110
  end
@@ -205,6 +205,7 @@ module Locomotive
205
205
  @conditions = conditions.try(:with_indifferent_access) || {}
206
206
  @fields, @operators = fields, {}
207
207
  @target_repository = target_repository
208
+ @locale = target_repository.locale
208
209
 
209
210
  @conditions.each do |name, value|
210
211
  _name, operator = name.to_s.split('.')
@@ -213,8 +214,11 @@ module Locomotive
213
214
  end
214
215
 
215
216
  def prepare
216
- # selects
217
217
  _prepare(@fields.selects) do |field, value|
218
+ # FIXME: [only in Wagon], if the user changes the locale, since all content is stored in memory,
219
+ # we have to change the locale in the repository used to fetch the select options.
220
+ field.select_options.locale = @locale
221
+
218
222
  field.select_options.by_name(value).try(:_id)
219
223
  end
220
224
 
@@ -7,7 +7,10 @@ module Locomotive
7
7
 
8
8
  # Entity mapping
9
9
  mapping :pages, entity: Page do
10
- localized_attributes :title, :slug, :permalink, :source, :raw_template, :template_path, :redirect_url, :fullpath, :seo_title, :meta_description, :meta_keywords
10
+ localized_attributes :title, :slug, :permalink,
11
+ :source, :raw_template, :template_path, :redirect_url, :fullpath,
12
+ :sections_content, :sections_dropzone_content,
13
+ :seo_title, :meta_description, :meta_keywords
11
14
 
12
15
  embedded_association :editable_elements, EditableElementRepository
13
16
  end
@@ -0,0 +1,14 @@
1
+ module Locomotive
2
+ module Steam
3
+ class SectionRepository
4
+
5
+ include Models::Repository
6
+
7
+ mapping :sections, entity: Section
8
+
9
+ def by_slug(slug)
10
+ first { where(slug: slug) }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -7,7 +7,7 @@ module Locomotive
7
7
 
8
8
  # Entity mapping
9
9
  mapping :sites, entity: Site do
10
- localized_attributes :seo_title, :meta_description, :meta_keywords
10
+ localized_attributes :seo_title, :meta_description, :meta_keywords, :sections_content
11
11
  end
12
12
 
13
13
  def by_domain(domain)
@@ -1,11 +1,9 @@
1
1
  module Locomotive
2
2
  module Steam
3
-
4
3
  class SnippetRepository
5
4
 
6
5
  include Models::Repository
7
6
 
8
- # Entity mapping
9
7
  mapping :snippets, entity: Snippet do
10
8
  localized_attributes :template_path, :template, :source
11
9
  end
@@ -13,7 +11,6 @@ module Locomotive
13
11
  def by_slug(slug)
14
12
  first { where(slug: slug) }
15
13
  end
16
-
17
14
  end
18
15
  end
19
16
  end
@@ -31,7 +31,7 @@ module Locomotive::Steam
31
31
  if configuration.serve_assets
32
32
  use ::Rack::Static, {
33
33
  root: configuration.asset_path,
34
- urls: ['/images', '/fonts', '/samples', '/sites']
34
+ urls: ['/images', '/fonts', '/samples', '/sites', '/misc', '/others', '/pdfs', '/media']
35
35
  }
36
36
  use Middlewares::DynamicAssets, {
37
37
  root: configuration.asset_path,
@@ -55,6 +55,7 @@ module Locomotive::Steam
55
55
  Middlewares::DefaultEnv,
56
56
  Middlewares::Site,
57
57
  Middlewares::Logging,
58
+ Middlewares::EncodedLinkRedirection,
58
59
  Middlewares::UrlRedirection,
59
60
  Middlewares::Robots,
60
61
  Middlewares::Timezone,
@@ -66,6 +67,7 @@ module Locomotive::Steam
66
67
  Middlewares::PrivateAccess,
67
68
  Middlewares::Path,
68
69
  Middlewares::Page,
70
+ Middlewares::Section,
69
71
  Middlewares::Sitemap,
70
72
  Middlewares::TemplatizedPage
71
73
  ]
@@ -15,6 +15,14 @@ module Locomotive
15
15
  end
16
16
  end
17
17
 
18
+ # Used to get an easy access to some of the services (url_builder)
19
+ # without passing a request
20
+ def self.build_simple_instance(site)
21
+ Instance.new(nil).tap do |instance|
22
+ instance.current_site = site
23
+ end
24
+ end
25
+
18
26
  class Defer < SimpleDelegator
19
27
  def initialize(&block)
20
28
  @constructor = block
@@ -62,6 +70,10 @@ module Locomotive
62
70
  Steam::SnippetFinderService.new(repositories.snippet)
63
71
  end
64
72
 
73
+ register :section_finder do
74
+ Steam::SectionFinderService.new(repositories.section)
75
+ end
76
+
65
77
  register :action do
66
78
  Steam::ActionService.new(current_site, email, content_entry: content_entry, api: external_api, redirection: page_redirection)
67
79
  end
@@ -79,7 +91,11 @@ module Locomotive
79
91
  end
80
92
 
81
93
  register :url_builder do
82
- Steam::UrlBuilderService.new(current_site, locale, request)
94
+ Steam::UrlBuilderService.new(current_site, locale, request&.env&.fetch('steam.mounted_on', nil))
95
+ end
96
+
97
+ register :url_finder do
98
+ Steam::UrlFinderService.new(url_builder, page_finder, content_entry)
83
99
  end
84
100
 
85
101
  register :page_redirection do
@@ -18,6 +18,8 @@ module Locomotive
18
18
  setProp
19
19
  getSessionProp
20
20
  setSessionProp
21
+ getCookiesProp
22
+ setCookiesProp
21
23
  sendEmail
22
24
  allEntries
23
25
  findEntry
@@ -81,6 +83,14 @@ module Locomotive
81
83
  -> (name, value) { liquid_context.registers[:session][name.to_sym] = value }
82
84
  end
83
85
 
86
+ def get_cookies_prop_lambda(liquid_context)
87
+ -> (name) { liquid_context.registers[:cookies][name.to_s].as_json }
88
+ end
89
+
90
+ def set_cookies_prop_lambda(liquid_context)
91
+ -> (name, value) { liquid_context.registers[:cookies][name.to_s] = value.to_s }
92
+ end
93
+
84
94
  def all_entries_lambda(liquid_context)
85
95
  -> (type, conditions) { content_entry_service.all(type, conditions, true) }
86
96
  end
@@ -26,9 +26,7 @@ module Locomotive
26
26
  def default_locale
27
27
  repository.site.default_locale
28
28
  end
29
-
30
29
  end
31
-
32
30
  end
33
31
  end
34
32
  end
@@ -42,7 +42,7 @@ module Locomotive
42
42
  elsif source.respond_to?(:url)
43
43
  source.url
44
44
  else
45
- source
45
+ source&.to_s
46
46
  end
47
47
  value.strip if value
48
48
  end
@@ -25,6 +25,12 @@ module Locomotive
25
25
  end
26
26
  end
27
27
 
28
+ def find_by_id(id)
29
+ decorate do
30
+ repository.find(id)
31
+ end
32
+ end
33
+
28
34
  private
29
35
 
30
36
  def decorate(&block)
@@ -0,0 +1,17 @@
1
+ module Locomotive
2
+ module Steam
3
+ class SectionFinderService
4
+
5
+ include Locomotive::Steam::Services::Concerns::Decorator
6
+
7
+ attr_accessor_initialize :repository
8
+
9
+ def find(slug)
10
+ decorate do
11
+ repository.by_slug(slug)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -3,25 +3,44 @@ module Locomotive
3
3
 
4
4
  class UrlBuilderService
5
5
 
6
- attr_accessor_initialize :site, :current_locale, :request
6
+ attr_accessor_initialize :site, :current_locale, :mounted_on
7
7
 
8
- def url_for(page, locale = nil)
9
- prefix(_url_for(page, locale))
8
+ def url_for(page, locale = nil, prefix_default_locale = nil)
9
+ prefix(_url_for(page, locale, prefix_default_locale))
10
10
  end
11
11
 
12
- def _url_for(page, locale = nil)
13
- [''].tap do |segments|
14
- locale ||= current_locale
15
- same_locale = locale.to_sym == site.default_locale.to_sym
12
+ def _url_for(page, locale = nil, prefix_default_locale = nil)
13
+ locale = locale&.to_sym
14
+ _locale = (locale || current_locale).to_sym
15
+ default_locale = site.default_locale.to_sym
16
+ same_locale = _locale == default_locale
17
+
18
+ # the prefix_default_locale can override the site.prefix_default_locale attribute
19
+ prefix_default_locale = prefix_default_locale.nil? ? site.prefix_default_locale : prefix_default_locale
16
20
 
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
21
+ fullpath = sanitized_fullpath(page, same_locale)
22
+
23
+ [''].tap do |segments|
24
+ # To insert the locale in the path, 2 cases:
25
+ #
26
+ # 1. if the prefix_default_locale is enabled, we need to
27
+ # add the locale no matter if the locale is the same as the default one.
28
+ #
29
+ # 2. since we also store the locale in session, calling the index page ("/")
30
+ # will always return the page in the locale stored in session.
31
+ # In order to see the index page in the default locale, we need to allow
32
+ # "/<default locale>" instead of just "/".
33
+ #
34
+ if prefix_default_locale || !same_locale
35
+ segments << _locale
36
+ elsif fullpath.blank? && locale == default_locale && current_locale != locale
20
37
  segments << locale
21
38
  end
22
39
 
23
- # fullpath
24
- segments << sanitized_fullpath(page, same_locale)
40
+ # we don't want a trailing slash for the home page if a locale is set
41
+ fullpath = nil if segments.size == 2 && fullpath.blank?
42
+
43
+ segments << fullpath
25
44
  end.compact.join('/')
26
45
  end
27
46
 
@@ -39,11 +58,6 @@ module Locomotive
39
58
 
40
59
  private
41
60
 
42
- def mounted_on
43
- return if request.nil?
44
- request.env['steam.mounted_on']
45
- end
46
-
47
61
  def sanitized_fullpath(page, same_locale)
48
62
  path = page.fullpath
49
63