dimarzio-model_translations 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/init.rb CHANGED
@@ -1 +1 @@
1
- ActiveRecord::Base.send(:include, Dimarzio::ModelTranslations)
1
+ ActiveRecord::Base.send(:include, ModelTranslations)
@@ -0,0 +1,95 @@
1
+ # option:
2
+ # with (for future use)
3
+ # choices:
4
+ # translations_table - translations for all models in one table
5
+ # same_table - translations in the same table as model
6
+ # child_table - translations in child translations table
7
+
8
+ module ModelTranslations
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ base.send(:include, InstanceMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ def translates(*args)
16
+ options = args.extract_options!
17
+ options[:with] ||= :translations_table
18
+
19
+ after_save :save_translated_fields
20
+
21
+ has_many :translations,
22
+ :dependent => :destroy,
23
+ :as => :owner
24
+
25
+ args.each do |field|
26
+ field = field.to_s
27
+
28
+ define_method(field) do
29
+ locale = I18n.locale.to_s
30
+ mem_str = "mem_#{field}_#{locale}"
31
+ mem_sym = mem_str.to_sym
32
+
33
+ make_mem_attr(mem_str)
34
+
35
+ unless self[mem_sym].nil?
36
+ self[mem_sym]
37
+ else
38
+ t = translations.first(:conditions => {
39
+ :field => field,
40
+ :locale => locale
41
+ })
42
+ unless t.nil?
43
+ self[mem_sym] = t.value
44
+ else
45
+ t = translations.find_by_field(field)
46
+ !t.nil? ? t.value : nil
47
+ end
48
+ end
49
+ end
50
+
51
+ define_method("#{field}=") do |value|
52
+ make_mem_attr("mem_#{field}_#{I18n.locale}")
53
+ self["mem_#{field}_#{I18n.locale}".to_sym] = value
54
+ end
55
+
56
+ end
57
+ end
58
+ end
59
+
60
+ module InstanceMethods
61
+ private
62
+ def make_mem_attr(mem_str)
63
+ unless respond_to?(mem_str)
64
+ self.class.class_eval do
65
+ attr_accessor mem_str.to_sym
66
+ end
67
+ end
68
+ end
69
+
70
+ def save_translated_fields
71
+ methods.grep(/^mem_\w+[^=]$/).map do |m|
72
+ m.sub(/^mem_(\w+)$/, '\1')
73
+ end.each do |attr|
74
+ field = attr[0..-4]
75
+ locale = attr[-2..-1]
76
+ value = self["mem_#{field}_#{locale}".to_sym]
77
+
78
+ t = translations.first(:conditions => {
79
+ :field => field,
80
+ :locale => locale
81
+ })
82
+ unless t.nil?
83
+ t.update_attribute :value, value
84
+ else
85
+ translations.create(
86
+ :field => field,
87
+ :locale => locale,
88
+ :value => value
89
+ )
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{model_translations}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dmitry Afanasyev"]
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
  "generators/model_translations/USAGE",
25
25
  "generators/model_translations/model_translations_generator.rb",
26
26
  "init.rb",
27
- "lib/dimarzio/model_translations.rb",
27
+ "lib/model_translations.rb",
28
28
  "model_translations.gemspec",
29
29
  "tasks/model_translations_tasks.rake",
30
30
  "test/model_translations_test.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dimarzio-model_translations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Afanasyev
@@ -49,7 +49,7 @@ files:
49
49
  - generators/model_translations/USAGE
50
50
  - generators/model_translations/model_translations_generator.rb
51
51
  - init.rb
52
- - lib/dimarzio/model_translations.rb
52
+ - lib/model_translations.rb
53
53
  - model_translations.gemspec
54
54
  - tasks/model_translations_tasks.rake
55
55
  - test/model_translations_test.rb
@@ -1,97 +0,0 @@
1
- # option:
2
- # with (for future use)
3
- # choices:
4
- # translations_table - translations for all models in one table
5
- # same_table - translations in the same table as model
6
- # child_table - translations in child translations table
7
-
8
- module Dimarzio
9
- module ModelTranslations
10
- def self.included(base)
11
- base.extend(ClassMethods)
12
- base.send(:include, InstanceMethods)
13
- end
14
-
15
- module ClassMethods
16
- def translates(*args)
17
- options = args.extract_options!
18
- options[:with] ||= :translations_table
19
-
20
- after_save :save_translated_fields
21
-
22
- has_many :translations,
23
- :dependent => :destroy,
24
- :as => :owner
25
-
26
- args.each do |field|
27
- field = field.to_s
28
-
29
- define_method(field) do
30
- locale = I18n.locale.to_s
31
- mem_str = "mem_#{field}_#{locale}"
32
- mem_sym = mem_str.to_sym
33
-
34
- make_mem_attr(mem_str)
35
-
36
- unless self[mem_sym].nil?
37
- self[mem_sym]
38
- else
39
- t = translations.first(:conditions => {
40
- :field => field,
41
- :locale => locale
42
- })
43
- unless t.nil?
44
- self[mem_sym] = t.value
45
- else
46
- t = translations.find_by_field(field)
47
- !t.nil? ? t.value : nil
48
- end
49
- end
50
- end
51
-
52
- define_method("#{field}=") do |value|
53
- make_mem_attr("mem_#{field}_#{I18n.locale}")
54
- self["mem_#{field}_#{I18n.locale}".to_sym] = value
55
- end
56
-
57
- end
58
- end
59
- end
60
-
61
- module InstanceMethods
62
- private
63
- def make_mem_attr(mem_str)
64
- unless respond_to?(mem_str)
65
- self.class.class_eval do
66
- attr_accessor mem_str.to_sym
67
- end
68
- end
69
- end
70
-
71
- def save_translated_fields
72
- methods.grep(/^mem_\w+[^=]$/).map do |m|
73
- m.sub(/^mem_(\w+)$/, '\1')
74
- end.each do |attr|
75
- field = attr[0..-4]
76
- locale = attr[-2..-1]
77
- value = self["mem_#{field}_#{locale}".to_sym]
78
-
79
- t = translations.first(:conditions => {
80
- :field => field,
81
- :locale => locale
82
- })
83
- unless t.nil?
84
- t.update_attribute :value, value
85
- else
86
- translations.create(
87
- :field => field,
88
- :locale => locale,
89
- :value => value
90
- )
91
- end
92
- end
93
- end
94
- end
95
-
96
- end
97
- end