Created
January 17, 2014 14:22
-
-
Save bourroush/8474101 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :maintenance do | |
desc "Switches the home page for a page with a given slug" | |
task :switch_home_page, [:site_code, :slug] => [:environment] do |t, args| | |
def main(args) | |
@args = args | |
switch_slugs(classic_page, original_home_page) | |
assign_new_home_page(classic_page) | |
republish_old_home_page(original_home_page) | |
republish(original_home_page.node.children) | |
end | |
def switch_slugs(page1, page2) | |
localization1 = page1.localizations.first | |
localization2 = page2.localizations.first | |
slug1 = localization1.slug | |
slug2 = localization2.slug | |
localization1.update_attribute(:slug, slug2) | |
localization2.update_attribute(:slug, slug1) | |
end | |
def assign_new_home_page(new_home_page) | |
site.home_page = new_home_page | |
site.save! | |
new_home_page.node.update_attributes!(parent_id: nil) | |
new_home_page.update_attribute(:parent_ids, []) | |
end | |
def republish_old_home_page(old_home_page) | |
service = Tarkett::ContentService.new(site: site, content: old_home_page) | |
service.republish(old_home_page.node_id, parent_id: classic_page.node_id) | |
end | |
def republish(nodes) | |
nodes.each do |child_node| | |
child_content = child_node.content | |
service = Tarkett::ContentService.new(site: site, content: child_content) | |
service.republish(child_node.id, parent_id: site.root_node_id) | |
end | |
end | |
def classic_page | |
@classic_page ||= Tarkett::Content.find_by_slug(@args[:slug]) | |
end | |
def original_home_page | |
@original_home_page ||= Tarkett::Content.find_by_slug('') | |
end | |
def site | |
@site ||= Tarkett::Site.find_by_code(@args[:site_code]) | |
end | |
main(args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment