From f5318ee2502854dfbb28be360e5a4d2cde4c3ebd Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Wed, 8 Jun 2022 07:37:44 +0200 Subject: [PATCH] refactor --- app/helpers/admin/blocks_helper.rb | 22 ++++++++----------- .../communication/block/component/base.rb | 4 ++++ .../communication/block/component/image.rb | 12 ++++++---- .../communication/block/template/base.rb | 12 +++++++--- .../block/template/gallery/image.rb | 10 ++++++--- .../communication/blocks/_preview.html.erb | 5 +---- .../blocks/components/image/_edit.html.erb | 17 +++++++++----- .../blocks/components/image/_preview.html.erb | 9 ++++---- .../blocks/components/image/_static.html.erb | 4 ++-- .../components/rich_text/_edit.html.erb | 19 +++++++++------- .../components/rich_text/_preview.html.erb | 2 +- .../blocks/components/string/_edit.html.erb | 19 +++++++++------- .../components/string/_preview.html.erb | 2 +- .../blocks/components/text/_edit.html.erb | 18 ++++++++------- .../blocks/components/text/_preview.html.erb | 2 +- .../blocks/templates/gallery/_edit.html.erb | 16 ++++++++++---- .../templates/gallery/_preview.html.erb | 22 ++++++++++--------- .../blocks/templates/gallery/_static.html.erb | 12 +++++----- 18 files changed, 120 insertions(+), 87 deletions(-) diff --git a/app/helpers/admin/blocks_helper.rb b/app/helpers/admin/blocks_helper.rb index cbec783b0..3c99f60dd 100644 --- a/app/helpers/admin/blocks_helper.rb +++ b/app/helpers/admin/blocks_helper.rb @@ -1,28 +1,24 @@ module Admin::BlocksHelper def block_component_edit(property, **options) - component = block_component property, options[:element] - partial = "admin/communication/blocks/components/#{component.kind}/edit" - render partial, property: property, **options + block_component_render :edit, property, **options end def block_component_preview(property, **options) - component = block_component property, options[:element] - partial = "admin/communication/blocks/components/#{component.kind}/preview" - render partial, property: property, **options + block_component_render :preview, property, **options end def block_component_static(property, **options) - component = block_component property, options[:element] - partial = "admin/communication/blocks/components/#{component.kind}/static" - render partial, property: property, **options + block_component_render :static, property, **options end protected - def block_component(property, element) - template = element.nil? ? @block.template - : element - template.public_send "#{property}_component" + def block_component_render(view, property, **options) + template = options.has_key?(:template) ? options[:template] + : @block.template + component = template.public_send "#{property}_component" + partial = "admin/communication/blocks/components/#{component.kind}/#{view}" + render partial, property: property, template: template, **options end end diff --git a/app/models/communication/block/component/base.rb b/app/models/communication/block/component/base.rb index cd1d13e91..2b2bde10d 100644 --- a/app/models/communication/block/component/base.rb +++ b/app/models/communication/block/component/base.rb @@ -17,4 +17,8 @@ class Communication::Block::Component::Base def kind self.class.name.demodulize.underscore end + + def git_dependencies + [] + end end diff --git a/app/models/communication/block/component/image.rb b/app/models/communication/block/component/image.rb index 3915f6f1e..2976c5af0 100644 --- a/app/models/communication/block/component/image.rb +++ b/app/models/communication/block/component/image.rb @@ -1,9 +1,13 @@ class Communication::Block::Component::Image < Communication::Block::Component::Base - def data - # Loading the blob when needed, not saved in the database - @data['blob'] = template.blob_with_id @data['id'] if @data&.has_key? 'id' - @data + # def data + # # Loading the blob when needed, not saved in the database + # @data['blob'] = template.blob_with_id @data['id'] if @data&.has_key? 'id' + # @data + # end + + def blob + @blob ||= template.blob_with_id data['id'] end end diff --git a/app/models/communication/block/template/base.rb b/app/models/communication/block/template/base.rb index fcbb9a53a..7723cc8ad 100644 --- a/app/models/communication/block/template/base.rb +++ b/app/models/communication/block/template/base.rb @@ -61,7 +61,7 @@ class Communication::Block::Template::Base self.data = json end - # Transforms raw json into ruby objects, based on componenets + # Transforms raw json into ruby objects, based on components def data=(value) if value.is_a? String json = JSON.parse(value) @@ -77,13 +77,14 @@ class Communication::Block::Template::Base return unless has_elements? # Objects are initialized from the database, # then data from the form replaces data from the db. - # We need to reset elements, otherwises it never deletes. + # We need to reset elements, otherwise it's never deleted. @elements = [] json['elements'].each do |json| @elements << default_element(json) end end + # Reads the data from the components def data hash = default_data components.each do |component| @@ -101,7 +102,12 @@ class Communication::Block::Template::Base def git_dependencies unless @git_dependencies @git_dependencies = [] - build_git_dependencies + components.each do |component| + add_dependency component.git_dependencies + end + elements.each do |element| + add_dependency element.git_dependencies + end @git_dependencies.uniq! end @git_dependencies diff --git a/app/models/communication/block/template/gallery/image.rb b/app/models/communication/block/template/gallery/image.rb index fe6e7ee06..6221774cd 100644 --- a/app/models/communication/block/template/gallery/image.rb +++ b/app/models/communication/block/template/gallery/image.rb @@ -4,13 +4,17 @@ class Communication::Block::Template::Gallery::Image < Communication::Block::Tem has_rich_text :credit has_text :text + def blob + image_component.blob + end + def default_data { - 'alt' => '', - 'credit' => '', 'image' => { 'id' => '' - } + }, + 'alt' => '', + 'credit' => '' } end end diff --git a/app/views/admin/communication/blocks/_preview.html.erb b/app/views/admin/communication/blocks/_preview.html.erb index 40d11ae7e..b8c7348a6 100644 --- a/app/views/admin/communication/blocks/_preview.html.erb +++ b/app/views/admin/communication/blocks/_preview.html.erb @@ -1,6 +1,3 @@ <h2 class="h4 mt-5"><%= block.title %></h2> -<% -@block = block -@preview = true -%> +<% @block = block %> <%= render "admin/communication/blocks/templates/#{@block.template_kind}/preview" %> diff --git a/app/views/admin/communication/blocks/components/image/_edit.html.erb b/app/views/admin/communication/blocks/components/image/_edit.html.erb index 4d197c255..57d980fca 100644 --- a/app/views/admin/communication/blocks/components/image/_edit.html.erb +++ b/app/views/admin/communication/blocks/components/image/_edit.html.erb @@ -1,17 +1,22 @@ <% -element ||= false -if element - model = 'element' - dom_id = "'element-' + index + '-#{property}'" -else +template_kind = @block.template_kind +i18n ||= "admin.communication.blocks.templates.#{template_kind}.edit" +template ||= @block.template +root_template = template == @block.template +if root_template model = 'data' dom_id = "#{property}" + i18n_component = "#{i18n}.#{property}" +else + # Nested element + model = 'element' + dom_id = "'element-' + index + '-#{property}'" + i18n_component = "#{i18n}.element.#{property}" end label ||= t 'admin.communication.blocks.components.image.input.label' remove ||= t 'admin.communication.blocks.components.image.input.remove' %> <div class="mb-3"> - <div v-if="!<%= model %>.<%= property %>.id"> <label class="form-label" :for="<%= dom_id.html_safe %>"> diff --git a/app/views/admin/communication/blocks/components/image/_preview.html.erb b/app/views/admin/communication/blocks/components/image/_preview.html.erb index 76241efe7..86c97ebd9 100644 --- a/app/views/admin/communication/blocks/components/image/_preview.html.erb +++ b/app/views/admin/communication/blocks/components/image/_preview.html.erb @@ -1,7 +1,6 @@ <% -image = @block.template.public_send property -blob = image['blob'] if image&.has_key? 'blob' +image = template.public_send "#{property}_component" %> -<%= kamifusen_tag blob, - width: 600, - class: 'img-fluid' if blob %> +<%= kamifusen_tag image.blob, + width: 400, + class: 'img-fluid' if image.blob %> diff --git a/app/views/admin/communication/blocks/components/image/_static.html.erb b/app/views/admin/communication/blocks/components/image/_static.html.erb index de58b80a8..8d2f396a9 100644 --- a/app/views/admin/communication/blocks/components/image/_static.html.erb +++ b/app/views/admin/communication/blocks/components/image/_static.html.erb @@ -1,6 +1,6 @@ <% -image = @block.template.public_send(property) -blob = image['blob'] if image&.has_key? 'blob' +component = template.public_send "#{property}_component" +blob = component.blob indentation = ' ' %> <% if blob %> diff --git a/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb b/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb index 9209df1b5..5c27e809d 100644 --- a/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb +++ b/app/views/admin/communication/blocks/components/rich_text/_edit.html.erb @@ -1,16 +1,19 @@ <% template_kind = @block.template_kind -i18n_template = "admin.communication.blocks.templates.#{template_kind}.edit" -element ||= false -if element - model = 'element' - dom_id = "'element-' + index + '-#{property}'" - i18n_component = "#{i18n_template}.element.#{property}" -else +i18n = "admin.communication.blocks.templates.#{template_kind}.edit" +template ||= @block.template +root_template = template == @block.template +if root_template model = 'data' dom_id = "#{property}" - i18n_component = "#{i18n_template}.#{property}" + i18n_component = "#{i18n}.#{property}" +else + # Nested element + model = 'element' + dom_id = "'element-' + index + '-#{property}'" + i18n_component = "#{i18n}.element.#{property}" end + label ||= t "#{i18n_component}.label" placeholder ||= t "#{i18n_component}.placeholder" %> diff --git a/app/views/admin/communication/blocks/components/rich_text/_preview.html.erb b/app/views/admin/communication/blocks/components/rich_text/_preview.html.erb index ccc6c24ea..0a579ffcd 100644 --- a/app/views/admin/communication/blocks/components/rich_text/_preview.html.erb +++ b/app/views/admin/communication/blocks/components/rich_text/_preview.html.erb @@ -1,2 +1,2 @@ -<% value = @block.template.public_send(property).to_s %> +<% value = template.public_send(property).to_s %> <%= value.html_safe %> diff --git a/app/views/admin/communication/blocks/components/string/_edit.html.erb b/app/views/admin/communication/blocks/components/string/_edit.html.erb index 6e771afbd..b7bc89a3e 100644 --- a/app/views/admin/communication/blocks/components/string/_edit.html.erb +++ b/app/views/admin/communication/blocks/components/string/_edit.html.erb @@ -1,16 +1,19 @@ <% template_kind = @block.template_kind -i18n_template = "admin.communication.blocks.templates.#{template_kind}.edit" -element ||= false -if element - model = 'element' - dom_id = "'element-' + index + '-#{property}'" - i18n_component = "#{i18n_template}.element.#{property}" -else +i18n = "admin.communication.blocks.templates.#{template_kind}.edit" +template ||= @block.template +root_template = template == @block.template +if root_template model = 'data' dom_id = "#{property}" - i18n_component = "#{i18n_template}.#{property}" + i18n_component = "#{i18n}.#{property}" +else + # Nested element + model = 'element' + dom_id = "'element-' + index + '-#{property}'" + i18n_component = "#{i18n}.element.#{property}" end + label ||= t "#{i18n_component}.label" placeholder ||= t "#{i18n_component}.placeholder" %> diff --git a/app/views/admin/communication/blocks/components/string/_preview.html.erb b/app/views/admin/communication/blocks/components/string/_preview.html.erb index 3bcad966b..5411e2b13 100644 --- a/app/views/admin/communication/blocks/components/string/_preview.html.erb +++ b/app/views/admin/communication/blocks/components/string/_preview.html.erb @@ -1 +1 @@ -<%= @block.template.public_send(property).to_s %> +<%= template.public_send(property).to_s %> diff --git a/app/views/admin/communication/blocks/components/text/_edit.html.erb b/app/views/admin/communication/blocks/components/text/_edit.html.erb index 434a82345..0e8e29611 100644 --- a/app/views/admin/communication/blocks/components/text/_edit.html.erb +++ b/app/views/admin/communication/blocks/components/text/_edit.html.erb @@ -1,15 +1,17 @@ <% template_kind = @block.template_kind -i18n_template = "admin.communication.blocks.templates.#{template_kind}.edit" -element ||= false -if element - model = 'element' - dom_id = "'element-' + index + '-#{property}'" - i18n_component = "#{i18n_template}.element.#{property}" -else +i18n = "admin.communication.blocks.templates.#{template_kind}.edit" +template ||= @block.template +root_template = template == @block.template +if root_template model = 'data' dom_id = "#{property}" - i18n_component = "#{i18n_template}.#{property}" + i18n_component = "#{i18n}.#{property}" +else + # Nested element + model = 'element' + dom_id = "'element-' + index + '-#{property}'" + i18n_component = "#{i18n}.element.#{property}" end label ||= t "#{i18n_component}.label" placeholder ||= t "#{i18n_component}.placeholder" diff --git a/app/views/admin/communication/blocks/components/text/_preview.html.erb b/app/views/admin/communication/blocks/components/text/_preview.html.erb index b5d2511a2..5591ba8da 100644 --- a/app/views/admin/communication/blocks/components/text/_preview.html.erb +++ b/app/views/admin/communication/blocks/components/text/_preview.html.erb @@ -1 +1 @@ -<%= simple_format @block.template.public_send(property).to_s %> +<%= simple_format template.public_send(property).to_s %> diff --git a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb index 2005bce92..8ae23ce25 100644 --- a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb +++ b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb @@ -25,10 +25,18 @@ element = @block.template.default_element </div> </div> <div class="card-body"> - <%= block_component_edit :image, element: element %> - <%= block_component_edit :alt, element: element %> - <%= block_component_edit :credit, element: element %> - <%= block_component_edit :text, element: element %> + <%= block_component_edit :image, + template: element %> + <%= block_component_edit :alt, + template: element, + label: t('admin.communication.blocks.components.image.alt.label'), + placeholder: t('admin.communication.blocks.components.image.alt.placeholder') %> + <%= block_component_edit :credit, + template: element, + label: t('admin.communication.blocks.components.image.credit.label'), + placeholder: t('admin.communication.blocks.components.image.credit.placeholder') %> + <%= block_component_edit :text, + template: element %> </div> </div> </div> diff --git a/app/views/admin/communication/blocks/templates/gallery/_preview.html.erb b/app/views/admin/communication/blocks/templates/gallery/_preview.html.erb index 296fb2da1..4307e5f9e 100644 --- a/app/views/admin/communication/blocks/templates/gallery/_preview.html.erb +++ b/app/views/admin/communication/blocks/templates/gallery/_preview.html.erb @@ -1,11 +1,13 @@ -<% @block.template.elements.each do |element| %> - <article class="card"> - <% if element.image['blob'] %> - <%= kamifusen_tag element.image['blob'], width: 500, class: 'img-fluid mb-3' %> - <% end %> - <div class="card-body"> - <p class="mb-0"><%= element.alt %></p> - <p class="mb-0 small"><%= element.text %></p> +<div class="row"> + <% @block.template.elements.each do |element| %> + <div class="col-6"> + <article class="card"> + <%= block_component_preview :image, template: element %> + <div class="card-body"> + <p class="mb-0"><%= element.alt %></p> + <p class="mb-0 small"><%= element.text %></p> + </div> + </article> </div> - </article> -<% end %> + <% end %> +</div> diff --git a/app/views/admin/communication/blocks/templates/gallery/_static.html.erb b/app/views/admin/communication/blocks/templates/gallery/_static.html.erb index 4b82da74f..0c2839e58 100644 --- a/app/views/admin/communication/blocks/templates/gallery/_static.html.erb +++ b/app/views/admin/communication/blocks/templates/gallery/_static.html.erb @@ -1,13 +1,13 @@ layout: <%= block.template.layout %> images: -<% block.template.images_with_alt.each do |image| %> - - id: "<%= image.blob.id %>" - file: "<%= image.blob.id %>" +<% block.template.elements.each do |element| %> + - id: "<%= element.blob.id %>" + file: "<%= element.blob.id %>" alt: > - <%= prepare_text_for_static image.alt, 6 %> + <%= prepare_text_for_static element.alt, 6 %> credit: > - <%= prepare_text_for_static image.credit, 6 %> + <%= prepare_text_for_static element.credit, 6 %> text: > - <%= prepare_text_for_static image.text, 6 %> + <%= prepare_text_for_static element.text, 6 %> <% end %> -- GitLab