acts_as_translatable 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.
@@ -1,50 +1,52 @@
1
+ require 'acts_as_translatable/instance_methods'
2
+
1
3
  class ActiveRecord::Base
2
4
  def self.acts_as_translatable_on(*fields)
3
- # after save hook
4
- self.after_save :save_translations
5
+ include ActsAsTranslatable::InstanceMethods
5
6
 
6
- # get the fields
7
- @translatable_fields = fields
8
-
9
- # loop through fields
10
7
  fields.each do |field|
11
- eval "def #{field}
12
- translations[I18n.locale] ||= {}
13
- translations[I18n.locale][\"#{field}\"]
14
- end
15
-
16
- def #{field}=(value)
17
- translations[I18n.locale] ||= {}
18
- translations[I18n.locale][\"#{field}\"] = value
19
- end
20
-
21
- def translations
22
- unless @translations
23
- @translations = {}
24
- record_translations.each do |translation|
25
- @translations[translation.locale.to_sym] ||= {}
26
- @translations[translation.locale.to_sym][translation.translatable_field] = translation.content
8
+ eval "class ::#{name}
9
+ after_save :save_translations
10
+
11
+ def #{field}
12
+ translations[I18n.locale] ||= {}
13
+ translations[I18n.locale][\"#{field}\"]
14
+ end
15
+
16
+ def #{field}=(value)
17
+ translations[I18n.locale] ||= {}
18
+ translations[I18n.locale][\"#{field}\"] = value
19
+ end
20
+
21
+ def translations
22
+ unless @translations
23
+ @translations = {}
24
+ record_translations.each do |translation|
25
+ @translations[translation.locale.to_sym] ||= {}
26
+ @translations[translation.locale.to_sym][translation.translatable_field] = translation.content
27
+ end
27
28
  end
29
+ @translations
30
+ end
31
+
32
+ def record_translations
33
+ @record_translations ||= RecordTranslation.where(:translatable_id => id, :translatable_type => self.class.name)
28
34
  end
29
- @translations
30
- end
31
35
 
32
- def record_translations
33
- @record_translations ||= RecordTranslation.where(:translatable_id => id, :translatable_type => self.class.name)
34
- end"
35
- end
36
- end
37
-
38
- def save_translations
39
- # delete all previous record translations
40
- record_translations.destroy_all
41
-
42
- # loop through updated translations
43
- translations.each_pair do |locale, fields|
44
- fields.each_pair do |field, content|
45
- # create translation record
46
- RecordTranslation.create :translatable_id => id, :translatable_type => self.class.name, :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
47
- end
36
+ def save_translations
37
+ # delete all previous record translations
38
+ record_translations.destroy_all
39
+
40
+ # loop through updated translations
41
+ translations.each_pair do |locale, fields|
42
+ fields.each_pair do |field, content|
43
+ # create translation record
44
+ RecordTranslation.create :translatable_id => id, :translatable_type => self.class.name, :translatable_field => field, :locale => locale.to_s, :content => content unless content.blank?
45
+ end
46
+ end
47
+ end
48
+ end"
48
49
  end
49
50
  end
50
- end
51
+ end
52
+
@@ -0,0 +1,4 @@
1
+ module ActsAsTranslatable
2
+ module InstanceMethods
3
+ end
4
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_translatable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lasse Bunk
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-13 00:00:00 Z
18
+ date: 2012-05-14 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Ruby on Rails plugin for easy translation of database fields.
@@ -28,6 +28,7 @@ extra_rdoc_files: []
28
28
 
29
29
  files:
30
30
  - lib/acts_as_translatable/active_record.rb
31
+ - lib/acts_as_translatable/instance_methods.rb
31
32
  - lib/acts_as_translatable.rb
32
33
  - lib/generators/acts_as_translatable/acts_as_translatable_generator.rb
33
34
  - lib/generators/acts_as_translatable/templates/migration.rb