translatable_records 1.0.7 → 1.1.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.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.rdoc +8 -8
- data/lib/generators/templates/{migration.rb.erb → migration.rb} +0 -0
- data/lib/generators/templates/model.rb +8 -0
- data/lib/generators/translation_generator.rb +4 -4
- data/lib/translatable_records/active_record/base.rb +11 -12
- data/lib/translatable_records/active_record/translatable.rb +3 -3
- data/lib/translatable_records/version.rb +1 -1
- data/test/dummy/app/models/model.rb +1 -0
- data/test/dummy/app/models/model_translation.rb +5 -6
- data/test/dummy/config/application.rb +1 -2
- data/test/dummy/config/initializers/secret_token.rb +1 -0
- data/test/dummy/db/migrate/20130819155126_create_models.rb +0 -1
- data/test/dummy/db/schema.rb +6 -7
- data/test/dummy/log/test.log +2144 -0
- data/test/records_test.rb +4 -2
- metadata +18 -12
- data/lib/generators/templates/model.rb.erb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 853fe418c0b123941ce33d64f67872df5045cc6b
|
4
|
+
data.tar.gz: a2cc5c10f08c42a2eb29eebaafbabd035fad64eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 166139b74c3c6bde0cfb6a34efc762c230914338b303ce5252a43ffa46d87f3ccd1dbe2112785295da21aa3c7fe38e4eba1d27343a5924650e380b6e3ab45238
|
7
|
+
data.tar.gz: ed33fe2d31a05cff0ec299e2ad1d11fcd052a7e92036ec381da566053f8287c1141ae0294a106525cc12ddfb52fa54192799f29aa416eef7e74d4446dfb86b9d
|
data/MIT-LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -17,26 +17,26 @@ Then bundle:
|
|
17
17
|
First define the model and set the name of the fields you want to translate with the attr_translatable method:
|
18
18
|
attr_translatable :attr
|
19
19
|
|
20
|
+
(If you are using rails 3 you should add attr_accessible :attr to pass the atrr to create, update, etc)
|
21
|
+
|
20
22
|
Generate the translation model and migration for that model:
|
21
23
|
rails g translation Model
|
22
|
-
|
24
|
+
|
23
25
|
Update the translation migration with the column for that field and remove the column from the original table:
|
24
26
|
t.string :attr
|
25
|
-
|
27
|
+
|
26
28
|
Update your db:
|
27
29
|
rake db:migrate
|
28
|
-
|
29
|
-
(Now you can use that field of the model as usual and any change would be saved using the current I18n.locale)
|
30
|
-
|
30
|
+
|
31
31
|
If you want to change the locale to something different than I18n.locale:
|
32
|
-
|
32
|
+
record.with_locale :es
|
33
33
|
|
34
34
|
If you want to build a translation for each available locale:
|
35
|
-
|
35
|
+
record.build_translations
|
36
36
|
|
37
37
|
If you want to save multiple translations:
|
38
38
|
<%= f.fields_for 'translations_attributes[]', t do |ff| %>
|
39
|
-
<%= field_set_tag t("
|
39
|
+
<%= field_set_tag t("locales.#{t.locale}") do %>
|
40
40
|
<% unless t.new_record? %>
|
41
41
|
<%= ff.hidden_field :id %>
|
42
42
|
<% end %>
|
File without changes
|
@@ -5,13 +5,13 @@ class TranslationGenerator < Rails::Generators::NamedBase
|
|
5
5
|
|
6
6
|
source_root File.expand_path('../templates', __FILE__)
|
7
7
|
|
8
|
-
def
|
8
|
+
def create_translation_model
|
9
9
|
@model = Object.const_get(class_name)
|
10
|
-
template 'model.rb
|
10
|
+
template 'model.rb', "app/models/#{file_name}_translation.rb"
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
migration_template 'migration.rb
|
13
|
+
def create_translation_migration
|
14
|
+
migration_template 'migration.rb', "db/migrate/create_#{singular_table_name}_translations.rb"
|
15
15
|
end
|
16
16
|
|
17
17
|
def self.next_migration_number(path)
|
@@ -8,18 +8,18 @@ module TranslatableRecords
|
|
8
8
|
def translatable?
|
9
9
|
translatable_attrs.any?
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def translatable_attrs
|
13
13
|
@translatable_attrs ||= []
|
14
14
|
end
|
15
15
|
|
16
16
|
def attr_translatable(*args)
|
17
17
|
make_translatable unless translatable?
|
18
|
-
args.each do |name|
|
18
|
+
args.each do |name|
|
19
19
|
define_translatable_attribute_methods name
|
20
20
|
translatable_attrs << name
|
21
21
|
end
|
22
|
-
end
|
22
|
+
end
|
23
23
|
|
24
24
|
protected
|
25
25
|
|
@@ -27,29 +27,28 @@ module TranslatableRecords
|
|
27
27
|
include TranslatableRecords::ActiveRecord::Translatable
|
28
28
|
default_scope -> { includes(:translations) }
|
29
29
|
attr_accessible :translations_attributes if Rails::VERSION::MAJOR < 4
|
30
|
-
has_many :translations, class_name: "#{name}Translation", autosave: true, dependent: :destroy
|
31
|
-
accepts_nested_attributes_for :translations
|
30
|
+
has_many :translations, class_name: "#{name}Translation", autosave: true, dependent: :destroy
|
31
|
+
accepts_nested_attributes_for :translations
|
32
32
|
end
|
33
33
|
|
34
34
|
def define_translatable_attribute_methods(attr)
|
35
|
-
|
35
|
+
%w(setter getter).each { |method| send "define_translatable_attribute_#{method}", attr }
|
36
36
|
end
|
37
37
|
|
38
38
|
def define_translatable_attribute_setter(attr)
|
39
39
|
name = :"#{attr}="
|
40
|
-
define_method name do |value|
|
40
|
+
define_method name do |value|
|
41
41
|
t = translation_by_locale(current_locale)
|
42
42
|
t ? t.send(name, value) : translations.build(locale: current_locale.to_s, attr.to_sym => value)
|
43
|
-
end
|
43
|
+
end
|
44
44
|
end
|
45
45
|
|
46
46
|
def define_translatable_attribute_getter(attr)
|
47
|
-
|
47
|
+
%w(_was _changed?).unshift('').each do |suffix|
|
48
48
|
name = :"#{attr}#{suffix}"
|
49
49
|
define_method name do
|
50
|
-
|
51
|
-
|
52
|
-
end
|
50
|
+
translation_by_locale(current_locale).try(name)
|
51
|
+
end
|
53
52
|
end
|
54
53
|
end
|
55
54
|
|
@@ -4,7 +4,7 @@ module TranslatableRecords
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
def with_locale(locale)
|
7
|
-
@
|
7
|
+
@current_locale = locale.to_sym
|
8
8
|
end
|
9
9
|
|
10
10
|
def build_translations
|
@@ -14,11 +14,11 @@ module TranslatableRecords
|
|
14
14
|
protected
|
15
15
|
|
16
16
|
def current_locale
|
17
|
-
@
|
17
|
+
@current_locale || I18n.locale
|
18
18
|
end
|
19
19
|
|
20
20
|
def translation_by_locale(locale)
|
21
|
-
translations.find { |t| t.locale == locale.to_s } || translations.find_by_locale(locale)
|
21
|
+
translations.to_a.find { |t| t.locale == locale.to_s } || translations.find_by_locale(locale)
|
22
22
|
end
|
23
23
|
|
24
24
|
end
|
@@ -1,9 +1,8 @@
|
|
1
1
|
class ModelTranslation < ActiveRecord::Base
|
2
|
-
|
2
|
+
|
3
3
|
belongs_to :models
|
4
|
-
|
5
|
-
validates :locale, presence: true
|
6
|
-
validates :
|
7
|
-
|
8
|
-
|
4
|
+
|
5
|
+
validates :locale, presence: true
|
6
|
+
validates :model_id, uniqueness: { scope: :locale }
|
7
|
+
|
9
8
|
end
|
@@ -3,7 +3,7 @@ require File.expand_path('../boot', __FILE__)
|
|
3
3
|
require 'rails/all'
|
4
4
|
|
5
5
|
Bundler.require(*Rails.groups)
|
6
|
-
require
|
6
|
+
require 'translatable_records'
|
7
7
|
|
8
8
|
module Dummy
|
9
9
|
class Application < Rails::Application
|
@@ -18,6 +18,5 @@ module Dummy
|
|
18
18
|
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
19
19
|
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
20
20
|
# config.i18n.default_locale = :de
|
21
|
-
I18n.enforce_available_locales = false
|
22
21
|
end
|
23
22
|
end
|
@@ -9,4 +9,5 @@
|
|
9
9
|
|
10
10
|
# Make sure your secret_key_base is kept private
|
11
11
|
# if you're sharing your code publicly.
|
12
|
+
Dummy::Application.config.secret_token = '7df7e2131e8049bf1397fd7586bd3dfa812f69914f258ee99734a62b8cc16896147a9276e35b6349629341cf1a9b62025e277ea721be144377dbc3979e90a629'
|
12
13
|
Dummy::Application.config.secret_key_base = '7df7e2131e8049bf1397fd7586bd3dfa812f69914f258ee99734a62b8cc16896147a9276e35b6349629341cf1a9b62025e277ea721be144377dbc3979e90a629'
|
data/test/dummy/db/schema.rb
CHANGED
@@ -9,11 +9,11 @@
|
|
9
9
|
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
10
|
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
11
|
#
|
12
|
-
# It's strongly recommended
|
12
|
+
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version
|
14
|
+
ActiveRecord::Schema.define(:version => 20130819165249) do
|
15
15
|
|
16
|
-
create_table "model_translations", force
|
16
|
+
create_table "model_translations", :force => true do |t|
|
17
17
|
t.integer "model_id"
|
18
18
|
t.string "locale"
|
19
19
|
t.string "name"
|
@@ -21,11 +21,10 @@ ActiveRecord::Schema.define(version: 20130819165249) do
|
|
21
21
|
t.datetime "updated_at"
|
22
22
|
end
|
23
23
|
|
24
|
-
add_index "model_translations", ["locale"], name
|
25
|
-
add_index "model_translations", ["model_id"], name
|
24
|
+
add_index "model_translations", ["locale"], :name => "index_model_translations_on_locale"
|
25
|
+
add_index "model_translations", ["model_id"], :name => "index_model_translations_on_model_id"
|
26
26
|
|
27
|
-
create_table "models", force
|
28
|
-
t.string "name"
|
27
|
+
create_table "models", :force => true do |t|
|
29
28
|
t.datetime "created_at"
|
30
29
|
t.datetime "updated_at"
|
31
30
|
end
|