Skip to content

Commit

Permalink
translated section titles; all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmh committed Jul 2, 2009
1 parent 867708b commit e994930
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions engines/adva_cms/app/models/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Section < ActiveRecord::Base
@@types = ['Page']
cattr_reader :types

translates :title
serialize :permissions

has_option :contents_per_page, :default => 15
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateSectionTranslations < ActiveRecord::Migration
def self.up
Section.create_translation_table! :title => :string
end

def self.down
Section.drop_translation_table
end
end
13 changes: 13 additions & 0 deletions engines/adva_cms/lib/tasks/translation.rake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ namespace :adva do
puts comp_keys - base_keys
end
end

desc "Migrate section titles. Specify e.g., LOCALE=de if app locale isn't en."
task :migrate_sections => :environment do
locale = ENV['locale'] || 'en'
sql = %{
INSERT INTO section_translations (section_id, locale, title, created_at, updated_at)
SELECT sections.id, "#{locale}", sections.title, sections.published_at, sections.published_at
FROM sections
}
ActiveRecord::Base.connection.insert sql, "migrating sections to globalize"
ActiveRecord::Base.connection.remove_column 'sections', 'title'
Rake::Task['db:schema:dump'].invoke
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions engines/adva_cms/test/unit/helpers/base_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def setup
@local_time = Time.zone.local 2008, 10, 9, 14, 0, 0
end

def teardown
super
Time.zone = 'UTC'
end

# DATETIME MICROFORMAT HELPERS

test "#datetime_with_microformat displays the passed object when passed a non-Date/Time object" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def i18n_attr(attribute_name)
end

module InstanceMethods
def reload_with_globalize
def reload_with_globalize(options = nil)
globalize.clear

# clear all globalized attributes
Expand All @@ -118,9 +118,13 @@ def reload_with_globalize
@attributes.delete attr.to_s
end

reload_without_globalize
reload_without_globalize(options)
end

def translated_attributes
self.class.globalize_options[:translated_attributes].inject({}) {|h, tf| h[tf] = send(tf); h }
end

def globalize
@globalize ||= Adapter.new self
end
Expand Down
13 changes: 10 additions & 3 deletions test/with-sugar/lib/with-sugar/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def it_does_not_change(*expressions)

def it_updates(*names)
names.each do |name|
before { record_before_state("@#{name}.attributes") }
before { record_before_state("all_attributes(@#{name})") }
assertion "it updates #{name}" do
previous = @before_states["@#{name}.attributes"]
current = assigns(name).reload.attributes
previous = @before_states["all_attributes(@#{name})"]
current = all_attributes(assigns(name).reload)
assert_not_equal previous, current, "expected #{name} to be updated, but wasn't"
end
end
Expand Down Expand Up @@ -125,6 +125,13 @@ def record_before_state(expression)
@before_states[expression] = instance_eval(expression)
end

# merges in translated attributes, if present
def all_attributes(model)
model.respond_to?(:translated_attributes) ?
model.attributes.merge( model.translated_attributes ) :
model.attributes
end

def assert_state_change(expression, difference, message = nil)
expected = @before_states[expression] + difference
result = instance_eval(expression)
Expand Down

0 comments on commit e994930

Please sign in to comment.