puret 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +18 -4
- data/lib/generators/puret/model/templates/migration.rb +1 -1
- data/lib/puret/active_record_extensions.rb +9 -3
- data/lib/puret/version.rb +1 -1
- data/test/puret_test.rb +34 -4
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
Puret is a minimal pure translation library for translating database values for Rails 3.
|
4
4
|
|
5
|
-
Puret is quiet like model_translations http://github.com/janne/model_translations (and borrowed much of its code), but comes with generators to help you get started
|
5
|
+
Puret is quiet like model_translations http://github.com/janne/model_translations (and borrowed much of its code), but comes with generators to help you get started.
|
6
|
+
|
7
|
+
Puret does not create the translation model dynamically (like model_translations does) but creates the file via generator. Puret wants to leave out as much magic as possible and want to allow customizing every part of your application.
|
6
8
|
|
7
9
|
|
8
10
|
== Installation
|
@@ -43,7 +45,7 @@ You now need to create a migration for the translations table:
|
|
43
45
|
|
44
46
|
create_table(:post_translations) do |t|
|
45
47
|
t.references :post
|
46
|
-
t.string :locale
|
48
|
+
t.string :locale
|
47
49
|
|
48
50
|
t.string :title
|
49
51
|
t.text :description
|
@@ -58,12 +60,24 @@ Now you are able to translate values for the attributes :title and :description
|
|
58
60
|
|
59
61
|
I18n.locale = :en
|
60
62
|
post.title = 'Puret really rocks!'
|
61
|
-
post.title #=> Puret really rocks!
|
62
63
|
I18n.locale = :de
|
63
64
|
post.title = 'Puret rockt wirklich!'
|
64
|
-
|
65
|
+
|
65
66
|
I18n.locale = :en
|
66
67
|
post.title #=> Puret really rocks!
|
68
|
+
I18n.locale = :de
|
69
|
+
post.title #=> Puret rockt wirklich!
|
70
|
+
|
71
|
+
|
72
|
+
== Translation lookup fallback
|
73
|
+
|
74
|
+
If a translation is not available in your locale, puret looks
|
75
|
+
|
76
|
+
1. for an instance method called *default_locale* and the corresponding translation
|
77
|
+
2. for a class method called *default_locale* and the corresponding translation
|
78
|
+
3. for a translation in I18n.default_locale
|
79
|
+
|
80
|
+
In case a translation is not available in the default locale, puret uses the first locale it could find. That order is specified by creation time, so the first created translation will be returned.
|
67
81
|
|
68
82
|
|
69
83
|
== Generators
|
@@ -2,7 +2,7 @@ class Create<%= translations_table_name.camelize %> < ActiveRecord::Migration
|
|
2
2
|
def self.up
|
3
3
|
create_table(:<%= translations_table_name %>) do |t|
|
4
4
|
t.references :<%= reference_name %>
|
5
|
-
t.string :locale
|
5
|
+
t.string :locale
|
6
6
|
|
7
7
|
<% attributes.each do |attribute| -%>
|
8
8
|
t.<%= attribute.type %> :<%= attribute.name %>
|
@@ -15,7 +15,7 @@ module Puret
|
|
15
15
|
# Configure translated attributes.
|
16
16
|
# Eg:
|
17
17
|
# class Post < ActiveRecord::Base
|
18
|
-
# puret :title, description
|
18
|
+
# puret :title, :description
|
19
19
|
# end
|
20
20
|
def puret(*attributes)
|
21
21
|
make_it_puret! unless included_modules.include?(InstanceMethods)
|
@@ -37,7 +37,7 @@ module Puret
|
|
37
37
|
# use default locale, if present.
|
38
38
|
# Otherwise use first translation
|
39
39
|
translation = translations.detect { |t| t.locale.to_sym == I18n.locale } ||
|
40
|
-
translations.detect { |t| t.locale.to_sym ==
|
40
|
+
translations.detect { |t| t.locale.to_sym == puret_default_locale } ||
|
41
41
|
translations.first
|
42
42
|
|
43
43
|
translation ? translation[attribute] : nil
|
@@ -51,12 +51,18 @@ module Puret
|
|
51
51
|
def make_it_puret!
|
52
52
|
include InstanceMethods
|
53
53
|
|
54
|
-
has_many :translations, :class_name => "#{self.to_s}Translation", :dependent => :destroy
|
54
|
+
has_many :translations, :class_name => "#{self.to_s}Translation", :dependent => :destroy, :order => "created_at DESC"
|
55
55
|
after_save :update_translations!
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
module InstanceMethods
|
60
|
+
def puret_default_locale
|
61
|
+
return default_locale.to_sym if respond_to?(:default_locale)
|
62
|
+
return self.class.default_locale.to_sym if self.class.respond_to?(:default_locale)
|
63
|
+
I18n.default_locale
|
64
|
+
end
|
65
|
+
|
60
66
|
# attributes are stored in @puret_attributes instance variable via setter
|
61
67
|
def puret_attributes
|
62
68
|
@puret_attributes ||= Hash.new { |hash, key| hash[key] = {} }
|
data/lib/puret/version.rb
CHANGED
data/test/puret_test.rb
CHANGED
@@ -12,7 +12,7 @@ class PuretTest < ActiveSupport::TestCase
|
|
12
12
|
end
|
13
13
|
|
14
14
|
test "database setup" do
|
15
|
-
|
15
|
+
assert_equal 1, Post.count
|
16
16
|
end
|
17
17
|
|
18
18
|
test "allow translation" do
|
@@ -23,10 +23,40 @@ class PuretTest < ActiveSupport::TestCase
|
|
23
23
|
assert_equal 'English title', Post.first.title
|
24
24
|
end
|
25
25
|
|
26
|
-
test "assert fallback to default" do
|
27
|
-
|
26
|
+
test "assert fallback to default locale" do
|
27
|
+
post = Post.first
|
28
|
+
I18n.locale = :sv
|
29
|
+
post.title = 'Svensk titel'
|
30
|
+
I18n.locale = :en
|
31
|
+
assert_equal 'English title', post.title
|
32
|
+
I18n.locale = :de
|
33
|
+
assert_equal 'English title', post.title
|
34
|
+
end
|
35
|
+
|
36
|
+
test "assert fallback to saved default locale defined on instance" do
|
37
|
+
post = Post.first
|
38
|
+
def post.default_locale() :sv; end
|
39
|
+
assert_equal :sv, post.puret_default_locale
|
40
|
+
I18n.locale = :sv
|
41
|
+
post.title = 'Svensk titel'
|
42
|
+
post.save!
|
43
|
+
I18n.locale = :en
|
44
|
+
assert_equal 'English title', post.title
|
45
|
+
I18n.locale = :de
|
46
|
+
assert_equal 'Svensk titel', post.title
|
47
|
+
end
|
48
|
+
|
49
|
+
test "assert fallback to saved default locale defined on class level" do
|
50
|
+
post = Post.first
|
51
|
+
def Post.default_locale() :sv; end
|
52
|
+
assert_equal :sv, post.puret_default_locale
|
53
|
+
I18n.locale = :sv
|
54
|
+
post.title = 'Svensk titel'
|
55
|
+
post.save!
|
56
|
+
I18n.locale = :en
|
57
|
+
assert_equal 'English title', post.title
|
28
58
|
I18n.locale = :de
|
29
|
-
|
59
|
+
assert_equal 'Svensk titel', post.title
|
30
60
|
end
|
31
61
|
|
32
62
|
test "post has_many translations" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puret
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Johannes J\xC3\xB6rg Schmidt"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-24 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|