efficient_translations 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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- efficient_translations (0.0.1)
4
+ efficient_translations (0.0.2)
5
5
  activerecord (~> 2.3)
6
6
  activesupport (~> 2.3)
7
7
 
@@ -8,10 +8,10 @@ module EfficientTranslations
8
8
  # the column name to create and its type.
9
9
  # eg. create_translation_table :product, :name => :string, :description => :string
10
10
  def create_translation_table model_name, translation_fields
11
- translation_table_name = "#{model_name.to_s}_translations"
11
+ translation_table_name = "#{model_name}_translations"
12
12
  create_table translation_table_name do |t|
13
13
  t.references model_name, :null => false
14
- t.string :locale , :null => false
14
+ t.string :locale, :null => false
15
15
  end
16
16
  translation_fields.each do |name, type|
17
17
  add_column translation_table_name, name.to_s, type.to_sym
@@ -7,7 +7,9 @@ module EfficientTranslations
7
7
  module ClassMethods
8
8
  def translates *field_names
9
9
  make_efficient_translatable! unless defined?(translation_model)
10
- field_names.each { |field| define_translation_accessors field }
10
+ field_names.each do |field|
11
+ define_translation_accessors field
12
+ end
11
13
  end
12
14
 
13
15
  def validates_presence_of_default_locale
@@ -18,28 +20,28 @@ module EfficientTranslations
18
20
 
19
21
  def make_efficient_translatable!
20
22
  cattr_accessor :translation_model
21
- self.translation_model = TranslationFactory::new_model self
22
-
23
+ self.translation_model = TranslationFactory.build self
23
24
  has_many :translations, :class_name => translation_model.name, :dependent => :destroy
24
25
  accepts_nested_attributes_for :translations
26
+ after_save :update_translations!
25
27
 
26
28
  named_scope :with_translations, :include => :translations
27
29
  named_scope :with_current_translation, lambda {
28
30
  {
29
31
  :include => :translations,
30
- :conditions => ["#{translation_model.table_name}.locale = ? OR #{translation_model.table_name}.locale = ?", I18n.locale.to_s, I18n.default_locale.to_s]
32
+ :conditions => [scope_conditions, I18n.locale.to_s, I18n.default_locale.to_s]
31
33
  }
32
34
  }
33
35
  named_scope :with_translation_for, lambda { |locale|
34
36
  {
35
37
  :inlude => :translations,
36
- :conditions => ["#{translation_model.table_name}.locale = ? OR #{translation_model.table_name}.locale = ?", locale.to_s, I18n.default_locale.to_s]
38
+ :conditions => [scope_conditions, locale.to_s, I18n.default_locale.to_s]
37
39
  }
38
40
  }
41
+ end
39
42
 
40
- after_save :update_translations!
41
-
42
- self.send :include, InstanceMethods
43
+ def self.scope_conditions
44
+ "#{translation_model.table_name}.locale = ? OR #{translation_model.table_name}.locale = ?"
43
45
  end
44
46
 
45
47
  def define_translation_accessors field
@@ -89,33 +91,32 @@ module EfficientTranslations
89
91
  end
90
92
  end
91
93
 
92
- module InstanceMethods
94
+ # instance methods - no need to put them a module
95
+ private
93
96
 
94
- private
95
-
96
- # attributes are stored in @efficient_attributes instance variable via setter
97
- def efficient_translations_attributes
98
- @efficient_translations_attributes ||= Hash.new { |hash, key| hash[key] = {} }
99
- end
97
+ # attributes are stored in @efficient_attributes instance variable via setter
98
+ def efficient_translations_attributes
99
+ @efficient_translations_attributes ||= Hash.new { |hash, key| hash[key] = {} }
100
+ end
100
101
 
101
- def update_translations!
102
- if efficient_translations_attributes.present?
103
- translations true #force reload all translations
104
- efficient_translations_attributes.each do |locale, attributes|
105
- translation = translations.detect { |t| t.locale.to_sym == locale } || begin
106
- args = { :locale => locale }
107
- args[self.class.translation_model.translatable_relation_field] = self
108
- self.class.translation_model.new args
109
- end
110
- translation.update_attributes! attributes
102
+ def update_translations!
103
+ if efficient_translations_attributes.present?
104
+ translations true # force reload all translations
105
+ efficient_translations_attributes.each do |locale, attributes|
106
+ translation = translations.detect { |t| t.locale.to_sym == locale } || begin
107
+ args = { :locale => locale }
108
+ args[self.class.translation_model.translatable_relation_field] = self
109
+ self.class.translation_model.new args
111
110
  end
111
+ translation.update_attributes! attributes
112
112
  end
113
113
  end
114
+ end
114
115
 
115
- def default_locale_required
116
- unless translations.detect { |t| t.locale.to_sym == I18n.default_locale }
117
- errors.add :translations, "for #{I18n.default_locale} is missing"
118
- end
116
+ def default_locale_required
117
+ unless translations.detect { |t| t.locale.to_sym == I18n.default_locale }
118
+ # people may expect this message to be localized too ;-)
119
+ errors.add :translations, "for #{I18n.default_locale} is missing"
119
120
  end
120
121
  end
121
122
  end
@@ -1,26 +1,24 @@
1
1
  module EfficientTranslations
2
2
  module TranslationFactory
3
- def self.new_model base_model
4
- if base_model.const_defined?(:Translation)
5
- base_model.const_get(:Translation)
3
+ def self.build base_model
4
+ if base_model.const_defined? :Translation
5
+ base_model.const_get :Translation
6
6
  else
7
- klass = base_model.const_set(:Translation, Class.new(::ActiveRecord::Base))
8
- klass.instance_eval do
7
+ klass = base_model.const_set :Translation, Class.new(ActiveRecord::Base)
8
+ klass.class_eval do
9
+ table_name = "#{base_model.table_name.singularize}_translations"
9
10
  cattr_accessor :translatable_model, :translatable_relation_field
10
11
  self.translatable_model = base_model
11
12
  self.translatable_relation_field = base_model.name.underscore.gsub '/', '_'
12
13
 
13
- table_name = "#{base_model.table_name.singularize}_translations"
14
14
  belongs_to translatable_relation_field
15
+ before_save :stringify_locale!
15
16
 
16
17
  named_scope :for_locale, lambda { |locale|
17
18
  { :conditions => ['locale = ? OR locale = ?', locale.to_s, I18n.locale.to_s] }
18
19
  }
19
20
 
20
- before_save :stringify_locale!
21
- end
22
- klass.class_eval do
23
- def stringify_locale!
21
+ define_method :stringify_locale! do
24
22
  self.locale = locale.to_s
25
23
  end
26
24
  end
@@ -1,3 +1,3 @@
1
1
  module EfficientTranslations
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -7,7 +7,7 @@ describe EfficientTranslations::TranslationFactory do
7
7
  end
8
8
  end
9
9
 
10
- describe '::new_model' do
10
+ describe '::build' do
11
11
  context 'when no translation model is found' do
12
12
  before do
13
13
  @klass = Class.new(ActiveRecord::Base)
@@ -16,43 +16,43 @@ describe EfficientTranslations::TranslationFactory do
16
16
 
17
17
  it 'should create a new ::Translation class' do
18
18
  @model_class.should_receive(:const_set).with(:Translation, kind_of(Class)) { |name, klass| klass }
19
- EfficientTranslations::TranslationFactory::new_model @model_class
19
+ EfficientTranslations::TranslationFactory::build @model_class
20
20
  end
21
21
 
22
22
  it 'should define a belongs_to association to the main model' do
23
23
  @klass.should_receive(:belongs_to)
24
- EfficientTranslations::TranslationFactory::new_model @model_class
24
+ EfficientTranslations::TranslationFactory::build @model_class
25
25
  end
26
26
 
27
27
  it 'should assign the translatable model in an accessor' do
28
- translation = EfficientTranslations::TranslationFactory::new_model @model_class
28
+ translation = EfficientTranslations::TranslationFactory::build @model_class
29
29
  translation.translatable_model.should == @model_class
30
30
  end
31
31
 
32
32
  it 'should assign the translatable model field in an accessor' do
33
- translation = EfficientTranslations::TranslationFactory::new_model @model_class
33
+ translation = EfficientTranslations::TranslationFactory::build @model_class
34
34
  translation.translatable_relation_field.should == @model_class.name.underscore.gsub('/','_')
35
35
  end
36
36
 
37
37
  it 'should return the created translation class' do
38
38
  klass = Class.new(ActiveRecord::Base)
39
39
  @model_class.stub :const_set => klass
40
- EfficientTranslations::TranslationFactory::new_model(@model_class).should == klass
40
+ EfficientTranslations::TranslationFactory::build(@model_class).should == klass
41
41
  end
42
42
  end
43
43
 
44
44
  context 'when a translation model is found' do
45
45
  it 'should not create a ::Translation class' do
46
- EfficientTranslations::TranslationFactory::new_model @model_class
46
+ EfficientTranslations::TranslationFactory::build @model_class
47
47
  @model_class.should_not_receive(:const_set)
48
- EfficientTranslations::TranslationFactory::new_model @model_class
48
+ EfficientTranslations::TranslationFactory::build @model_class
49
49
  end
50
50
 
51
51
  it 'should return the already defined translation class' do
52
52
  klass = Class.new(ActiveRecord::Base)
53
53
  @model_class.stub :const_set => klass
54
- EfficientTranslations::TranslationFactory::new_model @model_class
55
- EfficientTranslations::TranslationFactory::new_model(@model_class).should == klass
54
+ EfficientTranslations::TranslationFactory::build @model_class
55
+ EfficientTranslations::TranslationFactory::build(@model_class).should == klass
56
56
  end
57
57
  end
58
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: efficient_translations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
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: 2012-02-08 00:00:00.000000000Z
12
+ date: 2012-02-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70105172048080 !ruby/object:Gem::Requirement
16
+ requirement: &70304938783780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70105172048080
24
+ version_requirements: *70304938783780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70105172047020 !ruby/object:Gem::Requirement
27
+ requirement: &70304938782640 !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: *70105172047020
35
+ version_requirements: *70304938782640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70105172046400 !ruby/object:Gem::Requirement
38
+ requirement: &70304938781980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70105172046400
46
+ version_requirements: *70304938781980
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activerecord
49
- requirement: &70105172045360 !ruby/object:Gem::Requirement
49
+ requirement: &70304938780640 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '2.3'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70105172045360
57
+ version_requirements: *70304938780640
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activesupport
60
- requirement: &70105172043440 !ruby/object:Gem::Requirement
60
+ requirement: &70304938779720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '2.3'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70105172043440
68
+ version_requirements: *70304938779720
69
69
  description: Translation library for ActiveRecord models in Rails 2
70
70
  email:
71
71
  - nicola@nicolaracco.com
@@ -105,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  segments:
107
107
  - 0
108
- hash: 4009622527868518176
108
+ hash: 2503706901101072046
109
109
  required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  none: false
111
111
  requirements:
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  segments:
116
116
  - 0
117
- hash: 4009622527868518176
117
+ hash: 2503706901101072046
118
118
  requirements: []
119
119
  rubyforge_project:
120
120
  rubygems_version: 1.8.10