acts_as_translatable 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ require "acts_as_translatable/active_record"
@@ -0,0 +1,50 @@
1
+ class ActiveRecord::Base
2
+ def self.acts_as_translatable_on(*fields)
3
+ # after save hook
4
+ self.after_save :save_translations
5
+
6
+ # get the fields
7
+ @translatable_fields = fields
8
+
9
+ # loop through fields
10
+ fields.each do |field|
11
+ eval "def #{field}
12
+ translations[I18n.locale] ||= {}
13
+ translations[I18n.locale][\"#{field}\"]
14
+ end
15
+
16
+ def #{field}=(value)
17
+ translations[I18n.locale] ||= {}
18
+ translations[I18n.locale][\"#{field}\"] = value
19
+ end
20
+
21
+ def translations
22
+ unless @translations
23
+ @translations = {}
24
+ record_translations.each do |translation|
25
+ @translations[translation.locale.to_sym] ||= {}
26
+ @translations[translation.locale.to_sym][translation.translatable_field] = translation.content
27
+ end
28
+ end
29
+ @translations
30
+ end
31
+
32
+ def record_translations
33
+ @record_translations ||= RecordTranslation.where(:translatable_id => id, :translatable_type => self.class.name)
34
+ end"
35
+ end
36
+ end
37
+
38
+ def save_translations
39
+ # delete all previous record translations
40
+ record_translations.destroy_all
41
+
42
+ # loop through updated translations
43
+ translations.each_pair do |locale, fields|
44
+ fields.each_pair do |field, content|
45
+ # create translation record
46
+ RecordTranslation.create :translatable_id => id, :translatable_type => self.class.name, :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,44 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ class ActsAsTranslatableGenerator < Rails::Generators::NamedBase
5
+ include Rails::Generators::Migration
6
+ extend ActiveRecord::Generators::Migration
7
+
8
+ argument :model_name
9
+ argument :columns, :type => :array
10
+
11
+ source_root File.expand_path('../templates', __FILE__)
12
+
13
+ def create_model
14
+ template "model.rb", "app/models/record_translation.rb"
15
+ end
16
+
17
+ def create_migrations
18
+ migration_template "migration.rb", "db/migrate/#{migration_name}.rb"
19
+ end
20
+
21
+ def translatable_class
22
+ model_name.classify
23
+ end
24
+
25
+ def translatable_table
26
+ model_name.tableize.to_sym
27
+ end
28
+
29
+ def translatable_type
30
+ model_name.camelize
31
+ end
32
+
33
+ def migration_name
34
+ "translate_#{model_name.tableize}"
35
+ end
36
+
37
+ def migration_class_name
38
+ migration_name.camelize
39
+ end
40
+
41
+ def locale
42
+ file_name
43
+ end
44
+ end
@@ -0,0 +1,60 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ # create translations table if it doesn't exist
4
+ unless table_exists?(:record_translations)
5
+ create_table :record_translations do |t|
6
+ t.integer :translatable_id, :required => true
7
+ t.string :translatable_type, :required => true
8
+ t.string :translatable_field, :required => true
9
+ t.string :locale, :length => 5, :required => true
10
+ t.text :content
11
+ end
12
+
13
+ # add index
14
+ add_index :record_translations, [:translatable_id, :translatable_type, :translatable_field, :locale], :name => "record_translations_index", :unique => true
15
+ end
16
+
17
+ # loop through columns and insert into record translations table
18
+ <%= translatable_class %>.all.each do |record|<% columns.each do |column| %>
19
+ if content = record.attributes["<%= column %>"]
20
+ RecordTranslation.create :translatable_id => record.id, :translatable_type => "<%= translatable_type %>", :translatable_field => "<%= column %>", :locale => "<%= locale %>", :content => content
21
+ end
22
+ <% end %>end
23
+
24
+ # delete translated columns<% columns.each do |column| %>
25
+ remove_column :<%= translatable_table %>, :<%= column %>
26
+ <% end %>
27
+
28
+ # insert acts_as_translatable_on in model
29
+ model_file_path = "app/models/<%= model_name %>.rb"
30
+ old_contents = File.open(model_file_path).read
31
+ new_contents = old_contents.gsub("class <%= translatable_class %> < ActiveRecord::Base\n", "class <%= translatable_class %> < ActiveRecord::Base\n acts_as_translatable_on <%= columns.map { |c| ":#{c}" }.join(", ") %>\n\n")
32
+ File.open(model_file_path, 'w') { |f| f.write new_contents }
33
+ end
34
+
35
+ def self.down
36
+ # remove acts_as_translatable_on from model
37
+ model_file_path = "app/models/<%= model_name %>.rb"
38
+ old_contents = File.open(model_file_path).read
39
+ new_contents = old_contents.gsub(" acts_as_translatable_on <%= columns.map { |c| ":#{c}" }.join(", ") %>\n", "")
40
+ File.open(model_file_path, 'w') { |f| f.write new_contents }
41
+
42
+ # re-add deleted columns
43
+ # TODO: make sure that re-added columns are the same type as the original, if possible<% columns.each do |column| %>
44
+ add_column :<%= translatable_table %>, :<%= column %>, :text
45
+ <% end %>
46
+
47
+ # insert values back into original table
48
+ <%= translatable_class %>.all.each do |record|<% columns.each do |column| %>
49
+ # get content
50
+ if translation_record = RecordTranslation.where(["translatable_id = ? AND translatable_type = ? AND translatable_field = ? AND locale = ?", record.id, "<%= translatable_type %>", "<%= column %>", "<%= locale %>"]).first
51
+ content = translation_record.content
52
+ # update original record
53
+ record.update_attribute :<%= column %>, content
54
+ end
55
+ <% end %>end
56
+
57
+ # drop translations table
58
+ drop_table :record_translations
59
+ end
60
+ end
@@ -0,0 +1,2 @@
1
+ class RecordTranslation < ActiveRecord::Base
2
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_translatable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Lasse Bunk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-13 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Ruby on Rails plugin for easy translation of database fields.
22
+ email: lassebunk@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/acts_as_translatable/active_record.rb
31
+ - lib/acts_as_translatable.rb
32
+ - lib/generators/acts_as_translatable/acts_as_translatable_generator.rb
33
+ - lib/generators/acts_as_translatable/templates/migration.rb
34
+ - lib/generators/acts_as_translatable/templates/model.rb
35
+ homepage: http://github.com/lassebunk/acts_as_translatable
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.21
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: acts_as_translatable is a Ruby on Rails plugin for easy translation of database fields.
68
+ test_files: []
69
+