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 CHANGED
@@ -42,4 +42,3 @@ If you want to save multiple translations:
42
42
  %li
43
43
  = ff.label :prop
44
44
  = ff.text_field :prop
45
-
@@ -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
@@ -1,5 +1,5 @@
1
1
  module RailsI18nRecord
2
2
 
3
- VERSION = '1.0.4'
3
+ VERSION = '1.0.5'
4
4
 
5
5
  end
@@ -1,4 +1,4 @@
1
- require 'rails_i18n_record/base'
1
+ require 'rails_i18n_record/active_record/base'
2
2
  require 'rails_i18n_record/railtie'
3
3
  require 'rails_i18n_record/version'
4
4
 
@@ -2294,3 +2294,104 @@ Connecting to database specified by database.yml
2294
2294
   (0.1ms) UPDATE "models_i18n" SET "name" = 'new name', "updated_at" = '2013-04-05 12:40:36.749030' WHERE "models_i18n"."id" = 1
2295
2295
   (0.0ms) RELEASE SAVEPOINT active_record_1
2296
2296
   (0.1ms) rollback transaction
2297
+ Connecting to database specified by database.yml
2298
+ Connecting to database specified by database.yml
2299
+  (3.2ms) select sqlite_version(*)
2300
+  (0.3ms) CREATE TABLE "models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
2301
+  (0.1ms) CREATE 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) 
2302
+  (0.1ms) CREATE INDEX "index_models_i18n_on_created_at" ON "models_i18n" ("created_at")
2303
+  (0.1ms) CREATE INDEX "index_models_i18n_on_locale" ON "models_i18n" ("locale")
2304
+  (0.1ms) CREATE INDEX "index_models_i18n_on_model_id" ON "models_i18n" ("model_id")
2305
+  (0.1ms) CREATE INDEX "index_models_i18n_on_name" ON "models_i18n" ("name")
2306
+  (0.1ms) CREATE INDEX "index_models_i18n_on_updated_at" ON "models_i18n" ("updated_at")
2307
+  (0.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
2308
+  (0.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
2309
+  (0.0ms) SELECT version FROM "schema_migrations"
2310
+  (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20130404161713')
2311
+  (0.1ms) begin transaction
2312
+  (0.1ms) rollback transaction
2313
+  (0.1ms) begin transaction
2314
+  (0.0ms) rollback transaction
2315
+  (0.1ms) begin transaction
2316
+ ModelTranslation Load (19.9ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
2317
+  (0.1ms) SAVEPOINT active_record_1
2318
+ ModelTranslation Exists (0.1ms) SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
2319
+ SQL (48.8ms) 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]]
2320
+ SQL (0.5ms) 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
+  (0.1ms) RELEASE SAVEPOINT active_record_1
2322
+  (0.1ms) rollback transaction
2323
+  (0.1ms) begin transaction
2324
+ ModelTranslation Load (0.1ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
2325
+  (0.0ms) SAVEPOINT active_record_1
2326
+ ModelTranslation Exists (0.1ms) SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
2327
+ SQL (0.2ms) 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]]
2328
+ SQL (0.2ms) 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
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2330
+  (0.0ms) SAVEPOINT active_record_1
2331
+ SQL (0.1ms) DELETE FROM "models_i18n" WHERE "models_i18n"."id" = ? [["id", 1]]
2332
+ SQL (0.1ms) DELETE FROM "models" WHERE "models"."id" = ? [["id", 1]]
2333
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2334
+ ModelTranslation Load (0.2ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" = 1 LIMIT 1
2335
+  (0.1ms) rollback transaction
2336
+  (0.0ms) begin transaction
2337
+ ModelTranslation Load (0.2ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
2338
+  (0.1ms) SAVEPOINT active_record_1
2339
+ ModelTranslation Exists (0.1ms) SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
2340
+ SQL (0.3ms) 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
+ SQL (0.2ms) 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]]
2342
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2343
+  (0.0ms) SAVEPOINT active_record_1
2344
+ ModelTranslation Exists (0.1ms) 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
+  (0.1ms) UPDATE "models_i18n" SET "name" = 'new name', "updated_at" = '2013-04-06 18:10:53.988752' WHERE "models_i18n"."id" = 1
2346
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2347
+  (0.1ms) rollback transaction
2348
+ Connecting to database specified by database.yml
2349
+  (1.8ms) select sqlite_version(*)
2350
+  (0.3ms) CREATE TABLE "models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
2351
+  (0.1ms) CREATE 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) 
2352
+  (0.1ms) CREATE INDEX "index_models_i18n_on_created_at" ON "models_i18n" ("created_at")
2353
+  (0.1ms) CREATE INDEX "index_models_i18n_on_locale" ON "models_i18n" ("locale")
2354
+  (0.1ms) CREATE INDEX "index_models_i18n_on_model_id" ON "models_i18n" ("model_id")
2355
+  (0.1ms) CREATE INDEX "index_models_i18n_on_name" ON "models_i18n" ("name")
2356
+  (0.1ms) CREATE INDEX "index_models_i18n_on_updated_at" ON "models_i18n" ("updated_at")
2357
+  (0.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
2358
+  (0.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
2359
+  (0.0ms) SELECT version FROM "schema_migrations"
2360
+  (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20130404161713')
2361
+  (0.1ms) begin transaction
2362
+  (0.1ms) rollback transaction
2363
+  (0.1ms) begin transaction
2364
+  (0.1ms) rollback transaction
2365
+  (0.1ms) begin transaction
2366
+ ModelTranslation Load (1.6ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
2367
+  (0.1ms) SAVEPOINT active_record_1
2368
+ ModelTranslation Exists (0.1ms) SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
2369
+ SQL (5.9ms) INSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?) [["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
+ SQL (0.3ms) 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
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2372
+  (0.1ms) rollback transaction
2373
+  (0.0ms) begin transaction
2374
+ ModelTranslation Load (0.1ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
2375
+  (0.0ms) SAVEPOINT active_record_1
2376
+ ModelTranslation Exists (0.1ms) SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
2377
+ SQL (0.2ms) INSERT INTO "models" ("created_at", "updated_at") VALUES (?, ?) [["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
+ SQL (0.2ms) 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
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2380
+  (0.0ms) SAVEPOINT active_record_1
2381
+ SQL (0.1ms) DELETE FROM "models_i18n" WHERE "models_i18n"."id" = ? [["id", 1]]
2382
+ SQL (0.1ms) DELETE FROM "models" WHERE "models"."id" = ? [["id", 1]]
2383
+  (0.1ms) RELEASE SAVEPOINT active_record_1
2384
+ ModelTranslation Load (0.1ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" = 1 LIMIT 1
2385
+  (0.1ms) rollback transaction
2386
+  (0.0ms) begin transaction
2387
+ ModelTranslation Load (0.1ms) SELECT "models_i18n".* FROM "models_i18n" WHERE "models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en' LIMIT 1
2388
+  (0.0ms) SAVEPOINT active_record_1
2389
+ ModelTranslation Exists (0.1ms) SELECT 1 AS one FROM "models_i18n" WHERE ("models_i18n"."model_id" IS NULL AND "models_i18n"."locale" = 'en') LIMIT 1
2390
+ SQL (0.2ms) 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
+ SQL (0.2ms) INSERT INTO "models_i18n" ("created_at", "locale", "model_id", "name", "updated_at") VALUES (?, ?, ?, ?, ?) [["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
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2393
+  (0.0ms) SAVEPOINT active_record_1
2394
+ ModelTranslation Exists (0.1ms) 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
+  (0.1ms) UPDATE "models_i18n" SET "name" = 'new name', "updated_at" = '2013-04-06 18:30:30.012902' WHERE "models_i18n"."id" = 1
2396
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2397
+  (0.1ms) rollback transaction
@@ -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 'should exists' do
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'
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class RailsI18nRecordTest < ActiveSupport::TestCase
4
4
 
5
- test 'truth' do
5
+ test "truth" do
6
6
  assert_kind_of Module, RailsI18nRecord
7
7
  end
8
8
 
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 'should create associated translation' do
7
+ test "should create associated translation" do
8
8
  assert_equal @record.name, 'name'
9
9
  end
10
10
 
11
- test 'should edit associated translation' do
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 'should delete associated translation' do
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
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-04-05 00:00:00.000000000 Z
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