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
@@ -33,6 +33,7 @@ module Locomotive
33
33
 
34
34
  def build_field(name, attributes, position)
35
35
  attributes.tap do |attributes|
36
+ attributes[:_id] = name.to_s
36
37
  attributes[:name] = name.to_s
37
38
  attributes[:type] = (attributes[:type] || 'string').to_s.downcase
38
39
  attributes[:position] ||= position
@@ -64,7 +65,7 @@ module Locomotive
64
65
  options.each do |locale, values|
65
66
  values.each_with_index do |name, position|
66
67
  if (option = list.at(position)).nil?
67
- list << { _id: name, name: { locale => name }, position: position }
68
+ list << { _id: position, name: { locale => name }, position: position }
68
69
  else
69
70
  option[:name][locale] = name
70
71
  end
@@ -76,8 +77,8 @@ module Locomotive
76
77
  def build_select_options_from_array(options)
77
78
  [].tap do |list|
78
79
  options.each_with_index do |name, position|
79
- _id = name.is_a?(Hash) ? name.values.first : name
80
- list << { _id: _id, name: name, position: position }
80
+ # _id = name.is_a?(Hash) ? name.values.first : name
81
+ list << { _id: position, name: name, position: position }
81
82
  end
82
83
  end
83
84
  end
@@ -0,0 +1,40 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Adapters
4
+ module Filesystem
5
+ module YAMLLoaders
6
+ class Section
7
+
8
+ include Adapters::Filesystem::YAMLLoader
9
+
10
+ def load(scope)
11
+ super
12
+ load_list
13
+ end
14
+
15
+ private
16
+
17
+ def load_list
18
+ Dir.glob(File.join(path, "*.{#{template_extensions.join(',')}}")).map do |filepath|
19
+ slug = File.basename(filepath).split('.').first
20
+ build(filepath, slug.permalink)
21
+ end
22
+ end
23
+
24
+ def build(filepath, slug)
25
+ {
26
+ name: slug.humanize,
27
+ slug: slug,
28
+ template_path: filepath
29
+ }
30
+ end
31
+
32
+ def path
33
+ @path ||= File.join(site_path, 'app', 'views', 'sections')
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -45,7 +45,6 @@ module Locomotive
45
45
 
46
46
  def each_file(&block)
47
47
  Dir.glob(File.join(path, "*.{#{template_extensions.join(',')}}")).each do |filepath|
48
-
49
48
  slug, locale = File.basename(filepath).split('.')[0..1]
50
49
  locale = default_locale if template_extensions.include?(locale)
51
50
 
@@ -39,7 +39,7 @@ module Locomotive::Steam
39
39
 
40
40
  def memoized_dataset(mapper, scope)
41
41
  return @dataset if @dataset
42
- dataset(mapper, scope)
42
+ @dataset = dataset(mapper, scope)
43
43
  end
44
44
 
45
45
  def dataset(mapper, scope)
@@ -190,9 +190,21 @@ module Locomotive::Steam
190
190
  end
191
191
 
192
192
  def _cast_select(field)
193
- _cast_convertor(:"#{field.name}_id", true) do |value, locale|
194
- name = field.select_options.find(value).try(:name)
195
- locale.nil? ? name : name.try(:[], locale)
193
+ if (_value = attributes[:"#{field.name}_id"]).respond_to?(:translations)
194
+ # the field is localized, so get the labels in all the locales (2 different locales might point to different options)
195
+ # FIXME: dup is used because we want to preserve the original ids
196
+ attributes[field.name] = attributes[:"#{field.name}_id"].dup
197
+
198
+ _cast_convertor(field.name, true) do |value, locale|
199
+ name = field.select_options.find(value)&.name
200
+ locale.nil? ? name&.default : name.try(:[], locale)
201
+ end
202
+ else
203
+ # the field is not localized, we only have the id of the option,
204
+ # so just copy the labels (in all the locales) of the matching select option
205
+ if name = field.select_options.find(_value)&.name # this should either return an i18nField or nil
206
+ attributes[field.name] = name.dup
207
+ end
196
208
  end
197
209
  end
198
210
 
@@ -31,7 +31,8 @@ module Locomotive::Steam
31
31
  end
32
32
 
33
33
  def localized_names
34
- # FIXME: select_fields ALWAYS returns I18nField even if the site is not localized
34
+ # FIXME: select type fields are a bit specific. The label of the options is localized
35
+ # even if the select itself is not (see the _cast_select method in the content_entry entity class)
35
36
  fields.localized_names + select_fields.map(&:name)
36
37
  end
37
38
 
@@ -8,25 +8,27 @@ module Locomotive::Steam
8
8
 
9
9
  def initialize(attributes)
10
10
  super({
11
- handle: nil,
12
- listed: false,
13
- published: true,
14
- templatized: false,
15
- cache_enabled: true,
16
- fullpath: {},
17
- response_type: nil,
18
- content_type: nil,
19
- target_klass_name: nil,
20
- position: 99,
21
- raw_template: nil,
22
- source: nil,
23
- editable_elements: {},
24
- redirect: nil,
25
- redirect_url: {},
26
- redirect_type: nil,
27
- parent_id: nil,
28
- parent_ids: nil,
29
- updated_at: Time.zone.now
11
+ handle: nil,
12
+ listed: false,
13
+ published: true,
14
+ templatized: false,
15
+ cache_enabled: true,
16
+ fullpath: {},
17
+ response_type: nil,
18
+ content_type: nil,
19
+ target_klass_name: nil,
20
+ position: 99,
21
+ raw_template: nil,
22
+ source: nil,
23
+ editable_elements: {},
24
+ sections_content: {},
25
+ sections_dropzone_content: [],
26
+ redirect: nil,
27
+ redirect_url: {},
28
+ redirect_type: nil,
29
+ parent_id: nil,
30
+ parent_ids: nil,
31
+ updated_at: Time.zone.now
30
32
  }.merge(attributes))
31
33
  end
32
34
 
@@ -0,0 +1,24 @@
1
+ module Locomotive::Steam
2
+ class Section
3
+
4
+ include Locomotive::Steam::Models::Entity
5
+
6
+ def initialize(attributes = {})
7
+ super({
8
+ slug: nil,
9
+ template: nil,
10
+ source: nil,
11
+ definition: nil
12
+ }.merge(attributes))
13
+ end
14
+
15
+ def source
16
+ self[:template]
17
+ end
18
+
19
+ def type
20
+ self[:slug]
21
+ end
22
+
23
+ end
24
+ end
@@ -19,7 +19,9 @@ module Locomotive::Steam
19
19
  password: nil,
20
20
  metafields_schema: {},
21
21
  metafields: nil,
22
- asset_host: nil
22
+ sections: nil,
23
+ asset_host: nil,
24
+ routes: []
23
25
  }.merge(attributes))
24
26
  end
25
27
 
@@ -5,4 +5,6 @@ require_relative 'liquid/patches'
5
5
  require_relative 'liquid/drops/base'
6
6
  require_relative 'liquid/drops/i18n_base'
7
7
  require_relative 'liquid/tags/hybrid'
8
+ require_relative 'liquid/tags/concerns/section'
9
+ require_relative 'liquid/tags/section'
8
10
  require_relative_all %w(. drops filters tags/concerns tags), 'liquid'
@@ -4,8 +4,10 @@ module Locomotive
4
4
  module Drops
5
5
  class Page < I18nBase
6
6
 
7
- delegate :position, :fullpath, :depth, :seo_title, :meta_keywords, :meta_description, :redirect_url, :handle, to: :@_source
8
- delegate :listed?, :published?, :redirect?, :is_layout?, :templatized?, to: :@_source
7
+ delegate :position, :fullpath, :depth, :redirect_url, :handle, to: :@_source
8
+ delegate :seo_title, :meta_keywords, :meta_description, to: :@_source
9
+ delegate :listed?, :published?, :redirect?, :index?, :is_layout?, :templatized?, to: :@_source
10
+ delegate :sections_content, :sections_dropzone_content, to: :@_source
9
11
 
10
12
  def title
11
13
  title = @_source.templatized? ? @context['entry'].try(:_label) : nil
@@ -0,0 +1,51 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Drops
5
+
6
+ class Params < ::Liquid::Drop
7
+
8
+ def initialize(params)
9
+ @_params = params.stringify_keys
10
+ end
11
+
12
+ def before_method(meth)
13
+ Param.new(@_params[meth.to_s])
14
+ end
15
+
16
+ def as_json
17
+ @_params.as_json
18
+ end
19
+
20
+ end
21
+
22
+ class Param < ::Liquid::Drop
23
+
24
+ def initialize(param)
25
+ @param = param
26
+ end
27
+
28
+ def html_safe
29
+ @param
30
+ end
31
+
32
+ def to_liquid
33
+ @param.is_a?(String) ? html_escape(@param) : @param
34
+ end
35
+
36
+ def to_s
37
+ to_liquid.to_s
38
+ end
39
+
40
+ private
41
+
42
+ def html_escape(string)
43
+ string.blank? ? '' : CGI::escapeHTML(string)
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,56 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Drops
5
+
6
+ class Section < ::Liquid::Drop
7
+
8
+ def initialize(section, content)
9
+ @section = section
10
+ @content = content
11
+
12
+ if @content.blank?
13
+ @content = section.definition['default'] || { 'settings' => {}, 'blocks' => [] }
14
+ end
15
+ end
16
+
17
+ # FIXME: id acts as the domID to build HTML tags
18
+ def id
19
+ @content['id']
20
+ end
21
+
22
+ def id=(id)
23
+ @content['id'] = id
24
+ end
25
+
26
+ def type
27
+ @section.type
28
+ end
29
+
30
+ def settings
31
+ @content_proxy ||= SectionContentProxy.new(
32
+ @content['settings'] || {},
33
+ @section.definition['settings'] || []
34
+ )
35
+ end
36
+
37
+ def css_class
38
+ @section.definition['class']
39
+ end
40
+
41
+ def blocks
42
+ (@content['blocks'] || []).each_with_index.map do |block, index|
43
+ SectionBlock.new(@section, block, index)
44
+ end
45
+ end
46
+
47
+ def editor_setting_data
48
+ SectionEditorSettingData.new(@section)
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,47 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Drops
5
+
6
+ # Section block drop
7
+ class SectionBlock < ::Liquid::Drop
8
+
9
+ def initialize(section, block, index)
10
+ @section = section
11
+ @block = block || { 'settings' => {} }
12
+ @index = index
13
+ @definition = section.definition['blocks'].find do |block|
14
+ block['type'] == type
15
+ end
16
+ end
17
+
18
+ def id
19
+ @block['id'] || @index
20
+ end
21
+
22
+ def type
23
+ @block['type']
24
+ end
25
+
26
+ def settings
27
+ @content_proxy ||= SectionContentProxy.new(
28
+ @block['settings'] || {},
29
+ @definition['settings'] || []
30
+ )
31
+ end
32
+
33
+ def locomotive_attributes
34
+ if @context.registers[:live_editing]
35
+ value = "section-#{@context['section'].id}-block-#{id}"
36
+ %(data-locomotive-block="#{value}")
37
+ else
38
+ ''
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,97 @@
1
+ module Locomotive
2
+ module Steam
3
+ module Liquid
4
+ module Drops
5
+
6
+ # Represent the content of a section or a block
7
+ # This abstraction is required to handle content manipulation
8
+ # based on field setting type (url for instance).
9
+ class SectionContentProxy < ::Liquid::Drop
10
+
11
+ def initialize(content, settings)
12
+ @content, @settings = content, settings
13
+ end
14
+
15
+ def before_method(name)
16
+ value = @content[name.to_s]
17
+
18
+ case type_of(name)
19
+ when 'url' then SectionUrlField.new(*url_finder.url_for(value))
20
+ when 'image_picker' then SectionImagePickerField.new(value)
21
+ when 'text' then url_finder.decode_urls_for(value)
22
+ else value
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def type_of(name)
29
+ setting_of(name).try(:[], 'type')
30
+ end
31
+
32
+ def setting_of(name)
33
+ @settings.find { |setting| setting['id'] == name.to_s }
34
+ end
35
+
36
+ def url_finder
37
+ @context.registers[:services].url_finder
38
+ end
39
+
40
+ end
41
+
42
+ # Drop representing the valud of an image picker.
43
+ # It holds extra attributes like:
44
+ # the width, height, format and cropped of the image
45
+ class SectionImagePickerField < ::Liquid::Drop
46
+
47
+ def initialize(url_or_attributes)
48
+ if url_or_attributes.is_a?(String) || url_or_attributes.blank?
49
+ @attributes = { source: url_or_attributes }
50
+ else
51
+ @attributes = url_or_attributes.symbolize_keys || {}
52
+ end
53
+ end
54
+
55
+ def source
56
+ @attributes[:source]
57
+ end
58
+
59
+ def width
60
+ @attributes[:width]
61
+ end
62
+
63
+ def height
64
+ @attributes[:height]
65
+ end
66
+
67
+ def cropped
68
+ @attributes[:cropped]
69
+ end
70
+
71
+ def to_s
72
+ self.cropped || self.source || ''
73
+ end
74
+
75
+ end
76
+
77
+ # Drop representing the value of an url attribute
78
+ class SectionUrlField < ::Liquid::Drop
79
+
80
+ def initialize(url, new_window = false)
81
+ @url, @new_window = url || '#', new_window
82
+ end
83
+
84
+ def new_window
85
+ @new_window
86
+ end
87
+
88
+ def to_s
89
+ @url
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
96
+ end
97
+ end