Skip to content

Commit

Permalink
Change from :custom_title to :menu_title in existing migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ugisozols committed Aug 17, 2011
1 parent 11eace6 commit 04da8fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
26 changes: 13 additions & 13 deletions db/migrate/20110307025652_translate_custom_title_on_pages.rb
@@ -1,40 +1,40 @@
class TranslateCustomTitleOnPages < ActiveRecord::Migration
def self.up
unless ::Refinery::Page.translation_class.column_names.map(&:to_sym).include?(:custom_title)
add_column ::Refinery::Page.translation_class.table_name, :custom_title, :string
unless ::Refinery::Page.translation_class.column_names.map(&:to_sym).include?(:menu_title)
add_column ::Refinery::Page.translation_class.table_name, :menu_title, :string

say_with_time("Re-save custom_title") do
say_with_time("Re-save menu_title") do
::Refinery::Page.all.each do |page|
say "updating custom_title field for page##{page.id}"
page.update_attribute(:custom_title, page.untranslated_attributes['custom_title'])
say "updating menu_title field for page##{page.id}"
page.update_attribute(:menu_title, page.untranslated_attributes['menu_title'])
end
end
else
say "Nothing done, ::Refinery::Page.translation_class table already includes a custom_title field"
say "Nothing done, ::Refinery::Page.translation_class table already includes a menu_title field"
end

::Refinery::Page.translation_class.reset_column_information
end

def self.down
say_with_time("Re-save custom_title") do
say_with_time("Re-save menu_title") do
::Refinery::Page.all.each do |page|
if page.attributes['custom_title'].nil?
say "Nothing done, page##{page.id} custom_title field is nil"
if page.attributes['menu_title'].nil?
say "Nothing done, page##{page.id} menu_title field is nil"
else
say "updating custom_title field for page #{page.id}"
say "updating menu_title field for page #{page.id}"
::Refinery::Page.update_all({
:custom_title => page.attributes['custom_title']
:menu_title => page.attributes['menu_title']
}, {
:id => page.id.to_s
})
end
end
end

remove_column ::Refinery::Page.translation_class.table_name, :custom_title
remove_column ::Refinery::Page.translation_class.table_name, :menu_title

::Refinery::Page.translated_attribute_names.delete(:custom_title)
::Refinery::Page.translated_attribute_names.delete(:menu_title)

::Refinery::Page.translation_class.reset_column_information
end
Expand Down
43 changes: 29 additions & 14 deletions pages/db/migrate/20110307025652_translate_custom_title_on_pages.rb
@@ -1,26 +1,41 @@
class TranslateCustomTitleOnPages < ActiveRecord::Migration
def self.up
unless ::Refinery::Page.translation_class.column_names.map(&:to_sym).include?(:custom_title)
add_column ::Refinery::Page.translation_class.table_name, :custom_title, :string
unless ::Refinery::Page.translation_class.column_names.map(&:to_sym).include?(:menu_title)
add_column ::Refinery::Page.translation_class.table_name, :menu_title, :string

# Re-save custom_title
::Refinery::Page.all.each do |page|
page.update_attribute(:custom_title, page.untranslated_attributes['custom_title'])
say_with_time("Re-save menu_title") do
::Refinery::Page.all.each do |page|
say "updating menu_title field for page##{page.id}"
page.update_attribute(:menu_title, page.untranslated_attributes['menu_title'])
end
end

else
say "Nothing done, ::Refinery::Page.translation_class table already includes a menu_title field"
end

::Refinery::Page.translation_class.reset_column_information
end

def self.down
# Re-save custom_title
::Refinery::Page.all.each do |page|
::Refinery::Page.update_all({
:custom_title => page.attributes['custom_title']
}, {
:id => page.id.to_s
}) unless page.attributes['custom_title'].nil?
say_with_time("Re-save menu_title") do
::Refinery::Page.all.each do |page|
if page.attributes['menu_title'].nil?
say "Nothing done, page##{page.id} menu_title field is nil"
else
say "updating menu_title field for page #{page.id}"
::Refinery::Page.update_all({
:menu_title => page.attributes['menu_title']
}, {
:id => page.id.to_s
})
end
end
end

remove_column ::Refinery::Page.translation_class.table_name, :custom_title
remove_column ::Refinery::Page.translation_class.table_name, :menu_title

::Refinery::Page.translated_attribute_names.delete(:menu_title)

::Refinery::Page.translation_class.reset_column_information
end
end

0 comments on commit 04da8fe

Please sign in to comment.