teonimesic-translated_attributes 0.5.7 → 0.5.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +1 -1
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/generators/translated_attributes/templates/migration.rb +2 -2
- data/lib/translated_attributes.rb +11 -19
- data/spec/models.rb +3 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/translated_attributes_spec.rb +36 -1
- metadata +8 -5
data/README.markdown
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
This is an adaptation on Michael Grosser's translated attributes, but lightly adapted for rails 3 and my own needs.
|
1
|
+
This is an adaptation on Michael Grosser's translated attributes, but lightly adapted for rails 3 and ruby 1.9 and my own needs, and it may not work properly with older versions.
|
2
2
|
|
3
3
|
Rails plugin/ActiveRecord gem that creates 'virtual' attributes, which can be added on the fly without overhead or migrations, while storing all the data in a never-changing translations table.
|
4
4
|
This keeps the attatched model light and allows to add/remove fields on the fly without migrations.
|
data/Rakefile
CHANGED
@@ -11,8 +11,8 @@ begin
|
|
11
11
|
gem.email = "stefano.diem@gmail.com"
|
12
12
|
gem.homepage = "http://github.com/teonimesic/#{project_name}"
|
13
13
|
gem.authors = ["Michael Grosser","Stefano Diem Benatti"]
|
14
|
-
gem.add_dependency 'activerecord'
|
15
|
-
gem.
|
14
|
+
gem.add_dependency 'activerecord', '>= 3.0.0.beta4'
|
15
|
+
gem.add_development_dependency "rspec-core", ">= 2.0.0.beta.13"
|
16
16
|
end
|
17
17
|
|
18
18
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.8
|
@@ -3,10 +3,10 @@ class AddTranslations < ActiveRecord::Migration
|
|
3
3
|
#you can remove the limit/null constrains
|
4
4
|
#this is simply my recommended way of setting things up (save + limits needed storage space)
|
5
5
|
create_table :translations do |t|
|
6
|
-
t.
|
6
|
+
t.belongs_to :translatable, :null=>false
|
7
7
|
t.string :translatable_type, :limit=>40, :null=>false
|
8
8
|
t.string :language, :limit=>2, :null=>false
|
9
|
-
t.string :
|
9
|
+
t.string :translated_attribute, :limit=>40, :null=>false
|
10
10
|
t.text :text, :null=>false
|
11
11
|
end
|
12
12
|
add_index :translations, [:translatable_id, :translatable_type]
|
@@ -52,23 +52,11 @@ module TranslatedAttributes
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def attributes=(new_attributes, *args)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
new_attributes = get_language_attributes(l,non_serialized_attributes)
|
59
|
-
self.class.language(l){ super }
|
60
|
-
end
|
61
|
-
else
|
62
|
-
super
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def get_language_attributes(lang, params)
|
67
|
-
local_params = {}
|
68
|
-
if (params.include?(lang) || params.include?(lang.to_s) ) && I18n.available_locales.include?(lang.to_sym)
|
69
|
-
local_params = params.stringify_keys[lang.to_s]
|
55
|
+
unless ( translations_hash = new_attributes.select{|k,v| is_translated_attribute_hash?(k) } ).blank?
|
56
|
+
@translated_attributes = translations_hash.with_indifferent_access
|
57
|
+
new_attributes.delete_if{|k,v| is_translated_attribute_hash?(k) }
|
70
58
|
end
|
71
|
-
|
59
|
+
super
|
72
60
|
end
|
73
61
|
|
74
62
|
def get_translated_attribute(locale, field)
|
@@ -95,7 +83,7 @@ module TranslatedAttributes
|
|
95
83
|
|
96
84
|
def set_translated_attribute(locale, field, value)
|
97
85
|
old_value = translated_attributes_for(locale)[field]
|
98
|
-
return if old_value == value
|
86
|
+
return if old_value.to_s == value.to_s
|
99
87
|
changed_attributes.merge!("#{field}_in_#{locale}" => old_value)
|
100
88
|
translated_attributes_for(locale)[field] = value
|
101
89
|
@translated_attributes_changed = true
|
@@ -137,7 +125,7 @@ module TranslatedAttributes
|
|
137
125
|
attributes.each do |attribute, value|
|
138
126
|
next if value.blank?
|
139
127
|
next unless self.class.translated_attributes_options[:fields].include? attribute.to_sym
|
140
|
-
translations.create!(:
|
128
|
+
translations.create!(:translated_attribute=>attribute, :text=>value, :language=>locale)
|
141
129
|
end
|
142
130
|
end
|
143
131
|
@translated_attributes_changed = false
|
@@ -149,7 +137,7 @@ module TranslatedAttributes
|
|
149
137
|
return if new_record? or @db_translations_merged
|
150
138
|
@db_translations_merged = true
|
151
139
|
translations.each do |t|
|
152
|
-
translated_attributes_for(t.language)[t.
|
140
|
+
translated_attributes_for(t.language)[t.translated_attribute] = t.text
|
153
141
|
end
|
154
142
|
end
|
155
143
|
|
@@ -164,6 +152,10 @@ module TranslatedAttributes
|
|
164
152
|
fields = self.class.translated_attributes_options[:fields]
|
165
153
|
fields.include? method_name.sub('=','').to_sym
|
166
154
|
end
|
155
|
+
|
156
|
+
def is_translated_attribute_hash?(key)
|
157
|
+
I18n.available_locales.include?(key.to_s) || I18n.available_locales.include?(key.to_sym)
|
158
|
+
end
|
167
159
|
|
168
160
|
def translated_attributes_for(locale)
|
169
161
|
translated_attributes[locale] ||= {}.with_indifferent_access
|
data/spec/models.rb
CHANGED
@@ -7,12 +7,13 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
7
7
|
|
8
8
|
%w[translations user_translations].each do |table|
|
9
9
|
create_table table do |t|
|
10
|
-
t.
|
10
|
+
t.belongs_to :translatable, :null=>false
|
11
11
|
t.string :translatable_type, :limit=>40, :null=>false
|
12
12
|
t.string :language, :limit=>2, :null=>false
|
13
|
-
t.string :
|
13
|
+
t.string :translated_attribute, :limit=>40, :null=>false
|
14
14
|
t.text :text, :null=>false
|
15
15
|
end
|
16
|
+
#add_index :translations, [:translatable_id, :translatable_type]
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require_relative 'spec_helper'
|
2
4
|
require_relative 'models'
|
3
5
|
|
@@ -25,7 +27,7 @@ describe 'Translated attributes' do
|
|
25
27
|
p.title.should == 'def'
|
26
28
|
end
|
27
29
|
|
28
|
-
it "
|
30
|
+
it "can be unset" do
|
29
31
|
p = Product.new
|
30
32
|
p.title = 'abc'
|
31
33
|
p.title = nil
|
@@ -46,6 +48,21 @@ describe 'Translated attributes' do
|
|
46
48
|
p.title = 'bcd'
|
47
49
|
p.title_in_de.should == 'bcd'
|
48
50
|
end
|
51
|
+
|
52
|
+
it "can be set in a hash flavour syntax" do
|
53
|
+
I18n.available_locales = [:en, :pt]
|
54
|
+
p = Product.new(
|
55
|
+
en: { title: 'TitleInEn', description: 'DescriptionInEn' },
|
56
|
+
pt: { title: 'TítuloEmPt', description: 'DescriçãoEmPt'}
|
57
|
+
)
|
58
|
+
p.title.should == 'TitleInEn'
|
59
|
+
p.title_in_pt.should == 'TítuloEmPt'
|
60
|
+
end
|
61
|
+
|
62
|
+
it "cannot be set in a hash flavour syntax if not present in available locales" do
|
63
|
+
I18n.available_locales = []
|
64
|
+
lambda{ Product.new( en: { title: 'TitleInEn', description: 'DescriptionInEn' } ) }.should raise_error
|
65
|
+
end
|
49
66
|
|
50
67
|
it "returns current language when current can be found" do
|
51
68
|
I18n.locale = :de
|
@@ -68,6 +85,24 @@ describe 'Translated attributes' do
|
|
68
85
|
p.title_in_de = 'abc'
|
69
86
|
p.title.should == 'abc'
|
70
87
|
end
|
88
|
+
|
89
|
+
it "records a change" do
|
90
|
+
p = Product.new
|
91
|
+
p.title = 'abc'
|
92
|
+
p.send(:changed_attributes).should == {'title_in_en' => nil}
|
93
|
+
end
|
94
|
+
|
95
|
+
it "does not record changes for nil to blank" do
|
96
|
+
p = Product.new
|
97
|
+
p.title = ''
|
98
|
+
p.send(:changed_attributes).should == {}
|
99
|
+
end
|
100
|
+
|
101
|
+
it "does not record changes for unchanged" do
|
102
|
+
p = Product.create!(:title_in_en => 'abc')
|
103
|
+
p.title = 'abc'
|
104
|
+
p.send(:changed_attributes).should == {}
|
105
|
+
end
|
71
106
|
end
|
72
107
|
|
73
108
|
describe 'storing' do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 8
|
9
|
+
version: 0.5.8
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Grosser
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-15 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -27,8 +27,11 @@ dependencies:
|
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
segments:
|
30
|
+
- 3
|
30
31
|
- 0
|
31
|
-
|
32
|
+
- 0
|
33
|
+
- beta4
|
34
|
+
version: 3.0.0.beta4
|
32
35
|
type: :runtime
|
33
36
|
version_requirements: *id001
|
34
37
|
- !ruby/object:Gem::Dependency
|
@@ -46,7 +49,7 @@ dependencies:
|
|
46
49
|
- beta
|
47
50
|
- 13
|
48
51
|
version: 2.0.0.beta.13
|
49
|
-
type: :
|
52
|
+
type: :development
|
50
53
|
version_requirements: *id002
|
51
54
|
description:
|
52
55
|
email: stefano.diem@gmail.com
|