Skip to content

Commit

Permalink
Fix issue with uploading images
Browse files Browse the repository at this point in the history
There was an issue in which multiple forms were being created and this
make image uploading failed. This fixes the issue by making sure only
one form is created.
  • Loading branch information
kevinnio committed Mar 8, 2024
1 parent 8f3dc11 commit 6d2eb34
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
- render "mcm/component_form", component: @component do |component_form|
- component_form.fields_for :metadata, @component.metadata do |metadata_form|
.row
.col-6= metadata_form.text_field :title, required: true
.col-6= metadata_form.select :title_color, available_text_colors, required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
.row
.col-6= metadata_form.text_field :title, required: true
.col-6= metadata_form.select :title_color, available_text_colors, required: true

= metadata_form.label :content
= metadata_form.hidden_field :content, required: true
%trix-editor{ input: 'components_page_metadata_content' }
= metadata_form.label :content
= metadata_form.hidden_field :content, required: true
%trix-editor{ input: 'components_page_metadata_content' }

.row.mt-3
.col-6
= metadata_form.select :content_color, available_text_colors
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.row
.col-6
= metadata_form.text_field :cta, required: true, label: 'CTA'
.col-6
= metadata_form.text_field :cta_url, required: true, label: 'URL'
.row
.col-6
= metadata_form.select :button_color, available_button_colors
.row.mt-3
.col-6
= metadata_form.select :content_color, available_text_colors
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.row
.col-6
= metadata_form.text_field :cta, required: true, label: 'CTA'
.col-6
= metadata_form.text_field :cta_url, required: true, label: 'URL'
.row
.col-6
= metadata_form.select :button_color, available_button_colors

- 2.times { @component.assets.build } if @component.assets.empty?
- component_form.fields_for :assets do |assets_form|
= assets_form.hidden_field :position, value: assets_form.index + 1
= render Mcm::ImageComponent.new(form: assets_form,
styles: image_preview_styles(variant_for(assets_form.index)),
variant: variant_for(assets_form.index),
label: t("custom_pages.components.banner.image_#{assets_form.index + 1}"))
- 2.times { @component.assets.build } if @component.assets.empty?
- @component_form.fields_for :assets do |assets_form|
= assets_form.hidden_field :position, value: assets_form.index + 1
= render Mcm::ImageComponent.new(form: assets_form,
styles: image_preview_styles(variant_for(assets_form.index)),
variant: variant_for(assets_form.index),
label: t("custom_pages.components.banner.image_#{assets_form.index + 1}"))
3 changes: 2 additions & 1 deletion app/components/mcm/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ def self.component_type
"content"
end

def initialize(component:)
def initialize(component:, component_form: )
@component = component
@component_form = component_form
end

def defaults
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
- render "mcm/component_form", component: @component do |component_form|
- component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.text_field :section_title, help: t('custom_pages.messages.leave_empty')
.row
.col-6
= metadata_form.select :section_title_alignment, text_alignments, required: true
.col-6
= metadata_form.select :section_title_color, available_text_colors, required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.text_field :section_title, help: t('custom_pages.messages.leave_empty')
.row
.col-6
= metadata_form.select :section_title_alignment, text_alignments, required: true
.col-6
= metadata_form.select :section_title_color, available_text_colors, required: true

.row
.col-6
= metadata_form.text_field :background_color, label: t('custom_pages.components.background_color'), help: '#ffffff'
.col-6
= metadata_form.number_field :height, label: t('custom_pages.components.height.label'), help: t('custom_pages.components.height.help')
= metadata_form.check_box :full_width, label: t('custom_pages.components.full_width'), custom: :switch
.row
.col-6
= metadata_form.text_field :background_color, label: t('custom_pages.components.background_color'), help: '#ffffff'
.col-6
= metadata_form.number_field :height, label: t('custom_pages.components.height.label'), help: t('custom_pages.components.height.help')
= metadata_form.check_box :full_width, label: t('custom_pages.components.full_width'), custom: :switch
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- if @component.metadata.section_title.present?
%h2{ class: title_classes }= @component.metadata.section_title

.d-flex.align-content-center{ style: "height:#{height}px" }
.d-flex.align-content-center{ style: "height:#{height}" }
- @component.children.active.each do |component|
.d-flex.align-items-center.flex-wrap
= render "mcm/child_component", component: component
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- render "mcm/component_form", component: @component do |component_form|
- @component.assets.first_or_initialize
- component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, variant: :desktop, styles: image_preview_styles)
- @component.assets.first_or_initialize
- @component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, variant: :desktop, styles: image_preview_styles)
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
- render "mcm/component_form", component: @component do |component_form|
- @component.assets.first_or_initialize
- component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, styles: image_preview_styles)
- @component.assets.first_or_initialize
- @component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, styles: image_preview_styles)

- component_form.fields_for :metadata, @component.metadata do |metadata_form|
.row
.col-6
= metadata_form.text_field :title, required: true
.col-6
= metadata_form.select :title_color, available_text_colors, required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
.row
.col-6
= metadata_form.text_field :title, required: true
.col-6
= metadata_form.select :title_color, available_text_colors, required: true

= metadata_form.label :content
= metadata_form.hidden_field :content, required: true
%trix-editor{ input: 'components_page_metadata_content' }
= metadata_form.label :content
= metadata_form.hidden_field :content, required: true
%trix-editor{ input: 'components_page_metadata_content' }

.row.mt-3
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.col-6
= metadata_form.select :content_vertical_alignment, vertical_alignment_options, required: true
.row
.col-6
= metadata_form.select :content_color, available_text_colors, required: true
.col-6
= metadata_form.select :content_horizontal_alignment, horizontal_alignment_options, required: true
.row
.col-6
= metadata_form.text_field :cta
.col-6
= metadata_form.text_field :cta_url
.row
.col-6
= metadata_form.select :button_color, available_button_colors
.col-6
= metadata_form.select :button_alignment, text_alignments, required: true
.row.mt-3
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.col-6
= metadata_form.select :content_vertical_alignment, vertical_alignment_options, required: true
.row
.col-6
= metadata_form.select :content_color, available_text_colors, required: true
.col-6
= metadata_form.select :content_horizontal_alignment, horizontal_alignment_options, required: true
.row
.col-6
= metadata_form.text_field :cta
.col-6
= metadata_form.text_field :cta_url
.row
.col-6
= metadata_form.select :button_color, available_button_colors
.col-6
= metadata_form.select :button_alignment, text_alignments, required: true
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- render "mcm/component_form", component: @component do |component_form|
- component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.number_field :height, required: true
= metadata_form.check_box :full_width, label: t('custom_pages.components.full_width'), custom: :switch
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.number_field :height, required: true
= metadata_form.check_box :full_width, label: t('custom_pages.components.full_width'), custom: :switch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%div{ class: @component.full_width? ? 'container-fluid p-0' : 'container' }
.carousel.slide.w-100{ data: { ride: 'carousel' }, id: "hero-slide-#{@component.id}", style: "height: #{height}px;" }
.carousel.slide.w-100{ data: { ride: 'carousel' }, id: "hero-slide-#{@component.id}", style: "height: #{height};" }
.carousel-inner
- @component.children.active.each_with_index do |slide, index|
.carousel-item{ class: index.zero? ? 'active' : '' }= render "mcm/child_component", component: slide
Expand Down
2 changes: 1 addition & 1 deletion app/components/mcm/image_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})
protected

def image_src
return unless @asset.persisted?
return unless @asset && @asset.attachment.attached?

@asset.attachment.variant(@variant).processed.url
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
- render "mcm/component_form", component: @component do |component_form|
- @component.assets.first_or_initialize
- component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, styles: image_preview_styles, variant: :small)
- @component.assets.first_or_initialize
- @component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, styles: image_preview_styles, variant: :small)

- component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.text_field :title, required: true, help: 'Always centered'
= metadata_form.select :title_color, available_text_colors, required: true
= metadata_form.text_area :content, required: true
.row
.col-6
= metadata_form.select :content_color, available_text_colors, required: true
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.row
.col-6
= metadata_form.text_field :cta, label: "Call to Action"
.col-6
= metadata_form.text_field :cta_url, label: "Call to Action URL"
.row
.col-6
= metadata_form.select :button_color, available_button_colors
.col-6
= metadata_form.select :button_alignment, text_alignments, required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.text_field :title, required: true, help: 'Always centered'
= metadata_form.select :title_color, available_text_colors, required: true
= metadata_form.text_area :content, required: true
.row
.col-6
= metadata_form.select :content_color, available_text_colors, required: true
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.row
.col-6
= metadata_form.text_field :cta, label: "Call to Action"
.col-6
= metadata_form.text_field :cta_url, label: "Call to Action URL"
.row
.col-6
= metadata_form.select :button_color, available_button_colors
.col-6
= metadata_form.select :button_alignment, text_alignments, required: true
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
- render "mcm/component_form", component: @component do |component_form|
- component_form.fields_for :metadata, @component.metadata do |metadata_form|
%trix-editor{ input: 'components_page_metadata_body' }
= metadata_form.hidden_field :body
= metadata_form.select :content_color, available_text_colors, required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
%trix-editor{ input: 'components_page_metadata_body' }
= metadata_form.hidden_field :body
= metadata_form.select :content_color, available_text_colors, required: true
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
- render "mcm/component_form", component: @component do |component_form|
- component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.select :vertical_alignment, vertical_alignment_options, required: true
= metadata_form.select :horizontal_alignment, horizontal_alignment_options, required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.select :vertical_alignment, vertical_alignment_options, required: true
= metadata_form.select :horizontal_alignment, horizontal_alignment_options, required: true

- @component.assets.first_or_initialize
- component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, styles: image_preview_styles, variant: :small)
- @component.assets.first_or_initialize
- @component_form.fields_for :assets do |assets_form|
= render Mcm::ImageComponent.new(form: assets_form, styles: image_preview_styles, variant: :small)
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
- render "mcm/component_form", component: @component do |component_form|
- component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.text_field :title, required: true
.row
.col-6
= metadata_form.select :title_alignment, text_alignments, required: true
.col-6
= metadata_form.select :title_color, available_text_colors, required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.text_field :title, required: true
.row
.col-6
= metadata_form.select :title_alignment, text_alignments, required: true
.col-6
= metadata_form.select :title_color, available_text_colors, required: true

= metadata_form.hidden_field :content, required: true
%trix-editor{ input: 'components_page_metadata_content' }
= metadata_form.hidden_field :content, required: true
%trix-editor{ input: 'components_page_metadata_content' }

.row.mt-3
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.col-6
= metadata_form.select :content_color, available_text_colors, required: true
.row
.col-6
= metadata_form.text_field :cta_title, label: t('.cta.title')
= metadata_form.text_field :cta_url, label: t('.cta.url')
= metadata_form.select :button_color, available_button_colors, label: t('.cta.button')
= metadata_form.select :button_alignment, text_alignments, required: true
.col-6
= metadata_form.select :content_vertical_alignment, vertical_alignment_options, required: true, label: t('.position.vertical')
= metadata_form.select :content_horizontal_alignment, horizontal_alignment_options, required: true, label: t('.position.horizontal')
.row.mt-3
.col-6
= metadata_form.select :content_alignment, text_alignments, required: true
.col-6
= metadata_form.select :content_color, available_text_colors, required: true
.row
.col-6
= metadata_form.text_field :cta_title, label: t('.cta.title')
= metadata_form.text_field :cta_url, label: t('.cta.url')
= metadata_form.select :button_color, available_button_colors, label: t('.cta.button')
= metadata_form.select :button_alignment, text_alignments, required: true
.col-6
= metadata_form.select :content_vertical_alignment, vertical_alignment_options, required: true, label: t('.position.vertical')
= metadata_form.select :content_horizontal_alignment, horizontal_alignment_options, required: true, label: t('.position.horizontal')
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- render "mcm/component_form", component: @component do |component_form|
- component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.number_field :height, required: true
= metadata_form.text_field :youtube_video_id, placeholder: 'vlDzyIIMYma', help: 'Youtube video id', required: true
- @component_form.fields_for :metadata, @component.metadata do |metadata_form|
= metadata_form.number_field :height, required: true
= metadata_form.text_field :youtube_video_id, placeholder: 'vlDzyIIMYma', help: 'Youtube video id', required: true
2 changes: 1 addition & 1 deletion app/models/mcm/components_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ComponentsPage < Mcm::ApplicationRecord
before_destroy :update_related_positions

def view_component(component_form: nil)
"::Mcm::#{component_name.camelize}Component".constantize.new(component: self)
"::Mcm::#{component_name.camelize}Component".constantize.new(component: self, component_form:)
end

# rubocop:disable Rails/SkipsModelValidations
Expand Down
5 changes: 0 additions & 5 deletions app/views/mcm/_component_form.html.haml

This file was deleted.

5 changes: 2 additions & 3 deletions app/views/mcm/admin/components/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
- if @component.draft?
.alert.alert-warning= t('custom_pages.messages.draft')

= bootstrap_form_for @component, as: :components_page,
url: main_app.admin_component_path(@component), method: :put do |f|
= f.text_field :name, required: true
= bootstrap_form_for @component, as: :components_page, url: main_app.admin_component_path(@component), method: :put do |f|
.form-group
= f.text_field :name, required: true
= f.check_box :active, label: t('custom_pages.components.status'), custom: :switch

= render @component.view_component(component_form: f)
Expand Down
3 changes: 1 addition & 2 deletions app/views/mcm/pages/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
- Rails.logger.error(e)
.container
%h4= t('custom_pages.messages.something_went_wrong')
= link_to(t('custom_pages.actions.edit_component'),
main_app.edit_admin_component_path(component))
= link_to(t('custom_pages.actions.edit_component'), main_app.edit_admin_component_path(component))

- if @page.metadata.background_color.present?
:css
Expand Down

0 comments on commit 6d2eb34

Please sign in to comment.