Skip to content

Commit

Permalink
Merge pull request globalize#126 from seeweer/3_add_optios_for_column…
Browse files Browse the repository at this point in the history
…s_to_create_translation_table

globalize#3 - adds posibility to pass extra options for specific columns for create_translation_table! method
  • Loading branch information
parndt committed Mar 26, 2012
2 parents efe0d61 + 555f345 commit e4f3328
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ class CreatePosts < ActiveRecord::Migration
end
</code></pre>

Also, you can pass options for specific columns. Here’s an example:

<pre><code>
class CreatePosts < ActiveRecord::Migration
def up
create_table :posts do |t|
t.timestamps
end
Post.create_translation_table! :title => :string, :text => {:type => :text, :null => false, :default => 'abc'}
end
def down
drop_table :posts
Post.drop_translation_table!
end
end
</code></pre>

Note that the ActiveRecord model @Post@ must already exist and have a @translates@ directive listing the translated fields.

h2. Migrating existing data to and from the translated version
Expand Down
16 changes: 13 additions & 3 deletions lib/globalize/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ def create_translation_table
connection.create_table(translations_table_name) do |t|
t.references table_name.sub(/^#{table_name_prefix}/, '').singularize
t.string :locale
fields.each { |name, type| t.column name, type }
fields.each do |name, options|
if options.is_a? Hash
t.column name, options.delete(:type), options
else
t.column name, options
end
end
t.timestamps
end
end
Expand Down Expand Up @@ -112,9 +118,13 @@ def move_data_to_model_table
end

def validate_translated_fields
fields.each do |name, type|
fields.each do |name, options|
raise BadFieldName.new(name) unless valid_field_name?(name)
raise BadFieldType.new(name, type) unless valid_field_type?(name, type)
if options.is_a? Hash
raise BadFieldType.new(name, options[:type]) unless valid_field_type?(name, options[:type])
else
raise BadFieldType.new(name, options) unless valid_field_type?(name, options)
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions test/globalize3/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def teardown
end
end

test "create_translation_table!(:name => {:type => :test, :default => '123'}) adds the translations table with options" do
Migrated.create_translation_table!(:name => {:type => :text, :default => '123'})
assert_migration_table(:name => :text)
assert_equal column_default(:name), '123'
end

test 'passing a non-translated field name raises BadFieldName' do
assert_raise BadFieldName do
Migrated.create_translation_table!(:content => :text)
Expand Down Expand Up @@ -143,6 +149,10 @@ def column_type(name)
Migrated.translation_class.columns.detect { |c| c.name == name.to_s }.try(:type)
end

def column_default(name)
Migrated.translation_class.columns.detect { |c| c.name == name.to_s }.try(:default)
end

def assert_migration_table(fields)
assert Migrated.translation_class.table_exists?
assert Migrated.translation_class.index_exists_on?(:migrated_id)
Expand Down

0 comments on commit e4f3328

Please sign in to comment.