Skip to content

Commit

Permalink
Fix sync_components task
Browse files Browse the repository at this point in the history
This task was using Presenters, which have been removed in favor of
ViewComponents. This fixes the task.
  • Loading branch information
kevinnio committed Feb 23, 2024
1 parent 5ef49b2 commit 4df21f5
Showing 1 changed file with 5 additions and 53 deletions.
58 changes: 5 additions & 53 deletions lib/tasks/mcm_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,64 +1,16 @@
namespace :mcm do
task sync_components: :environment do
components = []
path = File.join(File.dirname(__FILE__), '..', '..', 'app', 'presenters', 'mcm', '*.rb')
path = File.join(File.dirname(__FILE__), '..', '..', 'app', 'components', 'mcm', '*.rb')
Dir[path].each do |file|
component_name = file.split('/').last.gsub('_presenter.rb', '')
next if component_name.eql?('components_base')
component_name = file.split('/').last.gsub('_component.rb', '')
next if %w[base image].include?(component_name)

puts "Syncing #{component_name}"
presenter = "Mcm::#{component_name.camelize}Presenter".constantize
Mcm::Component.find_or_create_by(name: component_name,
component_type: presenter.component_type)
component = "Mcm::#{component_name.camelize}Component".constantize
Mcm::Component.find_or_create_by(name: component_name,component_type: component.component_type)
components << component_name
end
Mcm::Component.where.not(name: components).destroy_all
end

task fill_positions: :environment do
Mcm::Page.find_each do |page|
page.components_pages.top_level.each_with_index do |component, index|
component.update(position: index + 1) unless component.position
component.children.each_with_index do |sub_component, sub_index|
sub_component.update(position: sub_index + 1) unless sub_component.position
end
end
end
end

task migrate_static_pages: :environment do
I18n.t('static_pages').each do |static_page|
static = Spree::Page.find_by(slug: static_page[:slug])
title = static&.title || static_page[:title]
body = static&.body || static_page[:body_html]
page = Mcm::Page.where(path: static_page[:slug]).first_or_create!(
name: title,
active: true
)

column = Mcm::Component.find_by(name: 'columns')
parent = page.components_pages.where(name: title).first_or_create!(
component: column
)
parent.update(
metadata: {
section_title: title
}
)

component = Mcm::Component.find_by(name: 'rich_text')
child = parent.children.where(name: title).first_or_create!(
page: page,
component: component
)

child.update(
metadata: {
body: body
}
)

static&.destroy
end
end
end

0 comments on commit 4df21f5

Please sign in to comment.