rails_i18n_record 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +0 -1
- data/lib/rails_i18n_record/active_record/base.rb +79 -0
- data/lib/rails_i18n_record/railtie.rb +1 -1
- data/lib/rails_i18n_record/version.rb +1 -1
- data/lib/rails_i18n_record.rb +1 -1
- data/test/dummy/log/test.log +101 -0
- data/test/generators_test.rb +1 -1
- data/test/rails_i18n_record_test.rb +1 -1
- data/test/records_test.rb +3 -3
- metadata +5 -5
- data/lib/rails_i18n_record/base.rb +0 -77
data/README.rdoc
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
module RailsI18nRecord
|
2
|
+
module ActiveRecord
|
3
|
+
module Base
|
4
|
+
module NonTranslatable
|
5
|
+
|
6
|
+
def translatable?
|
7
|
+
not defined?(@translatable_attrs).nil?
|
8
|
+
end
|
9
|
+
|
10
|
+
def attr_translatable(*args)
|
11
|
+
make_translatable unless translatable?
|
12
|
+
args.each do |name|
|
13
|
+
define_translatable_attribute_methods(name)
|
14
|
+
@translatable_attrs << name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def make_translatable
|
21
|
+
send :include, RailsI18nRecord::ActiveRecord::Base::Translatable
|
22
|
+
default_scope :include => :translations
|
23
|
+
attr_accessible :translations_attributes
|
24
|
+
has_many :translations, :class_name => "#{name}Translation", :autosave => true, :dependent => :destroy
|
25
|
+
accepts_nested_attributes_for :translations
|
26
|
+
@translatable_attrs = []
|
27
|
+
end
|
28
|
+
|
29
|
+
def define_translatable_attribute_methods(attr)
|
30
|
+
['set', 'get'].each do |method|
|
31
|
+
send "define_translatable_attribute_method_#{method}", attr
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def define_translatable_attribute_method_set(attr)
|
36
|
+
define_method "#{attr}=" do |value|
|
37
|
+
t = translation_by_locale(current_locale)
|
38
|
+
t ? t.send("#{attr}=".to_sym, value) : translations.build(:locale => current_locale.to_s, attr.to_sym => value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def define_translatable_attribute_method_get(attr)
|
43
|
+
['', '_was', '_changed?'].each do |suffix|
|
44
|
+
define_method "#{attr}#{suffix}" do
|
45
|
+
t = translation_by_locale(current_locale)
|
46
|
+
t ? t.send("#{attr}#{suffix}".to_sym) : nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
module Translatable
|
53
|
+
|
54
|
+
def with_locale(locale)
|
55
|
+
@locale = locale
|
56
|
+
end
|
57
|
+
|
58
|
+
def build_translations
|
59
|
+
I18n.available_locales.each do |locale|
|
60
|
+
t = translations.find_by_locale(locale)
|
61
|
+
translations.build :locale => locale.to_s unless t
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
|
67
|
+
def current_locale
|
68
|
+
defined?(@locale).nil? ? I18n.locale.to_s : @locale
|
69
|
+
end
|
70
|
+
|
71
|
+
def translation_by_locale(locale)
|
72
|
+
t = translations.find { |t| t.locale == locale.to_s }
|
73
|
+
t ? t : translations.find_by_locale(locale)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -2,7 +2,7 @@ module RailsI18nRecord
|
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
|
4
4
|
initializer 'rails_i18n_record' do
|
5
|
-
::ActiveRecord::Base.send :extend, RailsI18nRecord::Base::NonTranslatable
|
5
|
+
::ActiveRecord::Base.send :extend, RailsI18nRecord::ActiveRecord::Base::NonTranslatable
|
6
6
|
end
|
7
7
|
|
8
8
|
end
|
data/lib/rails_i18n_record.rb
CHANGED
data/test/dummy/log/test.log
CHANGED
@@ -2294,3 +2294,104 @@ Connecting to database specified by database.yml
|
|
2294
2294
|
[1m[36m (0.1ms)[0m [1mUPDATE "models_i18n" SET "name" = 'new name', "updated_at" = '2013-04-05 12:40:36.749030' WHERE "models_i18n"."id" = 1[0m
|
2295
2295
|
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
2296
2296
|
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
2297
|
+
Connecting to database specified by database.yml
|
2298
|
+
Connecting to database specified by database.yml
|
2299
|
+
[1m[36m (3.2ms)[0m [1mselect sqlite_version(*)[0m
|
2300
|
+
[1m[35m (0.3ms)[0m CREATE TABLE "models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
2301
|
+
[1m[36m (0.1ms)[0m [1mCREATE TABLE "models_i18n" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "model_id" integer NOT NULL, "locale" varchar(255) NOT NULL, "name" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) [0m
|
2302
|
+
[1m[35m (0.1ms)[0m CREATE INDEX "index_models_i18n_on_created_at" ON "models_i18n" ("created_at")
|
2303
|
+
[1m[36m (0.1ms)[0m [1mCREATE INDEX "index_models_i18n_on_locale" ON "models_i18n" ("locale")[0m
|
2304
|
+
[1m[35m (0.1ms)[0m CREATE INDEX "index_models_i18n_on_model_id" ON "models_i18n" ("model_id")
|
2305
|
+
[1m[36m (0.1ms)[0m [1mCREATE INDEX "index_models_i18n_on_name" ON "models_i18n" ("name")[0m
|
2306
|
+
[1m[35m (0.1ms)[0m CREATE INDEX "index_models_i18n_on_updated_at" ON "models_i18n" ("updated_at")
|
2307
|
+
[1m[36m (0.1ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
2308
|
+
[1m[35m (0.1ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
2309
|
+
[1m[36m (0.0ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
2310
|
+
[1m[35m (0.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130404161713')
|
2311
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
2312
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
2313
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
2314
|
+
[1m[35m (0.0ms)[0m rollback transaction
|
2315
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
2316
|
+
[1m[35mModelTranslation Load (19.9ms)[0m SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
|
2317
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
2318
|
+
[1m[35mModelTranslation Exists (0.1ms)[0m SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
|
2319
|
+
[1m[36mSQL (48.8ms)[0m [1mINSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?)[0m [["created_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00], ["updated_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00]]
|
2320
|
+
[1m[35mSQL (0.5ms)[0m INSERT INTO "models_i18n" ("created_at", "locale", "model_id", "name", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00], ["locale", "en"], ["model_id", 1], ["name", "name"], ["updated_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00]]
|
2321
|
+
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
2322
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
2323
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
2324
|
+
[1m[35mModelTranslation Load (0.1ms)[0m SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
|
2325
|
+
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
|
2326
|
+
[1m[35mModelTranslation Exists (0.1ms)[0m SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
|
2327
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?)[0m [["created_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00], ["updated_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00]]
|
2328
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "models_i18n" ("created_at", "locale", "model_id", "name", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00], ["locale", "en"], ["model_id", 1], ["name", "name"], ["updated_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00]]
|
2329
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
2330
|
+
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
|
2331
|
+
[1m[36mSQL (0.1ms)[0m [1mDELETE FROM "models_i18n" WHERE "models_i18n"."id" = ?[0m [["id", 1]]
|
2332
|
+
[1m[35mSQL (0.1ms)[0m DELETE FROM "models" WHERE "models"."id" = ? [["id", 1]]
|
2333
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
2334
|
+
[1m[35mModelTranslation Load (0.2ms)[0m SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" = 1 LIMIT 1
|
2335
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
2336
|
+
[1m[35m (0.0ms)[0m begin transaction
|
2337
|
+
[1m[36mModelTranslation Load (0.2ms)[0m [1mSELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1[0m
|
2338
|
+
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
|
2339
|
+
[1m[36mModelTranslation Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1[0m
|
2340
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?) [["created_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00], ["updated_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00]]
|
2341
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "models_i18n" ("created_at", "locale", "model_id", "name", "updated_at") VALUES (?, ?, ?, ?, ?)[0m [["created_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00], ["locale", "en"], ["model_id", 1], ["name", "name"], ["updated_at", Sat, 06 Apr 2013 18:10:53 UTC +00:00]]
|
2342
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
2343
|
+
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
|
2344
|
+
[1m[35mModelTranslation Exists (0.1ms)[0m SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" = 1 AND "models_i18n"."id" != 1 AND "models_i18n"."locale" = 'en') LIMIT 1
|
2345
|
+
[1m[36m (0.1ms)[0m [1mUPDATE "models_i18n" SET "name" = 'new name', "updated_at" = '2013-04-06 18:10:53.988752' WHERE "models_i18n"."id" = 1[0m
|
2346
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
2347
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
2348
|
+
Connecting to database specified by database.yml
|
2349
|
+
[1m[36m (1.8ms)[0m [1mselect sqlite_version(*)[0m
|
2350
|
+
[1m[35m (0.3ms)[0m CREATE TABLE "models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
2351
|
+
[1m[36m (0.1ms)[0m [1mCREATE TABLE "models_i18n" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "model_id" integer NOT NULL, "locale" varchar(255) NOT NULL, "name" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) [0m
|
2352
|
+
[1m[35m (0.1ms)[0m CREATE INDEX "index_models_i18n_on_created_at" ON "models_i18n" ("created_at")
|
2353
|
+
[1m[36m (0.1ms)[0m [1mCREATE INDEX "index_models_i18n_on_locale" ON "models_i18n" ("locale")[0m
|
2354
|
+
[1m[35m (0.1ms)[0m CREATE INDEX "index_models_i18n_on_model_id" ON "models_i18n" ("model_id")
|
2355
|
+
[1m[36m (0.1ms)[0m [1mCREATE INDEX "index_models_i18n_on_name" ON "models_i18n" ("name")[0m
|
2356
|
+
[1m[35m (0.1ms)[0m CREATE INDEX "index_models_i18n_on_updated_at" ON "models_i18n" ("updated_at")
|
2357
|
+
[1m[36m (0.1ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
2358
|
+
[1m[35m (0.1ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
2359
|
+
[1m[36m (0.0ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
2360
|
+
[1m[35m (0.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130404161713')
|
2361
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
2362
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
2363
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
2364
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
2365
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
2366
|
+
[1m[35mModelTranslation Load (1.6ms)[0m SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
|
2367
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
2368
|
+
[1m[35mModelTranslation Exists (0.1ms)[0m SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
|
2369
|
+
[1m[36mSQL (5.9ms)[0m [1mINSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?)[0m [["created_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00], ["updated_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00]]
|
2370
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "models_i18n" ("created_at", "locale", "model_id", "name", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00], ["locale", "en"], ["model_id", 1], ["name", "name"], ["updated_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00]]
|
2371
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
2372
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
2373
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
2374
|
+
[1m[35mModelTranslation Load (0.1ms)[0m SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
|
2375
|
+
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
|
2376
|
+
[1m[35mModelTranslation Exists (0.1ms)[0m SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
|
2377
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?)[0m [["created_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00], ["updated_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00]]
|
2378
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "models_i18n" ("created_at", "locale", "model_id", "name", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00], ["locale", "en"], ["model_id", 1], ["name", "name"], ["updated_at", Sat, 06 Apr 2013 18:30:29 UTC +00:00]]
|
2379
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
2380
|
+
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
|
2381
|
+
[1m[36mSQL (0.1ms)[0m [1mDELETE FROM "models_i18n" WHERE "models_i18n"."id" = ?[0m [["id", 1]]
|
2382
|
+
[1m[35mSQL (0.1ms)[0m DELETE FROM "models" WHERE "models"."id" = ? [["id", 1]]
|
2383
|
+
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
2384
|
+
[1m[35mModelTranslation Load (0.1ms)[0m SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" = 1 LIMIT 1
|
2385
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
2386
|
+
[1m[35m (0.0ms)[0m begin transaction
|
2387
|
+
[1m[36mModelTranslation Load (0.1ms)[0m [1mSELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1[0m
|
2388
|
+
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
|
2389
|
+
[1m[36mModelTranslation Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1[0m
|
2390
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?) [["created_at", Sat, 06 Apr 2013 18:30:30 UTC +00:00], ["updated_at", Sat, 06 Apr 2013 18:30:30 UTC +00:00]]
|
2391
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "models_i18n" ("created_at", "locale", "model_id", "name", "updated_at") VALUES (?, ?, ?, ?, ?)[0m [["created_at", Sat, 06 Apr 2013 18:30:30 UTC +00:00], ["locale", "en"], ["model_id", 1], ["name", "name"], ["updated_at", Sat, 06 Apr 2013 18:30:30 UTC +00:00]]
|
2392
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
2393
|
+
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
|
2394
|
+
[1m[35mModelTranslation Exists (0.1ms)[0m SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" = 1 AND "models_i18n"."id" != 1 AND "models_i18n"."locale" = 'en') LIMIT 1
|
2395
|
+
[1m[36m (0.1ms)[0m [1mUPDATE "models_i18n" SET "name" = 'new name', "updated_at" = '2013-04-06 18:30:30.012902' WHERE "models_i18n"."id" = 1[0m
|
2396
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
2397
|
+
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
|
data/test/generators_test.rb
CHANGED
@@ -9,7 +9,7 @@ class I18nGeneratorTest < Rails::Generators::TestCase
|
|
9
9
|
tests I18nGenerator
|
10
10
|
destination File.expand_path('../tmp', File.dirname(__FILE__))
|
11
11
|
|
12
|
-
test
|
12
|
+
test "should exists" do
|
13
13
|
run_generator %w(something)
|
14
14
|
assert_file 'app/models/something_translation.rb'
|
15
15
|
assert_migration 'db/migrate/create_somethings_i18n.rb'
|
data/test/records_test.rb
CHANGED
@@ -4,16 +4,16 @@ class RecrodsTest < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
setup :create_record
|
6
6
|
|
7
|
-
test
|
7
|
+
test "should create associated translation" do
|
8
8
|
assert_equal @record.name, 'name'
|
9
9
|
end
|
10
10
|
|
11
|
-
test
|
11
|
+
test "should edit associated translation" do
|
12
12
|
@record.update_attributes :name => 'new name'
|
13
13
|
assert_equal @record.name, 'new name'
|
14
14
|
end
|
15
15
|
|
16
|
-
test
|
16
|
+
test "should delete associated translation" do
|
17
17
|
@record.destroy
|
18
18
|
assert_equal ModelTranslation.find_by_model_id(@record.id), nil
|
19
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_i18n_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,14 +9,14 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 3.2.8
|
22
22
|
type: :runtime
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 3.2.8
|
30
30
|
- !ruby/object:Gem::Dependency
|
@@ -54,7 +54,7 @@ files:
|
|
54
54
|
- lib/generators/i18n_generator.rb
|
55
55
|
- lib/generators/templates/migration.erb
|
56
56
|
- lib/generators/templates/model.erb
|
57
|
-
- lib/rails_i18n_record/base.rb
|
57
|
+
- lib/rails_i18n_record/active_record/base.rb
|
58
58
|
- lib/rails_i18n_record/railtie.rb
|
59
59
|
- lib/rails_i18n_record/version.rb
|
60
60
|
- lib/rails_i18n_record.rb
|
@@ -1,77 +0,0 @@
|
|
1
|
-
module RailsI18nRecord
|
2
|
-
module Base
|
3
|
-
module NonTranslatable
|
4
|
-
|
5
|
-
def translatable?
|
6
|
-
not defined?(@translatable_attrs).nil?
|
7
|
-
end
|
8
|
-
|
9
|
-
def attr_translatable(*args)
|
10
|
-
make_translatable unless translatable?
|
11
|
-
args.each do |name|
|
12
|
-
define_translatable_attribute_methods(name)
|
13
|
-
@translatable_attrs << name
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
def make_translatable
|
20
|
-
send :include, RailsI18nRecord::Base::Translatable
|
21
|
-
default_scope :include => :translations
|
22
|
-
attr_accessible :translations_attributes
|
23
|
-
has_many :translations, :class_name => "#{name}Translation", :autosave => true, :dependent => :destroy
|
24
|
-
accepts_nested_attributes_for :translations
|
25
|
-
@translatable_attrs = []
|
26
|
-
end
|
27
|
-
|
28
|
-
def define_translatable_attribute_methods(attr)
|
29
|
-
['set', 'get'].each do |method|
|
30
|
-
send "define_translatable_attribute_method_#{method}", attr
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def define_translatable_attribute_method_set(attr)
|
35
|
-
define_method "#{attr}=" do |value|
|
36
|
-
t = translation_by_locale(current_locale)
|
37
|
-
t ? t.send("#{attr}=".to_sym, value) : translations.build(:locale => current_locale.to_s, attr.to_sym => value)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def define_translatable_attribute_method_get(attr)
|
42
|
-
['', '_was', '_changed?'].each do |suffix|
|
43
|
-
define_method "#{attr}#{suffix}" do
|
44
|
-
t = translation_by_locale(current_locale)
|
45
|
-
t ? t.send("#{attr}#{suffix}".to_sym) : nil
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
module Translatable
|
52
|
-
|
53
|
-
def with_locale(locale)
|
54
|
-
@locale = locale
|
55
|
-
end
|
56
|
-
|
57
|
-
def build_translations
|
58
|
-
I18n.available_locales.each do |locale|
|
59
|
-
t = translations.find_by_locale(locale)
|
60
|
-
translations.build :locale => locale.to_s unless t
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
protected
|
65
|
-
|
66
|
-
def current_locale
|
67
|
-
defined?(@locale).nil? ? I18n.locale.to_s : @locale
|
68
|
-
end
|
69
|
-
|
70
|
-
def translation_by_locale(locale)
|
71
|
-
t = translations.find { |t| t.locale == locale.to_s }
|
72
|
-
t ? t : translations.find_by_locale(locale)
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|