simple_model_translations 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +12 -14
- data/lib/simple_model_translations/attributes.rb +3 -3
- data/lib/simple_model_translations/base.rb +2 -2
- data/lib/simple_model_translations/class_methods.rb +3 -5
- data/lib/simple_model_translations/instance_methods.rb +4 -31
- data/lib/simple_model_translations/translation_helper.rb +45 -0
- data/lib/simple_model_translations/validations.rb +2 -4
- data/lib/simple_model_translations/version.rb +1 -1
- data/spec/simple_model_translations_spec.rb +3 -3
- data/spec/spec_helper.rb +2 -2
- metadata +6 -5
data/Gemfile.lock
CHANGED
@@ -1,28 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
simple_model_translations (0.2.
|
4
|
+
simple_model_translations (0.2.4)
|
5
5
|
activerecord (~> 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (3.0.
|
11
|
-
activesupport (= 3.0.
|
10
|
+
activemodel (3.0.5)
|
11
|
+
activesupport (= 3.0.5)
|
12
12
|
builder (~> 2.1.2)
|
13
|
-
i18n (~> 0.4
|
14
|
-
activerecord (3.0.
|
15
|
-
activemodel (= 3.0.
|
16
|
-
activesupport (= 3.0.
|
17
|
-
arel (~>
|
13
|
+
i18n (~> 0.4)
|
14
|
+
activerecord (3.0.5)
|
15
|
+
activemodel (= 3.0.5)
|
16
|
+
activesupport (= 3.0.5)
|
17
|
+
arel (~> 2.0.2)
|
18
18
|
tzinfo (~> 0.3.23)
|
19
|
-
activesupport (3.0.
|
20
|
-
arel (
|
21
|
-
activesupport (~> 3.0.0)
|
19
|
+
activesupport (3.0.5)
|
20
|
+
arel (2.0.9)
|
22
21
|
builder (2.1.2)
|
23
22
|
database_cleaner (0.5.2)
|
24
23
|
diff-lcs (1.1.2)
|
25
|
-
i18n (0.
|
24
|
+
i18n (0.5.0)
|
26
25
|
rspec (2.0.1)
|
27
26
|
rspec-core (~> 2.0.1)
|
28
27
|
rspec-expectations (~> 2.0.1)
|
@@ -35,13 +34,12 @@ GEM
|
|
35
34
|
rspec-expectations (~> 2.0.1)
|
36
35
|
shoulda (2.11.3)
|
37
36
|
sqlite3-ruby (1.3.1)
|
38
|
-
tzinfo (0.3.
|
37
|
+
tzinfo (0.3.24)
|
39
38
|
|
40
39
|
PLATFORMS
|
41
40
|
ruby
|
42
41
|
|
43
42
|
DEPENDENCIES
|
44
|
-
activerecord (~> 3.0.0)
|
45
43
|
database_cleaner (~> 0.5.2)
|
46
44
|
rspec (~> 2.0.0)
|
47
45
|
shoulda (~> 2.11.3)
|
@@ -1,19 +1,19 @@
|
|
1
1
|
module SimpleModelTranslations
|
2
2
|
module Attributes
|
3
3
|
def attributes=(attributes)
|
4
|
-
I18n.with_locale(attributes.delete(:locale) || current_locale_for_translation) do
|
4
|
+
I18n.with_locale(attributes.delete(:locale) || translation_helper.current_locale_for_translation) do
|
5
5
|
super(attributes)
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
def update_attributes!(attributes)
|
10
|
-
I18n.with_locale(attributes.delete(:locale) || current_locale_for_translation) do
|
10
|
+
I18n.with_locale(attributes.delete(:locale) || translation_helper.current_locale_for_translation) do
|
11
11
|
super(attributes)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
def update_attributes(attributes)
|
16
|
-
I18n.with_locale(attributes.delete(:locale) || current_locale_for_translation) do
|
16
|
+
I18n.with_locale(attributes.delete(:locale) || translation_helper.current_locale_for_translation) do
|
17
17
|
super(attributes)
|
18
18
|
end
|
19
19
|
end
|
@@ -21,8 +21,8 @@ module SimpleModelTranslations
|
|
21
21
|
configure_translations(attributes.extract_options!)
|
22
22
|
translation_class.class_eval(&block) if block_given?
|
23
23
|
|
24
|
-
delegate *(attributes + [:to =>
|
25
|
-
delegate *(attributes.map { |attr| "#{attr}=" } + [:to =>
|
24
|
+
delegate *(attributes + [:to => 'translation_helper.current_translation'.to_sym, :allow_nil => true])
|
25
|
+
delegate *(attributes.map { |attr| "#{attr}=" } + [:to => 'translation_helper.find_or_build_current_translation'.to_sym])
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
@@ -1,11 +1,9 @@
|
|
1
1
|
module SimpleModelTranslations
|
2
2
|
module ClassMethods
|
3
3
|
def with_translations(*args)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
includes(:translations).where("#{translated_column_name(:locale)} IN (?)", args)
|
8
|
-
end
|
4
|
+
current_scope = joins(:translations)
|
5
|
+
current_scope = current_scope.where("#{translated_column_name(:locale)} IN (?)", args) unless args.empty?
|
6
|
+
current_scope
|
9
7
|
end
|
10
8
|
alias with_translation with_translations
|
11
9
|
|
@@ -1,36 +1,9 @@
|
|
1
|
+
require 'simple_model_translations/translation_helper'
|
2
|
+
|
1
3
|
module SimpleModelTranslations
|
2
4
|
module InstanceMethods
|
3
|
-
def
|
4
|
-
|
5
|
+
def translation_helper
|
6
|
+
@translation_helper ||= SimpleModelTranslations::TranslationHelper.new(self)
|
5
7
|
end
|
6
|
-
|
7
|
-
def find_or_build_translation_by_locale(locale)
|
8
|
-
find_translation_by_locale(locale) || build_translation_for_locale(locale)
|
9
|
-
end
|
10
|
-
|
11
|
-
def build_translation_for_locale(locale)
|
12
|
-
translations.build(:locale => locale, foreign_object_key => self)
|
13
|
-
end
|
14
|
-
|
15
|
-
def current_locale_for_translation
|
16
|
-
I18n.locale
|
17
|
-
end
|
18
|
-
|
19
|
-
def default_locale_for_translation
|
20
|
-
I18n.default_locale
|
21
|
-
end
|
22
|
-
|
23
|
-
def current_translation
|
24
|
-
find_translation_by_locale(current_locale_for_translation) || find_translation_by_locale(default_locale_for_translation)
|
25
|
-
end
|
26
|
-
|
27
|
-
def find_or_build_current_translation
|
28
|
-
find_or_build_translation_by_locale(current_locale_for_translation)
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
def foreign_object_key
|
33
|
-
self.class.name.underscore.to_sym
|
34
|
-
end
|
35
8
|
end
|
36
9
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module SimpleModelTranslations
|
2
|
+
class TranslationHelper
|
3
|
+
def initialize(record, translation_association = :translations)
|
4
|
+
@record = record
|
5
|
+
@translation_association = translation_association
|
6
|
+
end
|
7
|
+
|
8
|
+
def find_translation_by_locale(locale)
|
9
|
+
translations.detect { |t| t.locale.to_sym == locale }
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_or_build_translation_by_locale(locale)
|
13
|
+
find_translation_by_locale(locale) || build_translation_for_locale(locale)
|
14
|
+
end
|
15
|
+
|
16
|
+
def build_translation_for_locale(locale)
|
17
|
+
translations.build(:locale => locale, foreign_object_key => @record)
|
18
|
+
end
|
19
|
+
|
20
|
+
def current_locale_for_translation
|
21
|
+
I18n.locale
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_locale_for_translation
|
25
|
+
I18n.default_locale
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_translation
|
29
|
+
find_translation_by_locale(current_locale_for_translation) || find_translation_by_locale(default_locale_for_translation)
|
30
|
+
end
|
31
|
+
|
32
|
+
def find_or_build_current_translation
|
33
|
+
find_or_build_translation_by_locale(current_locale_for_translation)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def foreign_object_key
|
38
|
+
@record.class.name.underscore.to_sym
|
39
|
+
end
|
40
|
+
|
41
|
+
def translations
|
42
|
+
@record.send(@translation_association)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -2,10 +2,8 @@ module SimpleModelTranslations
|
|
2
2
|
module Validations
|
3
3
|
class TranslationsValidator < ::ActiveModel::Validator
|
4
4
|
def validate(record)
|
5
|
-
|
6
|
-
|
7
|
-
locales.each do |locale|
|
8
|
-
unless record.find_translation_by_locale(locale)
|
5
|
+
Array.wrap(options[:locales]).each do |locale|
|
6
|
+
unless record.translation_helper.find_translation_by_locale(locale)
|
9
7
|
record.errors.add(:translations, "miss #{locale} translation")
|
10
8
|
end
|
11
9
|
end
|
@@ -28,18 +28,18 @@ describe Article do
|
|
28
28
|
describe '#current_translation' do
|
29
29
|
it 'should return translation for current locale if it exists' do
|
30
30
|
article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
31
|
-
article.current_translation.should == article.translations.find_by_locale(I18n.locale)
|
31
|
+
article.translation_helper.current_translation.should == article.translations.find_by_locale(I18n.locale)
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should return translation for default locale if translation for current locale does not exist' do
|
35
35
|
article = Article.create!(:slug => '__hello__', :name => 'Hello', :content => 'World')
|
36
36
|
I18n.locale = :de
|
37
|
-
article.current_translation.should == article.translations.find_by_locale(I18n.default_locale)
|
37
|
+
article.translation_helper.current_translation.should == article.translations.find_by_locale(I18n.default_locale)
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'should return nil if translations for current and default locales do not exist' do
|
41
41
|
article = Article.create!(:slug => '__hello__')
|
42
|
-
article.current_translation.should be_nil
|
42
|
+
article.translation_helper.current_translation.should be_nil
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -23,12 +23,12 @@ end
|
|
23
23
|
|
24
24
|
RSpec::Matchers.define :have_translation do |locale|
|
25
25
|
match do |record|
|
26
|
-
!record.find_translation_by_locale(locale).nil?
|
26
|
+
!record.translation_helper.find_translation_by_locale(locale).nil?
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
RSpec::Matchers.define :have_translated_attribute do |locale, attribute, value|
|
31
31
|
match do |record|
|
32
|
-
record.find_translation_by_locale(locale).send(attribute) == value
|
32
|
+
record.translation_helper.find_translation_by_locale(locale).send(attribute) == value
|
33
33
|
end
|
34
34
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_model_translations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 4
|
10
|
+
version: 0.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Pavel Forkert
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-03-13 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- lib/simple_model_translations/base.rb
|
122
122
|
- lib/simple_model_translations/class_methods.rb
|
123
123
|
- lib/simple_model_translations/instance_methods.rb
|
124
|
+
- lib/simple_model_translations/translation_helper.rb
|
124
125
|
- lib/simple_model_translations/validations.rb
|
125
126
|
- lib/simple_model_translations/version.rb
|
126
127
|
- lib/simple_model_translations.rb
|
@@ -163,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
164
|
requirements: []
|
164
165
|
|
165
166
|
rubyforge_project:
|
166
|
-
rubygems_version: 1.
|
167
|
+
rubygems_version: 1.5.2
|
167
168
|
signing_key:
|
168
169
|
specification_version: 3
|
169
170
|
summary: Simple ActiveRecord translations for Rails 3
|