activerecord_translatable 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +35 -36
- data/Rakefile +9 -19
- data/lib/activerecord_translatable.rb +7 -14
- data/lib/activerecord_translatable/extension.rb +1 -1
- data/lib/version.rb +1 -1
- data/spec/activerecord_translateable_spec.rb +127 -118
- data/spec/spec_helper.rb +5 -9
- metadata +39 -101
- data/spec/dummy/README.rdoc +0 -261
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/assets/javascripts/application.js +0 -15
- data/spec/dummy/app/assets/stylesheets/application.css +0 -13
- data/spec/dummy/app/controllers/application_controller.rb +0 -3
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/models/foo.rb +0 -2
- data/spec/dummy/app/models/noarraything.rb +0 -4
- data/spec/dummy/app/models/something.rb +0 -3
- data/spec/dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/config/application.rb +0 -65
- data/spec/dummy/config/boot.rb +0 -10
- data/spec/dummy/config/database.yml +0 -29
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -37
- data/spec/dummy/config/environments/production.rb +0 -67
- data/spec/dummy/config/environments/test.rb +0 -37
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/i18n_backend.rb +0 -1
- data/spec/dummy/config/initializers/inflections.rb +0 -15
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -58
- data/spec/dummy/db/migrate/20121015083259_create_somethings.rb +0 -8
- data/spec/dummy/db/migrate/20130124094113_create_foo.rb +0 -7
- data/spec/dummy/db/migrate/20130124100555_create_noarraythings.rb +0 -9
- data/spec/dummy/db/schema.rb +0 -33
- data/spec/dummy/log/development.log +0 -423
- data/spec/dummy/log/test.log +0 -7826
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -25
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 718f885dad1df1a6ef66c4e00661c60ce7853238
|
4
|
+
data.tar.gz: 86226beac98c3c53e76ae0417470750a5370ce35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d715d08d519140dd0cdded92914c3301fc03fa05aded68221bbc2e6c9bd43bda22e2ae9f9cb4b45d5512ef042f2347bfa7f7dcfb10af950bc9c7596f0f7abf52
|
7
|
+
data.tar.gz: c03b5a00ac340ddd8f111fdb959a07d68e204a4a2d1098795f220f696c230334ae2b974b9138815eec4bb94096b553a09c5607a572a189d225ab21e1f8ff7d94
|
data/README.rdoc
CHANGED
@@ -6,7 +6,16 @@ Make attributes of an ActiveRecord Model translatable, and store the
|
|
6
6
|
translations in the provided I18n backend. This is really helpful if there
|
7
7
|
already is, a interface to provide ie missing translations for elements in I18n.
|
8
8
|
|
9
|
+
== Install
|
10
|
+
|
11
|
+
Add
|
12
|
+
|
13
|
+
gem "activerecord_translatable"
|
14
|
+
|
15
|
+
to your gemfile, this should work for rails 3 and above.
|
16
|
+
|
9
17
|
== Usage
|
18
|
+
|
10
19
|
Use inside of the model
|
11
20
|
|
12
21
|
class MyModel < ActiveRecord::Base
|
@@ -22,40 +31,47 @@ Use inside of the model
|
|
22
31
|
mymodel.title_wk # => "Woohhaakkk"
|
23
32
|
|
24
33
|
== Prerequisites
|
34
|
+
|
25
35
|
To save the locales ActiveRecordTranslatable saves an array of the specified locale, for
|
26
|
-
this to work, the database needs the ability to save arrays.
|
27
|
-
handle this
|
36
|
+
this to work, the database needs the ability to save arrays. The easiest way to
|
37
|
+
handle this is to use ActiveRecords ability to serialize.
|
28
38
|
|
29
|
-
|
39
|
+
class MyModel < ActiveRecord::Base
|
40
|
+
serialize :locales
|
41
|
+
translate :name
|
42
|
+
end
|
30
43
|
|
31
|
-
|
44
|
+
And add the column as a string
|
32
45
|
|
33
46
|
class CreateMyModel < ActiveRecord::Migration
|
34
47
|
def change
|
35
48
|
create_table :my_model do |t|
|
36
|
-
t.
|
49
|
+
t.string :locales
|
37
50
|
end
|
38
51
|
end
|
39
52
|
end
|
40
53
|
|
41
|
-
If
|
42
|
-
ability to serialize the locales array
|
54
|
+
If you are using postgres you can also use the native postgres array
|
43
55
|
|
44
|
-
|
45
|
-
serialize :locales
|
46
|
-
translate :name
|
47
|
-
end
|
56
|
+
gem 'activerecord-postgres-array'
|
48
57
|
|
49
|
-
|
58
|
+
To add the array to the model the migration looks like this
|
50
59
|
|
51
60
|
class CreateMyModel < ActiveRecord::Migration
|
52
61
|
def change
|
53
62
|
create_table :my_model do |t|
|
54
|
-
t.
|
63
|
+
t.string_array :locales
|
55
64
|
end
|
56
65
|
end
|
57
66
|
end
|
58
67
|
|
68
|
+
And the model does not need to know anything about this
|
69
|
+
|
70
|
+
class MyModel < ActiveRecord::Base
|
71
|
+
translate :name
|
72
|
+
end
|
73
|
+
|
74
|
+
|
59
75
|
== How it works
|
60
76
|
|
61
77
|
Translateable saves the translation via I18n.backend.store_translations, this
|
@@ -63,31 +79,14 @@ means that the backend has to be able to store new items. So backend needs to
|
|
63
79
|
be for example the KeyValue or ActiveRecord one.
|
64
80
|
More http://railscasts.com/episodes/256-i18n-backends
|
65
81
|
|
66
|
-
==
|
67
|
-
|
68
|
-
=== Tests
|
69
|
-
The tests are using rspec, and there is a dummy app setup in the spec directory.
|
70
|
-
Since the example is using postgres, to get the tests working there needs to be
|
71
|
-
a setup postgres user with the needed rights.
|
72
|
-
|
73
|
-
$ createuser -h localhost
|
74
|
-
Enter name of role to add: translatable
|
75
|
-
Shall the new role be a superuser? (y/n) n
|
76
|
-
Shall the new role be allowed to create databases? (y/n) y
|
77
|
-
Shall the new role be allowed to create more new roles? (y/n) n
|
78
|
-
|
79
|
-
Afterwards the db needs to be setup
|
82
|
+
== Tests
|
80
83
|
|
81
|
-
|
82
|
-
$ rake db:create
|
83
|
-
$ rake db:migrate
|
84
|
-
$ rake db:test:prepare
|
84
|
+
The tests are using rspec and an im memory sqlite database, so all that is needed is to run
|
85
85
|
|
86
|
-
|
86
|
+
$ rake spec
|
87
87
|
|
88
|
-
|
88
|
+
== Current State
|
89
89
|
|
90
|
-
|
91
|
-
|
92
|
-
project. So any bug reports and contributions are highly welcome.
|
90
|
+
This has been extracted form an Rails application so it should be pretty stable
|
91
|
+
by now. Any contributions and fixes are of course highly welcome!
|
93
92
|
|
data/Rakefile
CHANGED
@@ -1,19 +1,15 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
1
|
begin
|
3
2
|
require 'bundler/setup'
|
4
3
|
rescue LoadError
|
5
4
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
5
|
end
|
7
|
-
begin
|
8
|
-
require 'rdoc/task'
|
9
|
-
require 'rspec/core/rake_task'
|
10
|
-
rescue LoadError
|
11
|
-
require 'rdoc/rdoc'
|
12
|
-
require 'rake/rdoctask'
|
13
|
-
RDoc::Task = Rake::RDocTask
|
14
|
-
end
|
15
6
|
|
16
|
-
|
7
|
+
require 'rdoc/task'
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
|
10
|
+
require "rake/clean"
|
11
|
+
|
12
|
+
RDoc::Task.new do |rdoc|
|
17
13
|
rdoc.rdoc_dir = 'rdoc'
|
18
14
|
rdoc.title = 'Translatable'
|
19
15
|
rdoc.options << '--line-numbers'
|
@@ -24,15 +20,9 @@ end
|
|
24
20
|
Bundler::GemHelper.install_tasks
|
25
21
|
|
26
22
|
desc 'Default: run the specs'
|
27
|
-
task :default =>
|
23
|
+
task :default => :spec
|
28
24
|
|
29
|
-
|
30
|
-
|
31
|
-
RSpec::Core::RakeTask.new('unit') do |t|
|
32
|
-
t.pattern = 'spec/{*_spec.rb}'
|
33
|
-
end
|
25
|
+
RSpec::Core::RakeTask.new do |t|
|
26
|
+
t.pattern = 'spec/{*_spec.rb}'
|
34
27
|
end
|
35
28
|
|
36
|
-
desc "Run the unit specs"
|
37
|
-
task :spec => ['spec:unit']
|
38
|
-
|
@@ -1,26 +1,19 @@
|
|
1
1
|
require "active_record"
|
2
2
|
require "active_support/concern"
|
3
|
-
require "rails"
|
4
3
|
require "activerecord_translatable/extension"
|
5
4
|
|
6
5
|
module ActiveRecordTranslatable
|
6
|
+
begin
|
7
|
+
require "rails"
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
ActiveRecordTranslatable::Railtie.load
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class Railtie
|
17
|
-
class << self
|
18
|
-
def load
|
19
|
-
if defined?(ActiveRecord)
|
9
|
+
class Railtie < Rails::Railtie
|
10
|
+
initializer "activerecord_translatable.load_into_active_record" do
|
11
|
+
ActiveSupport.on_load :active_record do
|
20
12
|
ActiveRecord::Base.send(:include, ActiveRecordTranslatable)
|
21
13
|
end
|
22
14
|
end
|
23
15
|
end
|
16
|
+
rescue LoadError
|
17
|
+
ActiveRecord::Base.send(:include, ActiveRecordTranslatable) if defined?(ActiveRecord)
|
24
18
|
end
|
25
|
-
|
26
19
|
end
|
data/lib/version.rb
CHANGED
@@ -1,124 +1,133 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
4
|
+
ActiveRecord::Schema.verbose = false
|
5
|
+
|
6
|
+
def setup_db
|
7
|
+
ActiveRecord::Base.connection.create_table :somethings do |t|
|
8
|
+
t.string :locales
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Base.connection.create_table :foos do |t|
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Something < ActiveRecord::Base
|
16
|
+
serialize :locales
|
17
|
+
translate :name
|
18
|
+
end
|
19
|
+
|
20
|
+
class Foo < ActiveRecord::Base
|
21
|
+
end
|
22
|
+
|
3
23
|
describe "ActiveRecordTranslateable" do
|
24
|
+
before(:all) { setup_db }
|
25
|
+
|
26
|
+
it "makes name translateable" do
|
27
|
+
Something.class_eval { translate :foo }
|
28
|
+
Something.translatable.should include(:foo)
|
29
|
+
end
|
30
|
+
|
31
|
+
context "class without translations" do
|
32
|
+
it "saves" do
|
33
|
+
foo = Foo.new
|
34
|
+
foo.save.should be_true
|
35
|
+
end
|
36
|
+
it "loads" do
|
37
|
+
foo = Foo.create!
|
38
|
+
Foo.find(foo.id).should_not be_nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "custom created methods" do
|
43
|
+
it "responds to translated attributes" do
|
44
|
+
something = Something.new
|
45
|
+
something.should respond_to(:name)
|
46
|
+
something.should respond_to(:name=)
|
47
|
+
something.should respond_to(:name_de)
|
48
|
+
something.should respond_to(:name_de=)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "attributes" do
|
53
|
+
before(:each) do
|
54
|
+
@locale = I18n.locale
|
55
|
+
end
|
56
|
+
|
57
|
+
after(:each) do
|
58
|
+
I18n.locale = @locale
|
59
|
+
end
|
60
|
+
|
61
|
+
it "read/writes by hash assignment" do
|
62
|
+
something = Something.new(name: "Something", name_de: "Etwas")
|
63
|
+
something.name.should == "Something"
|
64
|
+
something.name_de.should == "Etwas"
|
65
|
+
end
|
66
|
+
|
67
|
+
it "read/writes from database" do
|
68
|
+
something = Something.create!(name: "Something", name_de: "Etwas")
|
69
|
+
something.reload
|
70
|
+
something.name.should == "Something"
|
71
|
+
something.name_de.should == "Etwas"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "reads by locale" do
|
75
|
+
something = Something.create!(name_de: "Etwas")
|
76
|
+
I18n.locale = :de
|
77
|
+
something.name.should == "Etwas"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "writes by locale" do
|
81
|
+
I18n.locale = :de
|
82
|
+
something = Something.create!(name: "Etwas", name_en: "Something")
|
83
|
+
I18n.locale = :en
|
84
|
+
something.name_de.should == "Etwas"
|
85
|
+
something.name.should == "Something"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "store translations" do
|
90
|
+
let(:something) { Something.create(name: "Something") }
|
91
|
+
|
92
|
+
it "writes stored translations to the backend" do
|
93
|
+
i18n_key = "something.name-#{something.id}"
|
94
|
+
backend = double("Backend")
|
95
|
+
I18n.stub(:backend).and_return(backend)
|
96
|
+
backend.should_receive(:store_translations)
|
97
|
+
.with(:en, { i18n_key => "Something" }, escape: false)
|
98
|
+
something.save
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should save the model without translations" do
|
102
|
+
something = Something.new
|
103
|
+
something.save.should be_true
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should not include a read locale unless set to something" do
|
107
|
+
something.name_gr
|
108
|
+
something.locales.should_not include("gr")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "translation store" do
|
113
|
+
before(:each) do
|
114
|
+
@backend = double("Backend")
|
115
|
+
I18n.stub(:backend).and_return(@backend)
|
116
|
+
end
|
117
|
+
it "is called on save" do
|
118
|
+
@backend.should_receive(:store_translations).twice
|
119
|
+
Something.new(name: 'something', name_de: 'etwas').save
|
120
|
+
end
|
121
|
+
|
122
|
+
it "is called on create" do
|
123
|
+
@backend.should_receive(:store_translations).twice
|
124
|
+
Something.create(name: 'something', name_de: 'etwas')
|
125
|
+
end
|
4
126
|
|
5
|
-
it "
|
6
|
-
|
7
|
-
Something.
|
8
|
-
|
9
|
-
|
10
|
-
context "class without translations" do
|
11
|
-
it "saves" do
|
12
|
-
foo = Foo.new
|
13
|
-
foo.save.should be_true
|
14
|
-
end
|
15
|
-
it "loads" do
|
16
|
-
foo = Foo.create!
|
17
|
-
Foo.find(foo.id).should_not be_nil
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "custom created methods" do
|
22
|
-
it "responds to translated attributes" do
|
23
|
-
something = Something.new
|
24
|
-
something.should respond_to(:name)
|
25
|
-
something.should respond_to(:name=)
|
26
|
-
something.should respond_to(:name_de)
|
27
|
-
something.should respond_to(:name_de=)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "attributes" do
|
32
|
-
before(:each) do
|
33
|
-
@locale = I18n.locale
|
34
|
-
end
|
35
|
-
|
36
|
-
after(:each) do
|
37
|
-
I18n.locale = @locale
|
38
|
-
end
|
39
|
-
|
40
|
-
it "read/writes by hash assignment" do
|
41
|
-
something = Something.new(name: "Something", name_de: "Etwas")
|
42
|
-
something.name.should == "Something"
|
43
|
-
something.name_de.should == "Etwas"
|
44
|
-
end
|
45
|
-
|
46
|
-
it "read/writes from database" do
|
47
|
-
something = Something.create!(name: "Something", name_de: "Etwas")
|
48
|
-
something.reload
|
49
|
-
something.name.should == "Something"
|
50
|
-
something.name_de.should == "Etwas"
|
51
|
-
end
|
52
|
-
|
53
|
-
it "reads by locale" do
|
54
|
-
something = Something.create!(name_de: "Etwas")
|
55
|
-
I18n.locale = :de
|
56
|
-
something.name.should == "Etwas"
|
57
|
-
end
|
58
|
-
|
59
|
-
it "writes by locale" do
|
60
|
-
I18n.locale = :de
|
61
|
-
something = Something.create!(name: "Etwas", name_en: "Something")
|
62
|
-
I18n.locale = :en
|
63
|
-
something.name_de.should == "Etwas"
|
64
|
-
something.name.should == "Something"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context "store translations" do
|
69
|
-
let(:something) { Something.create(name: "Something") }
|
70
|
-
|
71
|
-
it "writes stored translations to the backend" do
|
72
|
-
i18n_key = "something.name-#{something.id}"
|
73
|
-
backend = double("Backend")
|
74
|
-
I18n.stub(:backend).and_return(backend)
|
75
|
-
backend.should_receive(:store_translations)
|
76
|
-
.with(:en, { i18n_key => "Something" }, escape: false)
|
77
|
-
something.save
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should save the model without translations" do
|
81
|
-
something = Something.new
|
82
|
-
something.save.should be_true
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should not include a read locale unless set to something" do
|
86
|
-
something.name_gr
|
87
|
-
something.locales.should_not include("gr")
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
context "translation store" do
|
92
|
-
before(:each) do
|
93
|
-
@backend = double("Backend")
|
94
|
-
I18n.stub(:backend).and_return(@backend)
|
95
|
-
end
|
96
|
-
it "is called on save" do
|
97
|
-
@backend.should_receive(:store_translations).twice
|
98
|
-
Something.new(name: 'something', name_de: 'etwas').save
|
99
|
-
end
|
100
|
-
|
101
|
-
it "is called on create" do
|
102
|
-
@backend.should_receive(:store_translations).twice
|
103
|
-
Something.create(name: 'something', name_de: 'etwas')
|
104
|
-
end
|
105
|
-
|
106
|
-
it "is called on update" do
|
107
|
-
@backend.should_receive(:store_translations).exactly(4)
|
108
|
-
sth = Something.create(name: 'something_old', name_de: 'etwas_old')
|
109
|
-
sth.update_attributes(name: 'something', name_de: 'etwas')
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
context "db array support" do
|
114
|
-
it "works with native support" do
|
115
|
-
something = Something.create!(name: "Something", name_de: "Etwas")
|
116
|
-
something.reload.available_locales.should include(:en, :de)
|
117
|
-
end
|
118
|
-
|
119
|
-
it "works with serialize" do
|
120
|
-
thing = Noarraything.create!(name: "thing", name_de: "ding")
|
121
|
-
thing.reload.available_locales.should include(:en, :de)
|
122
|
-
end
|
127
|
+
it "is called on update" do
|
128
|
+
@backend.should_receive(:store_translations).exactly(4)
|
129
|
+
sth = Something.create(name: 'something_old', name_de: 'etwas_old')
|
130
|
+
sth.update_attributes(name: 'something', name_de: 'etwas')
|
123
131
|
end
|
132
|
+
end
|
124
133
|
end
|