rails-translate-models 0.0.1 → 0.0.2
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.
- data/README.rdoc +37 -9
- data/VERSION +1 -1
- data/lib/rails-translate-models.rb +5 -4
- data/rails-translate-models.gemspec +2 -2
- metadata +13 -13
data/README.rdoc
CHANGED
@@ -30,8 +30,7 @@ For each model you have to create a table for the translations, you can do it on
|
|
30
30
|
t.timestamps
|
31
31
|
end
|
32
32
|
|
33
|
-
add_index :
|
34
|
-
add_index :pages_translations, [:page_id, :language_code]
|
33
|
+
add_index :article_translations, [:article_id, :language_code], :unique => true
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
@@ -41,7 +40,7 @@ For each model you have to create a table for the translations, you can do it on
|
|
41
40
|
has_translations :title, :body
|
42
41
|
end
|
43
42
|
|
44
|
-
==
|
43
|
+
== Basic usage
|
45
44
|
|
46
45
|
Self-explained usage given an article with two translations (en / es)
|
47
46
|
|
@@ -73,9 +72,7 @@ To create a new object.
|
|
73
72
|
|
74
73
|
When translation associated record is destroyed all translations are destroyed as well
|
75
74
|
|
76
|
-
==
|
77
|
-
|
78
|
-
== Scopes
|
75
|
+
== Model options
|
79
76
|
|
80
77
|
All models with 'has_translations' have 'has_many :model_translations' and 'default_scope :include => :model_translations' so you can take advantage of it and make use of some scopes, for example:
|
81
78
|
|
@@ -83,15 +80,46 @@ All models with 'has_translations' have 'has_many :model_translations' and 'defa
|
|
83
80
|
has_translations :title, :body
|
84
81
|
|
85
82
|
scope :ordered_by_title, :order => 'article_translations.title'
|
86
|
-
scope :with_title, lambda { |title|
|
83
|
+
scope :with_title, lambda { |title| where("article_translations.title = ?", title) }
|
87
84
|
end
|
88
85
|
|
86
|
+
You can also make use of validations, for example:
|
87
|
+
|
88
|
+
class Article < ActiveRecord::Base
|
89
|
+
has_translations :title, :body
|
90
|
+
validates_presence_of :title
|
91
|
+
end
|
92
|
+
|
93
|
+
Note all models with 'has_translations' already have the validations for object_id and language_code and also validate the uniqueness of language code for each object_id.
|
94
|
+
|
95
|
+
== Form example
|
96
|
+
|
97
|
+
Here's a simple example to make a form to edit the example article model in all languages.
|
98
|
+
|
99
|
+
form.html.erb
|
100
|
+
|
101
|
+
<%= form_for @article do |f| %>
|
102
|
+
<div class="panes">
|
103
|
+
<% I18n.available_locales.each do |locale| %>
|
104
|
+
<%= render "form_translations", {:f => f, :locale => locale } %>
|
105
|
+
<% end %>
|
106
|
+
</div>
|
107
|
+
<% end %>
|
108
|
+
|
109
|
+
_form_translations.html.erb partial
|
110
|
+
|
111
|
+
<label for='article_title_in_<%= locale %>'>Title</label><br />
|
112
|
+
<%= f.text_field "title_in_#{locale}".to_sym %>
|
113
|
+
|
114
|
+
<label for='article_body_in_<%= locale %>'>Content</label><br />
|
115
|
+
<%= f.text_area "body_in_#{locale}".to_sym %>
|
116
|
+
|
89
117
|
== TODO
|
90
118
|
|
91
119
|
It's an initial implementation for a work in progress project, it's just an initial release but does the basic stuff I need. Help is welcome ;)
|
92
120
|
|
93
|
-
|
94
|
-
|
121
|
+
* make tests
|
122
|
+
* translations models generators
|
95
123
|
|
96
124
|
|
97
125
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -12,6 +12,8 @@ module RailsTranslateModels
|
|
12
12
|
translations_klass = Class.new(ActiveRecord::Base) do
|
13
13
|
set_table_name translations_table_name
|
14
14
|
belongs_to type.to_sym
|
15
|
+
validates_presence_of type.to_sym, :language_code
|
16
|
+
validates_uniqueness_of :language_code, :scope => "#{type}_id"
|
15
17
|
end
|
16
18
|
|
17
19
|
Object.const_set(translations_klass_name, translations_klass)
|
@@ -65,8 +67,6 @@ module RailsTranslateModels
|
|
65
67
|
changed_attributes.merge!("#{attribute}_in_#{locale}" => old_value)
|
66
68
|
translated_attributes_for(locale)[attribute] = value
|
67
69
|
@translated_attributes_changed = true
|
68
|
-
# self.translations_attributes.merge!({ :language_code => locale, attribute.to_sym => value })
|
69
|
-
# raise self.translations.inspect
|
70
70
|
end
|
71
71
|
|
72
72
|
def translated_attributes
|
@@ -100,9 +100,10 @@ module RailsTranslateModels
|
|
100
100
|
|
101
101
|
def store_translated_attributes
|
102
102
|
return true unless @translated_attributes_changed
|
103
|
-
translations.delete_all
|
104
103
|
@translated_attributes.each do |locale, attributes|
|
105
|
-
translations.
|
104
|
+
translation = translations.find_or_initialize_by_language_code(locale.to_s)
|
105
|
+
translation.attributes = translation.attributes.merge(attributes)
|
106
|
+
translation.save!
|
106
107
|
end
|
107
108
|
@translated_attributes_changed = false
|
108
109
|
true
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "rails-translate-models"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Francesc Pla"]
|
12
|
-
s.date = "2011-12-
|
12
|
+
s.date = "2011-12-30"
|
13
13
|
s.description = "Simple gem to translate multi-lingual content in Rails models in separate tables for each model (modelname_translations)"
|
14
14
|
s.email = "francesc@francesc.net"
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-translate-models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70217636464000 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70217636464000
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: shoulda
|
27
|
-
requirement: &
|
27
|
+
requirement: &70217636497720 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70217636497720
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bundler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70217636496240 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70217636496240
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: jeweler
|
49
|
-
requirement: &
|
49
|
+
requirement: &70217636491800 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.6.4
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70217636491800
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rcov
|
60
|
-
requirement: &
|
60
|
+
requirement: &70217636521040 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70217636521040
|
69
69
|
description: Simple gem to translate multi-lingual content in Rails models in separate
|
70
70
|
tables for each model (modelname_translations)
|
71
71
|
email: francesc@francesc.net
|
@@ -101,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
101
|
version: '0'
|
102
102
|
segments:
|
103
103
|
- 0
|
104
|
-
hash:
|
104
|
+
hash: 3177321081275544915
|
105
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
106
|
none: false
|
107
107
|
requirements:
|