Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #126 from seeweer/3_add_optios_for_columns_to_crea…

…te_translation_table


#3 - adds posibility to pass extra options for specific columns for create_translation_table! method
  • Loading branch information...
commit e4f33283bb68de68352eb22b7597e8f6cfce27f7 2 parents efe0d61 + 555f345
@parndt parndt authored
View
17 README.textile
@@ -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
View
16 lib/globalize/active_record/migration.rb
@@ -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
@@ -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
View
10 test/globalize3/migration_test.rb
@@ -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)
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.