Sha256: 680bb97ebc85dab1533cebd2767f054219fc7e56ca29e003df850278c5f1382a

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

class UpdateUrlAndRedirectUrlValue < ActiveRecord::Migration
  def up
    @redirect_pages = Qbrick::Page.where(:page_type => 'redirect')
    I18n.available_locales.each do |locale|
      move_url_to_redirect_url(locale.to_s.underscore)
    end
  end

  def down
    @redirect_pages = Qbrick::Page.where(:page_type => 'redirect')
    I18n.available_locales.each do |locale|
      move_redirect_url_to_url(locale.to_s.underscore)
    end

  end

  def move_url_to_redirect_url(locale)
    I18n.with_locale(locale) do
      @redirect_pages.each do |redirect_page|
        redirect_url = redirect_page.url
        redirect_page.update_attributes(:url => update_url(redirect_page), :redirect_url => redirect_url) if redirect_page
      end
    end
  end

  def move_redirect_url_to_url(locale)
    I18n.with_locale(locale) do
      @redirect_pages.each do |redirect_page|
        url = redirect_page.redirect_url
        redirect_page.update_attributes(:url => url, :redirect_url => nil, :page_type => nil) if redirect_page
      end
    end
  end

  def update_url(page)
    complete_slug = ''
    if page.parent.present?
      complete_slug << page.parent.url.to_s
    else
      complete_slug = I18n.locale.to_s.underscore
    end
    complete_slug << "/#{page.slug}"
    complete_slug
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
qbrick-2.7.1 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.7 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.10 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.9 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.8 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.7 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.6 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.5 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.4 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.3 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.2 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.1 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.6.0 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.5.2 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.5.1 db/migrate/11_update_url_and_redirect_url_value.rb
qbrick-2.5.0 db/migrate/11_update_url_and_redirect_url_value.rb