globalize2 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/globalize2.gemspec +2 -2
- data/lib/globalize/active_record.rb +18 -1
- data/lib/globalize/active_record/migration.rb +6 -2
- data/test/active_record/translates_test.rb +9 -0
- data/test/active_record_test.rb +17 -1
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/globalize2.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{globalize2}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sven Fuchs, Joshua Harvey, Clemens Kofler"]
|
12
|
-
s.date = %q{2010-04-
|
12
|
+
s.date = %q{2010-04-29}
|
13
13
|
s.description = %q{Rails I18n: de-facto standard library for ActiveRecord data translation}
|
14
14
|
s.email = %q{joshmh@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -125,7 +125,12 @@ module Globalize
|
|
125
125
|
def find_first_by_translated_attr_and_locales(name, value)
|
126
126
|
query = "#{translated_attr_name(name)} = ? AND #{translated_attr_name('locale')} IN (?)"
|
127
127
|
locales = Globalize.fallbacks(locale || I18n.locale).map(&:to_s)
|
128
|
-
find(
|
128
|
+
find(
|
129
|
+
:first,
|
130
|
+
:joins => :translations,
|
131
|
+
:conditions => [query, value, locales],
|
132
|
+
:readonly => false
|
133
|
+
)
|
129
134
|
end
|
130
135
|
|
131
136
|
def translated_attr_accessor(name)
|
@@ -149,6 +154,14 @@ module Globalize
|
|
149
154
|
@globalize ||= Adapter.new self
|
150
155
|
end
|
151
156
|
|
157
|
+
def attributes
|
158
|
+
self.attribute_names.inject({}) do |attrs, name|
|
159
|
+
attrs[name] = read_attribute(name) ||
|
160
|
+
(globalize.fetch(I18n.locale, name) rescue nil)
|
161
|
+
attrs
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
152
165
|
def attributes=(attributes, *args)
|
153
166
|
if attributes.respond_to?(:delete) && locale = attributes.delete(:locale)
|
154
167
|
self.class.with_locale(locale) { super }
|
@@ -157,6 +170,10 @@ module Globalize
|
|
157
170
|
end
|
158
171
|
end
|
159
172
|
|
173
|
+
def attribute_names
|
174
|
+
translated_attribute_names.map(&:to_s) + super
|
175
|
+
end
|
176
|
+
|
160
177
|
def available_locales
|
161
178
|
translations.scoped(:select => 'DISTINCT locale').map(&:locale)
|
162
179
|
end
|
@@ -13,7 +13,7 @@ module Globalize
|
|
13
13
|
end
|
14
14
|
|
15
15
|
self.connection.create_table(translation_table_name) do |t|
|
16
|
-
t.references
|
16
|
+
t.references table_name.sub(/^#{table_name_prefix}/, "").singularize
|
17
17
|
t.string :locale
|
18
18
|
fields.each do |name, type|
|
19
19
|
t.column name, type
|
@@ -21,7 +21,11 @@ module Globalize
|
|
21
21
|
t.timestamps
|
22
22
|
end
|
23
23
|
|
24
|
-
self.connection.add_index(
|
24
|
+
self.connection.add_index(
|
25
|
+
translation_table_name,
|
26
|
+
"#{table_name.sub(/^#{table_name_prefix}/, "").singularize}_id",
|
27
|
+
:name => translation_index_name
|
28
|
+
)
|
25
29
|
end
|
26
30
|
|
27
31
|
def translation_index_name
|
@@ -84,4 +84,13 @@ class TranslatesTest < ActiveSupport::TestCase
|
|
84
84
|
post.subject = 'title'
|
85
85
|
assert_equal 'title', post.subject
|
86
86
|
end
|
87
|
+
|
88
|
+
test "find_by_xx records have writable attributes" do
|
89
|
+
Post.create :subject => "change me"
|
90
|
+
p = Post.find_by_subject("change me")
|
91
|
+
p.subject = "changed"
|
92
|
+
assert_nothing_raised(ActiveRecord::ReadOnlyRecord) do
|
93
|
+
p.save
|
94
|
+
end
|
95
|
+
end
|
87
96
|
end
|
data/test/active_record_test.rb
CHANGED
@@ -68,7 +68,7 @@ class ActiveRecordTest < ActiveSupport::TestCase
|
|
68
68
|
test "update_attributes fails with invalid values" do
|
69
69
|
post = Post.create(:subject => 'foo', :content => 'bar')
|
70
70
|
assert !post.update_attributes(:subject => '')
|
71
|
-
|
71
|
+
assert_not_nil post.reload.attributes['subject']
|
72
72
|
assert_equal 'foo', post.subject
|
73
73
|
end
|
74
74
|
|
@@ -442,6 +442,22 @@ class ActiveRecordTest < ActiveSupport::TestCase
|
|
442
442
|
User.expects(:reflect_on_all_validations => validations)
|
443
443
|
assert_equal [:name], User.required_attributes
|
444
444
|
end
|
445
|
+
|
446
|
+
test "attribute_names returns translated and regular attribute names" do
|
447
|
+
Post.create :subject => "foo", :content => "bar"
|
448
|
+
assert_equal Post.last.attribute_names.sort, %w[blog_id content id subject]
|
449
|
+
end
|
450
|
+
|
451
|
+
test "attributes returns translated and regular attributes" do
|
452
|
+
Post.create :subject => "foo", :content => "bar"
|
453
|
+
assert_equal Post.last.attributes.keys.sort, %w[blog_id content id subject]
|
454
|
+
end
|
455
|
+
|
456
|
+
test "to_xml includes translated fields" do
|
457
|
+
Post.create :subject => "foo", :content => "bar"
|
458
|
+
assert Post.last.to_xml =~ /subject/
|
459
|
+
assert Post.last.to_xml =~ /content/
|
460
|
+
end
|
445
461
|
end
|
446
462
|
|
447
463
|
# TODO error checking for fields that exist in main table, don't exist in
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Sven Fuchs, Joshua Harvey, Clemens Kofler
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-29 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|