cirandas.net

ref: master

script/apply-template


#!/usr/bin/env ruby

require_relative '../config/environment'
include GetText
ActionController::Base.init_gettext 'noosfero'
set_locale_all 'pt_BR'

env = Environment.default

def move_articles_to_blog(profile)
  profile.articles.each { |article|
    if !article.blog? && !article.is_a?(RssFeed) && article.published?
      puts 'including ' + article.path + ' in the blog'
      article.parent = profile.blog
      article.save!
    end
  }
end

def report_doing(n, text)
  puts "#{n} - #{text} ..."
end

def report_done
  puts "[done]"
end

case $ARGV[0]
when 'inactive-enterprise'
  offset = 0
  excluded = [env.inactive_enterprise_template, env.enterprise_template]
  template = excluded.first

  while enterprise = Enterprise.where(enabled: false).order(:id).offset(offset)
    # do nothing with templates
    next if excluded.include?(enterprise)

    # do the thing
    enterprise.apply_template(template)
    puts "#{offset} - #{enterprise.identifier}"

    # bring it on ...
    offset = offset + 1
  end
when 'active-enterprise'
  active_enterprises = Enterprise.where(enabled: true).all - [env.enterprise_template, env.enterprise_template]
  active_enterprises.each do |enterprise|
    old_home = enterprise.home_page
    enterprise.apply_template(env.enterprise_template)
    enterprise.home_page.update!(:body => old_home.body)
    enterprise.save!
  end
when 'community'
  excluded = ['espaco', 'anarquismo']
  template = env.community_template
  offset = 0
  while community = Community.order(:id).offset(offset)
    if community != template && !excluded.include?(community.identifier)
      report_doing offset, community.name
      community.apply_template(template)
      move_articles_to_blog(community)
      report_done
    end
    offset = offset + 1
  end
when 'person'
  template = env.person_template
  offset = 0
  while person = Person.order(:id).offset(offset).first
    if person != template
      report_doing offset, person.identifier
      person.apply_template(template)
      # move_articles_to_blog(person) # does not make sense in most cases
      report_done
    end
    offset = offset + 1
  end
end